From: Eric Wong Date: Sat, 12 Apr 2014 04:12:47 +0000 (+0000) Subject: use flock instead of fcntl locking X-Git-Tag: v0.0.0~14 X-Git-Url: http://git.tremily.us/?p=ssoma-mda.git;a=commitdiff_plain;h=22db06570ae15b2bfc565840ec30b73e555b90f2 use flock instead of fcntl locking We do not need range locking of fcntl locks, so using flock removes a dependency, hopefully making us easier-to-install. Also keep in mind Ruby (and perhaps other scripting language) supports flock out-of-the-box as well, so it seems flock is easier to support although fcntl locks offer superior functionality. --- diff --git a/Documentation/ssoma_repository.txt b/Documentation/ssoma_repository.txt index f7b24ad..6c6022f 100644 --- a/Documentation/ssoma_repository.txt +++ b/Documentation/ssoma_repository.txt @@ -48,7 +48,7 @@ two). # LOCKING -fcntl(2) locking exclusively locks the empty $GIT_DIR/ssoma.lock file +flock(2) locking exclusively locks the empty $GIT_DIR/ssoma.lock file for all non-atomic operations. # EXAMPLE INPUT FLOW (SERVER-SIDE MDA) @@ -61,7 +61,7 @@ ssoma-mda handles all steps once invoked. 2. Mail transport agent invokes ssoma-mda 3. reads message via stdin, extracting Message-ID -4. acquires fcntl lock on $GIT_DIR/ssoma.lock +4. acquires exclusive flock lock on $GIT_DIR/ssoma.lock 5. creates or updates the blob of associated 2/38 SHA-1 path 6. updates the index and commits 7. releases $GIT_DIR/ssoma.lock @@ -88,7 +88,7 @@ Special files in $GIT_DIR on the server: : The normal git index (in $GIT_DIR/index) is not used at all as there is typically no working tree. -* $GIT_DIR/ssoma.lock - empty file for fcntl(2) locking +* $GIT_DIR/ssoma.lock - empty file for flock(2) locking : This is necessary to ensure the index and commits are updated consistently and multiple processes running MDA do not step on each other. diff --git a/INSTALL b/INSTALL index d21a5c7..8f0e378 100644 --- a/INSTALL +++ b/INSTALL @@ -19,7 +19,6 @@ convenience. - Digest::SHA perl - Email::LocalDelivery libemail-localdelivery-perl - Email::Simple libemail-simple-perl - - File::FcntlLock libfile-fcntllock-perl - File::Path::Expand libfile-path-expand-perl - Net::IMAP::Simple libnet-imap-simple-perl diff --git a/Makefile.PL b/Makefile.PL index 7873a54..f500cc0 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -17,7 +17,6 @@ WriteMakefile( 'Digest::SHA' => 0, 'Email::LocalDelivery' => 0, 'Email::Simple' => 0, - 'File::FcntlLock' => 0, 'File::Path::Expand' => 0, 'Net::IMAP::Simple' => 0, }, diff --git a/README b/README index 3aec620..9c8bfac 100644 --- a/README +++ b/README @@ -31,7 +31,6 @@ Requirements (server MDA) * MTA - postfix is recommended * Perl and several modules: - Email::Simple - - File::FcntlLock - Digest::SHA Hacking diff --git a/lib/Ssoma/Git.pm b/lib/Ssoma/Git.pm index 9a9d82f..aa63e1d 100644 --- a/lib/Ssoma/Git.pm +++ b/lib/Ssoma/Git.pm @@ -13,9 +13,8 @@ package Ssoma::Git; use strict; use warnings; use File::Path qw/mkpath/; +use Fcntl qw/:DEFAULT :flock SEEK_END/; use IO::Handle; -use Fcntl; -use File::FcntlLock; use Email::Simple; use Digest::SHA qw/sha1_hex/; @@ -50,12 +49,6 @@ sub lockfile { $_[0]->{git_dir} . "/ssoma.lock" } sub sync_do { my ($self, $sub) = @_; - my $fs = File::FcntlLock->new; - $fs->l_type(F_WRLCK); - $fs->l_type(SEEK_CUR); - $fs->l_start(0); - $fs->l_len(0); - my $path = $self->lockfile; my $lock; @@ -66,14 +59,14 @@ sub sync_do { die "failed to open lock $path: $!\n"; # wait for other processes to be done - $fs->lock($lock, F_SETLKW) or die "lock failed: " . $fs->error . "\n"; + flock($lock, LOCK_EX) or die "lock failed: $!\n"; # run the sub! my @ret = eval { &$sub }; my $err = $@; # these would happen anyways, but be explicit so we can detect errors - $fs->lock($lock, F_UNLCK) or die "unlock failed: " . $fs->error . "\n"; + flock($lock, LOCK_UN) or die "unlock failed: $!\n"; close $lock or die "close lockfile($path) failed: $!\n"; die $err if $err;