From 8ee7959092d8363f35de49819b44b6b8fe19bc34 Mon Sep 17 00:00:00 2001 From: Florin Peter Date: Thu, 25 Apr 2013 22:49:47 +0200 Subject: [PATCH] implement postRename @samtuke no need anymore for fixPartialFilePath this is now handled by rename share-keys are now handled properly webdav .part files are handled properly --- apps/files_encryption/lib/keymanager.php | 22 ------- apps/files_encryption/lib/proxy.php | 84 +++++++++++++++++++++--- apps/files_encryption/lib/stream.php | 10 +-- 3 files changed, 77 insertions(+), 39 deletions(-) diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php index ceefe8887ac..cfc13ee132d 100755 --- a/apps/files_encryption/lib/keymanager.php +++ b/apps/files_encryption/lib/keymanager.php @@ -139,28 +139,6 @@ class Keymanager { } - /** - * @brief Remove .path extension from a file path - * @param string $path Path that may identify a .part file - * @return string File path without .part extension - */ - public static function fixPartialFilePath( $path ) { - - if ( preg_match( '/\.part$/', $path ) ) { - - $newLength = strlen( $path ) - 5; - $fPath = substr( $path, 0, $newLength ); - - return $fPath; - - } else { - - return $path; - - } - - } - /** * @brief retrieve keyfile for an encrypted file * @param \OC_FilesystemView $view diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php index 66ea282312e..7294c243664 100644 --- a/apps/files_encryption/lib/proxy.php +++ b/apps/files_encryption/lib/proxy.php @@ -237,8 +237,6 @@ class Proxy extends \OC_FileProxy { return true; } - $path = Keymanager::fixPartialFilePath( $path ); - // Disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; @@ -307,6 +305,15 @@ class Proxy extends \OC_FileProxy { if (!$view->is_dir($oldKeyfilePath)) { $oldKeyfilePath .= '.key'; $newKeyfilePath .= '.key'; + + // handle share-keys + $localKeyPath = $view->getLocalFile($userId.'/files_encryption/share-keys/'.$oldRelPath); + $matches = glob(preg_quote($localKeyPath).'*.shareKey'); + foreach ($matches as $src) { + $dst = str_replace($oldRelPath, $newRelPath, $src); + rename($src, $dst); + } + } else { // handle share-keys folders $oldShareKeyfilePath = $userId . '/' . 'files_encryption' . '/' . 'share-keys' . '/' . $oldRelPath; @@ -314,9 +321,6 @@ class Proxy extends \OC_FileProxy { $view->rename($oldShareKeyfilePath, $newShareKeyfilePath); } - //TODO add support for share-keys files - //... - // Rename keyfile so it isn't orphaned $result = $view->rename($oldKeyfilePath, $newKeyfilePath); @@ -326,6 +330,70 @@ class Proxy extends \OC_FileProxy { } + /** + * @brief When a file is renamed, rename its keyfile also + * @return bool Result of rename() + * @note This is pre rather than post because using post didn't work + */ + public function postRename( $oldPath, $newPath ) + { + + // Disable encryption proxy to prevent recursive calls + $proxyStatus = \OC_FileProxy::$enabled; + \OC_FileProxy::$enabled = false; + + $view = new \OC_FilesystemView('/'); + $userId = \OCP\User::getUser(); + $util = new Util( $view, $userId ); + + // Reformat path for use with OC_FSV + $newPathSplit = explode( '/', $newPath ); + $newPathRelative = implode( '/', array_slice( $newPathSplit, 3 ) ); + $newPathRelativeToUser = implode( '/', array_slice( $newPathSplit, 2 ) ); + + // get file info from database/cache + //$newFileInfo = \OC\Files\Filesystem::getFileInfo($newPathRelative); + + if ($util->isEncryptedPath($newPath)) { + $cached = $view->getFileInfo($newPath); + $cached['encrypted'] = 1; + + // get the size from filesystem + $size = $view->filesize($newPath); + + // calculate last chunk nr + $lastChunckNr = floor($size / 8192); + + // open stream + $result = fopen('crypt://' . $newPathRelative, "r"); + + if(is_resource($result)) { + // calculate last chunk position + $lastChunckPos = ($lastChunckNr * 8192); + + // seek to end + fseek($result, $lastChunckPos); + + // get the content of the last chunck + $lastChunkContent = fread($result, 8192); + + // calc the real file size with the size of the last chunk + $realSize = (($lastChunckNr * 6126) + strlen($lastChunkContent)); + + // set the size + $cached['unencrypted_size'] = $realSize; + } + + $view->putFileInfo( $newPath, $cached ); + + } + + \OC_FileProxy::$enabled = $proxyStatus; + + return true; + + } + public function postFopen( $path, &$result ){ if ( !$result ) { @@ -424,11 +492,11 @@ class Proxy extends \OC_FileProxy { // if path is a folder do nothing if(is_array($data) && array_key_exists('size', $data)) { + // Disable encryption proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; - // get file size $data['size'] = self::postFileSize($path, $data['size']); @@ -461,8 +529,6 @@ class Proxy extends \OC_FileProxy { return $size; } - $path = Keymanager::fixPartialFilePath( $path ); - // Reformat path for use with OC_FSV $path_split = explode('/', $path); $path_f = implode('/', array_slice($path_split, 3)); @@ -474,7 +540,7 @@ class Proxy extends \OC_FileProxy { if(is_array($fileInfo) && $fileInfo['encrypted'] == 1) { return $fileInfo['unencrypted_size']; } else { - return $fileInfo['size']; + return $size; } } } diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php index 7e42627f8ca..411bcdac92d 100644 --- a/apps/files_encryption/lib/stream.php +++ b/apps/files_encryption/lib/stream.php @@ -87,10 +87,7 @@ class Stream { // rawPath is relative to the data directory $this->rawPath = $this->userId . '/files/' . $this->relPath; - // Fix .part filenames - $this->rawPath = Keymanager::fixPartialFilePath( $this->rawPath ); - - if ( + if ( dirname( $this->rawPath ) == 'streams' and isset( self::$sourceStreams[basename( $this->rawPath )] ) ) { @@ -244,10 +241,7 @@ class Stream { } - // Avoid problems with .part file extensions - $this->relPath = Keymanager::fixPartialFilePath( $this->relPath ); - - // Fetch and decrypt keyfile + // Fetch and decrypt keyfile // Fetch existing keyfile $this->encKeyfile = Keymanager::getFileKey( $this->rootView, $this->userId, $this->relPath ); -- 2.39.5