fix stream wrapper to make initial encryption work

This commit is contained in:
Björn Schießle 2013-04-12 14:13:38 +02:00
parent c3a284569b
commit f87229ddaf
3 changed files with 27 additions and 21 deletions

View File

@ -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;

View File

@ -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 );
}

View File

@ -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 ), '' );