Skip to content

Commit

Permalink
Merge pull request #5513 from owncloud/fix_file_cache_updater
Browse files Browse the repository at this point in the history
Fix file cache updater
  • Loading branch information
schiessle committed Oct 29, 2013
2 parents b46c820 + 91a11e2 commit 072b827
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 36 deletions.
68 changes: 51 additions & 17 deletions apps/files_encryption/hooks/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class Hooks {
* @note This method should never be called for users using client side encryption
*/
public static function login($params) {

if (\OCP\App::isEnabled('files_encryption') === false) {
return true;
}

$l = new \OC_L10N('files_encryption');

$view = new \OC_FilesystemView('/');
Expand Down Expand Up @@ -129,38 +134,44 @@ public static function login($params) {
* @note This method should never be called for users using client side encryption
*/
public static function postCreateUser($params) {
$view = new \OC_FilesystemView('/');

$util = new Util($view, $params['uid']);

Helper::setupUser($util, $params['password']);
if (\OCP\App::isEnabled('files_encryption')) {
$view = new \OC_FilesystemView('/');
$util = new Util($view, $params['uid']);
Helper::setupUser($util, $params['password']);
}
}

/**
* @brief cleanup encryption backend upon user deleted
* @note This method should never be called for users using client side encryption
*/
public static function postDeleteUser($params) {
$view = new \OC_FilesystemView('/');

// cleanup public key
$publicKey = '/public-keys/' . $params['uid'] . '.public.key';
if (\OCP\App::isEnabled('files_encryption')) {
$view = new \OC_FilesystemView('/');

// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
// cleanup public key
$publicKey = '/public-keys/' . $params['uid'] . '.public.key';

$view->unlink($publicKey);
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;

\OC_FileProxy::$enabled = $proxyStatus;
$view->unlink($publicKey);

\OC_FileProxy::$enabled = $proxyStatus;
}
}

/**
* @brief If the password can't be changed within ownCloud, than update the key password in advance.
*/
public static function preSetPassphrase($params) {
if ( ! \OC_User::canUserChangePassword($params['uid']) ) {
self::setPassphrase($params);
if (\OCP\App::isEnabled('files_encryption')) {
if ( ! \OC_User::canUserChangePassword($params['uid']) ) {
self::setPassphrase($params);
}
}
}

Expand All @@ -170,6 +181,10 @@ public static function preSetPassphrase($params) {
*/
public static function setPassphrase($params) {

if (\OCP\App::isEnabled('files_encryption') === false) {
return true;
}

// Only attempt to change passphrase if server-side encryption
// is in use (client-side encryption does not have access to
// the necessary keys)
Expand Down Expand Up @@ -240,6 +255,10 @@ public static function setPassphrase($params) {
*/
public static function preShared($params) {

if (\OCP\App::isEnabled('files_encryption') === false) {
return true;
}

$l = new \OC_L10N('files_encryption');
$users = array();
$view = new \OC\Files\View('/public-keys/');
Expand Down Expand Up @@ -272,6 +291,10 @@ public static function preShared($params) {
*/
public static function postShared($params) {

if (\OCP\App::isEnabled('files_encryption') === false) {
return true;
}

// NOTE: $params has keys:
// [itemType] => file
// itemSource -> int, filecache file ID
Expand Down Expand Up @@ -378,6 +401,10 @@ public static function postShared($params) {
*/
public static function postUnshare($params) {

if (\OCP\App::isEnabled('files_encryption') === false) {
return true;
}

// NOTE: $params has keys:
// [itemType] => file
// [itemSource] => 13
Expand Down Expand Up @@ -466,6 +493,11 @@ public static function postUnshare($params) {
* of the stored versions along the actual file
*/
public static function postRename($params) {

if (\OCP\App::isEnabled('files_encryption') === false) {
return true;
}

// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
Expand Down Expand Up @@ -558,9 +590,11 @@ public static function postRename($params) {
* @param array $params contains the app ID
*/
public static function preDisable($params) {
if ($params['app'] === 'files_encryption') {
$query = \OC_DB::prepare('UPDATE `*PREFIX*encryption` SET `migration_status`=0');
$query->execute();
if (\OCP\App::isEnabled('files_encryption')) {
if ($params['app'] === 'files_encryption') {
$query = \OC_DB::prepare('UPDATE `*PREFIX*encryption` SET `migration_status`=0');
$query->execute();
}
}
}

Expand Down
7 changes: 0 additions & 7 deletions apps/files_sharing/lib/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ static public function correctFolders($target) {
}
$users = $reshareUsers;
}
// Correct folders of shared file owner
$target = substr($target, 8);
if ($uidOwner !== $uid && $source = \OC_Share_Backend_File::getSource($target)) {
\OC\Files\Filesystem::initMountPoints($uidOwner);
$source = '/'.$uidOwner.'/'.$source['path'];
\OC\Files\Cache\Updater::correctFolder($source, $info['mtime']);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/files/cache/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public function calculateFolderSize($path) {
if ($entry['size'] !== $totalSize) {
$this->update($id, array('size' => $totalSize));
}

}
}
return $totalSize;
Expand Down
50 changes: 39 additions & 11 deletions lib/files/cache/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,59 @@ static public function renameUpdate($from, $to) {
}
}

/**
* @brief get file owner and path
* @param string $filename
* @return array with the oweners uid and the owners path
*/
private static function getUidAndFilename($filename) {
$uid = \OC\Files\Filesystem::getOwner($filename);
\OC\Files\Filesystem::initMountPoints($uid);
if ( $uid != \OCP\User::getUser() ) {
$info = \OC\Files\Filesystem::getFileInfo($filename);
$ownerView = new \OC\Files\View('/'.$uid.'/files');
$filename = $ownerView->getPath($info['fileid']);
}
return array($uid, '/files/'.$filename);
}

/**
* Update the mtime and ETag of all parent folders
*
* @param string $path
* @param string $time
*/
static public function correctFolder($path, $time) {

if ($path !== '' && $path !== '/') {
$parent = dirname($path);
if ($parent === '.') {
$parent = '';
}

list($owner, $realPath) = self::getUidAndFilename(dirname($path));

/**
* @var \OC\Files\Storage\Storage $storage
* @var string $internalPath
*/
list($storage, $internalPath) = self::resolvePath($parent);
if ($storage) {
$cache = $storage->getCache();
$id = $cache->getId($internalPath);
if ($id !== -1) {
$cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
self::correctFolder($parent, $time);

$view = new \OC\Files\View('/' . $owner);

list($storage, $internalPath) = $view->resolvePath($realPath);
$cache = $storage->getCache();
$id = $cache->getId($internalPath);

while ($id !== -1) {
$cache->update($id, array('mtime' => $time, 'etag' => $storage->getETag($internalPath)));
$realPath = dirname($realPath);
// check storage for parent in case we change the storage in this step
list($storage, $internalPath) = $view->resolvePath($realPath);
if ($internalPath) {
$cache = $storage->getCache();
$id = $cache->getId($internalPath);
} else {
$id = -1;
}

}

}
}

Expand Down
17 changes: 17 additions & 0 deletions tests/lib/files/cache/updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class Updater extends \PHPUnit_Framework_TestCase {
*/
private $scanner;

private $stateFilesEncryption;

/**
* @var \OC\Files\Cache\Cache $cache
*/
Expand All @@ -29,6 +31,12 @@ class Updater extends \PHPUnit_Framework_TestCase {
private static $user;

public function setUp() {

// remember files_encryption state
$this->stateFilesEncryption = \OC_App::isEnabled('files_encryption');
// we want to tests with the encryption app disabled
\OC_App::disable('files_encryption');

$this->storage = new \OC\Files\Storage\Temporary(array());
$textData = "dummy file data\n";
$imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
Expand All @@ -46,6 +54,10 @@ public function setUp() {
if (!self::$user) {
self::$user = uniqid();
}

\OC_User::createUser(self::$user, 'password');
\OC_User::setUserId(self::$user);

\OC\Files\Filesystem::init(self::$user, '/' . self::$user . '/files');

Filesystem::clearMounts();
Expand All @@ -63,7 +75,12 @@ public function tearDown() {
if ($this->cache) {
$this->cache->clear();
}
\OC_User::deleteUser(self::$user);
Filesystem::tearDown();
// reset app files_encryption
if ($this->stateFilesEncryption) {
\OC_App::enable('files_encryption');
}
}

public function testWrite() {
Expand Down

0 comments on commit 072b827

Please sign in to comment.