Merge pull request #16237 from owncloud/file-locking

High level file locking
This commit is contained in:
Thomas Müller 2015-06-01 16:37:38 +02:00
commit 2f8296875f
26 changed files with 780 additions and 30 deletions

View File

@ -31,6 +31,9 @@ namespace OC\Files\Storage;
use OC\Files\Filesystem; use OC\Files\Filesystem;
use OCA\Files_Sharing\ISharedStorage; use OCA\Files_Sharing\ISharedStorage;
use OCA\Files_Sharing\Propagator;
use OCA\Files_Sharing\SharedMount;
use OCP\Lock\ILockingProvider;
/** /**
* Convert target path to source path and pass the function call to the correct storage provider * Convert target path to source path and pass the function call to the correct storage provider
@ -608,4 +611,38 @@ class Shared extends \OC\Files\Storage\Common implements ISharedStorage {
list($targetStorage, $targetInternalPath) = $this->resolvePath($targetInternalPath); list($targetStorage, $targetInternalPath) = $this->resolvePath($targetInternalPath);
return $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); return $targetStorage->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
} }
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
$targetStorage->acquireLock($targetInternalPath, $type, $provider);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
$targetStorage->releaseLock($targetInternalPath, $type, $provider);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function changeLock($path, $type, ILockingProvider $provider) {
/** @var \OCP\Files\Storage $targetStorage */
list($targetStorage, $targetInternalPath) = $this->resolvePath($path);
$targetStorage->changeLock($targetInternalPath, $type, $provider);
}
} }

View File

@ -1026,6 +1026,25 @@ $CONFIG = array(
*/ */
'max_filesize_animated_gifs_public_sharing' => 10, 'max_filesize_animated_gifs_public_sharing' => 10,
/**
* Enables the EXPERIMENTAL file locking.
* This is disabled by default as it is experimental.
*
* Prevents concurrent processes to access the same files
* at the same time. Can help prevent side effects that would
* be caused by concurrent operations.
*
* WARNING: EXPERIMENTAL
*/
'filelocking.enabled' => false,
/**
* Memory caching backend for file locking
* Because most memcache backends can clean values without warning using redis is recommended
*/
'memcache.locking' => '\OC\Memcache\Redis',
/** /**
* This entry is just here to show a warning in case somebody copied the sample * This entry is just here to show a warning in case somebody copied the sample
* configuration. DO NOT ADD THIS SWITCH TO YOUR CONFIGURATION! * configuration. DO NOT ADD THIS SWITCH TO YOUR CONFIGURATION!

View File

@ -666,6 +666,8 @@ class OC {
//make sure temporary files are cleaned up //make sure temporary files are cleaned up
$tmpManager = \OC::$server->getTempManager(); $tmpManager = \OC::$server->getTempManager();
register_shutdown_function(array($tmpManager, 'clean')); register_shutdown_function(array($tmpManager, 'clean'));
$lockProvider = \OC::$server->getLockingProvider();
register_shutdown_function(array($lockProvider, 'releaseAll'));
if ($systemConfig->getValue('installed', false) && !self::checkUpgrade(false)) { if ($systemConfig->getValue('installed', false) && !self::checkUpgrade(false)) {
if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') { if (\OC::$server->getConfig()->getAppValue('core', 'backgroundjobs_mode', 'ajax') == 'ajax') {

View File

@ -28,6 +28,8 @@
namespace OC\Connector\Sabre; namespace OC\Connector\Sabre;
use OC\Connector\Sabre\Exception\InvalidPath; use OC\Connector\Sabre\Exception\InvalidPath;
use OC\Connector\Sabre\Exception\FileLocked;
use OCP\Lock\LockedException;
class Directory extends \OC\Connector\Sabre\Node class Directory extends \OC\Connector\Sabre\Node
implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota { implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota {
@ -102,6 +104,8 @@ class Directory extends \OC\Connector\Sabre\Node
return $node->put($data); return $node->put($data);
} catch (\OCP\Files\StorageNotAvailableException $e) { } catch (\OCP\Files\StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
} }
@ -127,6 +131,8 @@ class Directory extends \OC\Connector\Sabre\Node
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (\OCP\Files\InvalidPathException $ex) { } catch (\OCP\Files\InvalidPathException $ex) {
throw new InvalidPath($ex->getMessage()); throw new InvalidPath($ex->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
} }
@ -211,11 +217,14 @@ class Directory extends \OC\Connector\Sabre\Node
throw new \Sabre\DAV\Exception\Forbidden(); throw new \Sabre\DAV\Exception\Forbidden();
} }
try {
if (!$this->fileView->rmdir($this->path)) { if (!$this->fileView->rmdir($this->path)) {
// assume it wasn't possible to remove due to permission issue // assume it wasn't possible to remove due to permission issue
throw new \Sabre\DAV\Exception\Forbidden(); throw new \Sabre\DAV\Exception\Forbidden();
} }
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
} }
/** /**

View File

@ -42,6 +42,6 @@ class FileLocked extends \Sabre\DAV\Exception {
*/ */
public function getHTTPCode() { public function getHTTPCode() {
return 503; return 423;
} }
} }

View File

