*/
public static function getFileKey(\OC_FilesystemView $view, $userId, $filePath) {
- // try reusing key file if part file
- if (self::isPartialFilePath($filePath)) {
-
- $result = self::getFileKey($view, $userId, self::fixPartialFilePath($filePath));
-
- if ($result) {
-
- return $result;
-
- }
-
- }
-
$util = new Util($view, \OCP\User::getUser());
list($owner, $filename) = $util->getUidAndFilename($filePath);
+ $filename = self::fixPartialFilePath($filename);
$filePath_f = ltrim($filename, '/');
// in case of system wide mount points the keys are stored directly in the data directory
public static function getShareKey(\OC_FilesystemView $view, $userId, $filePath) {
// try reusing key file if part file
- if (self::isPartialFilePath($filePath)) {
-
- $result = self::getShareKey($view, $userId, self::fixPartialFilePath($filePath));
-
- if ($result) {
-
- return $result;
-
- }
-
- }
-
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$util = new Util($view, \OCP\User::getUser());
list($owner, $filename) = $util->getUidAndFilename($filePath);
-
+ $filename = self::fixPartialFilePath($filename);
// in case of system wide mount points the keys are stored directly in the data directory
if ($util->isSystemWideMountPoint($filename)) {
$shareKeyPath = '/files_encryption/share-keys/' . $filename . '.' . $userId . '.shareKey';
* @return bool
*/
public function stream_open($path, $mode, $options, &$opened_path) {
-
+
// assume that the file already exist before we decide it finally in getKey()
$this->newFile = false;
if ($this->relPath === false) {
$this->relPath = Helper::getPathToRealFile($this->rawPath);
}
-
+
if($this->relPath === false) {
\OCP\Util::writeLog('Encryption library', 'failed to open file "' . $this->rawPath . '" expecting a path to user/files or to user/files_versions', \OCP\Util::ERROR);
return false;
}
-
+
// Disable fileproxies so we can get the file size and open the source file without recursive encryption
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
}
// Get the data from the file handle
- $data = fread($this->handle, 8192);
+ $data = fread($this->handle, $count);
$result = null;
} else {
$this->newFile = true;
-
+
return false;
}
return strlen($data);
}
- // Disable the file proxies so that encryption is not
- // automatically attempted when the file is written to disk -
- // we are handling that separately here and we don't want to
+ // Disable the file proxies so that encryption is not
+ // automatically attempted when the file is written to disk -
+ // we are handling that separately here and we don't want to
// get into an infinite loop
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$pointer = ftell($this->handle);
// Get / generate the keyfile for the file we're handling
- // If we're writing a new file (not overwriting an existing
+ // If we're writing a new file (not overwriting an existing
// one), save the newly generated keyfile
if (!$this->getKey()) {
}
- // If extra data is left over from the last round, make sure it
+ // If extra data is left over from the last round, make sure it
// is integrated into the next 6126 / 8192 block
if ($this->writeCache) {
if ($remainingLength < 6126) {
// Set writeCache to contents of $data
- // The writeCache will be carried over to the
- // next write round, and added to the start of
- // $data to ensure that written blocks are
- // always the correct length. If there is still
- // data in writeCache after the writing round
- // has finished, then the data will be written
+ // The writeCache will be carried over to the
+ // next write round, and added to the start of
+ // $data to ensure that written blocks are
+ // always the correct length. If there is still
+ // data in writeCache after the writing round
+ // has finished, then the data will be written
// to disk by $this->flush().
$this->writeCache = $data;
$encrypted = $this->preWriteEncrypt($chunk, $this->plainKey);
- // Write the data chunk to disk. This will be
+ // Write the data chunk to disk. This will be
// attended to the last data chunk if the file
// being handled totals more than 6126 bytes
fwrite($this->handle, $encrypted);
$this->meta['mode'] !== 'rb' &&
$this->size > 0
) {
+
// only write keyfiles if it was a new file
if ($this->newFile === true) {
// set fileinfo
$this->rootView->putFileInfo($this->rawPath, $fileInfo);
+
}
return fclose($this->handle);
// get the size from filesystem
$fullPath = $this->view->getLocalFile($path);
- $size = filesize($fullPath);
+ $size = $this->view->filesize($path);
// calculate last chunk nr
$lastChunkNr = floor($size / 8192);
+ $lastChunkSize = $size - ($lastChunkNr * 8192);
// open stream
$stream = fopen('crypt://' . $path, "r");
fseek($stream, $lastChunckPos);
// get the content of the last chunk
- $lastChunkContent = fread($stream, 8192);
+ $lastChunkContent = fread($stream, $lastChunkSize);
// calc the real file size with the size of the last chunk
$realSize = (($lastChunkNr * 6126) + strlen($lastChunkContent));