]>
git.p6c8.net - devedit.git/blob - modules/File/UseList.pm
6 # Run a list with files that are currently in use
7 # (bases upon Filing::UseList by Roland Bluethgen <calocybe@web.de>)
9 # Author: Patrick Canterino <patshaping@gmx.net>
10 # Last modified: 09-26-2003
23 # Params: Hash: listfile => File with list of files in use
24 # lockfile => Lock file (Default: List file + .lock)
25 # timeout => Lock timeout in seconds (Default: 10)
27 # Return: File::UseList object
31 my ($class,%args) = @_;
33 # Check if we got all the necessary information
35 croak
"Missing path of list file" unless($args{'listfile'});
36 $args{'lockfile'} = $args{'listfile'}.".lock" unless($args{'lockfile'}); # Default filename of lock file
37 $args{'timeout'} = 10 unless($args{'timeout'}); # Default timeout
42 $self->{'files'} = [];
43 $self->{'locked'} = 0;
45 return bless($self,$class);
50 # Lock list with files
55 # Return: Status code (Boolean)
60 my $lockfile = $self->{'lockfile'};
61 my $timeout = $self->{'timeout'};
63 return 1 if($self->{'locked'});
65 # Try to delete the lock file one time per second
66 # until the timeout is reached
68 for(my $x=$timeout;$x>=0;$x--)
72 $self->{'locked'} = 1;
86 # Unlock list with files, but only if _we_ locked it
91 # Return: Status code (Boolean)
96 my $lockfile = $self->{'lockfile'};
101 return 1 if(-f
$lockfile); # Hmmm...
103 open(LOCKFILE
,">",$lockfile) or return;
104 close(LOCKFILE
) or return;
106 $self->{'locked'} = 0;
111 # The list wasn't lock by ourselves
118 # Load the list with files
122 # Return: Status code (Boolean)
127 my $file = $self->{'listfile'};
130 # Read out the file and split the content line-per-line
132 open(FILE
,"<".$file) or return;
133 read(FILE
, my $content, -s
$file);
134 close(FILE
) or return;
136 my @files = split(/\015\012|\012|\015/,$content);
138 # Remove useless lines
140 for(my $x=0;$x<@files;$x++)
142 if($files[$x] eq "" || $files[$x] =~ /^\s+$/)
145 $x--; # <-- very important!
149 $self->{'files'} = \
@files;
155 # Save the list with files
159 # Return: Status code (Boolean)
164 my $file = $self->{'listfile'};
165 my $temp = $file.".temp";
166 my $files = $self->{'files'};
169 my $data = (@
$files) ?
join("\n",@
$files) : '';
171 open(FILE
,">",$temp) or return;
172 print FILE
$data or do { close(FILE
); return };
173 close(FILE
) or return;
175 rename($temp,$file) and return 1;
184 # Add a file to the list
190 my ($self,$file) = @_;
191 my $files = $self->{'files'};
193 # Check if the file is already in the list
195 return if($self->in_use($file));
202 # Remove a file from the list
208 my ($self,$file) = @_;
209 my $files = $self->{'files'};
211 # Check if the file is really in the list
213 return if($self->unused($file));
215 # Remove the file from the list
217 for(my $x=0;$x<@
$files;$x++)
219 if($files->[$x] eq $file)
221 splice(@
$files,$x,1);
229 # Check if a file is in the list
231 # Params: File to check
233 # Return: Status code (Boolean)
237 my ($self,$file) = @_;
238 my $files = $self->{'files'};
242 return 1 if($_ eq $file);
250 # Check if a file is not in the list
252 # Params: File to check
254 # Return: Status code (Boolean)
258 return not shift->in_use(shift);
261 # it's true, baby ;-)
patrick-canterino.de