]> git.p6c8.net - devedit.git/blob - modules/File/UseList.pm
22d1bcc87320a2568decae829eabfa64d68e101b
[devedit.git] / modules / File / UseList.pm
1 package File::UseList;
2
3 #
4 # File::UseList 1.1.1
5 #
6 # Run a list with files that are currently in use
7 # (bases on Filing::UseList by Roland Bluethgen <calocybe@web.de>)
8 #
9 # Author: Patrick Canterino <patshaping@gmx.net>
10 # Last modified: 2003-10-17
11 #
12
13 use strict;
14
15 use Carp qw(croak);
16
17 # new()
18 #
19 # Constructor
20 #
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)
24 #
25 # Return: File::UseList object (Blessed Reference)
26
27 sub new(%)
28 {
29 my ($class,%args) = @_;
30
31 # Check if we got all the necessary information
32
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
36
37 # Add some other information
38
39 $args{'files'} = [];
40 $args{'locked'} = 0;
41
42 return bless(\%args,$class);
43 }
44
45 # lock()
46 #
47 # Lock list with files
48 # (delete lock file)
49 #
50 # Params: -nothing-
51 #
52 # Return: Status code (Boolean)
53
54 sub lock
55 {
56 my $self = shift;
57 my $lockfile = $self->{'lockfile'};
58 my $timeout = $self->{'timeout'};
59
60 return 1 if($self->{'locked'});
61
62 # Try to delete the lock file one time per second
63 # until the timeout is reached
64
65 for(my $x=$timeout;$x>=0;$x--)
66 {
67 if(unlink($lockfile))
68 {
69 $self->{'locked'} = 1;
70 return 1;
71 }
72
73 sleep(1);
74 }
75
76 # Timeout
77
78 return;
79 }
80
81 # unlock()
82 #
83 # Unlock list with files, but only if _we_ locked it
84 # (create lock file)
85 #
86 # Params: -nothing-
87 #
88 # Return: Status code (Boolean)
89
90 sub unlock
91 {
92 my $self = shift;
93 my $lockfile = $self->{'lockfile'};
94 local *LOCKFILE;
95
96 if($self->{'locked'})
97 {
98 unless(-f $lockfile)
99 {
100 open(LOCKFILE,">",$lockfile) or return;
101 close(LOCKFILE) or return;
102 }
103
104 $self->{'locked'} = 0;
105 return 1;
106 }
107
108 # The list wasn't lock by us or it isn't locked at all
109
110 return;
111 }
112
113 # load()
114 #
115 # Load the list with files from the list file
116 #
117 # Params: -nothing-
118 #
119 # Return: Status code (Boolean)
120
121 sub load
122 {
123 my $self = shift;
124 my $file = $self->{'listfile'};
125 local *FILE;
126
127 # Read out the file and split the content line-per-line
128
129 open(FILE,"<",$file) or return;
130 read(FILE, my $content, -s $file);
131 close(FILE) or return;
132
133 my @files = split(/\015\012|\012|\015/,$content);
134
135 # Remove useless lines
136
137 for(my $x=0;$x<@files;$x++)
138 {
139 if($files[$x] eq "" || $files[$x] =~ /^\s+$/)
140 {
141 splice(@files,$x,1);
142 $x--; # <-- very important!
143 }
144 }
145
146 $self->{'files'} = \@files;
147 return 1;
148 }
149
150 # save()
151 #
152 # Write the list with files back to the list file
153 #
154 # Params: -nothing-
155 #
156 # Return: Status code (Boolean)
157
158 sub save
159 {
160 my $self = shift;
161 my $file = $self->{'listfile'};
162 my $temp = $file.".temp";
163 my $files = $self->{'files'};
164 local *FILE;
165
166 my $data = (@$files) ? join("\n",@$files) : '';
167
168 open(FILE,">",$temp) or return;
169 print FILE $data or do { close(FILE); return };
170 close(FILE) or return;
171
172 rename($temp,$file) or return;
173
174 return 1;
175 }
176
177 # add_file()
178 #
179 # Add a file to the list
180 #
181 # Params: File
182
183 sub add_file($)
184 {
185 my ($self,$file) = @_;
186 my $files = $self->{'files'};
187
188 # Check if the file is already in the list
189
190 return if($self->in_use($file));
191
192 push(@$files,$file);
193 }
194
195 # remove_file()
196 #
197 # Remove a file from the list
198 #
199 # Params: File
200
201 sub remove_file($)
202 {
203 my ($self,$file) = @_;
204 my $files = $self->{'files'};
205
206 # Check if the file is really in the list
207
208 return if($self->unused($file));
209
210 # Remove the file from the list
211
212 for(my $x=0;$x<@$files;$x++)
213 {
214 if($files->[$x] eq $file)
215 {
216 splice(@$files,$x,1);
217 last;
218 }
219 }
220 }
221
222 # in_use()
223 #
224 # Check if a file is in the list
225 #
226 # Params: File to check
227 #
228 # Return: Status code (Boolean)
229
230 sub in_use($)
231 {
232 my ($self,$file) = @_;
233 my $files = $self->{'files'};
234
235 foreach(@$files)
236 {
237 return 1 if($_ eq $file);
238 }
239
240 return;
241 }
242
243 # unused()
244 #
245 # Check if a file is not in the list
246 #
247 # Params: File to check
248 #
249 # Return: Status code (Boolean)
250
251 sub unused($)
252 {
253 return not shift->in_use(shift);
254 }
255
256 # it's true, baby ;-)
257
258 1;
259
260 #
261 ### End ###

patrick-canterino.de