@ -45,6 +45,8 @@ use OCP\Files\InvalidPathException;
use OCP\Files\LockNotAcquiredException; use OCP\Files\LockNotAcquiredException;
use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\Files\StorageNotAvailableException; use OCP\Files\StorageNotAvailableException;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use Sabre\DAV\Exception; use Sabre\DAV\Exception;
use Sabre\DAV\Exception\BadRequest; use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\Forbidden; use Sabre\DAV\Exception\Forbidden;
@ -79,6 +81,7 @@ class File extends Node implements IFile {
* @throws Exception * @throws Exception
* @throws EntityTooLarge * @throws EntityTooLarge
* @throws ServiceUnavailable * @throws ServiceUnavailable
* @throws FileLocked
* @return string|null * @return string|null
*/ */
public function put($data) { public function put($data) {
@ -110,6 +113,12 @@ class File extends Node implements IFile {
$partFilePath = $this->path; $partFilePath = $this->path;
} }
try {
$this->fileView->lockFile($this->path, ILockingProvider::LOCK_EXCLUSIVE);
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
}
// the part file and target file might be on a different storage in case of a single file storage (e.g. single file share) // the part file and target file might be on a different storage in case of a single file storage (e.g. single file share)
/** @var \OC\Files\Storage\Storage $partStorage */ /** @var \OC\Files\Storage\Storage $partStorage */
list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath); list($partStorage, $internalPartPath) = $this->fileView->resolvePath($partFilePath);
@ -232,6 +241,8 @@ class File extends Node implements IFile {
throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage()); throw new ServiceUnavailable("Failed to check file size: " . $e->getMessage());
} }
$this->fileView->unlockFile($this->path, ILockingProvider::LOCK_EXCLUSIVE);
return '"' . $this->info->getEtag() . '"'; return '"' . $this->info->getEtag() . '"';
} }
@ -252,6 +263,8 @@ class File extends Node implements IFile {
throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage()); throw new ServiceUnavailable("Encryption not ready: " . $e->getMessage());
} catch (StorageNotAvailableException $e) { } catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to open file: " . $e->getMessage()); throw new ServiceUnavailable("Failed to open file: " . $e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
} }
@ -273,6 +286,8 @@ class File extends Node implements IFile {
} }
} catch (StorageNotAvailableException $e) { } catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage()); throw new ServiceUnavailable("Failed to unlink: " . $e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
} }
@ -378,6 +393,8 @@ class File extends Node implements IFile {
return $info->getEtag(); return $info->getEtag();
} catch (StorageNotAvailableException $e) { } catch (StorageNotAvailableException $e) {
throw new ServiceUnavailable("Failed to put file: " . $e->getMessage()); throw new ServiceUnavailable("Failed to put file: " . $e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
} }

View File

