/** * Updates the data * * The data argument is a readable stream resource. * * After a succesful put operation, you may choose to return an ETag. The * etag must always be surrounded by double-quotes. These quotes must * appear in the actual string you're returning. * * Clients may use the ETag from a PUT request to later on make sure that * when they update the file, the contents haven't changed in the mean * time. * * If you don't plan to store the file byte-by-byte, and you return a * different object on a subsequent GET you are strongly recommended to not * return an ETag, and just return null. * * @param resource $data * @throws Sabre_DAV_Exception_Forbidden * @return string|null */ public function put($data) { if (!\OC\Files\Filesystem::isUpdatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } // mark file as partial while uploading (ignored by the scanner) $partpath = $this->path . '.part'; \OC\Files\Filesystem::file_put_contents($partpath, $data); //detect aborted upload if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { if (isset($_SERVER['CONTENT_LENGTH'])) { $expected = $_SERVER['CONTENT_LENGTH']; $actual = \OC\Files\Filesystem::filesize($partpath); if ($actual != $expected) { \OC\Files\Filesystem::unlink($partpath); throw new Sabre_DAV_Exception_BadRequest('expected filesize ' . $expected . ' got ' . $actual); } } } // rename to correct path \OC\Files\Filesystem::rename($partpath, $this->path); //allow sync clients to send the mtime along in a header $mtime = OC_Request::hasModificationTime(); if ($mtime !== false) { if (\OC\Files\Filesystem::touch($this->path, $mtime)) { header('X-OC-MTime: accepted'); } } return OC_Connector_Sabre_Node::getETagPropertyForPath($this->path); }
public function testUnLockAsRecipient() { $this->loginAsUser($this->ownerUid); Filesystem::initMountPoints($this->recipientUid); $recipientView = new View('/' . $this->recipientUid . '/files'); $recipientView->lockFile('bar.txt', ILockingProvider::LOCK_EXCLUSIVE); $recipientView->unlockFile('bar.txt', ILockingProvider::LOCK_EXCLUSIVE); $this->assertTrue(Filesystem::rename('/foo', '/asd')); }
/** * @brief Renames the node * @param string $name The new name * @return void */ public function setName($name) { list($parentPath, ) = Sabre_DAV_URLUtil::splitPath($this->path); list(, $newName) = Sabre_DAV_URLUtil::splitPath($name); $newPath = $parentPath . '/' . $newName; $oldPath = $this->path; \OC\Files\Filesystem::rename($this->path, $newPath); $this->path = $newPath; $query = OC_DB::prepare('UPDATE `*PREFIX*properties` SET `propertypath` = ?' . ' WHERE `userid` = ? AND `propertypath` = ?'); $query->execute(array($newPath, OC_User::getUser(), $oldPath)); }
/** * Creates a new file in the directory * * Data will either be supplied as a stream resource, or in certain cases * as a string. Keep in mind that you may have to support either. * * After succesful creation of the file, you may choose to return the ETag * of the new file here. * * The returned ETag must be surrounded by double-quotes (The quotes should * be part of the actual string). * * If you cannot accurately determine the ETag, you should not return it. * If you don't store the file exactly as-is (you're transforming it * somehow) you should also not return an ETag. * * This means that if a subsequent GET to this new file does not exactly * return the same contents of what was submitted here, you are strongly * recommended to omit the ETag. * * @param string $name Name of the file * @param resource|string $data Initial payload * @throws Sabre_DAV_Exception_Forbidden * @return null|string */ public function createFile($name, $data = null) { if (!\OC\Files\Filesystem::isCreatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } if (isset($_SERVER['HTTP_OC_CHUNKED'])) { $info = OC_FileChunking::decodeName($name); if (empty($info)) { throw new Sabre_DAV_Exception_NotImplemented(); } $chunk_handler = new OC_FileChunking($info); $chunk_handler->store($info['index'], $data); if ($chunk_handler->isComplete()) { $newPath = $this->path . '/' . $info['name']; $chunk_handler->file_assemble($newPath); return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); } } else { $newPath = $this->path . '/' . $name; // mark file as partial while uploading (ignored by the scanner) $partpath = $newPath . '.part'; \OC\Files\Filesystem::file_put_contents($partpath, $data); //detect aborted upload if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'PUT') { if (isset($_SERVER['CONTENT_LENGTH'])) { $expected = $_SERVER['CONTENT_LENGTH']; $actual = \OC\Files\Filesystem::filesize($partpath); if ($actual != $expected) { \OC\Files\Filesystem::unlink($partpath); throw new Sabre_DAV_Exception_BadRequest('expected filesize ' . $expected . ' got ' . $actual); } } } // rename to correct path \OC\Files\Filesystem::rename($partpath, $newPath); // allow sync clients to send the mtime along in a header $mtime = OC_Request::hasModificationTime(); if ($mtime !== false) { if (\OC\Files\Filesystem::touch($newPath, $mtime)) { header('X-OC-MTime: accepted'); } } return OC_Connector_Sabre_Node::getETagPropertyForPath($newPath); } return null; }
/** * @medium */ function testUnshareChildren() { $fileInfo2 = \OC\Files\Filesystem::getFileInfo($this->folder); $this->share(\OCP\Share::SHARE_TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // one folder should be shared with the user $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_USER); $this->assertCount(1, $shares); // move shared folder to 'localDir' \OC\Files\Filesystem::mkdir('localDir'); $result = \OC\Files\Filesystem::rename($this->folder, '/localDir/' . $this->folder); $this->assertTrue($result); \OC\Files\Filesystem::unlink('localDir'); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // after the parent directory was deleted the share should be unshared $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_USER); $this->assertEmpty($shares); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); // the folder for the owner should still exists $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder)); }
/** * @medium */ function testpreUnlink() { $fileInfo2 = \OC\Files\Filesystem::getFileInfo($this->folder); $result = \OCP\Share::shareItem('folder', $fileInfo2->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31); $this->assertTrue($result); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // one folder should be shared with the user $sharedFolders = \OCP\Share::getItemsSharedWith('folder'); $this->assertSame(1, count($sharedFolders)); // move shared folder to 'localDir' \OC\Files\Filesystem::mkdir('localDir'); $result = \OC\Files\Filesystem::rename($this->folder, '/localDir/' . $this->folder); $this->assertTrue($result); \OC\Files\Filesystem::unlink('localDir'); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); // after the parent directory was deleted the share should be unshared $sharedFolders = \OCP\Share::getItemsSharedWith('folder'); $this->assertTrue(empty($sharedFolders)); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); // the folder for the owner should still exists $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder)); }
<?php OCP\JSON::checkLoggedIn(); OCP\JSON::callCheck(); \OC::$server->getSession()->close(); // Get data $dir = isset($_POST['dir']) ? (string) $_POST['dir'] : ''; $file = isset($_POST['file']) ? (string) $_POST['file'] : ''; $target = isset($_POST['target']) ? rawurldecode((string) $_POST['target']) : ''; $l = \OC::$server->getL10N('files'); if (\OC\Files\Filesystem::file_exists($target . '/' . $file)) { OCP\JSON::error(array("data" => array("message" => $l->t("Could not move %s - File with this name already exists", array($file))))); exit; } if ($target != '' || strtolower($file) != 'shared') { $targetFile = \OC\Files\Filesystem::normalizePath($target . '/' . $file); $sourceFile = \OC\Files\Filesystem::normalizePath($dir . '/' . $file); try { if (\OC\Files\Filesystem::rename($sourceFile, $targetFile)) { OCP\JSON::success(array("data" => array("dir" => $dir, "files" => $file))); } else { OCP\JSON::error(array("data" => array("message" => $l->t("Could not move %s", array($file))))); } } catch (\OCP\Files\NotPermittedException $e) { OCP\JSON::error(array("data" => array("message" => $l->t("Permission denied")))); } catch (\Exception $e) { OCP\JSON::error(array("data" => array("message" => $e->getMessage()))); } } else { OCP\JSON::error(array("data" => array("message" => $l->t("Could not move %s", array($file))))); }
public function testRenameWithMountPoints() { $storage2 = new \OC\Files\Storage\Temporary(array()); $cache2 = $storage2->getCache(); Filesystem::mount($storage2, array(), '/' . self::$user . '/files/folder/substorage'); Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd'); $view = new View('/' . self::$user . '/files'); $this->assertTrue($cache2->inCache('foo.txt')); $folderCachedData = $view->getFileInfo('folder'); $substorageCachedData = $cache2->get(''); $fooCachedData = $cache2->get('foo.txt'); Filesystem::rename('folder/substorage/foo.txt', 'folder/substorage/bar.txt'); $this->assertFalse($cache2->inCache('foo.txt')); $this->assertTrue($cache2->inCache('bar.txt')); $cachedData = $cache2->get('bar.txt'); $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']); $mtime = $cachedData['mtime']; $cachedData = $cache2->get(''); $this->assertInternalType('string', $substorageCachedData['etag']); $this->assertInternalType('string', $cachedData['etag']); $this->assertNotSame($substorageCachedData['etag'], $cachedData['etag']); // rename can cause mtime change - invalid assert // $this->assertEquals($mtime, $cachedData['mtime']); $cachedData = $view->getFileInfo('folder'); $this->assertInternalType('string', $folderCachedData['etag']); $this->assertInternalType('string', $cachedData['etag']); $this->assertNotSame($folderCachedData['etag'], $cachedData['etag']); // rename can cause mtime change - invalid assert // $this->assertEquals($mtime, $cachedData['mtime']); }
/** * moved mountpoints of a group share should keep the same permission as their parent group share. * See #15253 * * @dataProvider dataPermissionMovedGroupShare */ function testPermissionMovedGroupShare($type, $beforePerm, $afterPerm) { if ($type === 'file') { $path = $this->filename; } else { if ($type === 'folder') { $path = $this->folder; } } \OC_Group::createGroup('testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup'); // Share item with group $share = $this->share(\OCP\Share::SHARE_TYPE_GROUP, $path, self::TEST_FILES_SHARING_API_USER1, 'testGroup', $beforePerm); // Login as user 2 and verify the item exists self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists($path)); $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); $this->assertEquals($beforePerm, $result->getPermissions()); // Now move the item forcing a new entry in the share table \OC\Files\Filesystem::rename($path, "newPath"); $this->assertTrue(\OC\Files\Filesystem::file_exists('newPath')); $this->assertFalse(\OC\Files\Filesystem::file_exists($path)); // change permissions $share->setPermissions($afterPerm); $this->shareManager->updateShare($share); // Login as user 3 and verify that the permissions are changed self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER3); $this->assertNotEmpty($result); $this->assertEquals($afterPerm, $result->getPermissions()); // Login as user 2 and verify that the permissions are changed self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2); $this->assertNotEmpty($result); $this->assertEquals($afterPerm, $result->getPermissions()); $this->assertEquals('/newPath', $result->getTarget()); //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $this->shareManager->deleteShare($share); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup'); }
/** * moved mountpoints of a group share should keep the same permission as their parent group share. * See #15253 * * @dataProvider dataPermissionMovedGroupShare */ function testPermissionMovedGroupShare($type, $beforePerm, $afterPerm) { if ($type === 'file') { $path = $this->filename; } else { if ($type === 'folder') { $path = $this->folder; } } \OC_Group::createGroup('testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup'); // Share item with group $fileinfo = $this->view->getFileInfo($path); $this->assertTrue(\OCP\Share::shareItem($type, $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, "testGroup", $beforePerm)); // Login as user 2 and verify the item exists self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists($path)); $result = \OCP\Share::getItemSharedWithBySource($type, $fileinfo['fileid']); $this->assertNotEmpty($result); $this->assertEquals($beforePerm, $result['permissions']); // Now move the item forcing a new entry in the share table \OC\Files\Filesystem::rename($path, "newPath"); $this->assertTrue(\OC\Files\Filesystem::file_exists('newPath')); $this->assertFalse(\OC\Files\Filesystem::file_exists($path)); // Login as user 1 again and change permissions self::loginHelper(self::TEST_FILES_SHARING_API_USER1); $this->assertTrue(\OCP\Share::setPermissions($type, $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, "testGroup", $afterPerm)); // Login as user 3 and verify that the permissions are changed self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $result = \OCP\Share::getItemSharedWithBySource($type, $fileinfo['fileid']); $this->assertNotEmpty($result); $this->assertEquals($afterPerm, $result['permissions']); $groupShareId = $result['id']; // Login as user 2 and verify that the permissions are changed self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $result = \OCP\Share::getItemSharedWithBySource($type, $fileinfo['fileid']); $this->assertNotEmpty($result); $this->assertEquals($afterPerm, $result['permissions']); $this->assertNotEquals($groupShareId, $result['id']); // Also verify in the DB $statement = "SELECT `permissions` FROM `*PREFIX*share` WHERE `id`=?"; $query = \OCP\DB::prepare($statement); $result = $query->execute([$result['id']]); $shares = $result->fetchAll(); $this->assertCount(1, $shares); $this->assertEquals($afterPerm, $shares[0]['permissions']); //cleanup self::loginHelper(self::TEST_FILES_SHARING_API_USER1); \OCP\Share::unshare($type, $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup'); }
public function getListing($FOLDER, $showdel) { // Get the listing from the database $requery = false; $uid = \OCP\User::getUser(); $query = \OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name"); $results = $query->execute(array($uid))->fetchAll(); $results2 = $results; if ($results) { foreach ($results as $result) { foreach ($results2 as $result2) { if ($result['id'] != $result2['id'] && $result['name'] == $result2['name'] && $result['grouping'] == $result2['grouping']) { // We have a duplicate that should not exist. Need to remove the offending record first $delid = -1; if ($result['mtime'] == $result2['mtime']) { // If the mtime's match, delete the oldest ID. $delid = $result['id']; if ($result['id'] > $result2['id']) { $delid = $result2['id']; } } elseif ($result['mtime'] > $result2['mtime']) { // Again, delete the oldest $delid = $result2['id']; } elseif ($result['mtime'] < $result2['mtime']) { // The only thing left is if result is older $delid = $result['id']; } if ($delid != -1) { $delquery = \OCP\DB::prepare("DELETE FROM *PREFIX*ownnote WHERE id=?"); $delquery->execute(array($delid)); $requery = true; } } } } } if ($requery) { $query = \OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name"); $results = $query->execute(array($uid))->fetchAll(); $requery = false; } // Tests to add a bunch of notes //$now = new DateTime(); //for ($x = 0; $x < 199; $x++) { //saveNote('', "Test ".$x, '', '', $now->getTimestamp()); //} $farray = array(); if ($FOLDER != '') { // Create the folder if it doesn't exist if (!\OC\Files\Filesystem::is_dir($FOLDER)) { if (!\OC\Files\Filesystem::mkdir($FOLDER)) { echo "ERROR: Could not create ownNote directory."; exit; } } // Synchronize files to the database $filearr = array(); if ($listing = \OC\Files\Filesystem::opendir($FOLDER)) { if (!$listing) { echo "ERROR: Error listing directory."; exit; } while (($file = readdir($listing)) !== false) { $tmpfile = $file; if ($tmpfile == "." || $tmpfile == "..") { continue; } if (!$this->endsWith($tmpfile, ".htm") && !$this->endsWith($tmpfile, ".html")) { continue; } if ($info = \OC\Files\Filesystem::getFileInfo($FOLDER . "/" . $tmpfile)) { // Check for EVERNOTE but wait to rename them to get around: // https://github.com/owncloud/core/issues/16202 if ($this->endsWith($tmpfile, ".html")) { $this->checkEvernote($FOLDER, $tmpfile); } // Separate the name and group name $name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $tmpfile); $group = ""; if (substr($name, 0, 1) == "[") { $end = strpos($name, ']'); $group = substr($name, 1, $end - 1); $name = substr($name, $end + 1, strlen($name) - $end + 1); $name = trim($name); } // Set array for later checking $filearr[] = $tmpfile; // Check to see if the file is in the DB $fileindb = false; if ($results) { foreach ($results as $result) { if ($result['deleted'] == 0) { if ($name == $result['name'] && $group == $result['grouping']) { $fileindb = true; // If it is in the DB, check if the filesystem file is newer than the DB if ($result['mtime'] < $info['mtime']) { // File is newer, this could happen if a user updates a file $html = ""; $html = \OC\Files\Filesystem::file_get_contents($FOLDER . "/" . $tmpfile); $this->saveNote('', $result['name'], $result['grouping'], $html, $info['mtime']); $requery = true; } } } } } if (!$fileindb) { // If it's not in the DB, add it. $html = ""; if ($html = \OC\Files\Filesystem::file_get_contents($FOLDER . "/" . $tmpfile)) { } else { $html = ""; } $this->saveNote('', $name, $group, $html, $info['mtime']); $requery = true; } // We moved the rename down here to overcome the OC issue if ($this->endsWith($tmpfile, ".html")) { $tmpfile = substr($tmpfile, 0, -1); if (!\OC\Files\Filesystem::file_exists($FOLDER . "/" . $tmpfile)) { \OC\Files\Filesystem::rename($FOLDER . "/" . $file, $FOLDER . "/" . $tmpfile); } } } } } if ($requery) { $query = \OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name"); $results = $query->execute(array($uid))->fetchAll(); } // Now also make sure the files exist, they may not if the user switched folders in admin. if ($results) { foreach ($results as $result) { if ($result['deleted'] == 0) { $tmpfile = $result['name'] . ".htm"; if ($result['grouping'] != '') { $tmpfile = '[' . $result['grouping'] . '] ' . $result['name'] . '.htm'; } $filefound = false; foreach ($filearr as $f) { if ($f == $tmpfile) { $filefound = true; break; } } if (!$filefound) { $content = $this->editNote($result['name'], $result['grouping']); $this->saveNote($FOLDER, $result['name'], $result['grouping'], $content, 0); } } } } } // Now loop through and return the listing if ($results) { $count = 0; $now = new DateTime(); $filetime = new DateTime(); $l = \OCP\Util::getL10N('ownnote'); foreach ($results as $result) { if ($result['deleted'] == 0 || $showdel == true) { $filetime->setTimestamp($result['mtime']); $timestring = $this->getTimeString($filetime, $now, $l); $f = array(); $f['id'] = $result['id']; $f['name'] = $result['name']; $f['group'] = $result['grouping']; $f['timestring'] = $timestring; $f['mtime'] = $result['mtime']; $f['timediff'] = $now->getTimestamp() - $result['mtime']; $f['deleted'] = $result['deleted']; $farray[$count] = $f; $count++; } } } return $farray; }
/** * if a folder gets renamed all children mount points should be renamed too */ function testRename() { $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder); $result = \OCP\Share::shareItem('folder', $fileinfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2, 31); $this->assertTrue($result); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); // make sure that the shared folder exists $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder)); \OC\Files\Filesystem::mkdir('oldTarget'); \OC\Files\Filesystem::mkdir('oldTarget/subfolder'); \OC\Files\Filesystem::mkdir('newTarget'); \OC\Files\Filesystem::rename($this->folder, 'oldTarget/subfolder/' . $this->folder); // re-login to make sure that the new mount points are initialized $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); \OC\Files\Filesystem::rename('/oldTarget', '/newTarget/oldTarget'); // re-login to make sure that the new mount points are initialized $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists('/newTarget/oldTarget/subfolder/' . $this->folder)); // cleanup $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); $result = \OCP\Share::unshare('folder', $fileinfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_FILES_SHARING_API_USER2); $this->assertTrue($result); }
/** * if a file was shared as group share and as individual share they should be grouped */ function testGroupingOfShares() { $fileinfo = $this->view->getFileInfo($this->filename); $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, \Test_Files_Sharing::TEST_FILES_SHARING_API_GROUP1, \OCP\PERMISSION_READ); $this->assertTrue($result); $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2, \OCP\PERMISSION_UPDATE); $this->assertTrue($result); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $result = \OCP\Share::getItemSharedWith('file', null); $this->assertTrue(is_array($result)); // test should return exactly one shares created from testCreateShare() $this->assertSame(1, count($result)); $share = reset($result); $this->assertSame(\OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE, $share['permissions']); \OC\Files\Filesystem::rename($this->filename, $this->filename . '-renamed'); $result = \OCP\Share::getItemSharedWith('file', null); $this->assertTrue(is_array($result)); // test should return exactly one shares created from testCreateShare() $this->assertSame(1, count($result)); $share = reset($result); $this->assertSame(\OCP\PERMISSION_READ | \OCP\PERMISSION_UPDATE, $share['permissions']); $this->assertSame($this->filename . '-renamed', $share['file_target']); self::loginHelper(self::TEST_FILES_SHARING_API_USER1); // unshare user share $result = \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Files_Sharing::TEST_FILES_SHARING_API_USER2); $this->assertTrue($result); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $result = \OCP\Share::getItemSharedWith('file', null); $this->assertTrue(is_array($result)); // test should return the remaining group share $this->assertSame(1, count($result)); $share = reset($result); // only the group share permissions should be available now $this->assertSame(\OCP\PERMISSION_READ, $share['permissions']); $this->assertSame($this->filename . '-renamed', $share['file_target']); }
public function testReshareRecipientRenameInReShare() { $this->loginAsUser(self::TEST_FILES_SHARING_API_USER4); Filesystem::rename('/sub1/sub2/inside/file.txt', '/sub1/sub2/inside/renamed.txt'); $this->assertEtagsForFoldersChanged([self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, self::TEST_FILES_SHARING_API_USER3, self::TEST_FILES_SHARING_API_USER4]); $this->assertAllUnchanged(); }
/** * test if additional share keys are added if we move a folder to a shared parent * @medium */ function testMoveFolder() { $view = new \OC\Files\View('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1); $filename = '/tmp-' . uniqid(); $folder = '/folder' . uniqid(); \OC\Files\Filesystem::mkdir($folder); // Save long data as encrypted file using stream wrapper $cryptedFile = \OC\Files\Filesystem::file_put_contents($folder . $filename, $this->dataShort); // Test that data was successfully written $this->assertTrue(is_int($cryptedFile)); // Get file decrypted contents $decrypt = \OC\Files\Filesystem::file_get_contents($folder . $filename); $this->assertEquals($this->dataShort, $decrypt); $newFolder = '/newfolder/subfolder' . uniqid(); \OC\Files\Filesystem::mkdir('/newfolder'); // get the file info from previous created file $fileInfo = \OC\Files\Filesystem::getFileInfo('/newfolder'); $this->assertTrue($fileInfo instanceof \OC\Files\FileInfo); // share the folder \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2, OCP\PERMISSION_ALL); \OC\Files\Filesystem::rename($folder, $newFolder); // Get file decrypted contents $newDecrypt = \OC\Files\Filesystem::file_get_contents($newFolder . $filename); $this->assertEquals($this->dataShort, $newDecrypt); // check if additional share key for user2 exists $this->assertTrue($view->file_exists('files_encryption/share-keys' . $newFolder . '/' . $filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); // tear down \OC\Files\Filesystem::unlink($newFolder); \OC\Files\Filesystem::unlink('/newfolder'); }
/** * share file with a group if a user renames the file the filename should not change * for the other users */ function testMoveGroupShare() { \OC_Group::createGroup('testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); \OC_Group::addToGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup'); $fileinfo = $this->view->getFileInfo($this->filename); $result = \OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, "testGroup", 31); self::loginHelper(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); \OC\Files\Filesystem::rename($this->filename, "newFileName"); $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName')); $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename)); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName")); self::loginHelper(self::TEST_FILES_SHARING_API_USER3); $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename)); $this->assertFalse(\OC\Files\Filesystem::file_exists("newFileName")); //cleanup \OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER1, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER2, 'testGroup'); \OC_Group::removeFromGroup(self::TEST_FILES_SHARING_API_USER3, 'testGroup'); }
/** * @deprecated OC_Filesystem is replaced by \OC\Files\Filesystem */ public static function rename($path1, $path2) { return \OC\Files\Filesystem::rename($path1, $path2); }
/** * Rename this file to the given name * @param string $newName name to give (without path) * @return boolean true if rename succeeded, false otherwise */ public function renameTo($newName) { list($owner, $path) = $this->getOwnerViewAndPath(); $newPath = dirname($path) . '/' . $newName; return \OC\Files\Filesystem::rename($path, $newPath); }
/** * @brief Renames the node * @param string $name The new name * @return void */ public function setName($name) { // rename is only allowed if the update privilege is granted if (!\OC\Files\Filesystem::isUpdatable($this->path)) { throw new \Sabre_DAV_Exception_Forbidden(); } list($parentPath, ) = Sabre_DAV_URLUtil::splitPath($this->path); list(, $newName) = Sabre_DAV_URLUtil::splitPath($name); $newPath = $parentPath . '/' . $newName; $oldPath = $this->path; \OC\Files\Filesystem::rename($this->path, $newPath); $this->path = $newPath; $query = OC_DB::prepare('UPDATE `*PREFIX*properties` SET `propertypath` = ?' . ' WHERE `userid` = ? AND `propertypath` = ?'); $query->execute(array($newPath, OC_User::getUser(), $oldPath)); }
public function testShareWithGroupUniqueName() { $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); \OC\Files\Filesystem::file_put_contents('test.txt', 'test'); $share = $this->share(\OCP\Share::SHARE_TYPE_GROUP, 'test.txt', self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_GROUP1, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP); $share = $shares[0]; $this->assertSame('/test.txt', $share->getTarget()); $this->assertSame(19, $share->getPermissions()); \OC\Files\Filesystem::rename('test.txt', 'new test.txt'); $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP); $share = $shares[0]; $this->assertSame('/new test.txt', $share->getTarget()); $this->assertSame(19, $share->getPermissions()); $share->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE); $this->shareManager->updateShare($share); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $shares = $this->shareManager->getSharedWith(self::TEST_FILES_SHARING_API_USER2, \OCP\Share::SHARE_TYPE_GROUP); $share = $shares[0]; $this->assertSame('/new test.txt', $share->getTarget()); $this->assertSame(3, $share->getPermissions()); }
public function testRenameSharedFile() { \OC\Files\Filesystem::file_put_contents("test.txt", "test file"); $t1 = time(); // second version is two weeks older, this way we make sure that no // version will be expired $t2 = $t1 - 60 * 60 * 24 * 14; $this->rootView->mkdir(self::USERS_VERSIONS_ROOT); // create some versions $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; // the renamed versions should not exist! Because we only moved the mount point! $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; $this->rootView->file_put_contents($v1, 'version1'); $this->rootView->file_put_contents($v2, 'version2'); $node = \OC::$server->getUserFolder(self::TEST_VERSIONS_USER)->get('test.txt'); $share = \OC::$server->getShareManager()->newShare(); $share->setNode($node)->setShareType(\OCP\Share::SHARE_TYPE_USER)->setSharedBy(self::TEST_VERSIONS_USER)->setSharedWith(self::TEST_VERSIONS_USER2)->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE); $share = \OC::$server->getShareManager()->createShare($share); self::loginHelper(self::TEST_VERSIONS_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists('test.txt')); // execute rename hook of versions app \OC\Files\Filesystem::rename('test.txt', 'test2.txt'); self::loginHelper(self::TEST_VERSIONS_USER); $this->runCommands(); $this->assertTrue($this->rootView->file_exists($v1)); $this->assertTrue($this->rootView->file_exists($v2)); $this->assertFalse($this->rootView->file_exists($v1Renamed)); $this->assertFalse($this->rootView->file_exists($v2Renamed)); \OC::$server->getShareManager()->deleteShare($share); }
public function testShareWithGroupUniqueName() { $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); \OC\Files\Filesystem::file_put_contents('test.txt', 'test'); $fileInfo = \OC\Files\Filesystem::getFileInfo('test.txt'); $this->assertTrue(\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, self::TEST_FILES_SHARING_API_GROUP1, 23)); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $items = \OCP\Share::getItemsSharedWith('file'); $this->assertSame('/test.txt', $items[0]['file_target']); $this->assertSame(23, $items[0]['permissions']); \OC\Files\Filesystem::rename('test.txt', 'new test.txt'); $items = \OCP\Share::getItemsSharedWith('file'); $this->assertSame('/new test.txt', $items[0]['file_target']); $this->assertSame(23, $items[0]['permissions']); $this->loginHelper(self::TEST_FILES_SHARING_API_USER1); \OCP\Share::setPermissions('file', $items[0]['item_source'], $items[0]['share_type'], $items[0]['share_with'], 3); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $items = \OCP\Share::getItemsSharedWith('file'); $this->assertSame('/new test.txt', $items[0]['file_target']); $this->assertSame(3, $items[0]['permissions']); }
/** * if a folder gets renamed all children mount points should be renamed too */ function testRename() { $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder); $share = $this->share(\OCP\Share::SHARE_TYPE_USER, $this->folder, self::TEST_FILES_SHARING_API_USER1, self::TEST_FILES_SHARING_API_USER2, \OCP\Constants::PERMISSION_ALL); $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); // make sure that the shared folder exists $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder)); \OC\Files\Filesystem::mkdir('oldTarget'); \OC\Files\Filesystem::mkdir('oldTarget/subfolder'); \OC\Files\Filesystem::mkdir('newTarget'); \OC\Files\Filesystem::rename($this->folder, 'oldTarget/subfolder/' . $this->folder); // re-login to make sure that the new mount points are initialized $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); \OC\Files\Filesystem::rename('/oldTarget', '/newTarget/oldTarget'); // re-login to make sure that the new mount points are initialized $this->loginHelper(self::TEST_FILES_SHARING_API_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists('/newTarget/oldTarget/subfolder/' . $this->folder)); // cleanup $this->shareManager->deleteShare($share); }
/** * @dataProvider usersProvider */ function testMoveFileToFolder($userId) { $view = new \OC\Files\View('/' . self::TEST_ENCRYPTION_SHARE_USER1); $filename = '/tmp-' . $this->getUniqueID(); $folder = '/folder' . $this->getUniqueID(); \OC\Files\Filesystem::mkdir($folder); // Save long data as encrypted file using stream wrapper $cryptedFile = \OC\Files\Filesystem::file_put_contents($folder . $filename, $this->dataShort); // Test that data was successfully written $this->assertInternalType('int', $cryptedFile); // Get file decrypted contents $decrypt = \OC\Files\Filesystem::file_get_contents($folder . $filename); $this->assertEquals($this->dataShort, $decrypt); $subFolder = $folder . '/subfolder' . $this->getUniqueID(); \OC\Files\Filesystem::mkdir($subFolder); // get the file info from previous created file $fileInfo = \OC\Files\Filesystem::getFileInfo($folder); $this->assertInstanceOf('\\OC\\Files\\FileInfo', $fileInfo); // share the folder \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_SHARE_USER2, \OCP\Constants::PERMISSION_ALL); // check that the share keys exist $this->assertTrue($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); $this->assertTrue($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); // move the file into the subfolder as the test user self::loginHelper($userId); \OC\Files\Filesystem::rename($folder . $filename, $subFolder . $filename); self::loginHelper(self::TEST_ENCRYPTION_SHARE_USER1); // Get file decrypted contents $newDecrypt = \OC\Files\Filesystem::file_get_contents($subFolder . $filename); $this->assertEquals($this->dataShort, $newDecrypt); // check if additional share key for user2 exists $this->assertTrue($view->file_exists('files_encryption/keys' . $subFolder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); $this->assertTrue($view->file_exists('files_encryption/keys' . $subFolder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); // check that old keys were removed/moved properly $this->assertFalse($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey')); $this->assertFalse($view->file_exists('files_encryption/keys' . $folder . '/' . $filename . '/' . self::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey')); // tear down \OC\Files\Filesystem::unlink($subFolder); \OC\Files\Filesystem::unlink($folder); }
public function viewToNodeProviderCopyRename() { return [[function () { Filesystem::file_put_contents('source', 'asd'); Filesystem::rename('source', 'target'); }, 'preRename'], [function () { Filesystem::file_put_contents('source', 'asd'); Filesystem::rename('source', 'target'); }, 'postRename'], [function () { Filesystem::file_put_contents('source', 'asd'); Filesystem::copy('source', 'target'); }, 'preCopy'], [function () { Filesystem::file_put_contents('source', 'asd'); Filesystem::copy('source', 'target'); }, 'postCopy']]; }
public function testRenameSharedFile() { \OC\Files\Filesystem::file_put_contents("test.txt", "test file"); $fileInfo = \OC\Files\Filesystem::getFileInfo('test.txt'); $t1 = time(); // second version is two weeks older, this way we make sure that no // version will be expired $t2 = $t1 - 60 * 60 * 24 * 14; $this->rootView->mkdir(self::USERS_VERSIONS_ROOT); // create some versions $v1 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t1; $v2 = self::USERS_VERSIONS_ROOT . '/test.txt.v' . $t2; // the renamed versions should not exist! Because we only moved the mount point! $v1Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t1; $v2Renamed = self::USERS_VERSIONS_ROOT . '/test2.txt.v' . $t2; $this->rootView->file_put_contents($v1, 'version1'); $this->rootView->file_put_contents($v2, 'version2'); \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_VERSIONS_USER2, \OCP\Constants::PERMISSION_ALL); self::loginHelper(self::TEST_VERSIONS_USER2); $this->assertTrue(\OC\Files\Filesystem::file_exists('test.txt')); // execute rename hook of versions app \OC\Files\Filesystem::rename('test.txt', 'test2.txt'); self::loginHelper(self::TEST_VERSIONS_USER); $this->runCommands(); $this->assertTrue($this->rootView->file_exists($v1)); $this->assertTrue($this->rootView->file_exists($v2)); $this->assertFalse($this->rootView->file_exists($v1Renamed)); $this->assertFalse($this->rootView->file_exists($v2Renamed)); }
function renameGroup($FOLDER, $originalgroupname, $editgroupname) { $ret = ""; if ($listing = \OC\Files\Filesystem::opendir($FOLDER)) { if (!$listing) { $ret .= "ERROR: Error listing directory."; } else { while (($file = readdir($listing)) !== false) { if (substr($file,0,1) == "[") { $group = ""; $end = strpos($file, ']'); $group = substr($file, 1, $end-1); if ($group == $originalgroupname) { $filename = substr($file, $end+1, strlen($file)-$end+1); $filename = trim($filename); $filename = "[".$editgroupname."] ".$filename; if (!\OC\Files\Filesystem::file_exists($FOLDER."/".$filename)) { if (\OC\Files\Filesystem::rename($FOLDER."/".$file, $FOLDER."/".$filename)) { $ret .= "SUCCESS"; } else { $ret .= "FAIL"; } } else { $ret .= "FAIL"; } } } } } } return $ret; }