To participate you must create an account on apostrophenow.org. If you have already done so, click Login.

Changeset 4531

Show
Ignore:
Timestamp:
01/29/12 13:39:51 (4 months ago)
Author:
tboutell
Message:

Nonrecursive implementation of aFiles::rmRf can cope with seriously deep directory structures resulting from various fascinating former bugs

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • plugins/apostrophePlugin/branches/1.5/lib/toolkit/BaseaFiles.class.php

    r4529 r4531  
    350350   
    351351  /** 
    352    * Be careful with this, it follows symlinks if any. Mainly for stream wrappers 
     352   * Be careful with this, it follows symlinks if any. Mainly for stream wrappers. 
     353   * Recursion has been removed in favor of building up and emptying out a paths array 
     354   * in multiple passes so that we don't hit the "100 levels of recursion, bailing out" problem  
     355   * when removing a big, deeply screwed up directory structure (: 
    353356   */ 
    354357  static public function rmRf($path) 
    355358  { 
    356     $stat = @stat($path); 
    357     if (!$stat) 
    358     { 
    359       return; 
    360     } 
    361     if (aFiles::statIsDir($stat)) 
    362     { 
    363       $list = aFiles::ls($path); 
    364       foreach ($list as $file) 
    365       { 
    366         $filePath = "$path/$file"; 
    367         if (strlen($filePath) < strlen($path)) 
    368         { 
    369           throw new sfException("I almost tried to delete something higher up the original, I don't like this, bailing out"); 
    370         } 
    371         aFiles::rmRf($filePath); 
    372       } 
    373       if (!aFiles::rmdir($path)) 
     359    $originalPath = $path; 
     360    $paths = array(); 
     361    error_log("Removing $path"); 
     362    $paths[] = $path; 
     363    while (count($paths)) 
     364    { 
     365      $path = array_shift($paths); 
     366      $stat = @stat($path); 
     367      if (!$stat) 
    374368      { 
    375369        return false; 
    376370      } 
    377     } 
    378     else 
    379     { 
    380       if (!aFiles::unlink($path)) 
    381       { 
    382         return false; 
     371      if (aFiles::statIsDir($stat)) 
     372      { 
     373        $list = aFiles::ls($path); 
     374        foreach ($list as $file) 
     375        { 
     376          $filePath = "$path/$file"; 
     377          if (strlen($filePath) < strlen($originalPath)) 
     378          { 
     379            throw new sfException("I almost tried to delete something higher up than the original, I don't like this, bailing out"); 
     380          } 
     381          $paths[] = $filePath; 
     382        } 
     383        if (!aFiles::rmdir($path)) 
     384        { 
     385          return false; 
     386        } 
     387      } 
     388      else 
     389      { 
     390        if (!aFiles::unlink($path)) 
     391        { 
     392          return false; 
     393        } 
    383394      } 
    384395    } 
     
    427438  static public function sync($from, $to, $options = array()) 
    428439  { 
     440    error_log("Syncing $from to $to"); 
    429441    // Let's be verbose for this first big scary migration on staging 
    430442    $fromList = aFiles::ls($from);