@ -89,6 +89,12 @@ class FilesPlugin extends \Sabre\DAV\ServerPlugin {
$this->server->on('afterBind', array($this, 'sendFileIdHeader')); $this->server->on('afterBind', array($this, 'sendFileIdHeader'));
$this->server->on('afterWriteContent', array($this, 'sendFileIdHeader')); $this->server->on('afterWriteContent', array($this, 'sendFileIdHeader'));
$this->server->on('afterMethod:GET', [$this,'httpGet']); $this->server->on('afterMethod:GET', [$this,'httpGet']);
$this->server->on('afterResponse', function($request, ResponseInterface $response) {
$body = $response->getBody();
if (is_resource($body)) {
fclose($body);
}
});
} }
/** /**

View File

@ -26,10 +26,12 @@
namespace OC\Connector\Sabre; namespace OC\Connector\Sabre;
use OC\Connector\Sabre\Exception\InvalidPath; use OC\Connector\Sabre\Exception\InvalidPath;
use OC\Connector\Sabre\Exception\FileLocked;
use OC\Files\FileInfo; use OC\Files\FileInfo;
use OC\Files\Mount\MoveableMount; use OC\Files\Mount\MoveableMount;
use OCP\Files\StorageInvalidException; use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException; use OCP\Files\StorageNotAvailableException;
use OCP\Lock\LockedException;
class ObjectTree extends \Sabre\DAV\Tree { class ObjectTree extends \Sabre\DAV\Tree {
@ -221,8 +223,10 @@ class ObjectTree extends \Sabre\DAV\Tree {
if (!$renameOkay) { if (!$renameOkay) {
throw new \Sabre\DAV\Exception\Forbidden(''); throw new \Sabre\DAV\Exception\Forbidden('');
} }
} catch (\OCP\Files\StorageNotAvailableException $e) { } catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
$this->markDirty($sourceDir); $this->markDirty($sourceDir);
@ -258,8 +262,10 @@ class ObjectTree extends \Sabre\DAV\Tree {
try { try {
$this->fileView->copy($source, $destination); $this->fileView->copy($source, $destination);
} catch (\OCP\Files\StorageNotAvailableException $e) { } catch (StorageNotAvailableException $e) {
throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage()); throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
} catch (LockedException $e) {
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
} }
list($destinationDir,) = \Sabre\HTTP\URLUtil::splitPath($destination); list($destinationDir,) = \Sabre\HTTP\URLUtil::splitPath($destination);

View File

@ -43,6 +43,7 @@ use OCP\Files\FileNameTooLongException;
use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException; use OCP\Files\InvalidPathException;
use OCP\Files\ReservedWordException; use OCP\Files\ReservedWordException;
use OCP\Lock\ILockingProvider;
/** /**
* Storage backend class for providing common filesystem operation methods * Storage backend class for providing common filesystem operation methods
@ -621,4 +622,32 @@ abstract class Common implements Storage {
return $data; return $data;
} }
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider) {
$provider->acquireLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider) {
$provider->releaseLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function changeLock($path, $type, ILockingProvider $provider) {
$provider->changeLock('files/' . md5($this->getId() . '::' . trim($path, '/')), $type);
}
} }

View File

@ -21,6 +21,7 @@
*/ */
namespace OC\Files\Storage; namespace OC\Files\Storage;
use OCP\Lock\ILockingProvider;
/** /**
* Provide a common interface to all different storage options * Provide a common interface to all different storage options
@ -76,4 +77,26 @@ interface Storage extends \OCP\Files\Storage {
*/ */
public function getMetaData($path); public function getMetaData($path);
/**
* @param string $path The path of the file to acquire the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider);
/**
* @param string $path The path of the file to release the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider);
/**
* @param string $path The path of the file to change the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function changeLock($path, $type, ILockingProvider $provider);
} }

View File

@ -23,6 +23,7 @@
namespace OC\Files\Storage\Wrapper; namespace OC\Files\Storage\Wrapper;
use OC\Files\Cache\Wrapper\CacheJail; use OC\Files\Cache\Wrapper\CacheJail;
use OCP\Lock\ILockingProvider;
/** /**
* Jail to a subdirectory of the wrapped storage * Jail to a subdirectory of the wrapped storage
@ -424,4 +425,32 @@ class Jail extends Wrapper {
public function getETag($path) { public function getETag($path) {
return $this->storage->getETag($this->getSourcePath($path)); return $this->storage->getETag($this->getSourcePath($path));
} }
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider) {
$this->storage->acquireLock($this->getSourcePath($path), $type, $provider);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider) {
$this->storage->releaseLock($this->getSourcePath($path), $type, $provider);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function changeLock($path, $type, ILockingProvider $provider) {
$this->storage->changeLock($this->getSourcePath($path), $type, $provider);
}
} }

View File

@ -25,6 +25,7 @@
namespace OC\Files\Storage\Wrapper; namespace OC\Files\Storage\Wrapper;
use OCP\Files\InvalidPathException; use OCP\Files\InvalidPathException;
use OCP\Lock\ILockingProvider;
class Wrapper implements \OC\Files\Storage\Storage { class Wrapper implements \OC\Files\Storage\Storage {
/** /**
@ -541,4 +542,32 @@ class Wrapper implements \OC\Files\Storage\Storage {
public function getMetaData($path) { public function getMetaData($path) {
return $this->storage->getMetaData($path); return $this->storage->getMetaData($path);
} }
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider) {
$this->storage->acquireLock($path, $type, $provider);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider) {
$this->storage->releaseLock($path, $type, $provider);
}
/**
* @param string $path
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function changeLock($path, $type, ILockingProvider $provider) {
$this->storage->changeLock($path, $type, $provider);
}
} }

View File

@ -41,12 +41,15 @@
namespace OC\Files; namespace OC\Files;
use Icewind\Streams\CallbackWrapper;
use OC\Files\Cache\Updater; use OC\Files\Cache\Updater;
use OC\Files\Mount\MoveableMount; use OC\Files\Mount\MoveableMount;
use OCP\Files\FileNameTooLongException; use OCP\Files\FileNameTooLongException;
use OCP\Files\InvalidCharacterInPathException; use OCP\Files\InvalidCharacterInPathException;
use OCP\Files\InvalidPathException; use OCP\Files\InvalidPathException;
use OCP\Files\ReservedWordException; use OCP\Files\ReservedWordException;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
/** /**
* Class to provide access to ownCloud filesystem via a "view", and methods for * Class to provide access to ownCloud filesystem via a "view", and methods for
@ -71,6 +74,11 @@ class View {
/** @var \OC\Files\Cache\Updater */ /** @var \OC\Files\Cache\Updater */
protected $updater; protected $updater;
/**
* @var \OCP\Lock\ILockingProvider
*/
private $lockingProvider;
/** /**
* @param string $root * @param string $root
* @throws \Exception If $root contains an invalid path * @throws \Exception If $root contains an invalid path
@ -79,12 +87,13 @@ class View {
if (is_null($root)) { if (is_null($root)) {
throw new \InvalidArgumentException('Root can\'t be null'); throw new \InvalidArgumentException('Root can\'t be null');
} }
if(!Filesystem::isValidPath($root)) { if (!Filesystem::isValidPath($root)) {
throw new \Exception(); throw new \Exception();
} }
$this->fakeRoot = $root; $this->fakeRoot = $root;
$this->updater = new Updater($this); $this->updater = new Updater($this);
$this->lockingProvider = \OC::$server->getLockingProvider();
} }
public function getAbsolutePath($path = '/') { public function getAbsolutePath($path = '/') {
@ -137,7 +146,7 @@ class View {
return $path; return $path;
} }
if (rtrim($path,'/') === rtrim($this->fakeRoot, '/')) { if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) {
return '/'; return '/';
} }
@ -525,6 +534,7 @@ class View {
and !Filesystem::isFileBlacklisted($path) and !Filesystem::isFileBlacklisted($path)
) { ) {
$path = $this->getRelativePath($absolutePath); $path = $this->getRelativePath($absolutePath);
$exists = $this->file_exists($path); $exists = $this->file_exists($path);
$run = true; $run = true;
if ($this->shouldEmitHooks($path)) { if ($this->shouldEmitHooks($path)) {
@ -604,6 +614,10 @@ class View {
if ($path1 == null or $path2 == null) { if ($path1 == null or $path2 == null) {
return false; return false;
} }
$this->lockFile($path1, ILockingProvider::LOCK_SHARED);
$this->lockFile($path2, ILockingProvider::LOCK_SHARED);
$run = true; $run = true;
if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { if ($this->shouldEmitHooks() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
// if it was a rename from a part file to a regular file it was a write and not a rename operation // if it was a rename from a part file to a regular file it was a write and not a rename operation
@ -629,6 +643,9 @@ class View {
$internalPath1 = $mount1->getInternalPath($absolutePath1); $internalPath1 = $mount1->getInternalPath($absolutePath1);
$internalPath2 = $mount2->getInternalPath($absolutePath2); $internalPath2 = $mount2->getInternalPath($absolutePath2);
$this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE);
$this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE);
if ($internalPath1 === '' and $mount1 instanceof MoveableMount) { if ($internalPath1 === '' and $mount1 instanceof MoveableMount) {
if ($this->isTargetAllowed($absolutePath2)) { if ($this->isTargetAllowed($absolutePath2)) {
/** /**
@ -649,6 +666,15 @@ class View {
} else { } else {
$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
} }
$this->unlockFile($path1, ILockingProvider::LOCK_EXCLUSIVE);
$this->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
if ($internalPath1 === '' and $mount1 instanceof MoveableMount) {
// since $path2 now points to a different storage we need to unlock the path on the old storage separately
$storage2->releaseLock($internalPath2, ILockingProvider::LOCK_EXCLUSIVE, $this->lockingProvider);
}
if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
// if it was a rename from a part file to a regular file it was a write and not a rename operation // if it was a rename from a part file to a regular file it was a write and not a rename operation
$this->updater->update($path2); $this->updater->update($path2);
@ -676,6 +702,8 @@ class View {
} }
return $result; return $result;
} else { } else {
$this->unlockFile($path1, ILockingProvider::LOCK_SHARED);
$this->unlockFile($path2, ILockingProvider::LOCK_SHARED);
return false; return false;
} }
} else { } else {
@ -707,6 +735,10 @@ class View {
return false; return false;
} }
$run = true; $run = true;
$this->lockFile($path2, ILockingProvider::LOCK_SHARED);
$this->lockFile($path1, ILockingProvider::LOCK_SHARED);
$exists = $this->file_exists($path2); $exists = $this->file_exists($path2);
if ($this->shouldEmitHooks()) { if ($this->shouldEmitHooks()) {
\OC_Hook::emit( \OC_Hook::emit(
@ -727,6 +759,9 @@ class View {
$internalPath1 = $mount1->getInternalPath($absolutePath1); $internalPath1 = $mount1->getInternalPath($absolutePath1);
$storage2 = $mount2->getStorage(); $storage2 = $mount2->getStorage();
$internalPath2 = $mount2->getInternalPath($absolutePath2); $internalPath2 = $mount2->getInternalPath($absolutePath2);
$this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE);
if ($mount1->getMountPoint() == $mount2->getMountPoint()) { if ($mount1->getMountPoint() == $mount2->getMountPoint()) {
if ($storage1) { if ($storage1) {
$result = $storage1->copy($internalPath1, $internalPath2); $result = $storage1->copy($internalPath1, $internalPath2);
@ -737,6 +772,10 @@ class View {
$result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
} }
$this->updater->update($path2); $this->updater->update($path2);
$this->unlockFile($path2, ILockingProvider::LOCK_EXCLUSIVE);
$this->unlockFile($path1, ILockingProvider::LOCK_SHARED);
if ($this->shouldEmitHooks() && $result !== false) { if ($this->shouldEmitHooks() && $result !== false) {
\OC_Hook::emit( \OC_Hook::emit(
Filesystem::CLASSNAME, Filesystem::CLASSNAME,
@ -750,6 +789,8 @@ class View {
} }
return $result; return $result;
} else { } else {
$this->unlockFile($path2, ILockingProvider::LOCK_SHARED);
$this->unlockFile($path1, ILockingProvider::LOCK_SHARED);
return false; return false;
} }
} else { } else {
@ -929,14 +970,31 @@ class View {
return false; return false;
} }
if(in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) {
// always a shared lock during pre-hooks so the hook can read the file
$this->lockFile($path, ILockingProvider::LOCK_SHARED);
}
$run = $this->runHooks($hooks, $path); $run = $this->runHooks($hooks, $path);
list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
if ($run and $storage) { if ($run and $storage) {
if (in_array('write', $hooks) || in_array('delete', $hooks)) {
$this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
}
try {
if (!is_null($extraParam)) { if (!is_null($extraParam)) {
$result = $storage->$operation($internalPath, $extraParam); $result = $storage->$operation($internalPath, $extraParam);
} else { } else {
$result = $storage->$operation($internalPath); $result = $storage->$operation($internalPath);
} }
} catch (\Exception $e) {
if (in_array('write', $hooks) || in_array('delete', $hooks)) {
$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
} else if (in_array('read', $hooks)) {
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
}
throw $e;
}
if (in_array('delete', $hooks) and $result) { if (in_array('delete', $hooks) and $result) {
$this->updater->remove($path); $this->updater->remove($path);
@ -948,12 +1006,31 @@ class View {
$this->updater->update($path, $extraParam); $this->updater->update($path, $extraParam);
} }
if ($operation === 'fopen' and $result) {
$result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) {
if (in_array('write', $hooks)) {
$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
} else if (in_array('read', $hooks)) {
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
}
});
} else {
if (in_array('write', $hooks) || in_array('delete', $hooks)) {
$this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
} else if (in_array('read', $hooks)) {
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
}
}
if ($this->shouldEmitHooks($path) && $result !== false) { if ($this->shouldEmitHooks($path) && $result !== false) {
if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
$this->runHooks($hooks, $path, true); $this->runHooks($hooks, $path, true);
} }
} }
return $result; return $result;
} else {
$this->unlockFile($path, ILockingProvider::LOCK_SHARED);
} }
} }
return null; return null;
@ -1553,4 +1630,114 @@ class View {
throw new InvalidPathException($l10n->t('File name is too long')); throw new InvalidPathException($l10n->t('File name is too long'));
} }
} }
/**
* get all parent folders of $path
*
* @param string $path
* @return string[]
*/
private function getParents($path) {
$path = trim($path, '/');
if (!$path) {
return [];
}
$parts = explode('/', $path);
// remove the single file
array_pop($parts);
$result = array('/');
$resultPath = '';
foreach ($parts as $part) {
if ($part) {
$resultPath .= '/' . $part;
$result[] = $resultPath;
}
}
return $result;
}
/**
* @param string $path the path of the file to lock, relative to the view
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
*/
private function lockPath($path, $type) {
$mount = $this->getMount($path);
if ($mount) {
$mount->getStorage()->acquireLock(
$mount->getInternalPath(
$this->getAbsolutePath($path)
),
$type,
$this->lockingProvider
);
}
}
/**
* @param string $path the path of the file to lock, relative to the view
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
*/
private function changeLock($path, $type) {
$mount = $this->getMount($path);
if ($mount) {
$mount->getStorage()->changeLock(
$mount->getInternalPath(
$this->getAbsolutePath($path)
),
$type,
$this->lockingProvider
);
}
}
/**
* @param string $path the path of the file to unlock, relative to the view
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
*/
private function unlockPath($path, $type) {
$mount = $this->getMount($path);
if ($mount) {
$mount->getStorage()->releaseLock(
$mount->getInternalPath(
$this->getAbsolutePath($path)
),
$type,
$this->lockingProvider
);
}
}
/**
* Lock a path and all its parents up to the root of the view
*
* @param string $path the path of the file to lock relative to the view
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
*/
public function lockFile($path, $type) {
$path = '/' . trim($path, '/');
$this->lockPath($path, $type);
$parents = $this->getParents($path);
foreach ($parents as $parent) {
$this->lockPath($parent, ILockingProvider::LOCK_SHARED);
}
}
/**
* Unlock a path and all its parents up to the root of the view
*
* @param string $path the path of the file to lock relative to the view
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
*/
public function unlockFile($path, $type) {
$path = rtrim($path, '/');
$this->unlockPath($path, $type);
$parents = $this->getParents($path);
foreach ($parents as $parent) {
$this->unlockPath($parent, ILockingProvider::LOCK_SHARED);
}
}
} }

