| 1 | package Proc::Lock::File; | ||
| 2 | |||
| 3 | #Proc::Lock::File::VERSION = (split " ", '# $Id: File.pm,v 1.3 2000/09/21 14:15:21 mkul Exp $ ')[3]; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Proc::Lock::File - lock interface module | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Proc::Lock::File; | ||
| 12 | my $lock = new Proc::Lock::File ( Directory => '/var/locks', | ||
| 13 | ProcessName => 'locker' ); | ||
| 14 | $lock->set () || die "already locked"; | ||
| 15 | die "locked" if $lock->isSet (); | ||
| 16 | $lock->unset (); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | Generic lock module. You must subclass this class and overwrite set, clear, isSet and new (possible) for do your work | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | =head2 new | ||
| 25 | |||
| 26 | Construct new file lock object. Add Directory parameter to parent constructor. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use IO::File; | ||
| 32 | use Proc::Lock; | ||
| 33 | use base qw(Proc::Lock); | ||
| 34 | use Fcntl ':flock'; | ||
| 35 | |||
| 36 | sub new | ||
| 37 | { | ||
| 38 | my ( $class, %params ) = @_; | ||
| 39 | my $this = $class->SUPER::new ( %params ); | ||
| 40 | $this->{directory} || die "You must set Directory parameter"; | ||
| 41 | $this; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _set | ||
| 45 | { | ||
| 46 | my $this = shift; | ||
| 47 | my $fileName = $this->{processname}; | ||
| 48 | $fileName .= '.' . $this->{hostname} if $this->{hostname}; | ||
| 49 | $fileName .= '.' . $this->{pid} if $this->{pid}; | ||
| 50 | $fileName = $this->{directory} . '/' . $fileName; | ||
| 51 | $fileName =~ s#//#/#g; | ||
| 52 | |||
| 53 | return undef if ( ( -e $fileName ) && ( ! $this->{wait} ) ); | ||
| 54 | |||
| 55 | my $file = new IO::File ( $fileName, 'w' ); | ||
| 56 | if ( ! $file ) | ||
| 57 | { | ||
| 58 | $this->notice ( "Error open file $fileName for write access: $!" ); | ||
| 59 | return undef; | ||
| 60 | } | ||
| 61 | |||
| 62 | my $lockMethod = LOCK_EX; | ||
| 63 | $lockMethod = $lockMethod | LOCK_NB if ( ! $this->{wait} ); | ||
| 64 | |||
| 65 | if ( ! flock ( $file, $lockMethod ) ) | ||
| 66 | { | ||
| 67 | $file->close (); | ||
| 68 | $this->log->notice ( "can't lock file $fileName: $!" ); | ||
| 69 | return undef; | ||
| 70 | }; | ||
| 71 | |||
| 72 | $file->print ( $$ ); | ||
| 73 | |||
| 74 | $this->{file} = $file; | ||
| 75 | $this->{filename} = $fileName; | ||
| 76 | $this->{isset} = 1; | ||
| 77 | |||
| 78 | 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub _unset | ||
| 82 | { | ||
| 83 | my $this = shift || return; | ||
| 84 | |||
| 85 | my $result = 1; | ||
| 86 | my $fileName = $this->{filename} || return; | ||
| 87 | |||
| 88 | if ( ! unlink ( $this->{filename} ) ) | ||
| 89 | { | ||
| 90 | $this->log->error ( "Error unlink file $fileName: $!" ); | ||
| 91 | $result = 0; | ||
| 92 | } | ||
| 93 | |||
| 94 | if ( ( $this->{file} ) and ( ! $this->{file}->close ) ) | ||
| 95 | { | ||
| 96 | $this->log->error ( "Error close file $fileName: $!" ); | ||
| 97 | $result = 0; | ||
| 98 | } | ||
| 99 | |||
| 100 | $result; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _isSet | ||
| 104 | { | ||
| 105 | my $this = shift; | ||
| 106 | $this->{isset}; | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | __END__ |