Skip to content

Commit ea07a42

Browse files
committed
Merge pull request #22506 from owncloud/node-get-from-cache
Query the cache when checking if a node exists
2 parents 7800b9d + 0b0b325 commit ea07a42

File tree

6 files changed

+131
-66
lines changed

6 files changed

+131
-66
lines changed

apps/files_trashbin/tests/trashbin.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ public static function setUpBeforeClass() {
8686
}
8787

8888

89-
9089
public static function tearDownAfterClass() {
9190
// cleanup test user
9291
$user = \OC::$server->getUserManager()->get(self::TEST_TRASHBIN_USER1);
93-
if ($user !== null) { $user->delete(); }
92+
if ($user !== null) {
93+
$user->delete();
94+
}
9495

9596
\OC::$server->getConfig()->setSystemValue('trashbin_retention_obligation', self::$rememberRetentionObligation);
9697

@@ -109,6 +110,18 @@ protected function setUp() {
109110
parent::setUp();
110111

111112
\OC::$server->getAppManager()->enableApp('files_trashbin');
113+
$config = \OC::$server->getConfig();
114+
$mockConfig = $this->getMock('\OCP\IConfig');
115+
$mockConfig->expects($this->any())
116+
->method('getSystemValue')
117+
->will($this->returnCallback(function ($key, $default) use ($config) {
118+
if ($key === 'filesystem_check_changes') {
119+
return \OC\Files\Cache\Watcher::CHECK_ONCE;
120+
} else {
121+
return $config->getSystemValue($key, $default);
122+
}
123+
}));
124+
$this->overwriteService('AllConfig', $mockConfig);
112125

113126
$this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin';
114127
$this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin';
@@ -117,6 +130,7 @@ protected function setUp() {
117130
}
118131

119132
protected function tearDown() {
133+
$this->restoreService('AllConfig');
120134
// disable trashbin to be able to properly clean up
121135
\OC::$server->getAppManager()->disableApp('files_trashbin');
122136

@@ -138,8 +152,8 @@ protected function tearDown() {
138152
public function testExpireOldFiles() {
139153

140154
$currentTime = time();
141-
$expireAt = $currentTime - 2*24*60*60;
142-
$expiredDate = $currentTime - 3*24*60*60;
155+
$expireAt = $currentTime - 2 * 24 * 60 * 60;
156+
$expiredDate = $currentTime - 3 * 24 * 60 * 60;
143157

144158
// create some files
145159
\OC\Files\Filesystem::file_put_contents('file1.txt', 'file1');
@@ -187,7 +201,7 @@ public function testExpireOldFilesShared() {
187201

188202
$currentTime = time();
189203
$folder = "trashTest-" . $currentTime . '/';
190-
$expiredDate = $currentTime - 3*24*60*60;
204+
$expiredDate = $currentTime - 3 * 24 * 60 * 60;
191205

192206
// create some files
193207
\OC\Files\Filesystem::mkdir($folder);
@@ -250,6 +264,7 @@ public function testExpireOldFilesShared() {
250264

251265
/**
252266
* verify that the array contains the expected results
267+
*
253268
* @param OCP\Files\FileInfo[] $result
254269
* @param string[] $expected
255270
*/
@@ -265,7 +280,7 @@ private function verifyArray($result, $expected) {
265280
}
266281
if (!$found) {
267282
// if we didn't found the expected file, something went wrong
268-
$this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin");
283+
$this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin");
269284
}
270285
}
271286
}
@@ -281,7 +296,7 @@ private function manipulateDeleteTime($files, $trashRoot, $expireDate) {
281296
// modify every second file
282297
$counter = ($counter + 1) % 2;
283298
if ($counter === 1) {
284-
$source = $trashRoot . '/files/' . $file['name'].'.d'.$file['mtime'];
299+
$source = $trashRoot . '/files/' . $file['name'] . '.d' . $file['mtime'];
285300
$target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate);
286301
$this->rootView->rename($source, $target);
287302
$file['mtime'] = $expireDate;
@@ -445,7 +460,7 @@ public function testRestoreFileFromTrashedSubfolder() {
445460
$trashedFile = $filesInTrash[0];
446461

447462
$this->assertTrue(
448-
OCA\Files_Trashbin\Trashbin::restore(
463+
OCA\Files_Trashbin\Trashbin::restore(
449464
'folder.d' . $trashedFile->getMtime() . '/file1.txt',
450465
'file1.txt',
451466
$trashedFile->getMtime()
@@ -639,7 +654,7 @@ public static function loginHelper($user, $create = false) {
639654
if ($create) {
640655
try {
641656
\OC::$server->getUserManager()->createUser($user, $user);
642-
} catch(\Exception $e) { // catch username is already being used from previous aborted runs
657+
} catch (\Exception $e) { // catch username is already being used from previous aborted runs
643658

644659
}
645660
}

apps/files_versions/tests/versions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ public static function tearDownAfterClass() {
7474
protected function setUp() {
7575
parent::setUp();
7676

77+
$config = \OC::$server->getConfig();
78+
$mockConfig = $this->getMock('\OCP\IConfig');
79+
$mockConfig->expects($this->any())
80+
->method('getSystemValue')
81+
->will($this->returnCallback(function ($key, $default) use ($config) {
82+
if ($key === 'filesystem_check_changes') {
83+
return \OC\Files\Cache\Watcher::CHECK_ONCE;
84+
} else {
85+
return $config->getSystemValue($key, $default);
86+
}
87+
}));
88+
$this->overwriteService('AllConfig', $mockConfig);
89+
7790
// clear hooks
7891
\OC_Hook::clear();
7992
\OC::registerShareHooks();
@@ -87,6 +100,8 @@ protected function setUp() {
87100
}
88101

89102
protected function tearDown() {
103+
$this->restoreService('AllConfig');
104+
90105
if ($this->rootView) {
91106
$this->rootView->deleteAll(self::TEST_VERSIONS_USER . '/files/');
92107
$this->rootView->deleteAll(self::TEST_VERSIONS_USER2 . '/files/');

lib/private/files/node/folder.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ public function getDirectoryListing() {
9090

9191
/**
9292
* @param string $path
93-
* @param array $info
93+
* @param FileInfo $info
9494
* @return File|Folder
9595
*/
96-
protected function createNode($path, $info = array()) {
97-
if (!isset($info['mimetype'])) {
96+
protected function createNode($path, FileInfo $info = null) {
97+
if (is_null($info)) {
9898
$isDir = $this->view->is_dir($path);
9999
} else {
100-
$isDir = $info['mimetype'] === 'httpd/unix-directory';
100+
$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
101101
}
102102
if ($isDir) {
103-
return new Folder($this->root, $this->view, $path);
103+
return new Folder($this->root, $this->view, $path, $info);
104104
} else {
105-
return new File($this->root, $this->view, $path);
105+
return new File($this->root, $this->view, $path, $info);
106106
}
107107
}
108108

@@ -211,10 +211,9 @@ public function searchByTag($tag, $userId) {
211211
private function searchCommon($method, $args) {
212212
$files = array();
213213
$rootLength = strlen($this->path);
214-
/**
215-
* @var \OC\Files\Storage\Storage $storage
216-
*/
217-
list($storage, $internalPath) = $this->view->resolvePath($this->path);
214+
$mount = $this->root->getMount($this->path);
215+
$storage = $mount->getStorage();
216+
$internalPath = $mount->getInternalPath($this->path);
218217
$internalPath = rtrim($internalPath, '/');
219218
if ($internalPath !== '') {
220219
$internalPath = $internalPath . '/';
@@ -229,7 +228,7 @@ private function searchCommon($method, $args) {
229228
$result['internalPath'] = $result['path'];
230229
$result['path'] = substr($result['path'], $internalRootLength);
231230
$result['storage'] = $storage;
232-
$files[] = $result;
231+
$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
233232
}
234233
}
235234

@@ -245,17 +244,14 @@ private function searchCommon($method, $args) {
245244
$result['internalPath'] = $result['path'];
246245
$result['path'] = $relativeMountPoint . $result['path'];
247246
$result['storage'] = $storage;
248-
$files[] = $result;
247+
$files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
249248
}
250249
}
251250
}
252251

253-
$result = array();
254-
foreach ($files as $file) {
255-
$result[] = $this->createNode($this->normalizePath($this->path . '/' . $file['path']), $file);
256-
}
257-
258-
return $result;
252+
return array_map(function(FileInfo $file) {
253+
return $this->createNode($file->getPath(), $file);
254+
}, $files);
259255
}
260256

261257
/**

lib/private/files/node/root.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ public function get($path) {
176176
$path = $this->normalizePath($path);
177177
if ($this->isValidPath($path)) {
178178
$fullPath = $this->getFullPath($path);
179-
if ($this->view->file_exists($fullPath)) {
180-
return $this->createNode($fullPath);
179+
$fileInfo = $this->view->getFileInfo($fullPath);
180+
if ($fileInfo) {
181+
return $this->createNode($fullPath, $fileInfo);
181182
} else {
182183
throw new NotFoundException($path);
183184
}
@@ -336,18 +337,18 @@ public function getUserFolder($userId) {
336337
$dir = '/' . $userId;
337338
$folder = null;
338339

339-
if (!$this->nodeExists($dir)) {
340-
$folder = $this->newFolder($dir);
341-
} else {
340+
try {
342341
$folder = $this->get($dir);
342+
} catch (NotFoundException $e) {
343+
$folder = $this->newFolder($dir);
343344
}
344345

345346
$dir = '/files';
346-
if (!$folder->nodeExists($dir)) {
347+
try {
348+
$folder = $folder->get($dir);
349+
} catch (NotFoundException $e) {
347350
$folder = $folder->newFolder($dir);
348351
\OC_Util::copySkeleton($userId, $folder);
349-
} else {
350-
$folder = $folder->get($dir);
351352
}
352353

353354
return $folder;

0 commit comments

Comments
 (0)