- Timestamp:
- 2013-05-21T14:45:41+12:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/perllib/FileUtils.pm
r27327 r27376 71 71 ################################################################################ 72 72 73 ## @function differentFiles() 74 # 75 # this function returns -1 if either file is not found assumes that 76 # $file1 and $file2 are absolute file names or in the current 77 # directory $file2 is allowed to be newer than $file1 78 # 79 sub differentFiles 80 { 81 my ($file1, $file2, $verbosity) = @_; 82 $verbosity = 1 unless defined $verbosity; 83 84 $file1 =~ s/\/+$//; 85 $file2 =~ s/\/+$//; 86 87 my ($file1name) = $file1 =~ /\/([^\/]*)$/; 88 my ($file2name) = $file2 =~ /\/([^\/]*)$/; 89 90 return -1 unless (-e $file1 && -e $file2); 91 if ($file1name ne $file2name) 92 { 93 print STDERR "filenames are not the same\n" if ($verbosity >= 2); 94 return 1; 95 } 96 97 my @file1stat = stat ($file1); 98 my @file2stat = stat ($file2); 99 100 if (-d $file1) 101 { 102 if (! -d $file2) 103 { 104 print STDERR "one file is a directory\n" if ($verbosity >= 2); 105 return 1; 106 } 107 return 0; 108 } 109 110 # both must be regular files 111 unless (-f $file1 && -f $file2) 112 { 113 print STDERR "one file is not a regular file\n" if ($verbosity >= 2); 114 return 1; 115 } 116 117 # the size of the files must be the same 118 if ($file1stat[7] != $file2stat[7]) 119 { 120 print STDERR "different sized files\n" if ($verbosity >= 2); 121 return 1; 122 } 123 124 # the second file cannot be older than the first 125 if ($file1stat[9] > $file2stat[9]) 126 { 127 print STDERR "file is older\n" if ($verbosity >= 2); 128 return 1; 129 } 130 131 return 0; 132 } 133 ## differentFiles() 134 135 ## @function directoryExists() 136 # 137 sub directoryExists 73 74 ## @function canRead() 75 # 76 sub canRead 138 77 { 139 78 my ($filename_full_path) = @_; 140 return &fileTest($filename_full_path, '-d'); 141 } 142 ## directoryExists() 79 return &fileTest($filename_full_path, '-R'); 80 } 81 ## canRead() 82 143 83 144 84 ## @function closeFileHandle … … 150 90 } 151 91 ## closeFileHandle() 92 152 93 153 94 ## @function copyFiles() … … 200 141 } 201 142 ## copyFiles() 143 202 144 203 145 ## @function copyFilesRecursive() … … 275 217 } 276 218 ## copyFilesRecursive() 219 277 220 278 221 ## @function copyFilesRecursiveNoSVN() … … 354 297 ## copyFilesRecursiveNoSVN() 355 298 299 356 300 ## @function copyFilesRecursiveTopLevel() 357 301 # … … 432 376 ## copyFilesRecursiveTopLevel() 433 377 378 379 ## @function differentFiles() 380 # 381 # this function returns -1 if either file is not found assumes that 382 # $file1 and $file2 are absolute file names or in the current 383 # directory $file2 is allowed to be newer than $file1 384 # 385 sub differentFiles 386 { 387 my ($file1, $file2, $verbosity) = @_; 388 $verbosity = 1 unless defined $verbosity; 389 390 $file1 =~ s/\/+$//; 391 $file2 =~ s/\/+$//; 392 393 my ($file1name) = $file1 =~ /\/([^\/]*)$/; 394 my ($file2name) = $file2 =~ /\/([^\/]*)$/; 395 396 return -1 unless (-e $file1 && -e $file2); 397 if ($file1name ne $file2name) 398 { 399 print STDERR "filenames are not the same\n" if ($verbosity >= 2); 400 return 1; 401 } 402 403 my @file1stat = stat ($file1); 404 my @file2stat = stat ($file2); 405 406 if (-d $file1) 407 { 408 if (! -d $file2) 409 { 410 print STDERR "one file is a directory\n" if ($verbosity >= 2); 411 return 1; 412 } 413 return 0; 414 } 415 416 # both must be regular files 417 unless (-f $file1 && -f $file2) 418 { 419 print STDERR "one file is not a regular file\n" if ($verbosity >= 2); 420 return 1; 421 } 422 423 # the size of the files must be the same 424 if ($file1stat[7] != $file2stat[7]) 425 { 426 print STDERR "different sized files\n" if ($verbosity >= 2); 427 return 1; 428 } 429 430 # the second file cannot be older than the first 431 if ($file1stat[9] > $file2stat[9]) 432 { 433 print STDERR "file is older\n" if ($verbosity >= 2); 434 return 1; 435 } 436 437 return 0; 438 } 439 ## differentFiles() 440 441 442 ## @function directoryExists() 443 # 444 sub directoryExists 445 { 446 my ($filename_full_path) = @_; 447 return &fileTest($filename_full_path, '-d'); 448 } 449 ## directoryExists() 450 451 434 452 ## @function fileExists() 435 453 # … … 677 695 } 678 696 } 697 return 1; 679 698 } 680 699 ## makeAllDirectories() … … 857 876 my $fh_ref = shift(@_); 858 877 my $encoding = shift(@_); 859 my $mode_symbol = '<';860 if ($mode eq 'w' )878 my $mode_symbol; 879 if ($mode eq 'w' || $mode eq '>') 861 880 { 862 881 $mode_symbol = '>'; 882 $mode = 'writing'; 883 } 884 elsif ($mode eq 'a' || $mode eq '>>') 885 { 886 $mode_symbol = '>>'; 887 $mode = 'appending'; 888 } 889 else 890 { 891 $mode_symbol = '<'; 892 $mode = 'reading'; 863 893 } 864 894 if (defined $encoding) … … 868 898 if (!open($$fh_ref, $mode_symbol, $path)) 869 899 { 870 $mode = 'writing';871 if ($mode eq '>')872 {873 $mode = 'reading';874 }875 900 die("Error! Failed to open file for " . $mode . ": " . $path . "\n"); 876 901 }
Note:
See TracChangeset
for help on using the changeset viewer.