]> 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)
committerRobin Appelman <icewind@owncloud.com>
Sun, 10 Feb 2013 11:44:27 +0000 (12:44 +0100)
lib/files/view.php
tests/lib/files/view.php

index 1a234228eab34c4370effa6d98fe8c63baea0291..69e2f1ad349bb68978e28497d0c4a43f2688ee9a 100644 (file)
@@ -242,7 +242,11 @@ class View {
                if (!is_null($mtime) and !is_numeric($mtime)) {
                        $mtime = strtotime($mtime);
                }
-               return $this->basicOperation('touch', $path, array('write'), $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) {
@@ -917,11 +921,11 @@ class View {
        }
 
        /**
-       * Get the owner for a file or folder
-       *
-       * @param string $path
-       * @return string
-       */
+        * Get the owner for a file or folder
+        *
+        * @param string $path
+        * @return string
+        */
        public function getOwner($path) {
                return $this->basicOperation('getOwner', $path);
        }
index c9a8a0fb081f84192cc089abee97e5a12a0ceb11..af97761bbb7aa849823b9a4a95f2327e83543e48 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;
@@ -228,12 +234,36 @@ class View extends \PHPUnit_Framework_TestCase {
                $this->assertEquals(3, $cachedData['size']);
        }
 
+       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');