\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
}
/**
- * @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);
+
}
/**
* @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;
}
/**