Skip to content

Commit

Permalink
Upload and download checksum is working owncloud#26655
Browse files Browse the repository at this point in the history
  • Loading branch information
IljaN committed Feb 3, 2017
1 parent b6d745a commit b7ea159
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/dav/lib/Connector/Sabre/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function put($data) {
}
list($count, $result) = \OC_Helper::streamCopy($data, $target);
fclose($target);
$foo = $partStorage->getMetaData($internalPartPath);

if ($result === false) {
$expected = -1;
if (isset($_SERVER['CONTENT_LENGTH'])) {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Cache/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function scanFile($file, $reuseExisting = 0, $parentId = -1, $cacheData =
}
if (!empty($newData)) {
// Reset the checksum if the data has changed
$newData['checksum'] = '';
unset($newData['checksum']);
$data['fileid'] = $this->addToCache($file, $newData, $fileId);
}
if (isset($cacheData['size'])) {
Expand Down
47 changes: 37 additions & 10 deletions lib/private/Files/Storage/Wrapper/Checksum.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

class Checksum extends Wrapper {

private $checksummedPaths = [];

/**
* @param array $parameters
*/
Expand All @@ -40,33 +38,62 @@ public function __construct($parameters) {
*/
public function fopen($path, $mode) {
$stream = $this->getWrapperStorage()->fopen($path, $mode);
if (!self::requiresChecksum($path, $mode)) {
if (!self::requiresChecksum($path)) {
return $stream;
}

$this->checksummedPaths[] = $path;

return \OC\Files\Stream\Checksum::wrap($stream, $path);
}


/**
* Checksum is only required for everything under files/
* @param $path
* @param $mode
* @return bool
*/
private static function requiresChecksum($path, $mode) {
return substr($path, 0, 6) === 'files/' && $mode !== 'r';
private static function requiresChecksum($path) {
return substr($path, 0, 6) === 'files/';
}

/**
* @param string $path1
* @param string $path2
* @return bool
*/
public function rename($path1, $path2) {
return $this->getWrapperStorage()->rename($path1, $path2);
$renameOk = $this->getWrapperStorage()->rename($path1, $path2);
$meta = $this->getMetaData($path1);

if (isset($meta['checksum'])) {
$this->getCache()->put(
$path2,
["checksum" => $meta['checksum']]
);
}

return $renameOk;
}

/**
* @param string $path
* @param string $data
* @return bool
*/
public function file_put_contents($path, $data) {
return parent::file_put_contents($path, $data); // TODO: Change the autogenerated stub
}

/**
* @param string $path
* @return array
*/
public function getMetaData($path) {
$parentMetaData = parent::getMetaData($path);
$checksums = ChecksumStream::getChecksums();
$parentMetaData['checksum'] = $checksums[$path];

if (isset($checksums['path'])) {
$parentMetaData['checksum'] = $checksums[$path];
}

return $parentMetaData;
}
Expand Down
63 changes: 61 additions & 2 deletions lib/private/Files/Stream/Checksum.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
<?php

/**
* @author Ilja Neumann <[email protected]>
*
* @copyright Copyright (c) 2017, ownCloud GmbH.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Files\Stream;

Expand All @@ -19,6 +38,12 @@ public function __construct() {
$this->hashCtx = hash_init('sha1');
}


/**
* @param $source
* @param $path
* @return resource
*/
public static function wrap($source, $path) {
$context = stream_context_create([
'occhecksum' => [
Expand All @@ -32,38 +57,72 @@ public static function wrap($source, $path) {
);
}

/**
* @param string $path
* @param array $options
*/
public function dir_opendir($path, $options) {
#return parent::dir_opendir($path, $options);
}

/**
* @param string $path
* @param string $mode
* @param int $options
* @param string $opened_path
* @return bool
*/
public function stream_open($path, $mode, $options, &$opened_path) {
$context = parent::loadContext('occhecksum');
$this->setSourceStream($context['source']);

return true;
}

/**
* @param int $count
* @return string
*/
public function stream_read($count) {
hash_update($this->hashCtx, $this->source);
return parent::stream_read($count);
$data = parent::stream_read($count);
hash_update($this->hashCtx, $data);

return $data;
}

/**
* @param string $data
* @return int
*/
public function stream_write($data) {
hash_update($this->hashCtx, $data);

return parent::stream_write($data);
}

/**
* @return bool
*/
public function stream_close() {
self::$checksums[$this->getPathFromContext()] = hash_final($this->hashCtx);

return parent::stream_close();
}

/**
* @return mixed
*/
private function getPathFromContext() {
$ctx = stream_context_get_options($this->context);

return $ctx['occhecksum']['path'];
}

/**
* @return array
*/
public static function getChecksums() {

return self::$checksums;
}
}

0 comments on commit b7ea159

Please sign in to comment.