mirror of
https://codeberg.org/h3xx/optipdf
synced 2024-08-15 00:03:23 +00:00
Avoid creating temp files until necessary
For instance, if one ran "optipdf --help" it would create and delete an empty directory in /tmp.
This commit is contained in:
parent
18515e5794
commit
2497e27b04
1 changed files with 13 additions and 6 deletions
19
optipdf
19
optipdf
|
@ -42,12 +42,6 @@ KEEP_BACKUP_SUFFIX=
|
||||||
FORCE_OVERWRITE=0
|
FORCE_OVERWRITE=0
|
||||||
ENCODE_THRU_WARNINGS=0
|
ENCODE_THRU_WARNINGS=0
|
||||||
|
|
||||||
TEMP_DIR=$(mktemp -d -t "${0##*/}.XXXXXX")
|
|
||||||
cleanup() {
|
|
||||||
rm -fr -- "$TEMP_DIR"
|
|
||||||
}
|
|
||||||
trap 'cleanup' EXIT
|
|
||||||
|
|
||||||
FILES=()
|
FILES=()
|
||||||
NO_MORE_FLAGS=0
|
NO_MORE_FLAGS=0
|
||||||
for ARG; do
|
for ARG; do
|
||||||
|
@ -104,6 +98,8 @@ for ARG; do
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
TEMP_DIR=
|
||||||
|
|
||||||
if [[ ${#FILES[@]} -eq 0 ]]; then
|
if [[ ${#FILES[@]} -eq 0 ]]; then
|
||||||
USAGE >&2
|
USAGE >&2
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -159,6 +155,16 @@ hr_size() (
|
||||||
printf '%g %s\n' "$HR_VAL" "$HR_UNIT"
|
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
|
# copies $2 over to $1 if $2 is smaller than $1
|
||||||
use_smaller() {
|
use_smaller() {
|
||||||
# if `$TEMP' isn't empty and it's of a smaller size than `$FILE',
|
# if `$TEMP' isn't empty and it's of a smaller size than `$FILE',
|
||||||
|
@ -250,6 +256,7 @@ fi
|
||||||
ERRORS=0
|
ERRORS=0
|
||||||
FREED_TOTAL=0
|
FREED_TOTAL=0
|
||||||
for FILE in "${FILES[@]}"; do
|
for FILE in "${FILES[@]}"; do
|
||||||
|
setup_tempdir
|
||||||
TEMP=$(mktemp -p "$TEMP_DIR" -t 'file.XXXXXX')
|
TEMP=$(mktemp -p "$TEMP_DIR" -t 'file.XXXXXX')
|
||||||
rm -f -- "$TEMP"
|
rm -f -- "$TEMP"
|
||||||
BEGIN_FILESIZE=$(file_size "$FILE")
|
BEGIN_FILESIZE=$(file_size "$FILE")
|
||||||
|
|
Loading…
Reference in a new issue