Graceful error handling and logging

Right now a failed "copyr" will result in the error log being spammed with not really helpful error messages. Also situations such as `$dir` returning `false` are not really caught.

This adds more error handling and logging to make debugging such situations easier.

Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
This commit is contained in:
Lukas Reschke 2016-09-25 18:58:05 +02:00
parent e0dd6768be
commit 2ecf5722ec
No known key found for this signature in database
GPG Key ID: B9F6980CF6E759B1
1 changed files with 16 additions and 1 deletions

View File

@ -336,7 +336,16 @@ class OC_Util {
* @return void * @return void
*/ */
public static function copyr($source, \OCP\Files\Folder $target) { public static function copyr($source, \OCP\Files\Folder $target) {
$logger = \OC::$server->getLogger();
// Verify if folder exists
$dir = opendir($source); $dir = opendir($source);
if($dir === false) {
$logger->error(sprintf('Could not opendir "%s"', $source), ['app' => 'core']);
return;
}
// Copy the files
while (false !== ($file = readdir($dir))) { while (false !== ($file = readdir($dir))) {
if (!\OC\Files\Filesystem::isIgnoredDir($file)) { if (!\OC\Files\Filesystem::isIgnoredDir($file)) {
if (is_dir($source . '/' . $file)) { if (is_dir($source . '/' . $file)) {
@ -344,7 +353,13 @@ class OC_Util {
self::copyr($source . '/' . $file, $child); self::copyr($source . '/' . $file, $child);
} else { } else {
$child = $target->newFile($file); $child = $target->newFile($file);
stream_copy_to_stream(fopen($source . '/' . $file,'r'), $child->fopen('w')); $sourceStream = fopen($source . '/' . $file, 'r');
if($sourceStream === false) {
$logger->error(sprintf('Could not fopen "%s"', $source . '/' . $file), ['app' => 'core']);
closedir($dir);
return;
}
stream_copy_to_stream($sourceStream, $child->fopen('w'));
} }
} }
} }