/**
  * walk up the users file tree and update the etags
  * @param string $user
  * @param string $path
  */
 private static function correctUsersFolder($user, $path)
 {
     // $path points to the mount point which is a virtual folder, so we start with
     // the parent
     $path = '/files' . dirname($path);
     \OC\Files\Filesystem::initMountPoints($user);
     $view = new \OC\Files\View('/' . $user);
     if ($view->file_exists($path)) {
         while ($path !== dirname($path)) {
             $etag = $view->getETag($path);
             $view->putFileInfo($path, array('etag' => $etag));
             $path = dirname($path);
         }
     } else {
         \OCP\Util::writeLog('files_sharing', 'can not update etags on ' . $path . ' for user ' . $user . '. Path does not exists', \OCP\Util::DEBUG);
     }
 }
Beispiel #2
0
 /**
  * @medium
  */
 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($oldCachedData['mtime'], $cachedData['mtime']);
     $this->assertEquals($cachedData['storage_mtime'], $cachedData['mtime']);
 }
Beispiel #3
0
 public function testFilePutContentsClearsChecksum()
 {
     $storage = new Temporary(array());
     $scanner = $storage->getScanner();
     $storage->file_put_contents('foo.txt', 'bar');
     \OC\Files\Filesystem::mount($storage, array(), '/test/');
     $scanner->scan('');
     $view = new \OC\Files\View('/test/foo.txt');
     $view->putFileInfo('.', ['checksum' => '42']);
     $this->assertEquals('bar', $view->file_get_contents(''));
     $fh = tmpfile();
     fwrite($fh, 'fooo');
     rewind($fh);
     $view->file_put_contents('', $fh);
     $this->assertEquals('fooo', $view->file_get_contents(''));
     $data = $view->getFileInfo('.');
     $this->assertEquals('', $data->getChecksum());
 }
Beispiel #4
0
 /**
  * @param string $path
  * @param int $size
  * @return int|bool
  */
 public function postFileSize($path, $size, $fileInfo = null)
 {
     $view = new \OC\Files\View('/');
     $userId = Helper::getUser($path);
     $util = new Util($view, $userId);
     // if encryption is no longer enabled or if the files aren't migrated yet
     // we return the default file size
     if (!\OCP\App::isEnabled('files_encryption') || $util->getMigrationStatus() !== Util::MIGRATION_COMPLETED) {
         return $size;
     }
     // if path is a folder do nothing
     if ($view->is_dir($path)) {
         $proxyState = \OC_FileProxy::$enabled;
         \OC_FileProxy::$enabled = false;
         $fileInfo = $view->getFileInfo($path);
         \OC_FileProxy::$enabled = $proxyState;
         if (isset($fileInfo['unencrypted_size']) && $fileInfo['unencrypted_size'] > 0) {
             return $fileInfo['unencrypted_size'];
         }
         return $size;
     }
     // get relative path
     $relativePath = \OCA\Encryption\Helper::stripUserFilesPath($path);
     // if path is empty we cannot resolve anything
     if (empty($relativePath)) {
         return $size;
     }
     // get file info from database/cache if not .part file
     if (empty($fileInfo) && !Helper::isPartialFilePath($path)) {
         $proxyState = \OC_FileProxy::$enabled;
         \OC_FileProxy::$enabled = false;
         $fileInfo = $view->getFileInfo($path);
         \OC_FileProxy::$enabled = $proxyState;
     }
     // if file is encrypted return real file size
     if (isset($fileInfo['encrypted']) && $fileInfo['encrypted'] === true) {
         // try to fix unencrypted file size if it doesn't look plausible
         if ((int) $fileInfo['size'] > 0 && (int) $fileInfo['unencrypted_size'] === 0) {
             $fixSize = $util->getFileSize($path);
             $fileInfo['unencrypted_size'] = $fixSize;
             // put file info if not .part file
             if (!Helper::isPartialFilePath($relativePath)) {
                 $view->putFileInfo($path, array('unencrypted_size' => $fixSize));
             }
         }
         $size = $fileInfo['unencrypted_size'];
     } else {
         $fileInfoUpdates = array();
         $fixSize = $util->getFileSize($path);
         if ($fixSize > 0) {
             $size = $fixSize;
             $fileInfoUpdates['encrypted'] = true;
             $fileInfoUpdates['unencrypted_size'] = $size;
             // put file info if not .part file
             if (!Helper::isPartialFilePath($relativePath)) {
                 $view->putFileInfo($path, $fileInfoUpdates);
             }
         }
     }
     return $size;
 }
Beispiel #5
0
 /**
  * @large
  */
 function testEncryptLegacyFiles()
 {
     \Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $userView = new \OC\Files\View('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $view = new \OC\Files\View('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files');
     // Disable encryption proxy to prevent recursive calls
     $proxyStatus = \OC_FileProxy::$enabled;
     \OC_FileProxy::$enabled = false;
     $encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
     $userView->file_put_contents('/encryption.key', $encryptionKeyContent);
     $legacyEncryptedData = file_get_contents($this->legacyEncryptedData);
     $view->mkdir('/test/');
     $view->mkdir('/test/subtest/');
     $view->file_put_contents('/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData);
     $fileInfo = $view->getFileInfo('/test/subtest/legacy-encrypted-text.txt');
     $fileInfo['encrypted'] = true;
     $view->putFileInfo('/test/subtest/legacy-encrypted-text.txt', $fileInfo);
     \OC_FileProxy::$enabled = $proxyStatus;
     $params['uid'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
     $params['password'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
     $util = new Encryption\Util($this->view, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $this->setMigrationStatus(0, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
     $this->assertTrue(OCA\Encryption\Hooks::login($params));
     $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
     $files = $util->findEncFiles('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files/');
     $this->assertTrue(is_array($files));
     $found = false;
     foreach ($files['encrypted'] as $encryptedFile) {
         if ($encryptedFile['name'] === 'legacy-encrypted-text.txt') {
             $found = true;
             break;
         }
     }
     $this->assertTrue($found);
 }