View File

@ -31,6 +31,11 @@ class MemcacheLockingProvider implements ILockingProvider {
*/ */
private $memcache; private $memcache;
private $acquiredLocks = [
'shared' => [],
'exclusive' => []
];
/** /**
* @param \OCP\IMemcache $memcache * @param \OCP\IMemcache $memcache
*/ */
@ -64,11 +69,16 @@ class MemcacheLockingProvider implements ILockingProvider {
if (!$this->memcache->inc($path)) { if (!$this->memcache->inc($path)) {
throw new LockedException($path); throw new LockedException($path);
} }
if (!isset($this->acquiredLocks['shared'][$path])) {
$this->acquiredLocks['shared'][$path] = 0;
}
$this->acquiredLocks['shared'][$path]++;
} else { } else {
$this->memcache->add($path, 0); $this->memcache->add($path, 0);
if (!$this->memcache->cas($path, 0, 'exclusive')) { if (!$this->memcache->cas($path, 0, 'exclusive')) {
throw new LockedException($path); throw new LockedException($path);
} }
$this->acquiredLocks['exclusive'][$path] = true;
} }
} }
@ -78,9 +88,55 @@ class MemcacheLockingProvider implements ILockingProvider {
*/ */
public function releaseLock($path, $type) { public function releaseLock($path, $type) {
if ($type === self::LOCK_SHARED) { if ($type === self::LOCK_SHARED) {
if (isset($this->acquiredLocks['shared'][$path]) and $this->acquiredLocks['shared'][$path] > 0) {
$this->memcache->dec($path); $this->memcache->dec($path);
$this->acquiredLocks['shared'][$path]--;
}
} else if ($type === self::LOCK_EXCLUSIVE) { } else if ($type === self::LOCK_EXCLUSIVE) {
$this->memcache->cas($path, 'exclusive', 0); $this->memcache->cas($path, 'exclusive', 0);
unset($this->acquiredLocks['exclusive'][$path]);
}
}
/**
* Change the type of an existing lock
*
* @param string $path
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException
*/
public function changeLock($path, $targetType) {
if ($targetType === self::LOCK_SHARED) {
if (!$this->memcache->cas($path, 'exclusive', 1)) {
throw new LockedException($path);
}
unset($this->acquiredLocks['exclusive'][$path]);
if (!isset($this->acquiredLocks['shared'][$path])) {
$this->acquiredLocks['shared'][$path] = 0;
}
$this->acquiredLocks['shared'][$path]++;
} else if ($targetType === self::LOCK_EXCLUSIVE) {
// we can only change a shared lock to an exclusive if there's only a single owner of the shared lock
if (!$this->memcache->cas($path, 1, 'exclusive')) {
throw new LockedException($path);
}
$this->acquiredLocks['exclusive'][$path] = true;
$this->acquiredLocks['shared'][$path]--;
}
}
/**
* release all lock acquired by this instance
*/
public function releaseAll() {
foreach ($this->acquiredLocks['shared'] as $path => $count) {
for ($i = 0; $i < $count; $i++) {
$this->releaseLock($path, self::LOCK_SHARED);
}
}
foreach ($this->acquiredLocks['exclusive'] as $path => $hasLock) {
$this->releaseLock($path, self::LOCK_EXCLUSIVE);
} }
} }
} }

