Skip to content

Commit

Permalink
Checksum calculating storage wrapper: Initial draft - rewrite to stre…
Browse files Browse the repository at this point in the history
…am wrapper owncloud#26655
  • Loading branch information
IljaN committed Jan 31, 2017
1 parent 2eb954a commit 3b9b01b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 92 deletions.
2 changes: 1 addition & 1 deletion apps/dav/lib/Connector/Sabre/File.php
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
31 changes: 11 additions & 20 deletions lib/private/Files/Storage/Wrapper/Checksum.php
Expand Up @@ -20,19 +20,17 @@
*/
namespace OC\Files\Storage\Wrapper;

use OC\Files\Stream\Checksum as ChecksumStream;

class Checksum extends Wrapper {

const FILTER_NAME = 'sha1_checksum';

private $fileHashes = [];
private $checksummedPaths = [];

/**
* @param array $parameters
*/
public function __construct($parameters) {
parent::__construct($parameters);
stream_filter_register(self::FILTER_NAME, '\OC\Files\Stream\Filter\Sha1');
}

/**
Expand All @@ -42,19 +40,13 @@ public function __construct($parameters) {
*/
public function fopen($path, $mode) {
$stream = $this->getWrapperStorage()->fopen($path, $mode);

if (!self::requiresChecksum($path, $mode)) {
return $stream;
}

stream_filter_append(
$stream,
self::FILTER_NAME,
STREAM_FILTER_WRITE,
['path' => $path, 'callback' => [$this, 'addFileHash']]
);
$this->checksummedPaths[] = $path;

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

/**
Expand All @@ -64,19 +56,18 @@ public function fopen($path, $mode) {
* @return bool
*/
private static function requiresChecksum($path, $mode) {
return substr($path, 0, 6) == 'files/' && $mode != 'r';
return substr($path, 0, 6) === 'files/' && $mode !== 'r';
}

public function rename($path1, $path2) {
if (array_key_exists($path1, $this->fileHashes)) {
$checksum = $this->fileHashes[$path1];
// Update checksum in oc_filecache here?
}

return $this->getWrapperStorage()->rename($path1, $path2);
}

public function addFileHash($path, $hash) {
$this->fileHashes[$path] = $hash;
public function getMetaData($path) {
$parentMetaData = parent::getMetaData($path);
$checksums = ChecksumStream::getChecksums();
$parentMetaData['checksum'] = $checksums[$path];

return $parentMetaData;
}
}
69 changes: 69 additions & 0 deletions lib/private/Files/Stream/Checksum.php
@@ -0,0 +1,69 @@
<?php


namespace OC\Files\Stream;


use Icewind\Streams\Wrapper;

class Checksum extends Wrapper {

/** @var resource */
private $hashCtx;


/** @var array Key is path, value is the checksum */
private static $checksums = [];

public function __construct() {
$this->hashCtx = hash_init('sha1');
}

public static function wrap($source, $path) {
$context = stream_context_create([
'occhecksum' => [
'source' => $source,
'path' => $path
]
]);

return Wrapper::wrapSource(
$source, $context, 'occhecksum', self::class
);
}

public function dir_opendir($path, $options) {
#return parent::dir_opendir($path, $options);
}

public function stream_open($path, $mode, $options, &$opened_path) {
$context = parent::loadContext('occhecksum');
$this->setSourceStream($context['source']);

return true;
}

public function stream_read($count) {
hash_update($this->hashCtx, $this->source);
return parent::stream_read($count);
}

public function stream_write($data) {
hash_update($this->hashCtx, $data);
return parent::stream_write($data);
}

public function stream_close() {
self::$checksums[$this->getPathFromContext()] = hash_final($this->hashCtx);
return parent::stream_close();
}

private function getPathFromContext() {
$ctx = stream_context_get_options($this->context);
return $ctx['occhecksum']['path'];
}

public static function getChecksums() {
return self::$checksums;
}
}
71 changes: 0 additions & 71 deletions lib/private/Files/Stream/Filter/Sha1.php

This file was deleted.

0 comments on commit 3b9b01b

Please sign in to comment.