fix stream wrapper to make initial encryption work
This commit is contained in:
parent
c3a284569b
commit
f87229ddaf
|
@ -113,17 +113,19 @@ class Keymanager {
|
||||||
|
|
||||||
$targetPath = self::keySetPreparation( $view, $path, $basePath, $userId );
|
$targetPath = self::keySetPreparation( $view, $path, $basePath, $userId );
|
||||||
|
|
||||||
if ( $view->is_dir( $basePath . '/' . $targetPath ) ) {
|
if ( !$view->is_dir( $basePath . '/' . $targetPath ) ) {
|
||||||
|
|
||||||
// FIXME: write me
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
// 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;
|
\OC_FileProxy::$enabled = true;
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
|
@ -52,7 +52,7 @@ class Stream {
|
||||||
// TODO: make all below properties private again once unit testing is
|
// TODO: make all below properties private again once unit testing is
|
||||||
// configured correctly
|
// configured correctly
|
||||||
public $rawPath; // The raw path received by stream_open
|
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 $userId;
|
||||||
private $handle; // Resource returned by fopen
|
private $handle; // Resource returned by fopen
|
||||||
private $path;
|
private $path;
|
||||||
|
@ -80,8 +80,9 @@ class Stream {
|
||||||
// Strip identifier text from path
|
// Strip identifier text from path
|
||||||
$this->rawPath = str_replace( 'crypt://', '', $path );
|
$this->rawPath = str_replace( 'crypt://', '', $path );
|
||||||
|
|
||||||
// Set file path relative to user files dir
|
// Set file path relative to user files dir (7 = string length of '/files/')
|
||||||
$this->relPath = $this->userId . '/files/' . $this->rawPath;
|
$this->relPath = substr($this->rawPath, strlen($this->userId)+7);
|
||||||
|
//$this->relPath = $this->userId . '/files/' . $this->rawPath;
|
||||||
|
|
||||||
if (
|
if (
|
||||||
dirname( $this->rawPath ) == 'streams'
|
dirname( $this->rawPath ) == 'streams'
|
||||||
|
@ -110,7 +111,7 @@ class Stream {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$this->size = $this->rootView->filesize( $this->relPath, $mode );
|
$this->size = $this->rootView->filesize( $this->rawPath, $mode );
|
||||||
|
|
||||||
//$this->size = filesize( $this->rawPath );
|
//$this->size = filesize( $this->rawPath );
|
||||||
|
|
||||||
|
@ -121,13 +122,13 @@ class Stream {
|
||||||
|
|
||||||
//$this->handle = fopen( $this->rawPath, $mode );
|
//$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;
|
\OC_FileProxy::$enabled = true;
|
||||||
|
|
||||||
if ( ! is_resource( $this->handle ) ) {
|
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 {
|
} else {
|
||||||
|
|
||||||
|
@ -226,13 +227,13 @@ class Stream {
|
||||||
|
|
||||||
// If a keyfile already exists for a file named identically to
|
// If a keyfile already exists for a file named identically to
|
||||||
// file to be written
|
// 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
|
// TODO: add error handling for when file exists but no
|
||||||
// keyfile
|
// keyfile
|
||||||
|
|
||||||
// Fetch existing 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();
|
$this->getUser();
|
||||||
|
|
||||||
|
@ -317,7 +318,7 @@ class Stream {
|
||||||
$userId = \OCP\User::getUser();
|
$userId = \OCP\User::getUser();
|
||||||
|
|
||||||
// Save the new encrypted file key
|
// Save the new encrypted file key
|
||||||
Keymanager::setFileKey( $view, $this->rawPath, $userId, $this->encKeyfile );
|
Keymanager::setFileKey( $view, $this->relPath, $userId, $this->encKeyfile );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -511,17 +511,20 @@ class Util {
|
||||||
// Open handle with for binary reading
|
// Open handle with for binary reading
|
||||||
$plainHandle = $this->view->fopen( $plainFile['path'], 'rb' );
|
$plainHandle = $this->view->fopen( $plainFile['path'], 'rb' );
|
||||||
// Open handle with for binary writing
|
// 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
|
// Overwrite the existing file with the encrypted one
|
||||||
//$this->view->file_put_contents( $plainFile['path'], $encrypted['data'] );
|
//$this->view->file_put_contents( $plainFile['path'], $encrypted['data'] );
|
||||||
$size = stream_copy_to_stream( $plainHandle, $encHandle );
|
$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
|
// 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
|
// Save keyfile
|
||||||
Keymanager::setFileKey( $this->view, $relPath, $this->userId, $encKey );
|
//Keymanager::setFileKey( $this->view, $relPath, $this->userId, $encKey );
|
||||||
|
|
||||||
// Add the file to the cache
|
// Add the file to the cache
|
||||||
\OC\Files\Filesystem::putFileInfo( $plainFile['path'], array( 'encrypted'=>true, 'size' => $size ), '' );
|
\OC\Files\Filesystem::putFileInfo( $plainFile['path'], array( 'encrypted'=>true, 'size' => $size ), '' );
|
||||||
|
|
Loading…
Reference in New Issue