Page 1 of 1

mogrify may cause data loss

Posted: 2008-12-19T08:34:43-07:00
by naoliv
Hi!

From http://bugs.debian.org/324203 and http://bugs.debian.org/358407, we can see that a file can get lost/corrupted if interrupted when mogrify is running.

A test script that shows this behavior:

Code: Select all

#!/usr/bin/perl
use Time::HiRes qw( usleep );
use POSIX;
$file = "file.jpg";
$size=(stat($file))[7];
$i=0;
while( $size > 0 && $i < 100000 ){
        if($p=fork) {
                usleep(rand(10000));
                for($i=0;$i<10000;$i++) {
                        kill(15,$p);
                }
        } else {
                usleep(rand(10000));
                system("mogrify -scale 1024 $file") or die "mog";
                exit;
        }
        $i++;
}
print "Done $i $size.\n";
(maybe you want to echo 2 > /proc/sys/vm/overcommit_memory on a Linux system, before testing it).

Can mogrify be modified to work atomically, please?

Thank you!

Re: mogrify may cause data loss

Posted: 2008-12-19T10:29:42-07:00
by magick
I agree with Daniel Kobras on this one:
If you rely on data integrity, use 'convert foo.jpg tmp.jpg && mv tmp.jpg foo.jpg'
You can also use the mogrify -path or -format options to leave the original image file alone and write to an alternative file format or path.

Re: mogrify may cause data loss

Posted: 2009-01-04T20:33:51-07:00
by anthony
I would go further and test for file existance and size as well!

In many scripts I also check the final image size to make sure I have it right!

Remember Murphy's Law, and it it's always IM that is the problem either! Disk space memory limits, user aborts, networking failures on NFS or faked file systems, even full system crashes. It is a messy world we live in even if most of the time computers try to make it neat and clean for us.

Viva Chaos!