/** * @param int $mtime * @throws \OCP\Files\NotPermittedException */ public function touch($mtime = null) { if ($this->checkPermissions(\OCP\PERMISSION_UPDATE)) { $this->sendHooks(array('preTouch')); $this->view->touch($this->path, $mtime); $this->sendHooks(array('postTouch')); } else { throw new NotPermittedException(); } }
/** * Encrypt all files in a directory * @param string $dirPath the directory whose files will be encrypted * @return bool * @note Encryption is recursive */ public function encryptAll($dirPath) { $result = true; $found = $this->findEncFiles($dirPath); // Disable proxy to prevent file being encrypted twice \OC_FileProxy::$enabled = false; $versionStatus = \OCP\App::isEnabled('files_versions'); \OC_App::disable('files_versions'); $encryptedFiles = array(); // Encrypt unencrypted files foreach ($found['plain'] as $plainFile) { //get file info $fileInfo = \OC\Files\Filesystem::getFileInfo($plainFile['path']); //relative to data/<user>/file $relPath = $plainFile['path']; //relative to /data $rawPath = '/' . $this->userId . '/files/' . $plainFile['path']; // keep timestamp $timestamp = $fileInfo['mtime']; // Open plain file handle for binary reading $plainHandle = $this->view->fopen($rawPath, 'rb'); // Open enc file handle for binary writing, with same filename as original plain file $encHandle = fopen('crypt://' . $rawPath . '.part', 'wb'); if (is_resource($encHandle) && is_resource($plainHandle)) { // Move plain file to a temporary location $size = stream_copy_to_stream($plainHandle, $encHandle); fclose($encHandle); fclose($plainHandle); $fakeRoot = $this->view->getRoot(); $this->view->chroot('/' . $this->userId . '/files'); $this->view->rename($relPath . '.part', $relPath); // set timestamp $this->view->touch($relPath, $timestamp); $encSize = $this->view->filesize($relPath); $this->view->chroot($fakeRoot); // Add the file to the cache \OC\Files\Filesystem::putFileInfo($relPath, array('encrypted' => true, 'size' => $encSize, 'unencrypted_size' => $size, 'etag' => $fileInfo['etag'])); $encryptedFiles[] = $relPath; } else { \OCP\Util::writeLog('files_encryption', 'initial encryption: could not encrypt ' . $rawPath, \OCP\Util::FATAL); $result = false; } } \OC_FileProxy::$enabled = true; if ($versionStatus) { \OC_App::enable('files_versions'); } $result = $result && $this->encryptVersions($encryptedFiles); return $result; }
/** * @param int $mtime * @throws \OCP\Files\NotPermittedException */ public function touch($mtime = null) { if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) { $this->sendHooks(array('preTouch')); $this->view->touch($this->path, $mtime); $this->sendHooks(array('postTouch')); if ($this->fileInfo) { if (is_null($mtime)) { $mtime = time(); } $this->fileInfo['mtime'] = $mtime; } } else { throw new NotPermittedException(); } }
public function setUp() { $app = new Application(); $container = $app->getContainer(); // reset backend $um = $container->getServer()->getUserManager(); $this->userSession = $container->getServer()->getUserSession(); $um->clearBackends(); $um->registerBackend(new \OC_User_Database()); // create test user $this->userName = '******'; \OC_User::deleteUser($this->userName); $um->createUser($this->userName, $this->userName); \OC_Util::tearDownFS(); $this->userSession->setUser(null); Filesystem::tearDown(); \OC_Util::setupFS($this->userName); $this->userSession->setUser($um->get($this->userName)); $view = new \OC\Files\View('/' . $this->userName . '/files'); // setup files $filesToCopy = array('documents' => array('document.pdf', 'document.docx', 'document.odt', 'document.txt')); $count = 0; foreach ($filesToCopy as $folder => $files) { foreach ($files as $file) { $imgData = file_get_contents(__DIR__ . '/data/' . $file); $view->mkdir($folder); $path = $folder . '/' . $file; $view->file_put_contents($path, $imgData); // set mtime to get fixed sorting with respect to recentFiles $count++; $view->touch($path, 1000 + $count); } } list($storage, ) = $view->resolvePath(''); /** @var $storage Storage */ $this->storage = $storage; $this->scanner = $storage->getScanner(); $this->scanner->scan(''); }
/** * recursive copy to copy a whole directory * * @param string $source source path, relative to the users files directory * @param string $destination destination path relative to the users root directoy * @param \OC\Files\View $view file view for the users root directory */ private static function copy_recursive($source, $destination, $view) { $size = 0; if ($view->is_dir($source)) { $view->mkdir($destination); $view->touch($destination, $view->filemtime($source)); foreach ($view->getDirectoryContent($source) as $i) { $pathDir = $source . '/' . $i['name']; if ($view->is_dir($pathDir)) { $size += self::copy_recursive($pathDir, $destination . '/' . $i['name'], $view); } else { $size += $view->filesize($pathDir); $result = $view->copy($pathDir, $destination . '/' . $i['name']); if (!$result) { throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); } $view->touch($destination . '/' . $i['name'], $view->filemtime($pathDir)); } } } else { $size += $view->filesize($source); $result = $view->copy($source, $destination); if (!$result) { throw new \OCA\Files_Trashbin\Exceptions\CopyRecursiveException(); } $view->touch($destination, $view->filemtime($source)); } return $size; }
/** * sets the last modification time of the file (mtime) to the value given * in the second parameter or to now if the second param is empty. * Even if the modification time is set to a custom value the access time is set to now. */ public function touch($mtime) { $this->fileView->touch($this->path, $mtime); $this->refreshInfo(); }
/** * Rollback to an old version of a file. * * @param string $file file name * @param int $revision revision timestamp */ public static function rollback($file, $revision) { if (\OCP\Config::getSystemValue('files_versions', Storage::DEFAULTENABLED) == 'true') { // add expected leading slash $file = '/' . ltrim($file, '/'); list($uid, $filename) = self::getUidAndFilename($file); $users_view = new View('/' . $uid); $files_view = new View('/' . User::getUser() . '/files'); $versionCreated = false; //first create a new version $version = 'files_versions' . $filename . '.v' . $users_view->filemtime('files' . $filename); if (!$users_view->file_exists($version)) { $users_view->copy('files' . $filename, 'files_versions' . $filename . '.v' . $users_view->filemtime('files' . $filename)); $versionCreated = true; } $fileToRestore = 'files_versions' . $filename . '.v' . $revision; // Restore encrypted version of the old file for the newly restored file // This has to happen manually here since the file is manually copied below $oldVersion = $users_view->getFileInfo($fileToRestore)->getEncryptedVersion(); $newFileInfo = $files_view->getFileInfo($filename); $cache = $newFileInfo->getStorage()->getCache(); $cache->update($newFileInfo->getId(), ['encrypted' => $oldVersion, 'encryptedVersion' => $oldVersion]); // rollback if (self::copyFileContents($users_view, $fileToRestore, 'files' . $filename)) { $files_view->touch($file, $revision); Storage::scheduleExpire($uid, $file); \OC_Hook::emit('\\OCP\\Versions', 'rollback', array('path' => $filename, 'revision' => $revision)); return true; } else { if ($versionCreated) { self::deleteVersion($users_view, $version); } } } return false; }
public static function touch($path, $mtime = null) { return self::$defaultInstance->touch($path, $mtime); }