You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Directory.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Jakob Sack <mail@jakobsack.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OCA\DAV\Connector\Sabre;
  34. use OC\Files\Mount\MoveableMount;
  35. use OC\Files\View;
  36. use OCA\DAV\Connector\Sabre\Exception\FileLocked;
  37. use OCA\DAV\Connector\Sabre\Exception\Forbidden;
  38. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  39. use OCP\Files\FileInfo;
  40. use OCP\Files\ForbiddenException;
  41. use OCP\Files\InvalidPathException;
  42. use OCP\Files\StorageNotAvailableException;
  43. use OCP\Lock\ILockingProvider;
  44. use OCP\Lock\LockedException;
  45. use Sabre\DAV\Exception\BadRequest;
  46. use Sabre\DAV\Exception\Locked;
  47. use Sabre\DAV\Exception\NotFound;
  48. use Sabre\DAV\Exception\ServiceUnavailable;
  49. use Sabre\DAV\IFile;
  50. use Sabre\DAV\INode;
  51. class Directory extends \OCA\DAV\Connector\Sabre\Node implements \Sabre\DAV\ICollection, \Sabre\DAV\IQuota, \Sabre\DAV\IMoveTarget, \Sabre\DAV\ICopyTarget {
  52. /**
  53. * Cached directory content
  54. *
  55. * @var \OCP\Files\FileInfo[]
  56. */
  57. private $dirContent;
  58. /**
  59. * Cached quota info
  60. *
  61. * @var array
  62. */
  63. private $quotaInfo;
  64. /**
  65. * @var ObjectTree|null
  66. */
  67. private $tree;
  68. /**
  69. * Sets up the node, expects a full path name
  70. *
  71. * @param \OC\Files\View $view
  72. * @param \OCP\Files\FileInfo $info
  73. * @param ObjectTree|null $tree
  74. * @param \OCP\Share\IManager $shareManager
  75. */
  76. public function __construct(View $view, FileInfo $info, $tree = null, $shareManager = null) {
  77. parent::__construct($view, $info, $shareManager);
  78. $this->tree = $tree;
  79. }
  80. /**
  81. * Creates a new file in the directory
  82. *
  83. * Data will either be supplied as a stream resource, or in certain cases
  84. * as a string. Keep in mind that you may have to support either.
  85. *
  86. * After successful creation of the file, you may choose to return the ETag
  87. * of the new file here.
  88. *
  89. * The returned ETag must be surrounded by double-quotes (The quotes should
  90. * be part of the actual string).
  91. *
  92. * If you cannot accurately determine the ETag, you should not return it.
  93. * If you don't store the file exactly as-is (you're transforming it
  94. * somehow) you should also not return an ETag.
  95. *
  96. * This means that if a subsequent GET to this new file does not exactly
  97. * return the same contents of what was submitted here, you are strongly
  98. * recommended to omit the ETag.
  99. *
  100. * @param string $name Name of the file
  101. * @param resource|string $data Initial payload
  102. * @return null|string
  103. * @throws Exception\EntityTooLarge
  104. * @throws Exception\UnsupportedMediaType
  105. * @throws FileLocked
  106. * @throws InvalidPath
  107. * @throws \Sabre\DAV\Exception
  108. * @throws \Sabre\DAV\Exception\BadRequest
  109. * @throws \Sabre\DAV\Exception\Forbidden
  110. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  111. */
  112. public function createFile($name, $data = null) {
  113. try {
  114. // for chunked upload also updating a existing file is a "createFile"
  115. // because we create all the chunks before re-assemble them to the existing file.
  116. if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  117. // exit if we can't create a new file and we don't updatable existing file
  118. $chunkInfo = \OC_FileChunking::decodeName($name);
  119. if (!$this->fileView->isCreatable($this->path) &&
  120. !$this->fileView->isUpdatable($this->path . '/' . $chunkInfo['name'])
  121. ) {
  122. throw new \Sabre\DAV\Exception\Forbidden();
  123. }
  124. } else {
  125. // For non-chunked upload it is enough to check if we can create a new file
  126. if (!$this->fileView->isCreatable($this->path)) {
  127. throw new \Sabre\DAV\Exception\Forbidden();
  128. }
  129. }
  130. $this->fileView->verifyPath($this->path, $name);
  131. $path = $this->fileView->getAbsolutePath($this->path) . '/' . $name;
  132. // in case the file already exists/overwriting
  133. $info = $this->fileView->getFileInfo($this->path . '/' . $name);
  134. if (!$info) {
  135. // use a dummy FileInfo which is acceptable here since it will be refreshed after the put is complete
  136. $info = new \OC\Files\FileInfo($path, null, null, [], null);
  137. }
  138. $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info);
  139. // only allow 1 process to upload a file at once but still allow reading the file while writing the part file
  140. $node->acquireLock(ILockingProvider::LOCK_SHARED);
  141. $this->fileView->lockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE);
  142. $result = $node->put($data);
  143. $this->fileView->unlockFile($path . '.upload.part', ILockingProvider::LOCK_EXCLUSIVE);
  144. $node->releaseLock(ILockingProvider::LOCK_SHARED);
  145. return $result;
  146. } catch (\OCP\Files\StorageNotAvailableException $e) {
  147. throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage(), $e->getCode(), $e);
  148. } catch (InvalidPathException $ex) {
  149. throw new InvalidPath($ex->getMessage(), false, $ex);
  150. } catch (ForbiddenException $ex) {
  151. throw new Forbidden($ex->getMessage(), $ex->getRetry(), $ex);
  152. } catch (LockedException $e) {
  153. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  154. }
  155. }
  156. /**
  157. * Creates a new subdirectory
  158. *
  159. * @param string $name
  160. * @throws FileLocked
  161. * @throws InvalidPath
  162. * @throws \Sabre\DAV\Exception\Forbidden
  163. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  164. */
  165. public function createDirectory($name) {
  166. try {
  167. if (!$this->info->isCreatable()) {
  168. throw new \Sabre\DAV\Exception\Forbidden();
  169. }
  170. $this->fileView->verifyPath($this->path, $name);
  171. $newPath = $this->path . '/' . $name;
  172. if (!$this->fileView->mkdir($newPath)) {
  173. throw new \Sabre\DAV\Exception\Forbidden('Could not create directory ' . $newPath);
  174. }
  175. } catch (\OCP\Files\StorageNotAvailableException $e) {
  176. throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
  177. } catch (InvalidPathException $ex) {
  178. throw new InvalidPath($ex->getMessage());
  179. } catch (ForbiddenException $ex) {
  180. throw new Forbidden($ex->getMessage(), $ex->getRetry());
  181. } catch (LockedException $e) {
  182. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  183. }
  184. }
  185. /**
  186. * Returns a specific child node, referenced by its name
  187. *
  188. * @param string $name
  189. * @param \OCP\Files\FileInfo $info
  190. * @return \Sabre\DAV\INode
  191. * @throws InvalidPath
  192. * @throws \Sabre\DAV\Exception\NotFound
  193. * @throws \Sabre\DAV\Exception\ServiceUnavailable
  194. */
  195. public function getChild($name, $info = null) {
  196. if (!$this->info->isReadable()) {
  197. // avoid detecting files through this way
  198. throw new NotFound();
  199. }
  200. $path = $this->path . '/' . $name;
  201. if (is_null($info)) {
  202. try {
  203. $this->fileView->verifyPath($this->path, $name);
  204. $info = $this->fileView->getFileInfo($path);
  205. } catch (\OCP\Files\StorageNotAvailableException $e) {
  206. throw new \Sabre\DAV\Exception\ServiceUnavailable($e->getMessage());
  207. } catch (InvalidPathException $ex) {
  208. throw new InvalidPath($ex->getMessage());
  209. } catch (ForbiddenException $e) {
  210. throw new \Sabre\DAV\Exception\Forbidden();
  211. }
  212. }
  213. if (!$info) {
  214. throw new \Sabre\DAV\Exception\NotFound('File with name ' . $path . ' could not be located');
  215. }
  216. if ($info['mimetype'] === 'httpd/unix-directory') {
  217. $node = new \OCA\DAV\Connector\Sabre\Directory($this->fileView, $info, $this->tree, $this->shareManager);
  218. } else {
  219. $node = new \OCA\DAV\Connector\Sabre\File($this->fileView, $info, $this->shareManager);
  220. }
  221. if ($this->tree) {
  222. $this->tree->cacheNode($node);
  223. }
  224. return $node;
  225. }
  226. /**
  227. * Returns an array with all the child nodes
  228. *
  229. * @return \Sabre\DAV\INode[]
  230. * @throws \Sabre\DAV\Exception\Locked
  231. * @throws \OCA\DAV\Connector\Sabre\Exception\Forbidden
  232. */
  233. public function getChildren() {
  234. if (!is_null($this->dirContent)) {
  235. return $this->dirContent;
  236. }
  237. try {
  238. if (!$this->info->isReadable()) {
  239. // return 403 instead of 404 because a 404 would make
  240. // the caller believe that the collection itself does not exist
  241. throw new Forbidden('No read permissions');
  242. }
  243. $folderContent = $this->fileView->getDirectoryContent($this->path);
  244. } catch (LockedException $e) {
  245. throw new Locked();
  246. }
  247. $nodes = [];
  248. foreach ($folderContent as $info) {
  249. $node = $this->getChild($info->getName(), $info);
  250. $nodes[] = $node;
  251. }
  252. $this->dirContent = $nodes;
  253. return $this->dirContent;
  254. }
  255. /**
  256. * Checks if a child exists.
  257. *
  258. * @param string $name
  259. * @return bool
  260. */
  261. public function childExists($name) {
  262. // note: here we do NOT resolve the chunk file name to the real file name
  263. // to make sure we return false when checking for file existence with a chunk
  264. // file name.
  265. // This is to make sure that "createFile" is still triggered
  266. // (required old code) instead of "updateFile".
  267. //
  268. // TODO: resolve chunk file name here and implement "updateFile"
  269. $path = $this->path . '/' . $name;
  270. return $this->fileView->file_exists($path);
  271. }
  272. /**
  273. * Deletes all files in this directory, and then itself
  274. *
  275. * @return void
  276. * @throws FileLocked
  277. * @throws \Sabre\DAV\Exception\Forbidden
  278. */
  279. public function delete() {
  280. if ($this->path === '' || $this->path === '/' || !$this->info->isDeletable()) {
  281. throw new \Sabre\DAV\Exception\Forbidden();
  282. }
  283. try {
  284. if (!$this->fileView->rmdir($this->path)) {
  285. // assume it wasn't possible to remove due to permission issue
  286. throw new \Sabre\DAV\Exception\Forbidden();
  287. }
  288. } catch (ForbiddenException $ex) {
  289. throw new Forbidden($ex->getMessage(), $ex->getRetry());
  290. } catch (LockedException $e) {
  291. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  292. }
  293. }
  294. /**
  295. * Returns available diskspace information
  296. *
  297. * @return array
  298. */
  299. public function getQuotaInfo() {
  300. if ($this->quotaInfo) {
  301. return $this->quotaInfo;
  302. }
  303. try {
  304. $info = $this->fileView->getFileInfo($this->path, false);
  305. $storageInfo = \OC_Helper::getStorageInfo($this->info->getPath(), $info);
  306. if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) {
  307. $free = \OCP\Files\FileInfo::SPACE_UNLIMITED;
  308. } else {
  309. $free = $storageInfo['free'];
  310. }
  311. $this->quotaInfo = [
  312. $storageInfo['used'],
  313. $free
  314. ];
  315. return $this->quotaInfo;
  316. } catch (\OCP\Files\StorageNotAvailableException $e) {
  317. return [0, 0];
  318. }
  319. }
  320. /**
  321. * Moves a node into this collection.
  322. *
  323. * It is up to the implementors to:
  324. * 1. Create the new resource.
  325. * 2. Remove the old resource.
  326. * 3. Transfer any properties or other data.
  327. *
  328. * Generally you should make very sure that your collection can easily move
  329. * the move.
  330. *
  331. * If you don't, just return false, which will trigger sabre/dav to handle
  332. * the move itself. If you return true from this function, the assumption
  333. * is that the move was successful.
  334. *
  335. * @param string $targetName New local file/collection name.
  336. * @param string $fullSourcePath Full path to source node
  337. * @param INode $sourceNode Source node itself
  338. * @return bool
  339. * @throws BadRequest
  340. * @throws ServiceUnavailable
  341. * @throws Forbidden
  342. * @throws FileLocked
  343. * @throws \Sabre\DAV\Exception\Forbidden
  344. */
  345. public function moveInto($targetName, $fullSourcePath, INode $sourceNode) {
  346. if (!$sourceNode instanceof Node) {
  347. // it's a file of another kind, like FutureFile
  348. if ($sourceNode instanceof IFile) {
  349. // fallback to default copy+delete handling
  350. return false;
  351. }
  352. throw new BadRequest('Incompatible node types');
  353. }
  354. if (!$this->fileView) {
  355. throw new ServiceUnavailable('filesystem not setup');
  356. }
  357. $destinationPath = $this->getPath() . '/' . $targetName;
  358. $targetNodeExists = $this->childExists($targetName);
  359. // at getNodeForPath we also check the path for isForbiddenFileOrDir
  360. // with that we have covered both source and destination
  361. if ($sourceNode instanceof Directory && $targetNodeExists) {
  362. throw new \Sabre\DAV\Exception\Forbidden('Could not copy directory ' . $sourceNode->getName() . ', target exists');
  363. }
  364. [$sourceDir,] = \Sabre\Uri\split($sourceNode->getPath());
  365. $destinationDir = $this->getPath();
  366. $sourcePath = $sourceNode->getPath();
  367. $isMovableMount = false;
  368. $sourceMount = \OC::$server->getMountManager()->find($this->fileView->getAbsolutePath($sourcePath));
  369. $internalPath = $sourceMount->getInternalPath($this->fileView->getAbsolutePath($sourcePath));
  370. if ($sourceMount instanceof MoveableMount && $internalPath === '') {
  371. $isMovableMount = true;
  372. }
  373. try {
  374. $sameFolder = ($sourceDir === $destinationDir);
  375. // if we're overwriting or same folder
  376. if ($targetNodeExists || $sameFolder) {
  377. // note that renaming a share mount point is always allowed
  378. if (!$this->fileView->isUpdatable($destinationDir) && !$isMovableMount) {
  379. throw new \Sabre\DAV\Exception\Forbidden();
  380. }
  381. } else {
  382. if (!$this->fileView->isCreatable($destinationDir)) {
  383. throw new \Sabre\DAV\Exception\Forbidden();
  384. }
  385. }
  386. if (!$sameFolder) {
  387. // moving to a different folder, source will be gone, like a deletion
  388. // note that moving a share mount point is always allowed
  389. if (!$this->fileView->isDeletable($sourcePath) && !$isMovableMount) {
  390. throw new \Sabre\DAV\Exception\Forbidden();
  391. }
  392. }
  393. $fileName = basename($destinationPath);
  394. try {
  395. $this->fileView->verifyPath($destinationDir, $fileName);
  396. } catch (InvalidPathException $ex) {
  397. throw new InvalidPath($ex->getMessage());
  398. }
  399. $renameOkay = $this->fileView->rename($sourcePath, $destinationPath);
  400. if (!$renameOkay) {
  401. throw new \Sabre\DAV\Exception\Forbidden('');
  402. }
  403. } catch (StorageNotAvailableException $e) {
  404. throw new ServiceUnavailable($e->getMessage());
  405. } catch (ForbiddenException $ex) {
  406. throw new Forbidden($ex->getMessage(), $ex->getRetry());
  407. } catch (LockedException $e) {
  408. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  409. }
  410. return true;
  411. }
  412. public function copyInto($targetName, $sourcePath, INode $sourceNode) {
  413. if ($sourceNode instanceof File) {
  414. $destinationPath = $this->getPath() . '/' . $targetName;
  415. $sourcePath = $sourceNode->getPath();
  416. if (!$this->fileView->isCreatable($this->getPath())) {
  417. throw new \Sabre\DAV\Exception\Forbidden();
  418. }
  419. try {
  420. $this->fileView->verifyPath($this->getPath(), $targetName);
  421. } catch (InvalidPathException $ex) {
  422. throw new InvalidPath($ex->getMessage());
  423. }
  424. return $this->fileView->copy($sourcePath, $destinationPath);
  425. }
  426. return false;
  427. }
  428. }