View File

@ -0,0 +1,67 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @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\Lock;
use OCP\Lock\ILockingProvider;
/**
* Locking provider that does nothing.
*
* To be used when locking is disabled.
*/
class NoopLockingProvider implements ILockingProvider {
/**
* {@inheritdoc}
*/
public function isLocked($path, $type) {
return false;
}
/**
* {@inheritdoc}
*/
public function acquireLock($path, $type) {
// do nothing
}
/**
* {@inheritdoc}
*/
public function releaseLock($path, $type) {
// do nothing
}
/**1
* {@inheritdoc}
*/
public function releaseAll() {
// do nothing
}
/**
* {@inheritdoc}
*/
public function changeLock($path, $targetType) {
// do nothing
}
}

View File

@ -46,13 +46,19 @@ class Factory implements ICacheFactory {
*/ */
private $distributedCacheClass; private $distributedCacheClass;
/**
* @var string $lockingCacheClass
*/
private $lockingCacheClass;
/** /**
* @param string $globalPrefix * @param string $globalPrefix
* @param string|null $localCacheClass * @param string|null $localCacheClass
* @param string|null $distributedCacheClass * @param string|null $distributedCacheClass
* @param string|null $lockingCacheClass
*/ */
public function __construct($globalPrefix, public function __construct($globalPrefix,
$localCacheClass = null, $distributedCacheClass = null) $localCacheClass = null, $distributedCacheClass = null, $lockingCacheClass = null)
{ {
$this->globalPrefix = $globalPrefix; $this->globalPrefix = $globalPrefix;
@ -62,8 +68,23 @@ class Factory implements ICacheFactory {
if (!($distributedCacheClass && $distributedCacheClass::isAvailable())) { if (!($distributedCacheClass && $distributedCacheClass::isAvailable())) {
$distributedCacheClass = $localCacheClass; $distributedCacheClass = $localCacheClass;
} }
if (!($lockingCacheClass && $lockingCacheClass::isAvailable())) {
// dont fallback since the fallback might not be suitable for storing lock
$lockingCacheClass = '\OC\Memcache\Null';
}
$this->localCacheClass = $localCacheClass; $this->localCacheClass = $localCacheClass;
$this->distributedCacheClass = $distributedCacheClass; $this->distributedCacheClass = $distributedCacheClass;
$this->lockingCacheClass = $lockingCacheClass;
}
/**
* create a cache instance for storing locks
*
* @param string $prefix
* @return \OCP\IMemcache
*/
public function createLocking($prefix = '') {
return new $this->lockingCacheClass($this->globalPrefix . '/' . $prefix);
} }
/** /**

View File

@ -22,7 +22,7 @@
namespace OC\Memcache; namespace OC\Memcache;
class Null extends Cache { class Null extends Cache implements \OCP\IMemcache {
public function get($key) { public function get($key) {
return null; return null;
} }
@ -39,6 +39,22 @@ class Null extends Cache {
return true; return true;
} }
public function add($key, $value, $ttl = 0) {
return true;
}
public function inc($key, $step = 1) {
return true;
}
public function dec($key, $step = 1) {
return true;
}
public function cas($key, $old, $new) {
return true;
}
public function clear($prefix = '') { public function clear($prefix = '') {
return true; return true;
} }

View File

@ -43,6 +43,8 @@ use OC\Command\AsyncBus;
use OC\Diagnostics\NullQueryLogger; use OC\Diagnostics\NullQueryLogger;
use OC\Diagnostics\EventLogger; use OC\Diagnostics\EventLogger;
use OC\Diagnostics\QueryLogger; use OC\Diagnostics\QueryLogger;
use OC\Lock\MemcacheLockingProvider;
use OC\Lock\NoopLockingProvider;
use OC\Mail\Mailer; use OC\Mail\Mailer;
use OC\Memcache\ArrayCache; use OC\Memcache\ArrayCache;
use OC\Http\Client\ClientService; use OC\Http\Client\ClientService;
@ -223,7 +225,7 @@ class Server extends SimpleContainer implements IServerContainer {
$this->registerService('MemCacheFactory', function (Server $c) { $this->registerService('MemCacheFactory', function (Server $c) {
$config = $c->getConfig(); $config = $c->getConfig();
if($config->getSystemValue('installed', false)) { if($config->getSystemValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
$v = \OC_App::getAppVersions(); $v = \OC_App::getAppVersions();
$v['core'] = implode('.', \OC_Util::getVersion()); $v['core'] = implode('.', \OC_Util::getVersion());
$version = implode(',', $v); $version = implode(',', $v);
@ -232,11 +234,13 @@ class Server extends SimpleContainer implements IServerContainer {
$prefix = md5($instanceId.'-'.$version.'-'.$path); $prefix = md5($instanceId.'-'.$version.'-'.$path);
return new \OC\Memcache\Factory($prefix, return new \OC\Memcache\Factory($prefix,
$config->getSystemValue('memcache.local', null), $config->getSystemValue('memcache.local', null),
$config->getSystemValue('memcache.distributed', null) $config->getSystemValue('memcache.distributed', null),
$config->getSystemValue('memcache.locking', null)
); );
} }
return new \OC\Memcache\Factory('', return new \OC\Memcache\Factory('',
new ArrayCache(),
new ArrayCache(), new ArrayCache(),
new ArrayCache() new ArrayCache()
); );
@ -420,6 +424,17 @@ class Server extends SimpleContainer implements IServerContainer {
$this->getLogger() $this->getLogger()
); );
}); });
$this->registerService('LockingProvider', function (Server $c) {
if ($c->getConfig()->getSystemValue('filelocking.enabled', false) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
/** @var \OC\Memcache\Factory $memcacheFactory */
$memcacheFactory = $c->getMemCacheFactory();
$memcache = $memcacheFactory->createLocking('lock');
if (!($memcache instanceof \OC\Memcache\Null)) {
return new MemcacheLockingProvider($memcache);
}
}
return new NoopLockingProvider();
});
} }
/** /**
@ -908,4 +923,14 @@ class Server extends SimpleContainer implements IServerContainer {
public function getTrustedDomainHelper() { public function getTrustedDomainHelper() {
return $this->query('TrustedDomainHelper'); return $this->query('TrustedDomainHelper');
} }
/**
* Get the locking provider
*
* @return \OCP\Lock\ILockingProvider
* @since 8.1.0
*/
public function getLockingProvider() {
return $this->query('LockingProvider');
}
} }

