]>
git.p6c8.net - devedit.git/blob - modules/File/UseList.pm
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-11-21
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'};
98 open(LOCKFILE
,">$lockfile") or return;
99 close(LOCKFILE
) or return;
101 $self->{'locked'} = 0;
105 # The list wasn't lock by us or it isn't locked at all
112 # Load the list with files from the list file
116 # Return: Status code (Boolean)
121 my $file = $self->{'listfile'};
124 # Read out the file and split the content line-per-line
126 open(FILE
,"<$file") or return;
127 read(FILE
, my $content, -s
$file);
128 close(FILE
) or return;
130 my @files = split(/\015\012|\012|\015/,$content);
132 # Remove useless lines
134 for(my $x=0;$x<@files;$x++)
136 if($files[$x] eq "" || $files[$x] =~ /^\s+$/)
139 $x--; # <-- very important!
143 $self->{'files'} = \
@files;
149 # Write the list with files back to the list file
153 # Return: Status code (Boolean)
158 my $file = $self->{'listfile'};
159 my $temp = $file.".temp";
160 my $files = $self->{'files'};
163 my $data = (@
$files) ?
join("\n",@
$files) : '';
165 open(FILE
,">$temp") or return;
166 print FILE
$data or do { close(FILE
); return };
167 close(FILE
) or return;
169 rename($temp,$file) or return;
176 # Add a file to the list
180 # Return: Status code (Boolean)
184 my ($self,$file) = @_;
185 my $files = $self->{'files'};
187 # Check if the file is already in the list
189 return if($self->in_use($file));
197 # Remove a file from the list
201 # Return: Status code (Boolean)
205 my ($self,$file) = @_;
206 my $files = $self->{'files'};
208 # Check if the file is really in the list
210 return if($self->unused($file));
212 # Remove the file from the list
214 for(my $x=0;$x<@
$files;$x++)
216 if($files->[$x] eq $file)
218 splice(@
$files,$x,1);
226 # Check if a file is in the list
228 # Params: File to check
230 # Return: Status code (Boolean)
234 my ($self,$file) = @_;
235 my $files = $self->{'files'};
239 return 1 if($_ eq $file);
247 # Check if a file is not in the list
249 # Params: File to check
251 # Return: Status code (Boolean)
255 return not shift->in_use(shift);
258 # it's true, baby ;-)
patrick-canterino.de