--- exifautotran/exifautotran 2008-01-17 02:43:06.000000000 +0000 +++ exifautotran-ilugo/exifautotran 2008-01-17 04:28:49.000000000 +0000 @@ -3,15 +3,23 @@ # # Transforms Exif files so that Orientation becomes 1 # + +trap "if test -n \"\$tempfile\"; then rm -f \"\$tempfile\"; fi" INT QUIT TERM + +preserve_timestamps="" + for i do - case $i in + case "$i" in + -p|--pretend) preserve_timestamps="yes"; continue;; -v|--version) echo "exifautotran"; exit 0;; -h|--help) cat <&2 - jpegtran -copy all $transform "$i" > tempfile + tempfile=`mktemp` || { + echo "Failed to create temporary file" >&2 + exit 1; + } + echo "Executing: jpegtran -copy all $transform $i" >&2 + jpegtran -copy all $transform "$i" > "$tempfile" if test $? -ne 0; then - echo Error while transforming $i - skipped. >&2 + echo "Error while transforming $i - skipped." >&2 + rm "$tempfile" else - rm "$i" - mv tempfile "$i" - jpegexiforient -1 "$i" > /dev/null + jpegexiforient -1 "$tempfile" > /dev/null + if test -n "$preserve_timestamps"; then + touch -r "$i" "$tempfile" + fi + # read original permissions for setting later + original_mode=`stat -c %a "$i"` + mv "$tempfile" "$i" || { + echo "Failed to update $i - skipped." >&2 + rm "$tempfile" + continue + } + # set original permissions only after moving the file back, + # to avoid setting potentially unsafe permissions on the file + # while it's in /tmp, where it could be accessed by anyone + chmod "$original_mode" "$i" fi fi done