From f87229ddafff57980bfc93f52d6aff3427e9a0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Fri, 12 Apr 2013 14:13:38 +0200 Subject: [PATCH] fix stream wrapper to make initial encryption work --- apps/files_encryption/lib/keymanager.php | 18 ++++++++++-------- apps/files_encryption/lib/stream.php | 19 ++++++++++--------- apps/files_encryption/lib/util.php | 11 +++++++---- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php index 9bb062d0fd..3e26e6bb69 100755 --- a/apps/files_encryption/lib/keymanager.php +++ b/apps/files_encryption/lib/keymanager.php @@ -113,17 +113,19 @@ class Keymanager { $targetPath = self::keySetPreparation( $view, $path, $basePath, $userId ); - if ( $view->is_dir( $basePath . '/' . $targetPath ) ) { - - // FIXME: write me - - } else { + if ( !$view->is_dir( $basePath . '/' . $targetPath ) ) { - // Save the keyfile in parallel directory - $result = $view->file_put_contents( $basePath . '/' . $targetPath . '.key', $catfile ); - + // create all parent folders + $info=pathinfo($basePath . '/' . $targetPath); + $keyfileFolderName=$view->getLocalFolder($info['dirname']); + if(!file_exists($keyfileFolderName)) { + mkdir($keyfileFolderName, 0750, true); + } } + $result = $view->file_put_contents( $basePath . '/' . $targetPath . '.key', $catfile ); + + \OC_FileProxy::$enabled = true; return $result; diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php index 8bacb98126..3bad43de2e 100644 --- a/apps/files_encryption/lib/stream.php +++ b/apps/files_encryption/lib/stream.php @@ -52,7 +52,7 @@ class Stream { // TODO: make all below properties private again once unit testing is // configured correctly public $rawPath; // The raw path received by stream_open - public $path_f; // The raw path formatted to include username and data dir + public $relPath; // rel path to users file dir private $userId; private $handle; // Resource returned by fopen private $path; @@ -80,8 +80,9 @@ class Stream { // Strip identifier text from path $this->rawPath = str_replace( 'crypt://', '', $path ); - // Set file path relative to user files dir - $this->relPath = $this->userId . '/files/' . $this->rawPath; + // Set file path relative to user files dir (7 = string length of '/files/') + $this->relPath = substr($this->rawPath, strlen($this->userId)+7); + //$this->relPath = $this->userId . '/files/' . $this->rawPath; if ( dirname( $this->rawPath ) == 'streams' @@ -110,7 +111,7 @@ class Stream { } else { - $this->size = $this->rootView->filesize( $this->relPath, $mode ); + $this->size = $this->rootView->filesize( $this->rawPath, $mode ); //$this->size = filesize( $this->rawPath ); @@ -121,13 +122,13 @@ class Stream { //$this->handle = fopen( $this->rawPath, $mode ); - $this->handle = $this->rootView->fopen( $this->relPath, $mode ); + $this->handle = $this->rootView->fopen( $this->rawPath, $mode ); \OC_FileProxy::$enabled = true; if ( ! is_resource( $this->handle ) ) { - \OCP\Util::writeLog( 'files_encryption', 'failed to open file "' . $this->relPath . '"', \OCP\Util::ERROR ); + \OCP\Util::writeLog( 'files_encryption', 'failed to open file "' . $this->rawPath . '"', \OCP\Util::ERROR ); } else { @@ -226,13 +227,13 @@ class Stream { // If a keyfile already exists for a file named identically to // file to be written - if ( $this->rootView->file_exists( $this->userId . '/'. 'files_encryption' . '/' . 'keyfiles' . '/' . $this->rawPath . '.key' ) ) { + if ( $this->rootView->file_exists( $this->userId . '/'. 'files_encryption' . '/' . 'keyfiles' . '/' . $this->relPath . '.key' ) ) { // TODO: add error handling for when file exists but no // keyfile // Fetch existing keyfile - $this->encKeyfile = Keymanager::getFileKey( $this->rootView, $this->userId, $this->rawPath ); + $this->encKeyfile = Keymanager::getFileKey( $this->rootView, $this->userId, $this->relPath ); $this->getUser(); @@ -317,7 +318,7 @@ class Stream { $userId = \OCP\User::getUser(); // Save the new encrypted file key - Keymanager::setFileKey( $view, $this->rawPath, $userId, $this->encKeyfile ); + Keymanager::setFileKey( $view, $this->relPath, $userId, $this->encKeyfile ); } diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 815f2594ce..4605c0f597 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -511,17 +511,20 @@ class Util { // Open handle with for binary reading $plainHandle = $this->view->fopen( $plainFile['path'], 'rb' ); // Open handle with for binary writing - $encHandle = fopen( 'crypt://' . 'var/www/oc6/data/' . $plainFile['path'] . '.tmp', 'ab' ); + + $encHandle = fopen( 'crypt://' . $plainFile['path'] . '.tmp', 'wb' ); // Overwrite the existing file with the encrypted one //$this->view->file_put_contents( $plainFile['path'], $encrypted['data'] ); $size = stream_copy_to_stream( $plainHandle, $encHandle ); - + + $this->view->rename($plainFile['path'] . '.tmp', $plainFile['path']); + // Fetch the key that has just been set/updated by the stream - $encKey = Keymanager::getFileKey( $this->view, $this->userId, $relPath ); + //$encKey = Keymanager::getFileKey( $this->view, $this->userId, $relPath ); // Save keyfile - Keymanager::setFileKey( $this->view, $relPath, $this->userId, $encKey ); + //Keymanager::setFileKey( $this->view, $relPath, $this->userId, $encKey ); // Add the file to the cache \OC\Files\Filesystem::putFileInfo( $plainFile['path'], array( 'encrypted'=>true, 'size' => $size ), '' );