Directory.php 14 KB

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