View File

@ -33,6 +33,7 @@
// This means that they should be used by apps instead of the internal ownCloud classes // This means that they should be used by apps instead of the internal ownCloud classes
namespace OCP\Files; namespace OCP\Files;
use OCP\Files\InvalidPathException; use OCP\Files\InvalidPathException;
use OCP\Lock\ILockingProvider;
/** /**
* Provide a common interface to all different storage options * Provide a common interface to all different storage options
@ -413,4 +414,27 @@ interface Storage {
* @since 8.1.0 * @since 8.1.0
*/ */
public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath); public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath);
/**
* @param string $path The path of the file to acquire the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function acquireLock($path, $type, ILockingProvider $provider);
/**
* @param string $path The path of the file to acquire the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
*/
public function releaseLock($path, $type, ILockingProvider $provider);
/**
* @param string $path The path of the file to change the lock for
* @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
* @param \OCP\Lock\ILockingProvider $provider
* @throws \OCP\Lock\LockedException
*/
public function changeLock($path, $type, ILockingProvider $provider);
} }

View File

@ -413,4 +413,12 @@ interface IServerContainer {
* @since 8.1.0 * @since 8.1.0
*/ */
public function getMailer(); public function getMailer();
/**
* Get the locking provider
*
* @return \OCP\Lock\ILockingProvider
* @since 8.1.0
*/
public function getLockingProvider();
} }

View File

@ -44,4 +44,18 @@ interface ILockingProvider {
* @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE * @param int $type self::LOCK_SHARED or self::LOCK_EXCLUSIVE
*/ */
public function releaseLock($path, $type); public function releaseLock($path, $type);
/**
* Change the type of an existing lock
*
* @param string $path
* @param int $targetType self::LOCK_SHARED or self::LOCK_EXCLUSIVE
* @throws \OCP\Lock\LockedException
*/
public function changeLock($path, $targetType);
/**
* release all lock acquired by this instance
*/
public function releaseAll();
} }

