Compare commits

...

3 Commits

Author SHA1 Message Date
Dan Church 8dcfacd5d7
Update 'mktemp' invocation
Turns out using "-t TEMPLATE" instead of "TEMPLATE" is causing TMPDIR to
override the setting from `-p`, which is the opposite of what the man
page says it does.
2023-03-27 11:32:18 -05:00
Dan Church 4dd8e17f0b
Fix inconsistent script exit codes
Exit 2 always in invalid argument situations.
2023-03-27 11:03:24 -05:00
Dan Church 2497e27b04
Avoid creating temp files until necessary
For instance, if one ran "optipdf --help" it would create and delete an
empty directory in /tmp.
2023-03-27 11:07:14 -05:00
1 changed files with 15 additions and 8 deletions

23
optipdf
View File

@ -42,12 +42,6 @@ KEEP_BACKUP_SUFFIX=
FORCE_OVERWRITE=0
ENCODE_THRU_WARNINGS=0
TEMP_DIR=$(mktemp -d -t "${0##*/}.XXXXXX")
cleanup() {
rm -fr -- "$TEMP_DIR"
}
trap 'cleanup' EXIT
FILES=()
NO_MORE_FLAGS=0
for ARG; do
@ -104,9 +98,11 @@ for ARG; do
fi
done
TEMP_DIR=
if [[ ${#FILES[@]} -eq 0 ]]; then
USAGE >&2
exit 1
exit 2
fi
file_size() {
@ -159,6 +155,16 @@ hr_size() (
printf '%g %s\n' "$HR_VAL" "$HR_UNIT"
)
setup_tempdir() {
if [[ -z $TEMP_DIR ]]; then
TEMP_DIR=$(mktemp -d -t "${0##*/}.XXXXXX")
cleanup() {
rm -fr -- "$TEMP_DIR"
}
trap 'cleanup' EXIT
fi
}
# copies $2 over to $1 if $2 is smaller than $1
use_smaller() {
# if `$TEMP' isn't empty and it's of a smaller size than `$FILE',
@ -250,7 +256,8 @@ fi
ERRORS=0
FREED_TOTAL=0
for FILE in "${FILES[@]}"; do
TEMP=$(mktemp -p "$TEMP_DIR" -t 'file.XXXXXX')
setup_tempdir
TEMP=$(mktemp -p "$TEMP_DIR" 'file.XXXXXX')
rm -f -- "$TEMP"
BEGIN_FILESIZE=$(file_size "$FILE")