From bfa8ff63017caa505ed984a9aa33e8446cab10c8 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Thu, 23 May 2013 19:15:47 +1000 Subject: [PATCH] MDL-39177 make sure we don't remove existing directories when cleaning up unzip --- repository/draftfiles_ajax.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/repository/draftfiles_ajax.php b/repository/draftfiles_ajax.php index 9a8acfa0dab..e50544ab836 100644 --- a/repository/draftfiles_ajax.php +++ b/repository/draftfiles_ajax.php @@ -219,6 +219,8 @@ switch ($action) { // Find unused name for directory to extract the archive. $temppath = $fs->get_unused_dirname($user_context->id, 'user', 'draft', $draftid, $filepath. pathinfo($filename, PATHINFO_FILENAME). '/'); + $donotremovedirs = array(); + $doremovedirs = array($temppath); // Extract archive and move all files from $temppath to $filepath if ($file->extract_to_storage($zipper, $user_context->id, 'user', 'draft', $draftid, $temppath, $USER->id) !== false) { $extractedfiles = $fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $temppath, true); @@ -233,8 +235,12 @@ switch ($action) { // File or directory did not exist, just move it. $file->rename($realpath, $file->get_filename()); } else if (!$file->is_directory()) { - // File already existed, overwrite it (nothing to do with existing directories). + // File already existed, overwrite it repository::overwrite_existing_draftfile($draftid, $realpath, $file->get_filename(), $file->get_filepath(), $file->get_filename()); + } else { + // Directory already existed, remove temporary dir but make sure we don't remove the existing dir + $doremovedirs[] = $file->get_filepath(); + $donotremovedirs[] = $realpath; } } $return = new stdClass(); @@ -243,11 +249,10 @@ switch ($action) { $return = false; } // Remove remaining temporary directories. - foreach ($fs->get_directory_files($user_context->id, 'user', 'draft', $draftid, $temppath, true, true, 'filepath desc, filename desc') as $file) { - $file->delete(); - } - if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $temppath, '.')) { - $file->delete(); + foreach (array_diff($doremovedirs, $donotremovedirs) as $filepath) { + if ($file = $fs->get_file($user_context->id, 'user', 'draft', $draftid, $filepath, '.')) { + $file->delete(); + } } die(json_encode($return)); -- 2.43.0