View File

@ -29,6 +29,8 @@
* *
*/ */
use OC\Lock\NoopLockingProvider;
OC_Util::checkAdminUser(); OC_Util::checkAdminUser();
OC_App::setActiveNavigationEntry("admin"); OC_App::setActiveNavigationEntry("admin");
@ -175,6 +177,11 @@ $template->assign('fileSharingSettings', $fileSharingSettings);
$template->assign('filesExternal', $filesExternal); $template->assign('filesExternal', $filesExternal);
$template->assign('updaterAppPanel', $updaterAppPanel); $template->assign('updaterAppPanel', $updaterAppPanel);
$template->assign('ocDefaultEncryptionModulePanel', $ocDefaultEncryptionModulePanel); $template->assign('ocDefaultEncryptionModulePanel', $ocDefaultEncryptionModulePanel);
if (\OC::$server->getLockingProvider() instanceof NoopLockingProvider) {
$template->assign('fileLockingEnabled', false);
} else {
$template->assign('fileLockingEnabled', true);
}
$formsMap = array_map(function ($form) { $formsMap = array_map(function ($form) {
if (preg_match('%(<h2[^>]*>.*?</h2>)%i', $form, $regs)) { if (preg_match('%(<h2[^>]*>.*?</h2>)%i', $form, $regs)) {
@ -200,6 +207,7 @@ $formsAndMore = array_merge($formsAndMore, $formsMap);
$formsAndMore[] = ['anchor' => 'backgroundjobs', 'section-name' => $l->t('Cron')]; $formsAndMore[] = ['anchor' => 'backgroundjobs', 'section-name' => $l->t('Cron')];
$formsAndMore[] = ['anchor' => 'mail_general_settings', 'section-name' => $l->t('Email server')]; $formsAndMore[] = ['anchor' => 'mail_general_settings', 'section-name' => $l->t('Email server')];
$formsAndMore[] = ['anchor' => 'log-section', 'section-name' => $l->t('Log')]; $formsAndMore[] = ['anchor' => 'log-section', 'section-name' => $l->t('Log')];
$formsAndMore[] = ['anchor' => 'server-status', 'section-name' => $l->t('Server Status')];
$formsAndMore[] = ['anchor' => 'admin-tips', 'section-name' => $l->t('Tips & tricks')]; $formsAndMore[] = ['anchor' => 'admin-tips', 'section-name' => $l->t('Tips & tricks')];
if ($updaterAppPanel) { if ($updaterAppPanel) {
$formsAndMore[] = ['anchor' => 'updater', 'section-name' => $l->t('Updates')]; $formsAndMore[] = ['anchor' => 'updater', 'section-name' => $l->t('Updates')];

View File

@ -7,6 +7,7 @@
/** /**
* @var array $_ * @var array $_
* @var \OCP\IL10N $l * @var \OCP\IL10N $l
* @var OC_Defaults $theme
*/ */
style('settings', 'settings'); style('settings', 'settings');
@ -15,32 +16,32 @@ script('core', ['multiselect', 'setupchecks']);
vendor_script('select2/select2'); vendor_script('select2/select2');
vendor_style('select2/select2'); vendor_style('select2/select2');
$levels = array('Debug', 'Info', 'Warning', 'Error', 'Fatal'); $levels = ['Debug', 'Info', 'Warning', 'Error', 'Fatal'];
$levelLabels = array( $levelLabels = [
$l->t( 'Everything (fatal issues, errors, warnings, info, debug)' ), $l->t( 'Everything (fatal issues, errors, warnings, info, debug)' ),
$l->t( 'Info, warnings, errors and fatal issues' ), $l->t( 'Info, warnings, errors and fatal issues' ),
$l->t( 'Warnings, errors and fatal issues' ), $l->t( 'Warnings, errors and fatal issues' ),
$l->t( 'Errors and fatal issues' ), $l->t( 'Errors and fatal issues' ),
$l->t( 'Fatal issues only' ), $l->t( 'Fatal issues only' ),
); ];
$mail_smtpauthtype = array( $mail_smtpauthtype = [
'' => $l->t('None'), '' => $l->t('None'),
'LOGIN' => $l->t('Login'), 'LOGIN' => $l->t('Login'),
'PLAIN' => $l->t('Plain'), 'PLAIN' => $l->t('Plain'),
'NTLM' => $l->t('NT LAN Manager'), 'NTLM' => $l->t('NT LAN Manager'),
); ];
$mail_smtpsecure = array( $mail_smtpsecure = [
'' => $l->t('None'), '' => $l->t('None'),
'ssl' => $l->t('SSL'), 'ssl' => $l->t('SSL'),
'tls' => $l->t('TLS'), 'tls' => $l->t('TLS'),
); ];
$mail_smtpmode = array( $mail_smtpmode = [
'php', 'php',
'smtp', 'smtp',
); ];
if ($_['sendmail_is_available']) { if ($_['sendmail_is_available']) {
$mail_smtpmode[] = 'sendmail'; $mail_smtpmode[] = 'sendmail';
} }
@ -137,7 +138,7 @@ if (!$_['isLocaleWorking']) {
?> ?>
<br> <br>
<?php <?php
p($l->t('We strongly suggest installing the required packages on your system to support one of the following locales: %s.', array($locales))); p($l->t('We strongly suggest installing the required packages on your system to support one of the following locales: %s.', [$locales]));
?> ?>
</li> </li>
<?php <?php
@ -266,12 +267,12 @@ if ($_['cronErrors']) {
if (time() - $_['lastcron'] <= 3600): ?> if (time() - $_['lastcron'] <= 3600): ?>
<span class="cronstatus success"></span> <span class="cronstatus success"></span>
<span class="crondate" original-title="<?php p($absolute_time);?>"> <span class="crondate" original-title="<?php p($absolute_time);?>">
<?php p($l->t("Last cron job execution: %s.", array($relative_time)));?> <?php p($l->t("Last cron job execution: %s.", [$relative_time]));?>
</span> </span>
<?php else: ?> <?php else: ?>
<span class="cronstatus error"></span> <span class="cronstatus error"></span>
<span class="crondate" original-title="<?php p($absolute_time);?>"> <span class="crondate" original-title="<?php p($absolute_time);?>">
<?php p($l->t("Last cron job execution: %s. Something seems wrong.", array($relative_time)));?> <?php p($l->t("Last cron job execution: %s. Something seems wrong.", [$relative_time]));?>
</span> </span>
<?php endif; <?php endif;
else: ?> else: ?>
@ -527,6 +528,18 @@ if ($_['cronErrors']) {
<li><a target="_blank" href="<?php p(link_to_docs('admin-security')); ?>"><?php p($l->t('Hardening and security guidance'));?> ↗</a></li> <li><a target="_blank" href="<?php p(link_to_docs('admin-security')); ?>"><?php p($l->t('Hardening and security guidance'));?> ↗</a></li>
</ul> </ul>
</div> </div>
<div class="section" id="server-status">
<h2><?php p($l->t('Server Status'));?></h2>
<ul>
<li>
<?php if ($_['fileLockingEnabled']) {
p($l->t('Experimental File Lock is enabled.'));
} else {
p($l->t('Experimental File Lock is disabled.'));
} ?>
</li>
</ul>
</div>
<div class="section"> <div class="section">
<h2><?php p($l->t('Version'));?></h2> <h2><?php p($l->t('Version'));?></h2>

View File

@ -11,6 +11,7 @@ use OC\Files\Cache\Watcher;
use OC\Files\Storage\Common; use OC\Files\Storage\Common;
use OC\Files\Mount\MountPoint; use OC\Files\Mount\MountPoint;
use OC\Files\Storage\Temporary; use OC\Files\Storage\Temporary;
use OCP\Lock\ILockingProvider;
class TemporaryNoTouch extends \OC\Files\Storage\Temporary { class TemporaryNoTouch extends \OC\Files\Storage\Temporary {
public function touch($path, $mtime = null) { public function touch($path, $mtime = null) {
@ -1080,4 +1081,30 @@ class View extends \Test\TestCase {
public function testNullAsRoot() { public function testNullAsRoot() {
new \OC\Files\View(null); new \OC\Files\View(null);
} }
/**
* e.g. reading from a folder that's being renamed
*
* @expectedException \OCP\Lock\LockedException
*/
public function testReadFromWriteLockedPath() {
$view = new \OC\Files\View();
$storage = new Temporary(array());
\OC\Files\Filesystem::mount($storage, [], '/');
$view->lockFile('/foo/bar', ILockingProvider::LOCK_EXCLUSIVE);
$view->lockFile('/foo/bar/asd', ILockingProvider::LOCK_SHARED);
}
/**
* e.g. writing a file that's being downloaded
*
* @expectedException \OCP\Lock\LockedException
*/
public function testWriteToReadLockedFile() {
$view = new \OC\Files\View();
$storage = new Temporary(array());
\OC\Files\Filesystem::mount($storage, [], '/');
$view->lockFile('/foo/bar', ILockingProvider::LOCK_SHARED);
$view->lockFile('/foo/bar', ILockingProvider::LOCK_EXCLUSIVE);
}
} }

View File

@ -107,6 +107,30 @@ abstract class LockingProvider extends TestCase {
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE)); $this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
} }
public function testReleaseAll() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->acquireLock('bar', ILockingProvider::LOCK_SHARED);
$this->instance->acquireLock('asd', ILockingProvider::LOCK_EXCLUSIVE);
$this->instance->releaseAll();
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
$this->assertFalse($this->instance->isLocked('bar', ILockingProvider::LOCK_SHARED));
$this->assertFalse($this->instance->isLocked('asd', ILockingProvider::LOCK_EXCLUSIVE));
}
public function testReleaseAfterReleaseAll() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->releaseAll();
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
$this->instance->releaseLock('foo', ILockingProvider::LOCK_SHARED);
}
/** /**
* @expectedException \OCP\Lock\LockedException * @expectedException \OCP\Lock\LockedException
@ -134,4 +158,57 @@ abstract class LockingProvider extends TestCase {
$this->assertEquals('foo', $e->getPath()); $this->assertEquals('foo', $e->getPath());
} }
} }
public function testChangeLockToExclusive() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
}
public function testChangeLockToShared() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
$this->instance->changeLock('foo', ILockingProvider::LOCK_SHARED);
$this->assertFalse($this->instance->isLocked('foo', ILockingProvider::LOCK_EXCLUSIVE));
$this->assertTrue($this->instance->isLocked('foo', ILockingProvider::LOCK_SHARED));
}
/**
* @expectedException \OCP\Lock\LockedException
*/
public function testChangeLockToExclusiveDoubleShared() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
}
/**
* @expectedException \OCP\Lock\LockedException
*/
public function testChangeLockToExclusiveNoShared() {
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
}
/**
* @expectedException \OCP\Lock\LockedException
*/
public function testChangeLockToExclusiveFromExclusive() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
$this->instance->changeLock('foo', ILockingProvider::LOCK_EXCLUSIVE);
}
/**
* @expectedException \OCP\Lock\LockedException
*/
public function testChangeLockToSharedNoExclusive() {
$this->instance->changeLock('foo', ILockingProvider::LOCK_SHARED);
}
/**
* @expectedException \OCP\Lock\LockedException
*/
public function testChangeLockToSharedFromShared() {
$this->instance->acquireLock('foo', ILockingProvider::LOCK_SHARED);
$this->instance->changeLock('foo', ILockingProvider::LOCK_SHARED);
}
} }

View File

@ -43,6 +43,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase {
protected function tearDown() { protected function tearDown() {
$hookExceptions = \OC_Hook::$thrownExceptions; $hookExceptions = \OC_Hook::$thrownExceptions;
\OC_Hook::$thrownExceptions = []; \OC_Hook::$thrownExceptions = [];
\OC::$server->getLockingProvider()->releaseAll();
if(!empty($hookExceptions)) { if(!empty($hookExceptions)) {
throw $hookExceptions[0]; throw $hookExceptions[0];
} }