Changeset 27481

Show
Ignore:
Timestamp:
27.05.2013 12:27:31 (6 years ago)
Author:
jmt12
Message:

Adding makeAllDirectories() (which I'd only implemented in LocalFS) to FileUtils? (which in turn calls the Driver specific makeDirectory() recursively) and added test for this function

Location:
gs2-extensions/parallel-building/trunk/src
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • gs2-extensions/parallel-building/trunk/src/bin/script/test_fileutils.pl

    r27480 r27481  
    1010my $display_errors = 0; 
    1111 
    12 my $test_localfs = 1; 
     12my $test_localfs = 0; 
    1313my $test_hdthriftfs = 1; 
    14 my $test_hdfsshell = 1; 
     14my $test_hdfsshell = 0; 
    1515 
    1616# Globals 
     
    467467  $pass_count += &testAction(\$test_count, 'makeDirectory() to create a new directory', 1, &FileUtils::makeDirectory($path)); 
    468468  $pass_count += &testAction(\$test_count, 'makeDirectory() for an existing directory', 1, &FileUtils::makeDirectory($path)); 
     469 
     470  my $multiple_dirs_path = $path . '/foo/bar/wibble'; 
     471  $pass_count += &testAction(\$test_count, 'makeAllDirectories() to create several nested directories', 1, &FileUtils::makeAllDirectories($multiple_dirs_path)); 
     472  &FileUtils::removeFilesRecursive($path . '/foo'); 
    469473} 
    470474 
  • gs2-extensions/parallel-building/trunk/src/perllib/FileUtils.pm

    r27421 r27481  
    398398 
    399399 
     400## @function makeAllDirectories() 
     401# 
     402# in case anyone cares - I did some testing (using perls Benchmark module) 
     403# on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently 
     404# slightly faster (surprisingly) - Stefan. 
     405# 
     406sub makeAllDirectories 
     407{ 
     408  my ($raw_dir) = @_; 
     409  # use / for the directory separator, remove duplicate and 
     410  # trailing slashes 
     411  &sanitizePath($raw_dir); 
     412  # ensure the directory doesn't already exist 
     413  if (&directoryExists($raw_dir)) 
     414  { 
     415    return 0; 
     416  } 
     417  if ($raw_dir =~ /^(.+?:\/\/)(.*)/) 
     418  { 
     419    my $dirsofar = ''; 
     420    if (defined $1) 
     421    { 
     422      $dirsofar = $1; 
     423    } 
     424    my $dir = $2; 
     425    my $first = 1; 
     426    foreach my $dirname (split ("/", $dir)) 
     427    { 
     428      $dirsofar .= "/" unless $first; 
     429      $first = 0; 
     430      $dirsofar .= $dirname; 
     431      next if $dirname =~ /^(|[a-z]:)$/i; 
     432      if (!-e $dirsofar) 
     433      { 
     434        my $mkdir_ok = &makeDirectory($dirsofar); 
     435        if (!$mkdir_ok) 
     436        { 
     437          &FileUtils::printError('Could not create directory: ' . $dirsofar); 
     438          return 0; 
     439        } 
     440      } 
     441    } 
     442  } 
     443  return (&directoryExists($raw_dir)); 
     444} 
     445## makeAllDirectories() 
     446 
     447 
    400448## @function sanitizePath() 
    401449# 
     
    543591## isSymbolicLink() 
    544592 
    545 # /** @function makeAllDirectories 
    546 #  */ 
    547 sub makeAllDirectories 
    548 { 
    549   my $path = shift(@_); 
    550   my $driver = &FileUtils::_determineDriver($path); 
    551   return &FileUtils::_callFunction($driver, 'makeAllDirectories', $path); 
    552 } 
    553 # /** makeAllDirectories() **/ 
    554593 
    555594# /** 
  • gs2-extensions/parallel-building/trunk/src/perllib/FileUtils/HDThriftFS.pm

    r27478 r27481  
    130130    if ($path !~ /HDThriftFS:\/\//) 
    131131    { 
    132       &_printError('Not a valid thrift URI: ' . $path); 
     132      &FileUtils::printError('Not a valid thrift URI: ' . $path); 
    133133    } 
    134134    else 
  • gs2-extensions/parallel-building/trunk/src/perllib/FileUtils/LocalFS.pm

    r27422 r27481  
    215215} 
    216216# /** linkFile() **/ 
    217  
    218 ## @function makeAllDirectories() 
    219 # 
    220 # in case anyone cares - I did some testing (using perls Benchmark module) 
    221 # on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently 
    222 # slightly faster (surprisingly) - Stefan. 
    223 # 
    224 sub makeAllDirectories 
    225 { 
    226   my ($dir) = @_; 
    227  
    228   # use / for the directory separator, remove duplicate and 
    229   # trailing slashes 
    230   $dir=~s/[\\\/]+/\//g; 
    231   $dir=~s/[\\\/]+$//; 
    232  
    233   # ensure the directory doesn't already exist 
    234   if (-e $dir) 
    235   { 
    236     return 0; 
    237   } 
    238  
    239   # make sure the cache directory exists 
    240   my $dirsofar = ""; 
    241   my $first = 1; 
    242   foreach my $dirname (split ("/", $dir)) 
    243   { 
    244     $dirsofar .= "/" unless $first; 
    245     $first = 0; 
    246  
    247     $dirsofar .= $dirname; 
    248  
    249     next if $dirname =~ /^(|[a-z]:)$/i; 
    250     if (!-e $dirsofar) 
    251     { 
    252       my $store_umask = umask(0002); 
    253       my $mkdir_ok = mkdir ($dirsofar, 0777); 
    254       umask($store_umask); 
    255       if (!$mkdir_ok) 
    256       { 
    257         &FileUtils::printError('Could not create directory: ' . $dirsofar); 
    258         return 0; 
    259       } 
    260     } 
    261   } 
    262   return (-e $dir); 
    263 } 
    264 ## makeAllDirectories() 
    265  
    266217 
    267218## @function makeDirectory()