]> source.dussan.org Git - nextcloud-server.git/commitdiff
Emulate touch() for backends that don't support it
authorRobin Appelman <icewind@owncloud.com>
Sun, 10 Feb 2013 11:44:27 +0000 (12:44 +0100)
committerVincent Petry <pvince81@owncloud.com>
Thu, 21 Nov 2013 14:55:24 +0000 (15:55 +0100)
Backport of 9738fae3cf1ad18593d21eb62e138e00c01f5f36

lib/files/view.php
tests/lib/files/view.php

index f02e3d4d513a222f7a951655cb4423615a68197e..65c5a527a1bed5a75f972c1a7cee5c3c65d17944 100644 (file)
@@ -254,7 +254,11 @@ class View {
                        $hooks[] = 'write';
                }
 
-               return $this->basicOperation('touch', $path, $hooks, $mtime);
+               $result = $this->basicOperation('touch', $path, array('write'), $mtime);
+               if (!$result) { //if native touch fails, we emulate it by changing the mtime in the cache
+                       $this->putFileInfo($path, array('mtime' => $mtime));
+               }
+               return true;
        }
 
        public function file_get_contents($path) {
index 732f61dc5bdece780abb513120bdb8a6793f01cb..88b4113008b38e35f6db914558033f11ee7c4b19 100644 (file)
@@ -7,6 +7,12 @@
 
 namespace Test\Files;
 
+class TemporaryNoTouch extends \OC\Files\Storage\Temporary {
+       public function touch($path, $mtime = null) {
+               return false;
+       }
+}
+
 class View extends \PHPUnit_Framework_TestCase {
        /**
         * @var \OC\Files\Storage\Storage[] $storages;
@@ -262,12 +268,36 @@ class View extends \PHPUnit_Framework_TestCase {
                $this->hookPath = $params['path'];
        }
 
+       function testTouch() {
+               $storage = $this->getTestStorage(true, '\Test\Files\TemporaryNoTouch');
+
+               \OC\Files\Filesystem::mount($storage, array(), '/');
+
+               $rootView = new \OC\Files\View('');
+               $oldCachedData = $rootView->getFileInfo('foo.txt');
+
+               $rootView->touch('foo.txt', 500);
+
+               $cachedData = $rootView->getFileInfo('foo.txt');
+               $this->assertEquals(500, $cachedData['mtime']);
+               $this->assertEquals($oldCachedData['storage_mtime'], $cachedData['storage_mtime']);
+
+               $rootView->putFileInfo('foo.txt', array('storage_mtime' => 1000)); //make sure the watcher detects the change
+               $rootView->file_put_contents('foo.txt', 'asd');
+               $cachedData = $rootView->getFileInfo('foo.txt');
+               $this->assertGreaterThanOrEqual($cachedData['mtime'], $oldCachedData['mtime']);
+               $this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']);
+       }
+
        /**
         * @param bool $scan
         * @return \OC\Files\Storage\Storage
         */
-       private function getTestStorage($scan = true) {
-               $storage = new \OC\Files\Storage\Temporary(array());
+       private function getTestStorage($scan = true, $class = '\OC\Files\Storage\Temporary') {
+               /**
+                * @var \OC\Files\Storage\Storage $storage
+                */
+               $storage = new $class(array());
                $textData = "dummy file data\n";
                $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
                $storage->mkdir('folder');