From a7a7ef2b3a79607677679ea96212a20a633065e3 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Tue, 30 Jul 2013 09:48:30 +0200 Subject: [PATCH] improved error handling --- apps/files_encryption/lib/util.php | 36 +++++++++++++++++++++--------- settings/ajax/decryptall.php | 12 ++++++---- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 03e2fae4c6..5649472e0b 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -661,7 +661,7 @@ class Util { } } - + /** * @brief Decrypt all files * @return bool @@ -670,6 +670,8 @@ class Util { $found = $this->findEncFiles($this->userId . '/files'); + $successful = true; + if ($found) { // Disable proxy to prevent file being encrypted twice @@ -687,11 +689,28 @@ class Util { // Open enc file handle for binary reading $encHandle = fopen('crypt://' . $rawPath, 'rb'); + if ($encHandle === false) { + \OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $rawPath . '", decryption failed!', \OCP\Util::FATAL); + $successful = false; + continue; + } + // Open plain file handle for binary writing, with same filename as original plain file $plainHandle = $this->view->fopen($rawPath . '.part', 'wb'); + if ($plainHandle === false) { + \OCP\Util::writeLog('Encryption library', 'couldn\'t open "' . $rawPath . '.part", decryption failed!', \OCP\Util::FATAL); + $successful = false; + continue; + } // Move plain file to a temporary location $size = stream_copy_to_stream($encHandle, $plainHandle); + if ($size === 0) { + \OCP\Util::writeLog('Encryption library', 'Zero bytes copied of "' . $rawPath . '", decryption failed!', \OCP\Util::FATAL); + $successful = false; + continue; + } + fclose($encHandle); fclose($plainHandle); @@ -711,18 +730,15 @@ class Util { )); } - $this->view->deleteAll($this->keyfilesPath); - $this->view->deleteAll($this->shareKeysPath); + if ($successful) { + $this->view->deleteAll($this->keyfilesPath); + $this->view->deleteAll($this->shareKeysPath); + } \OC_FileProxy::$enabled = true; - - // If files were found, return true - return true; - } else { - - // If no files were found, return false - return false; } + + return $successful; } /** diff --git a/settings/ajax/decryptall.php b/settings/ajax/decryptall.php index 7adacb9802..e53067931e 100644 --- a/settings/ajax/decryptall.php +++ b/settings/ajax/decryptall.php @@ -1,5 +1,5 @@ initEncryption($params); if ($result !== false) { - $util->decryptAll(); - \OCP\JSON::success(array('data' => array('message' => 'Files decrypted successfully'))); + $successful = $util->decryptAll(); + if ($successful === true) { + \OCP\JSON::success(array('data' => array('message' => 'Files decrypted successfully'))); + } else { + \OCP\JSON::error(array('data' => array('message' => 'Couldn\'t decrypt your files, please check your owncloud.log or ask your administrator'))); + } } else { - \OCP\JSON::error(array('data' => array('message' => 'Couldn\'t decrypt files, check your password and try again'))); + \OCP\JSON::error(array('data' => array('message' => 'Couldn\'t decrypt your files, check your password and try again'))); }