X Tutup
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
6 changes: 6 additions & 0 deletions changelog/unreleased/40389
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Change: Allow specifying available space for objectstorages

Before this change, objectstorages were reporting only infinite storage space. This could have caused problems in other apps that rely on this storage method, e.g. metrics app that monitors available space

https://github.com/owncloud/core/pull/40192
https://github.com/owncloud/enterprise/issues/5384
46 changes: 46 additions & 0 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

use Icewind\Streams\IteratorDirectory;
use OC\Files\Cache\CacheEntry;
use OC\Files\Filesystem;
use OCP\Files\FileInfo;
use OCP\Files\NotFoundException;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\Files\ObjectStore\IVersionedObjectStorage;
Expand All @@ -47,6 +49,10 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common {
* @var string $id
*/
protected $id;
/**
* @var int|null $availableStorage
*/
private $availableStorage;
/**
* @var \OC\User\User $user
*/
Expand All @@ -68,6 +74,10 @@ public function __construct($params) {
if (isset($params['objectPrefix'])) {
$this->objectPrefix = $params['objectPrefix'];
}
if (isset($params['availableStorage']) && \is_int($params['availableStorage'])) {
$this->availableStorage = $params['availableStorage'];
}

//initialize cache with root directory in cache
if (!$this->is_dir('/')) {
$this->mkdir('/');
Expand Down Expand Up @@ -420,6 +430,42 @@ public function getMimeType($path) {
}
}

/**
* overwrite this method if objectstorage supports getting total bucket objects size
*
* @return int
*/
protected function getTotalUsedStorage() {
$rootInfo = Filesystem::getFileInfo('/', false);
return $rootInfo->getSize();
}

/**
* get the free space in the object storage as indicated by the objectstore config
*
* NOTE: getting total free space for objectstorage is not possible,
* and this can only be set to administrator allowed volume
* e.g. due to billing or monitoring concerns
*
* @param string $path
* @return int
*/
public function free_space($path) {
if (isset($this->availableStorage)) {
$used = $this->getTotalUsedStorage();
if ($used < 0) {
$used = 0;
}

$free = $this->availableStorage - $used;
if ($free < 0) {
return 0;
}
return $free;
}
return FileInfo::SPACE_UNLIMITED;
}

public function touch($path, $mtime = null) {
if ($mtime === null) {
$mtime = \time();
Expand Down
32 changes: 28 additions & 4 deletions tests/lib/Files/ObjectStore/ObjectStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
/**
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
<<<<<<< HEAD
* @copyright Copyright (c) 2018, ownCloud GmbH
=======
* @copyright Copyright (c) 2017, ownCloud GmbH
>>>>>>> 478c4d51eb... Add versioning to objectstore
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
Expand All @@ -29,6 +25,7 @@
use OC\Files\Cache\Updater;
use OC\Files\ObjectStore\NoopScanner;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OCP\Files\FileInfo;
use OCP\Files\NotFoundException;
use OCP\Files\ObjectStore\IObjectStore;
use OCP\Files\ObjectStore\IVersionedObjectStorage;
Expand Down Expand Up @@ -99,6 +96,33 @@ public function testGetAndPutContents() {
public function testGetScanner() {
$this->assertInstanceOf(NoopScanner::class, $this->objectStore->getScanner());
}
public function providersFreeSpace() {
return [
[100, 10, 90],
[100, -1, 100],
[100, 110, 0],
[null, 10, FileInfo::SPACE_UNLIMITED]
];
}

/**
* @dataProvider providersFreeSpace
* @param int $availableStorage
* @param int $usedStorage
* @param int $expected
*/
public function testFreeSpace($availableStorage, $usedStorage, $expected) {
$objectStore = $this->getMockBuilder(ObjectStoreStorage::class)
->setMethods(['getTotalUsedStorage'])
->setConstructorArgs([[
'objectstore' => $this->impl,
'availableStorage' => $availableStorage
]])
->getMock();
$objectStore->method('getTotalUsedStorage')->willReturn($usedStorage);
$free = $objectStore->free_space('test');
$this->assertEquals($expected, $free);
}

public function testUnlink() {
$this->impl->expects($this->once())->method('writeObject');
Expand Down
X Tutup