Skip to content

Commit

Permalink
* recursive dst creation at copyDirectory.
Browse files Browse the repository at this point in the history
* dir access mode is set by chmod, again (note: if dst is created recursively, then only last dir will have newDirMode access).
  • Loading branch information
Konstantin G Romanov committed Aug 7, 2012
1 parent f763dba commit 8381505
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions framework/utils/CFileHelper.php
Expand Up @@ -32,7 +32,7 @@ public static function getExtension($path)

/**
* Copies a directory recursively as another.
* If the destination directory does not exist, it will be created.
* If the destination directory does not exist, it will be created recursively.
* @param string $src the source directory
* @param string $dst the destination directory
* @param array $options options for directory copy. Valid options are:
Expand All @@ -56,6 +56,8 @@ public static function copyDirectory($src,$dst,$options=array())
$exclude=array();
$level=-1;
extract($options);
if(!is_dir($dst)) self::mkdir($dst, self::getModeFromOptions($options), true);

self::copyDirectoryRecursive($src,$dst,'',$fileTypes,$exclude,$level,$options);
}

Expand Down Expand Up @@ -110,12 +112,7 @@ public static function findFiles($dir,$options=array())
*/
protected static function copyDirectoryRecursive($src,$dst,$base,$fileTypes,$exclude,$level,$options)
{
if(!is_dir($dst))
{
$oldumask=umask(0);
mkdir($dst, isset($options['newDirMode']) ? $options['newDirMode'] : 0777, true);
umask($oldumask);
}
if(!is_dir($dst)) self::mkdir($dst, self::getModeFromOptions($options), false);

$folder=opendir($src);
while(($file=readdir($folder))!==false)
Expand Down Expand Up @@ -259,4 +256,34 @@ public static function getMimeTypeByExtension($file,$magicFile=null)
}
return null;
}

/**
* Creates directory for $dst path with $mode and $recursive creation is allowed.
* For concurrent compatibility chmod is used instead of mkdir's $mode.
*
* @static
* @param string $dst path to be created
* @param int $mode access bitmask
* @param bool $recursive
* @return bool result of mkdir
* @see \mkdir
*/
private static function mkdir($dst, $mode, $recursive)
{
$res = mkdir($dst, $mode, $recursive);
@chmod($dst, $mode);
return $res;
}

/**
* Returns dir access mode from options, if set, or default value (0777).
* @static
* @param array $options
* @return int
*/
private static function getModeFromOptions(array $options)
{
return isset($options['newDirMode']) ? $options['newDirMode'] : 0777;
}

}

0 comments on commit 8381505

Please sign in to comment.