Skip to content
Merged
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
13 changes: 13 additions & 0 deletions lib/private/Files/ObjectStore/Azure.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,17 @@ public function writeObject($urn, $stream) {
public function deleteObject($urn) {
$this->getBlobClient()->deleteBlob($this->containerName, $urn);
}

public function objectExists($urn) {
try {
$this->getBlobClient()->getBlobMetadata($this->containerName, $urn);
return true;
} catch (ServiceException $e) {
if ($e->getCode() === 404) {
return false;
} else {
throw $e;
}
}
}
}
18 changes: 15 additions & 3 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,18 +413,30 @@ public function writeBack($tmpFile, $path) {
$stat['mimetype'] = $mimetype;
$stat['etag'] = $this->getETag($path);

$fileId = $this->getCache()->put($path, $stat);
$exists = $this->getCache()->inCache($path);
$uploadPath = $exists ? $path : $path . '.part';
$fileId = $this->getCache()->put($uploadPath, $stat);
$urn = $this->getURN($fileId);
try {
//upload to object storage
$this->objectStore->writeObject($this->getURN($fileId), fopen($tmpFile, 'r'));
} catch (\Exception $ex) {
$this->getCache()->remove($path);
$this->getCache()->remove($uploadPath);
$this->logger->logException($ex, [
'app' => 'objectstore',
'message' => 'Could not create object ' . $this->getURN($fileId) . ' for ' . $path,
'message' => 'Could not create object ' . $urn . ' for ' . $path,
]);
throw $ex; // make this bubble up
}

if (!$exists) {
if ($this->objectStore->objectExists($urn)) {
$this->getCache()->move($uploadPath, $path);
} else {
$this->getCache()->remove($uploadPath);
throw new \Exception("Object not found after writing (urn: $urn, path: $path)", 404);
}
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions lib/private/Files/ObjectStore/S3ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ function deleteObject($urn) {
'Key' => $urn
]);
}

public function objectExists($urn) {
return $this->getConnection()->doesObjectExist($this->bucket, $urn);
}
}
3 changes: 3 additions & 0 deletions lib/private/Files/ObjectStore/StorageObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@ function deleteObject($urn) {
$this->storage->unlink($urn);
}

public function objectExists($urn) {
return $this->storage->file_exists($urn);
}
}
3 changes: 3 additions & 0 deletions lib/private/Files/ObjectStore/Swift.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,7 @@ public function deleteContainer() {
$this->getContainer()->delete();
}

public function objectExists($urn) {
return $this->getContainer()->objectExists($urn);
}
}
9 changes: 9 additions & 0 deletions lib/public/Files/ObjectStore/IObjectStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ public function writeObject($urn, $stream);
* @since 7.0.0
*/
public function deleteObject($urn);

/**
* Check if an object exists in the object store
*
* @param string $urn
* @return bool
* @since 14.0.5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's 14.0.7, but that should be fine as it should be used in 16+ only anyways.

*/
public function objectExists($urn);
}
53 changes: 53 additions & 0 deletions tests/lib/Files/ObjectStore/FailWriteObjectStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Test\Files\ObjectStore;

use OCP\Files\ObjectStore\IObjectStore;

class FailWriteObjectStore implements IObjectStore {
private $objectStore;

public function __construct(IObjectStore $objectStore) {
$this->objectStore = $objectStore;
}

public function getStorageId() {
return $this->objectStore->getStorageId();
}

public function readObject($urn) {
return $this->objectStore->readObject($urn);
}

public function writeObject($urn, $stream) {
// emulate a failed write that didn't throw an error
return true;
}

public function deleteObject($urn) {
$this->objectStore->deleteObject($urn);
}

public function objectExists($urn) {
return $this->objectStore->objectExists($urn);
}
}
38 changes: 38 additions & 0 deletions tests/lib/Files/ObjectStore/ObjectStoreStorageOverwrite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);
/**
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace Test\Files\ObjectStore;

use OC\Files\ObjectStore\ObjectStoreStorage;
use OCP\Files\ObjectStore\IObjectStore;

/**
* Allow overwriting the object store instance for test purposes
*/
class ObjectStoreStorageOverwrite extends ObjectStoreStorage {
public function setObjectStore(IObjectStore $objectStore) {
$this->objectStore = $objectStore;
}

public function getObjectStore(): IObjectStore {
return $this->objectStore;
}
}
17 changes: 16 additions & 1 deletion tests/lib/Files/ObjectStore/ObjectStoreStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
* @group DB
*/
class ObjectStoreStorageTest extends Storage {
/** @var ObjectStoreStorageOverwrite */
protected $instance;

/**
* @var IObjectStore
Expand All @@ -42,7 +44,7 @@ protected function setUp() {
$baseStorage = new Temporary();
$this->objectStorage = new StorageObjectStore($baseStorage);
$config['objectstore'] = $this->objectStorage;
$this->instance = new ObjectStoreStorage($config);
$this->instance = new ObjectStoreStorageOverwrite($config);
}

protected function tearDown() {
Expand Down Expand Up @@ -166,4 +168,17 @@ public function testRenameOverWriteDirectoryOverFile() {
$targetId = $this->instance->getCache()->getId('target');
$this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
}

public function testWriteObjectSilentFailure() {
$objectStore = $this->instance->getObjectStore();
$this->instance->setObjectStore(new FailWriteObjectStore($objectStore));

try {
$this->instance->file_put_contents('test.txt', 'foo');
$this->fail('expected exception');
} catch (\Exception $e) {
$this->assertStringStartsWith('Object not found after writing', $e->getMessage());
}
$this->assertFalse($this->instance->file_exists('test.txt'));
}
}
15 changes: 15 additions & 0 deletions tests/lib/Files/ObjectStore/ObjectStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,19 @@ public function testDeleteNonExisting() {
$this->assertEquals(1, 1);
}
}

public function testExists() {
$stream = $this->stringToStream('foobar');

$instance = $this->getInstance();
$this->assertFalse($instance->objectExists('2'));

$instance->writeObject('2', $stream);

$this->assertTrue($instance->objectExists('2'));

$instance->deleteObject('2');

$this->assertFalse($instance->objectExists('2'));
}
}