2014-09-28 19:09:07 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-26 13:37:37 +03:00
|
|
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
2014-09-28 19:09:07 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-09-28 19:09:07 +04:00
|
|
|
namespace OCA\Files_External;
|
|
|
|
|
|
|
|
use OC\Files\Filesystem;
|
|
|
|
|
2014-10-07 18:41:37 +04:00
|
|
|
/**
|
|
|
|
* Updates the etag of parent folders whenever a new external storage mount
|
|
|
|
* point has been created or deleted. Updates need to be triggered using
|
|
|
|
* the updateHook() method.
|
|
|
|
*
|
|
|
|
* There are two modes of operation:
|
|
|
|
* - for personal mount points, the etag is propagated directly
|
|
|
|
* - for system mount points, a dirty flag is saved in the configuration and
|
|
|
|
* the etag will be updated the next time propagateDirtyMountPoints() is called
|
|
|
|
*/
|
2014-09-28 19:09:07 +04:00
|
|
|
class EtagPropagator {
|
|
|
|
/**
|
|
|
|
* @var \OCP\IUser
|
|
|
|
*/
|
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\ChangePropagator
|
|
|
|
*/
|
|
|
|
protected $changePropagator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\IConfig
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
2014-10-07 18:41:37 +04:00
|
|
|
* @param \OCP\IUser $user current user, must match the propagator's
|
|
|
|
* user
|
|
|
|
* @param \OC\Files\Cache\ChangePropagator $changePropagator change propagator
|
|
|
|
* initialized with a view for $user
|
2014-09-28 19:09:07 +04:00
|
|
|
* @param \OCP\IConfig $config
|
|
|
|
*/
|
|
|
|
public function __construct($user, $changePropagator, $config) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->changePropagator = $changePropagator;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
|
|
|
|
2014-09-30 14:28:44 +04:00
|
|
|
/**
|
|
|
|
* Propagate the etag changes for all mountpoints marked as dirty and mark the mountpoints as clean
|
|
|
|
*
|
|
|
|
* @param int $time
|
|
|
|
*/
|
2014-09-28 19:09:07 +04:00
|
|
|
public function propagateDirtyMountPoints($time = null) {
|
|
|
|
if ($time === null) {
|
|
|
|
$time = time();
|
|
|
|
}
|
|
|
|
$mountPoints = $this->getDirtyMountPoints();
|
|
|
|
foreach ($mountPoints as $mountPoint) {
|
|
|
|
$this->changePropagator->addChange($mountPoint);
|
|
|
|
$this->config->setUserValue($this->user->getUID(), 'files_external', $mountPoint, $time);
|
|
|
|
}
|
|
|
|
if (count($mountPoints)) {
|
|
|
|
$this->changePropagator->propagateChanges($time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all mountpoints we need to update the etag for
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
protected function getDirtyMountPoints() {
|
|
|
|
$dirty = array();
|
|
|
|
$mountPoints = $this->config->getAppKeys('files_external');
|
|
|
|
foreach ($mountPoints as $mountPoint) {
|
|
|
|
if (substr($mountPoint, 0, 1) === '/') {
|
|
|
|
$updateTime = $this->config->getAppValue('files_external', $mountPoint);
|
|
|
|
$userTime = $this->config->getUserValue($this->user->getUID(), 'files_external', $mountPoint);
|
|
|
|
if ($updateTime > $userTime) {
|
|
|
|
$dirty[] = $mountPoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
|
|
|
* @param int $time
|
|
|
|
*/
|
|
|
|
protected function markDirty($mountPoint, $time = null) {
|
|
|
|
if ($time === null) {
|
|
|
|
$time = time();
|
|
|
|
}
|
|
|
|
$this->config->setAppValue('files_external', $mountPoint, $time);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update etags for mount points for known user
|
|
|
|
* For global or group mount points, updating the etag for every user is not feasible
|
|
|
|
* instead we mark the mount point as dirty and update the etag when the filesystem is loaded for the user
|
2014-10-07 18:41:37 +04:00
|
|
|
* For personal mount points, the change is propagated directly
|
2014-09-28 19:09:07 +04:00
|
|
|
*
|
2014-10-07 18:41:37 +04:00
|
|
|
* @param array $params hook parameters
|
|
|
|
* @param int $time update time to use when marking a mount point as dirty
|
2014-09-28 19:09:07 +04:00
|
|
|
*/
|
|
|
|
public function updateHook($params, $time = null) {
|
|
|
|
if ($time === null) {
|
|
|
|
$time = time();
|
|
|
|
}
|
|
|
|
$users = $params[Filesystem::signal_param_users];
|
|
|
|
$type = $params[Filesystem::signal_param_mount_type];
|
|
|
|
$mountPoint = $params[Filesystem::signal_param_path];
|
2014-09-29 15:46:27 +04:00
|
|
|
$mountPoint = Filesystem::normalizePath($mountPoint);
|
2014-09-28 19:09:07 +04:00
|
|
|
if ($type === \OC_Mount_Config::MOUNT_TYPE_GROUP or $users === 'all') {
|
|
|
|
$this->markDirty($mountPoint, $time);
|
|
|
|
} else {
|
|
|
|
$this->changePropagator->addChange($mountPoint);
|
|
|
|
$this->changePropagator->propagateChanges($time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|