Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable locking for upload chunks #33918

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions apps/dav/lib/Connector/Sabre/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,24 @@ public function createFile($name, $data = null) {
}
$node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);

// only allow 1 process to upload a file at once but still allow reading the file while writing the part file

/** @var LoggerInterface $logger */
$logger = \OC::$server->get(LoggerInterface::class);

// we get a shared lock on the file being uploaded and an exclusive lock on a virtual '.upload.part' file.
//
// The shared lock prevents writes or deletes during the upload while still allowing reading the file.
// The exclusive lock stops concurrent upload requests.
//
// Additionally, the shared lock will be changed into an exclusive one for the actual file write
$node->acquireLock(ILockingProvider::LOCK_SHARED);
$this->fileView->lockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE);
try {
$logger->debug("Getting lock for upload: $path");
$this->fileView->lockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE);
} catch (LockedException $e) {
$logger->error("Failed to acquire lock for upload: $path", ['exception' => $e]);
throw $e;
}

$result = $node->put($data);

Expand Down
6 changes: 3 additions & 3 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -1957,7 +1957,7 @@ private function lockPath($path, $type, $lockMountPoint = false) {
} catch (LockedException $e) {
// rethrow with the a human-readable path
throw new LockedException(
$this->getPathRelativeToFiles($absolutePath),
$this->getRelativePath($absolutePath),
$e,
$e->getExistingLock()
);
Expand Down Expand Up @@ -2000,7 +2000,7 @@ public function changeLock($path, $type, $lockMountPoint = false) {
try {
// rethrow with the a human-readable path
throw new LockedException(
$this->getPathRelativeToFiles($absolutePath),
$this->getRelativePath($absolutePath),
$e,
$e->getExistingLock()
);
Expand Down Expand Up @@ -2115,7 +2115,7 @@ protected function shouldLockFile($path) {
$pathSegments = explode('/', $path);
if (isset($pathSegments[2])) {
// E.g.: /username/files/path-to-file
return ($pathSegments[2] === 'files') && (count($pathSegments) > 3);
return ($pathSegments[2] === 'files' || $pathSegments[2] === 'uploads') && (count($pathSegments) > 3);
}

return strpos($path, '/appdata_') !== 0;
Expand Down