MDL-12725 remove_dir() does not return correct status and fails if dir does not exist + improved sanity test in check_dir_exists(); merged from MOODLE_19_STABLE

This commit is contained in:
skodak 2007-12-24 21:16:30 +00:00
parent ba7d966bae
commit 4ef8e74c53

View file

@ -7491,25 +7491,33 @@ function apd_get_profiling() {
return shell_exec('pprofp -u ' . ini_get('apd.dumpdir') . '/pprof.' . getmypid() . '.*'); return shell_exec('pprofp -u ' . ini_get('apd.dumpdir') . '/pprof.' . getmypid() . '.*');
} }
/**
* Delete directory or only it's content
* @param string $dir directory path
* @param bool $content_only
* @return bool success, true also if dir does not exist
*/
function remove_dir($dir, $content_only=false) { function remove_dir($dir, $content_only=false) {
// if content_only=true then delete all but if (!file_exists($dir)) {
// the directory itself // nothing to do
return true;
}
$handle = opendir($dir); $handle = opendir($dir);
$result = true;
while (false!==($item = readdir($handle))) { while (false!==($item = readdir($handle))) {
if($item != '.' && $item != '..') { if($item != '.' && $item != '..') {
if(is_dir($dir.'/'.$item)) { if(is_dir($dir.'/'.$item)) {
remove_dir($dir.'/'.$item); $result = remove_dir($dir.'/'.$item) && $result;
}else{ }else{
unlink($dir.'/'.$item); $result = unlink($dir.'/'.$item) && $result;
} }
} }
} }
closedir($handle); closedir($handle);
if ($content_only) { if ($content_only) {
return true; return $result;
} }
return rmdir($dir); return rmdir($dir); // if anything left the result will be false, noo need for && $result
} }
/** /**
@ -7525,7 +7533,7 @@ function check_dir_exists($dir, $create=false, $recursive=false) {
global $CFG; global $CFG;
if (strstr($dir, $CFG->dataroot) === false) { if (strstr($dir, $CFG->dataroot.'/') === false) {
debugging('Warning. Wrong call to check_dir_exists(). $dir must be an absolute path under $CFG->dataroot ("' . $dir . '" is incorrect)', DEBUG_DEVELOPER); debugging('Warning. Wrong call to check_dir_exists(). $dir must be an absolute path under $CFG->dataroot ("' . $dir . '" is incorrect)', DEBUG_DEVELOPER);
} }