]> source.dussan.org Git - nextcloud-server.git/commitdiff
catch errors during initial encryption
authorBjoern Schiessle <schiessle@owncloud.com>
Mon, 10 Feb 2014 15:43:04 +0000 (16:43 +0100)
committerBjoern Schiessle <schiessle@owncloud.com>
Mon, 17 Feb 2014 08:58:46 +0000 (09:58 +0100)
apps/files_encryption/hooks/hooks.php
apps/files_encryption/lib/util.php

index 83abf3ba9de61700f3f8990d915068fd26a381e3..1eb5f4c41e47ec9ec16acdce9791d063ab656954 100644 (file)
@@ -109,21 +109,27 @@ class Hooks {
 \r
                        }\r
 \r
-                       // Encrypt existing user files:\r
-                       if (\r
-                               $util->encryptAll('/' . $params['uid'] . '/' . 'files', $session->getLegacyKey(), $params['password'])\r
-                       ) {\r
+                       // Encrypt existing user files\r
+                       try {\r
+                               $result = $util->encryptAll('/' . $params['uid'] . '/' . 'files', $session->getLegacyKey(), $params['password']);\r
+                       } catch (\Exception $ex) {\r
+                               \OCP\Util::writeLog('Encryption library', 'Initial encryption failed! Error: ' . $ex->getMessage(), \OCP\Util::FATAL);\r
+                               $util->resetMigrationStatus();\r
+                               \OCP\User::logout();\r
+                               $result = false;\r
+                       }\r
+\r
+                       if ($result) {\r
 \r
                                \OC_Log::write(\r
                                        'Encryption library', 'Encryption of existing files belonging to "' . $params['uid'] . '" completed'\r
                                        , \OC_Log::INFO\r
                                );\r
 \r
-                       }\r
-\r
-                       // Register successful migration in DB\r
-                       $util->finishMigration();\r
+                               // Register successful migration in DB\r
+                               $util->finishMigration();\r
 \r
+                       }\r
                }\r
 \r
                return true;\r
index ae3e2a2e15a529d31d3a765fa96487cf30810eb0..ced4b823cf04cf27623aa0d60c847b6e479a93a9 100644 (file)
@@ -1186,26 +1186,48 @@ class Util {
        }
 
        /**
-        * @brief start migration mode to initially encrypt users data
+        * @brief set migration status
+        * @param int $status
         * @return boolean
         */
-       public function beginMigration() {
+       private function setMigrationStatus($status) {
 
-               $return = false;
-
-               $sql = 'UPDATE `*PREFIX*encryption` SET `migration_status` = ? WHERE `uid` = ? and `migration_status` = ?';
-               $args = array(self::MIGRATION_IN_PROGRESS, $this->userId, self::MIGRATION_OPEN);
+               $sql = 'UPDATE `*PREFIX*encryption` SET `migration_status` = ? WHERE `uid` = ?';
+               $args = array($status, $this->userId);
                $query = \OCP\DB::prepare($sql);
                $manipulatedRows = $query->execute($args);
 
                if ($manipulatedRows === 1) {
-                       $return = true;
+                       $result = true;
+                       \OCP\Util::writeLog('Encryption library', "Migration status set to " . self::MIGRATION_OPEN, \OCP\Util::INFO);
+               } else {
+                       $result = false;
+                       \OCP\Util::writeLog('Encryption library', "Could not set migration status to " . self::MIGRATION_OPEN, \OCP\Util::WARN);
+               }
+
+               return $result;
+       }
+
+       /**
+        * @brief start migration mode to initially encrypt users data
+        * @return boolean
+        */
+       public function beginMigration() {
+
+               $result = $this->setMigrationStatus(self::MIGRATION_IN_PROGRESS);
+
+               if ($result) {
                        \OCP\Util::writeLog('Encryption library', "Start migration to encryption mode for " . $this->userId, \OCP\Util::INFO);
                } else {
                        \OCP\Util::writeLog('Encryption library', "Could not activate migration mode for " . $this->userId . ". Probably another process already started the initial encryption", \OCP\Util::WARN);
                }
 
-               return $return;
+               return $result;
+       }
+
+       public function resetMigrationStatus() {
+               return $this->setMigrationStatus(self::MIGRATION_OPEN);
+
        }
 
        /**
@@ -1213,22 +1235,15 @@ class Util {
         * @return boolean
         */
        public function finishMigration() {
+               $result = $this->setMigrationStatus(self::MIGRATION_COMPLETED);
 
-               $return = false;
-
-               $sql = 'UPDATE `*PREFIX*encryption` SET `migration_status` = ? WHERE `uid` = ? and `migration_status` = ?';
-               $args = array(self::MIGRATION_COMPLETED, $this->userId, self::MIGRATION_IN_PROGRESS);
-               $query = \OCP\DB::prepare($sql);
-               $manipulatedRows = $query->execute($args);
-
-               if ($manipulatedRows === 1) {
-                       $return = true;
+               if ($result) {
                        \OCP\Util::writeLog('Encryption library', "Finish migration successfully for " . $this->userId, \OCP\Util::INFO);
                } else {
                        \OCP\Util::writeLog('Encryption library', "Could not deactivate migration mode for " . $this->userId, \OCP\Util::WARN);
                }
 
-               return $return;
+               return $result;
        }
 
        /**