Changeset 27376

Show
Ignore:
Timestamp:
21.05.2013 14:45:41 (6 years ago)
Author:
jmt12
Message:

Added a canRead() function, make openFileHandle() a bit smarter in terms of accepting append mode, and moved some functions around as my OCD wouldn't allow me to leave them in nonalphabetic order

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/FileUtils.pm

    r27327 r27376  
    7171################################################################################ 
    7272 
    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# 
     76sub canRead 
    13877{ 
    13978  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 
    14383 
    14484## @function closeFileHandle 
     
    15090} 
    15191## closeFileHandle() 
     92 
    15293 
    15394## @function copyFiles() 
     
    200141} 
    201142## copyFiles() 
     143 
    202144 
    203145## @function copyFilesRecursive() 
     
    275217} 
    276218## copyFilesRecursive() 
     219 
    277220 
    278221## @function copyFilesRecursiveNoSVN() 
     
    354297## copyFilesRecursiveNoSVN() 
    355298 
     299 
    356300## @function copyFilesRecursiveTopLevel() 
    357301# 
     
    432376## copyFilesRecursiveTopLevel() 
    433377 
     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# 
     385sub 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# 
     444sub directoryExists 
     445{ 
     446  my ($filename_full_path) = @_; 
     447  return &fileTest($filename_full_path, '-d'); 
     448} 
     449## directoryExists() 
     450 
     451 
    434452## @function fileExists() 
    435453# 
     
    677695    } 
    678696  } 
     697 return 1; 
    679698} 
    680699## makeAllDirectories() 
     
    857876  my $fh_ref = shift(@_); 
    858877  my $encoding = shift(@_); 
    859   my $mode_symbol = '<'; 
    860   if ($mode eq 'w') 
     878  my $mode_symbol; 
     879  if ($mode eq 'w' || $mode eq '>') 
    861880  { 
    862881    $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'; 
    863893  } 
    864894  if (defined $encoding) 
     
    868898  if (!open($$fh_ref, $mode_symbol, $path)) 
    869899  { 
    870     $mode = 'writing'; 
    871     if ($mode eq '>') 
    872     { 
    873       $mode = 'reading'; 
    874     } 
    875900    die("Error! Failed to open file for " . $mode . ": " . $path . "\n"); 
    876901  }