VBTK/File.pm untested

filter: Statementstmt 0.0% 0 / 231 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 24 totaltotal 0.0% 0 / 417 CC 31 SCAR 29.1
CC
31
Cov
0%
put
42.0
sync
42.0
hasChanged
30.0
CRAP 18.3
1 #! /bin/perl
2 #############################################################################
3 #
4 # NOTE: This file under revision control using RCS
5 # Any changes made without RCS will be lost
6 #
7 # $Source: /usr/local/cvsroot/vbtk/VBTK/File.pm,v $
8 # $Revision: 1.12 $
9 # $Date: 2002/03/04 20:53:06 $
10 # $Author: bhenry $
11 # $Locker: $
12 # $State: Exp $
13 #
14 # Purpose: A perl library used to handle interaction with
15 # files.
16 #
17 # Copyright (C) 1996 - 2002 Brent Henry
18 #
19 # This program is free software; you can redistribute it and/or
20 # modify it under the terms of version 2 of the GNU General Public
21 # License as published by the Free Software Foundation available at:
22 # http://www.gnu.org/copyleft/gpl.html
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 #############################################################################
30 #
31 #
32 # REVISION HISTORY:
33 #
34 # $Log: File.pm,v $
35 # Revision 1.12 2002/03/04 20:53:06 bhenry
36 # *** empty log message ***
37 #
38 # Revision 1.11 2002/03/04 16:49:09 bhenry
39 # Changed requirement back to perl 5.6.0
40 #
41 # Revision 1.10 2002/03/02 00:53:54 bhenry
42 # Documentation updates
43 #
44 # Revision 1.9 2002/02/20 19:25:18 bhenry
45 # *** empty log message ***
46 #
47 # Revision 1.8 2002/02/19 19:06:44 bhenry
48 # *** empty log message ***
49 #
50 # Revision 1.7 2002/02/13 07:41:43 bhenry
51 # *** empty log message ***
52 #
53 # Revision 1.6 2002/02/09 08:46:28 bhenry
54 # Added several methods to assist in syncing files
55 #
56 # Revision 1.5 2002/02/08 02:15:26 bhenry
57 # Stopped closing STDOUT, so output wouldn't get chopped off
58 #
59 # Revision 1.4 2002/01/25 07:17:15 bhenry
60 # Added serPut and serGet methods
61 #
62 #
63  
64 require 5.004;
65  
66 package VBTK::File;
67  
68 use 5.6.0;
69 use strict;
70 use warnings;
71 # I like using undef as a value so I'm turning off the uninitialized warnings
72 no warnings qw(uninitialized);
73  
74 use VBTK::Common;
75 use FileHandle;
76 use Algorithm::Diff qw(diff);
77 use File::Basename;
78 use File::Path;
79 use Storable qw(store retrieve);
80  
81 our $VERBOSE=$ENV{'VERBOSE'};
82  
83 #-------------------------------------------------------------------------------
84 # Function: new
85 # Description: Object constructor. Allocates memory for all class members
86 # Input Parms: Configuration filename
87 # Output Parms: Pointer to class
88 #-------------------------------------------------------------------------------
89 sub new
90 {
91 my $type = shift;
92 my $self = {};
93 bless $self, $type;
94  
95 my ($fileName) = @_;
96  
97 log("Setting up FileCache object for '$fileName'") if ($VERBOSE > 3);
98  
99 # Store the file name
100 $self->setFileName($fileName);
101 $self->{basePath} = $::VBHOME;
102  
103 return $self;
104 }
105  
106 #-------------------------------------------------------------------------------
107 # Function: getCache
108 # Description: Retrieve the contents of the associated file, using cached data
109 # if possible.
110 # Input Parms: None
111 # Output Parms: Contents of file
112 #-------------------------------------------------------------------------------
113 sub getCache
114 {
115 my $self = shift;
116  
117 $self->load() || return undef;
118 $self->{contents};
119 }
120  
121 #-------------------------------------------------------------------------------
122 # Function: load
123 # Description: Load the file contents into the object, only after checking to
124 # see if it changed since the last time.
125 # Input Parms: None
126 # Output Parms: Return code 1 - Success, or 0 - Failure
127 #-------------------------------------------------------------------------------
128 sub load
129 {
130 my $self = shift;
131 my $fileName = $self->{fileName};
132 my $contents = $self->{contents};
133 my $mtime = $self->{mtime};
134 my $size = $self->{size};
135  
136 my ($currSize,$currMtime) = (stat($fileName))[7,9];
137  
138 # Check for file access error
139 if (! defined $currSize)
140 {
141 &error("Can't stat file '$fileName'");
142 return 0;
143 }
144  
145 # If the file has changed, or the contents were never loaded, then reload
146 # the contents
147 if ((! defined $contents)||($currMtime != $mtime)||($currSize != $size))
148 {
149 $contents = $self->get;
150 return 0 if (! defined $contents);
151  
152 $self->{contents} = $contents;
153 $self->{mtime} = $currMtime;
154 $self->{size} = $currSize;
155 }
156  
157 1;
158 }
159  
160 #-------------------------------------------------------------------------------
161 # Function: loadStat
162 # Description: Run stat and retrieve the size and mtime of the file. Store them
163 # in the object.
164 # Input Parms: None
165 # Output Parms: size, mtime
166 #-------------------------------------------------------------------------------
167 sub loadStat
168 {
169 my $self = shift;
170 my $fileName = $self->{fileName};
171
172 my ($size,$mtime) = (stat($fileName))[7,9];
173
174 $self->{mtime} = $mtime;
175 $self->{size} = $size;
176
177 ($size,$mtime);
178 }
179  
180 #-------------------------------------------------------------------------------
181 # Function: hasChanged
182 # Description: Run stat and compare the current mtime and size with the stored
183 # mtime and size.
184 # Input Parms: None
185 # Output Parms: 1 or 0.
186 #-------------------------------------------------------------------------------
187 sub hasChanged
188 {
189 my $self = shift;
190 my $fileName = $self->{fileName};
191  
192 # Try to load the file now, if it's not already loaded
193 if (! defined $self->{mtime})
194 {
195 $self->loadStat() || return 0;
196 }
197
198 my $mtime = $self->{mtime};
199 my $size = $self->{size};
200 my $target = shift || $fileName;
201 my $dirName = &dirname($target);
202  
203 &log("Checking to see if '$target' has changed") if ($VERBOSE > 2);
204
205 my ($currSize,$currMtime) = (stat($target))[7,9];
206  
207 if(($currSize != $size)||($currMtime != $mtime))
208 {
209 &log("File '$target' has changed or does not exist") if ($VERBOSE > 2);
210 (1);
211 }
212 else
213 {
214 (0);
215 }
216 }
217  
218 #-------------------------------------------------------------------------------
219 # Function: isNewer
220 # Description: Run stat and compare the current mtime with the stored mtime.
221 # Input Parms: None
222 # Output Parms: 1 or 0.
223 #-------------------------------------------------------------------------------
224 sub isNewer
225 {
226 my $self = shift;
227 my $fileName = $self->{fileName};
228 my $mtime = $self->{mtime};
229  
230 &log("Checking to see if '$fileName' is newer") if ($VERBOSE > 2);
231
232 my ($currSize,$currMtime) = (stat($fileName))[7,9];
233  
234 if($currMtime > $mtime)
235 {
236 &log("File '$fileName' is newer than original file") if ($VERBOSE > 2);
237 (1);
238 }
239 else
240 {
241 (0);
242 }
243 }
244  
245 #-------------------------------------------------------------------------------
246 # Function: exists
247 # Description: Check to see if the associated file exists
248 # Input Parms: None
249 # Output Parms: Contents of file
250 #-------------------------------------------------------------------------------
251 sub exists
252 {
253 my $self = shift;
254 my $fileName = $self->{fileName};
255  
256 (-f $fileName);
257 }
258  
259 #-------------------------------------------------------------------------------
260 # Function: get
261 # Description: Retrieve the contents of the associated file.
262 # Input Parms: None
263 # Output Parms: Contents of file
264 #-------------------------------------------------------------------------------
265 sub get
266 {
267 my $self = shift;
268 my $fileName = $self->{fileName};
269 my ($fh);
270  
271 $fh = new FileHandle "< $fileName";
272  
273 unless (defined $fh)
274 {
275 &error("Can't open file '$fileName'");
276 return undef;
277 }
278  
279 # Slurp mode
280 local $/;
281  
282 my $contents = <$fh>;
283 $fh->close;
284  
285 ($contents);
286 }
287  
288 #-------------------------------------------------------------------------------
289 # Function: serGet
290 # Description: Retrieve data from the specified file and attempt to thaw it into
291 # a reference.
292 # Input Parms: None
293 # Output Parms: Reference to thawed structure
294 #-------------------------------------------------------------------------------
295 sub serGet
296 {
297 my $self = shift;
298 my $fileName = $self->{fileName};
299
300 my $ref = retrieve($fileName);
301
302 ($ref);
303 }
304  
305 #-------------------------------------------------------------------------------
306 # Function: put
307 # Description: Write out the passed text to the associated file.
308 # Input Parms: File text
309 # Output Parms: None
310 #-------------------------------------------------------------------------------
311 sub put
312 {
313 my $self = shift;
314 my $fileName = $self->{fileName};
315 my $baseName = $self->{baseName};
316 my $dirName = $self->{dirName};
317  
318 my (@data) = @_;
319  
320 my $suffix=$$;
321 my ($tmpFile);
322
323 # Come up with a non-existant tempfile name
324 do { $tmpFile = "$dirName/.tmp$baseName." . $suffix++; } while (-f $tmpFile);
325  
326 &log("Writing to '$tmpFile'") if ($VERBOSE > 2);
327  
328 # Open the filehandle
329 my $fh = new FileHandle "> $tmpFile";
330  
331 # Check for errors
332 unless ($fh)
333 {
334 &error("Can't write to file '$tmpFile'");
335 return undef;
336 }
337  
338 # Step through each data element passed, dumping it out
339 # to a file.
340 foreach (@data)
341 {
342 print $fh (ref($_)) ? join('',@{$_}) : $_;
343 }
344  
345 $fh->close;
346  
347 &log("Renaming '$tmpFile' to '$fileName'") if ($VERBOSE > 2);
348  
349 # Now rename the temp file to the original filename
350 unless( rename $tmpFile, $fileName )
351 {
352 &error("Can't rename '$tmpFile' to '$fileName'");
353 return undef;
354 }
355  
356 # Clear out the object members related to the file
357 $self->{contents} = undef;
358 $self->{mtime} = undef;
359 $self->{size} = undef;
360  
361 (1);
362 }
363  
364 #-------------------------------------------------------------------------------
365 # Function: sync
366 # Description: Sync the local file using the data in the object structure. This
367 # includes updating the contents and mtime.
368 # Input Parms: None
369 # Output Parms: Success - 1 or Failure - 0
370 #-------------------------------------------------------------------------------
371 sub sync
372 {
373 my $self = shift;
374 my $fileName = $self->{fileName};
375  
376 # Try to load the file now, if it's not already loaded
377 if (! defined $self->{mtime} || ! defined $self->{contents})
378 {
379 $self->load() || return 0;
380 }
381  
382 my $mtime = $self->{mtime};
383 my $target = shift || $fileName;
384 my $dirName = &dirname($target);
385  
386 my ($fh);
387  
388 &log("Attempting to sync '$target'") if ($VERBOSE > 1);
389  
390 if((! -d $dirName)&&(mkpath([$dirName]))&&(! -d $dirName))
391 {
392 &error("Can't create directory '$dirName'");
393 return 0;
394 }
395  
396 # Dump the contents of the object to the file
397 unless($fh = new FileHandle "> $target")
398 {
399 &error("Can't write to '$target', skipping sync");
400 return 0;
401 }
402
403 print $fh $self->{contents};
404 $fh->close();
405  
406 # Now update the mtime on the file
407 unless( utime $mtime, $mtime, $target )
408 {
409 &error("Can't update mtime on '$target'");
410 return 0;
411 }
412  
413 1;
414 }
415  
416 #-------------------------------------------------------------------------------
417 # Function: serPut
418 # Description: Serialize the passed reference and write the result out to the
419 # specified file.
420 # Input Parms: Reference to data structure
421 # Output Parms: None
422 #-------------------------------------------------------------------------------
423 sub serPut
424 {
425 my $self = shift;
426 my $fileName = $self->{fileName};
427 my $baseName = $self->{baseName};
428 my $dirName = $self->{dirName};
429  
430 my ($ref) = @_;
431  
432 my $suffix=$$;
433 my ($tmpFile);
434  
435 # Come up with a non-existant tempfile name
436 do { $tmpFile = "$dirName/.tmp$baseName." . $suffix++; } while (-f $tmpFile);
437  
438 &log("Writing to '$tmpFile'") if ($VERBOSE > 2);
439  
440 # Open the filehandle
441 unless(&store ($ref,$tmpFile))
442 {
443 &error("Can't write to file '$tmpFile'");
444 return undef;
445 }
446  
447 &log("Renaming '$tmpFile' to '$fileName'") if ($VERBOSE > 2);
448  
449 # Now rename the temp file to the original filename
450 unless( rename $tmpFile, $fileName )
451 {
452 &error("Can't rename '$tmpFile' to '$fileName'");
453 return undef;
454 }
455  
456 # Clear out the object members related to the file
457 $self->{contents} = undef;
458 $self->{mtime} = undef;
459 $self->{size} = undef;
460  
461 (1);
462 }
463  
464 #-------------------------------------------------------------------------------
465 # Function: unlink
466 # Description: Delete the associated file.
467 # Input Parms: None
468 # Output Parms: None
469 #-------------------------------------------------------------------------------
470 sub unlink
471 {
472 my $self = shift;
473 my $fileName = $self->{fileName};
474  
475 unlink $fileName;
476 }
477  
478 #-------------------------------------------------------------------------------
479 # Function: doDiff
480 # Description: Run a unix diff between this object's file and the passed text
481 # Input Parms: Text to compare with
482 # Output Parms: Output of diff
483 #-------------------------------------------------------------------------------
484 sub doDiff
485 {
486 my $self = shift;
487 my $cmpText = shift;
488 my $objText = $self->get || return undef;
489 my ($chunk,$line,$diffText,$sign,$lineno,$text);
490  
491 my @objLines = split(/\n/,$objText);
492 my @cmpLines = split(/\n/,$cmpText);
493  
494 my $diffList = diff(\@objLines, \@cmpLines);
495  
496 foreach $chunk (@$diffList)
497 {
498 foreach $line (@$chunk)
499 {
500 ($sign, $lineno, $text) = @$line;
501 $diffText .= sprintf "%4d$sign %s\n", $lineno+1, $text;
502 }
503 $diffText .= "--------\n";
504 }
505 $diffText;
506 }
507  
508 #-------------------------------------------------------------------------------
509 # Function: setFileName
510 # Description: Override the filename of the object to be the passed value.
511 # Input Parms: FileName
512 # Output Parms: None
513 #-------------------------------------------------------------------------------
514 sub setFileName
515 {
516 my $self = shift || return undef;
517 my $fileName = shift || return undef;
518  
519 $self->{fileName} = $fileName;
520 $self->{baseName} = &basename($fileName);
521 $self->{dirName} = &dirname($fileName);
522
523 (1);
524 }
525  
526 #-------------------------------------------------------------------------------
527 # Function: setBasePath
528 # Description: Store a base path for the file
529 # Input Parms: Base Path
530 # Output Parms: None
531 #-------------------------------------------------------------------------------
532 sub setBasePath
533 {
534 my $self = shift || return undef;
535 $self->{basePath} = shift || return undef;
536
537 (1);
538 }
539  
540 #-------------------------------------------------------------------------------
541 # Function: changeBasePath
542 # Description: Check to see if the passed base path is the same as the original
543 # base path. If not, then change the path of the filename to use
544 # the new base path.
545 # Input Parms: New Base Path
546 # Output Parms: None
547 #-------------------------------------------------------------------------------
548 sub changeBasePath
549 {
550 my $self = shift;
551 my $newBasePath = shift;
552 my $origBasePath = $self->{basePath};
553 my $fileName = $self->{fileName};
554
555 return 1 if ($newBasePath eq $origBasePath);
556
557 unless($origBasePath)
558 {
559 &error("Can't change base path when origBasePath was never set!");
560 return undef;
561 }
562
563 unless($fileName =~ s/^$origBasePath/$newBasePath/)
564 {
565 &error("Invalid base path '$origBasePath' specified for '$fileName'");
566 return undef;
567 }
568  
569 # Store the original filename as well as the new
570 $self->{origFileName} = $self->{fileName};
571 $self->setFileName($fileName);
572
573 &log("Altered base path from '$self->{origFileName}' to '$fileName'")
574 if ($VERBOSE > 2);
575  
576 (1);
577 }
578  
579 # Simple get methods
580 sub getBaseName { $_[0]->{baseName}; }
581 sub getDirName { $_[0]->{dirName}; }
582 sub getFileName { $_[0]->{fileName}; }
583 sub getOrigFileName { $_[0]->{origFileName} || $_[0]->{fileName}; }
584 sub getMtime { $_[0]->{mtime}; }
585 sub getSize { $_[0]->{size}; }
586 sub getContents { $_[0]->{contents}; }
587  
588 1;
589 __END__