]>
git.p6c8.net - devedit.git/blob - modules/File/UseList.pm
22d1bcc87320a2568decae829eabfa64d68e101b
6 # Run a list with files that are currently in use
7 # (bases on Filing::UseList by Roland Bluethgen <calocybe@web.de>)
9 # Author: Patrick Canterino <patshaping@gmx.net>
10 # Last modified: 2003-10-17
21 # Params: Hash: listfile => File with list of files in use
22 # lockfile => Lock file (Default: List file + .lock)
23 # timeout => Lock timeout in seconds (Default: 10)
25 # Return: File::UseList object (Blessed Reference)
29 my ($class,%args) = @_;
31 # Check if we got all the necessary information
33 croak
"Missing path to list file" unless($args{'listfile'});
34 $args{'lockfile'} = $args{'listfile'}.".lock" unless($args{'lockfile'}); # Default filename of lock file
35 $args{'timeout'} = 10 unless($args{'timeout'}); # Default timeout
37 # Add some other information
42 return bless(\
%args,$class);
47 # Lock list with files
52 # Return: Status code (Boolean)
57 my $lockfile = $self->{'lockfile'};
58 my $timeout = $self->{'timeout'};
60 return 1 if($self->{'locked'});
62 # Try to delete the lock file one time per second
63 # until the timeout is reached
65 for(my $x=$timeout;$x>=0;$x--)
69 $self->{'locked'} = 1;
83 # Unlock list with files, but only if _we_ locked it
88 # Return: Status code (Boolean)
93 my $lockfile = $self->{'lockfile'};
100 open(LOCKFILE
,">",$lockfile) or return;
101 close(LOCKFILE
) or return;
104 $self->{'locked'} = 0;
108 # The list wasn't lock by us or it isn't locked at all
115 # Load the list with files from the list file
119 # Return: Status code (Boolean)
124 my $file = $self->{'listfile'};
127 # Read out the file and split the content line-per-line
129 open(FILE
,"<",$file) or return;
130 read(FILE
, my $content, -s
$file);
131 close(FILE
) or return;
133 my @files = split(/\015\012|\012|\015/,$content);
135 # Remove useless lines
137 for(my $x=0;$x<@files;$x++)
139 if($files[$x] eq "" || $files[$x] =~ /^\s+$/)
142 $x--; # <-- very important!
146 $self->{'files'} = \
@files;
152 # Write the list with files back to the list file
156 # Return: Status code (Boolean)
161 my $file = $self->{'listfile'};
162 my $temp = $file.".temp";
163 my $files = $self->{'files'};
166 my $data = (@
$files) ?
join("\n",@
$files) : '';
168 open(FILE
,">",$temp) or return;
169 print FILE
$data or do { close(FILE
); return };
170 close(FILE
) or return;
172 rename($temp,$file) or return;
179 # Add a file to the list
185 my ($self,$file) = @_;
186 my $files = $self->{'files'};
188 # Check if the file is already in the list
190 return if($self->in_use($file));
197 # Remove a file from the list
203 my ($self,$file) = @_;
204 my $files = $self->{'files'};
206 # Check if the file is really in the list
208 return if($self->unused($file));
210 # Remove the file from the list
212 for(my $x=0;$x<@
$files;$x++)
214 if($files->[$x] eq $file)
216 splice(@
$files,$x,1);
224 # Check if a file is in the list
226 # Params: File to check
228 # Return: Status code (Boolean)
232 my ($self,$file) = @_;
233 my $files = $self->{'files'};
237 return 1 if($_ eq $file);
245 # Check if a file is not in the list
247 # Params: File to check
249 # Return: Status code (Boolean)
253 return not shift->in_use(shift);
256 # it's true, baby ;-)
patrick-canterino.de