Subject: Atomic Transactions on Files
From: [email-masked] (Tim Freeman)
Date: 15 Jul 1995 12:53:58 GMT
Newsgroups: comp.lang.perl.announce,comp.lang.perl.misc,comp.lang.perl

I wrote a package that implements atomic transactions on Unix files in
Perl.  The package can ensure that consistent changes are made to a
set of files, even in the presence of power failures, concurrent use,
and errors.  

The package includes (and requires installation of) an interface to
the sync and fsync Unix calls.

It is available by downloading the URL
file://ftp.netcom.com/pub/ts/tsf/trans.tar.gz, or if you can't parse
URL's you can get it by anonymous FTP to ftp.netcom.com, directory
pub/ts/tsf/trans.tar.gz.  It requires Perl 5.000 or higher and I tested
it with Linux 1.2.2, Perl 5.001e.

For example, the following program will never reach the
"Integrity lost" error unless some other code manipulates a and b,
even in the presence of power failures as the script is running:

   sub doit {
       my ($num);
       if (! -e "a") {
	   $num = 0;
       } else {
	   open (A, "<a") || die "Can't read a: $!";
	   $num = <A>; chop $num;
	   close (A);
	   open (B, "<b") || die "Can't read b: $!";
	   die "Integrity lost" unless <B>=="$num\n";
	   close (B);
       }
       $num++;
       open (A, ">a") || die "Can't write a: $!";
       print A $num,"\n";
       close (A);
       open (B, ">b") || die "Can't write b: $!";
       print B $num,"\n";
       close (B);
   }
   
   Trans::transact (\&doit, "a", "b");

Tim Freeman
[email-masked]
[email-masked]
-- 
Tim Freeman
[email-masked]