]>
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 <patrick@patshaping.de>
10 # Last modified: 2004-12-03
22 # Params: Hash: listfile => File with list of files in use
23 # lockfile => Lock file (Default: List file + .lock)
24 # timeout => Lock timeout in seconds (Default: 10)
26 # Return: File::UseList object (Blessed Reference)
30 my ($class,%args) = @_;
32 # Check if we got all the necessary information
34 croak
"Missing path to list file" unless($args{'listfile'});
35 $args{'lockfile'} = $args{'listfile'}.".lock" unless($args{'lockfile'}); # Default filename of lock file
36 $args{'timeout'} = 10 unless($args{'timeout'}); # Default timeout
38 # Add some other information
43 return bless(\
%args,$class);
48 # Lock list with files
53 # Return: Status code (Boolean)
58 my $lockfile = $self->{'lockfile'};
59 my $timeout = $self->{'timeout'};
61 return 1 if($self->{'locked'});
63 # Try to delete the lock file one time per second
64 # until the timeout is reached
66 for(my $x=$timeout;$x>=0;$x--)
70 $self->{'locked'} = 1;
84 # Unlock list with files, but only if _we_ locked it
89 # Return: Status code (Boolean)
94 my $lockfile = $self->{'lockfile'};
99 sysopen(LOCKFILE
,$lockfile,O_WRONLY
| O_CREAT
| O_TRUNC
) or return;
100 close(LOCKFILE
) or return;
102 $self->{'locked'} = 0;
106 # The list wasn't lock by us or it isn't locked at all
113 # Load the list with files from the list file
117 # Return: Status code (Boolean)
122 my $file = $self->{'listfile'};
125 # Read out the file and split the content line-per-line
127 sysopen(FILE
,$file,O_RDONLY
) or return;
128 read(FILE
, my $content, -s
$file);
129 close(FILE
) or return;
131 my @files = split(/\015\012|\012|\015/,$content);
133 # Remove useless lines
135 for(my $x=0;$x<@files;$x++)
137 if($files[$x] eq "" || $files[$x] =~ /^\s+$/)
140 $x--; # <-- very important!
144 $self->{'files'} = \
@files;
150 # Write the list with files back to the list file
154 # Return: Status code (Boolean)
159 my $file = $self->{'listfile'};
160 my $temp = $file.".temp";
161 my $files = $self->{'files'};
164 my $data = (@
$files) ?
join("\n",@
$files) : '';
166 sysopen(FILE
,$temp,O_WRONLY
| O_CREAT
| O_TRUNC
) or return;
167 print FILE
$data or do { close(FILE
); return };
168 close(FILE
) or return;
170 rename($temp,$file) or return;
177 # Add a file to the list
181 # Return: Status code (Boolean)
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));
198 # Remove a file from the list
202 # Return: Status code (Boolean)
206 my ($self,$file) = @_;
207 my $files = $self->{'files'};
209 # Check if the file is really in the list
211 return if($self->unused($file));
213 # Remove the file from the list
215 for(my $x=0;$x<@
$files;$x++)
217 if($files->[$x] eq $file)
219 splice(@
$files,$x,1);
227 # Remove all files from the list
237 $self->{'files'} = [];
244 # Check if a file is in the list
246 # Params: File to check
248 # Return: Status code (Boolean)
252 my ($self,$file) = @_;
253 my $files = $self->{'files'};
257 return 1 if($_ eq $file);
265 # Check if a file is not in the list
267 # Params: File to check
269 # Return: Status code (Boolean)
273 return not shift->in_use(shift);
276 # it's true, baby ;-)
patrick-canterino.de