Some atomicity fix to QscaleProxy that I don't seem to remember if was
authorSteinar H. Gunderson <sesse@debian.org>
Sun, 16 Nov 2008 13:34:59 +0000 (14:34 +0100)
committerSteinar H. Gunderson <sesse@debian.org>
Sun, 16 Nov 2008 13:34:59 +0000 (14:34 +0100)
sane or not, but has been in production for a while. :-)

perl/Sesse/pr0n/QscaleProxy.pm

index 3f0052c2c0b70b4454296ceb2b9174075863b126..69d7c76d8f6a4e8daf24bc862f6460cfa05f30aa 100644 (file)
@@ -159,6 +159,9 @@ sub write {
                return $self->{'magick'}->write(%args);
        }
 
+       # For some reason we seem to get conditions of some sort when using
+       # qscale for this, but not when using ImageMagick. Thus, we put the
+       # atomic-write code here and not elsewhere in pr0n.
        my $filename = $args{'filename'};
        my $quality = $args{'quality'};
 
@@ -171,11 +174,17 @@ sub write {
                die "Unknown interlacing mode " . $args{'interlace'};
        }
 
-       my $ret = qscale::qscale_save_jpeg($self->{'qscale'}, $filename, $quality, $jpeg_mode);
+       my $tmpname = $filename . "-tmp-$$-" . int(rand(100000));
+       unlink($filename);
+       my $ret = qscale::qscale_save_jpeg($self->{'qscale'}, $tmpname, $quality, $jpeg_mode);
        if ($ret == 0) {
-               return 0;
+               if (rename($tmpname, $filename)) {
+                       return 0;
+               } else {
+                       return "400 Image renaming to from $tmpname to $filename failed: $!";
+               }
        } else {
-               return "400 Image saving to $filename failed";
+               return "400 Image saving to $tmpname failed";
        }
 }