node.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Jakob Sack <mail@jakobsack.de>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Klaas Freitag <freitag@owncloud.com>
  8. * @author Markus Goetz <markus@woboq.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <icewind@owncloud.com>
  11. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @copyright Copyright (c) 2016, ownCloud, Inc.
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\Connector\Sabre;
  32. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  33. abstract class Node implements \Sabre\DAV\INode {
  34. /**
  35. * @var \OC\Files\View
  36. */
  37. protected $fileView;
  38. /**
  39. * The path to the current node
  40. *
  41. * @var string
  42. */
  43. protected $path;
  44. /**
  45. * node properties cache
  46. *
  47. * @var array
  48. */
  49. protected $property_cache = null;
  50. /**
  51. * @var \OCP\Files\FileInfo
  52. */
  53. protected $info;
  54. /**
  55. * Sets up the node, expects a full path name
  56. *
  57. * @param \OC\Files\View $view
  58. * @param \OCP\Files\FileInfo $info
  59. */
  60. public function __construct($view, $info) {
  61. $this->fileView = $view;
  62. $this->path = $this->fileView->getRelativePath($info->getPath());
  63. $this->info = $info;
  64. }
  65. protected function refreshInfo() {
  66. $this->info = $this->fileView->getFileInfo($this->path);
  67. }
  68. /**
  69. * Returns the name of the node
  70. *
  71. * @return string
  72. */
  73. public function getName() {
  74. return $this->info->getName();
  75. }
  76. /**
  77. * Returns the full path
  78. *
  79. * @return string
  80. */
  81. public function getPath() {
  82. return $this->path;
  83. }
  84. /**
  85. * Renames the node
  86. *
  87. * @param string $name The new name
  88. * @throws \Sabre\DAV\Exception\BadRequest
  89. * @throws \Sabre\DAV\Exception\Forbidden
  90. */
  91. public function setName($name) {
  92. // rename is only allowed if the update privilege is granted
  93. if (!$this->info->isUpdateable()) {
  94. throw new \Sabre\DAV\Exception\Forbidden();
  95. }
  96. list($parentPath,) = \Sabre\HTTP\URLUtil::splitPath($this->path);
  97. list(, $newName) = \Sabre\HTTP\URLUtil::splitPath($name);
  98. // verify path of the target
  99. $this->verifyPath();
  100. $newPath = $parentPath . '/' . $newName;
  101. $this->fileView->rename($this->path, $newPath);
  102. $this->path = $newPath;
  103. $this->refreshInfo();
  104. }
  105. public function setPropertyCache($property_cache) {
  106. $this->property_cache = $property_cache;
  107. }
  108. /**
  109. * Returns the last modification time, as a unix timestamp
  110. *
  111. * @return int timestamp as integer
  112. */
  113. public function getLastModified() {
  114. $timestamp = $this->info->getMtime();
  115. if (!empty($timestamp)) {
  116. return (int)$timestamp;
  117. }
  118. return $timestamp;
  119. }
  120. /**
  121. * sets the last modification time of the file (mtime) to the value given
  122. * in the second parameter or to now if the second param is empty.
  123. * Even if the modification time is set to a custom value the access time is set to now.
  124. */
  125. public function touch($mtime) {
  126. $this->fileView->touch($this->path, $mtime);
  127. $this->refreshInfo();
  128. }
  129. /**
  130. * Returns the ETag for a file
  131. *
  132. * An ETag is a unique identifier representing the current version of the
  133. * file. If the file changes, the ETag MUST change. The ETag is an
  134. * arbitrary string, but MUST be surrounded by double-quotes.
  135. *
  136. * Return null if the ETag can not effectively be determined
  137. *
  138. * @return string
  139. */
  140. public function getETag() {
  141. return '"' . $this->info->getEtag() . '"';
  142. }
  143. /**
  144. * Sets the ETag
  145. *
  146. * @param string $etag
  147. *
  148. * @return int file id of updated file or -1 on failure
  149. */
  150. public function setETag($etag) {
  151. return $this->fileView->putFileInfo($this->path, array('etag' => $etag));
  152. }
  153. /**
  154. * Returns the size of the node, in bytes
  155. *
  156. * @return integer
  157. */
  158. public function getSize() {
  159. return $this->info->getSize();
  160. }
  161. /**
  162. * Returns the cache's file id
  163. *
  164. * @return int
  165. */
  166. public function getId() {
  167. return $this->info->getId();
  168. }
  169. /**
  170. * @return string|null
  171. */
  172. public function getFileId() {
  173. if ($this->info->getId()) {
  174. $instanceId = \OC_Util::getInstanceId();
  175. $id = sprintf('%08d', $this->info->getId());
  176. return $id . $instanceId;
  177. }
  178. return null;
  179. }
  180. /**
  181. * @return integer
  182. */
  183. public function getInternalFileId() {
  184. return $this->info->getId();
  185. }
  186. /**
  187. * @return string
  188. */
  189. public function getDavPermissions() {
  190. $p = '';
  191. if ($this->info->isShared()) {
  192. $p .= 'S';
  193. }
  194. if ($this->info->isShareable()) {
  195. $p .= 'R';
  196. }
  197. if ($this->info->isMounted()) {
  198. $p .= 'M';
  199. }
  200. if ($this->info->isDeletable()) {
  201. $p .= 'D';
  202. }
  203. if ($this->info->isDeletable()) {
  204. $p .= 'NV'; // Renameable, Moveable
  205. }
  206. if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
  207. if ($this->info->isUpdateable()) {
  208. $p .= 'W';
  209. }
  210. } else {
  211. if ($this->info->isCreatable()) {
  212. $p .= 'CK';
  213. }
  214. }
  215. return $p;
  216. }
  217. public function getOwner() {
  218. return $this->info->getOwner();
  219. }
  220. protected function verifyPath() {
  221. try {
  222. $fileName = basename($this->info->getPath());
  223. $this->fileView->verifyPath($this->path, $fileName);
  224. } catch (\OCP\Files\InvalidPathException $ex) {
  225. throw new InvalidPath($ex->getMessage());
  226. }
  227. }
  228. /**
  229. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  230. */
  231. public function acquireLock($type) {
  232. $this->fileView->lockFile($this->path, $type);
  233. }
  234. /**
  235. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  236. */
  237. public function releaseLock($type) {
  238. $this->fileView->unlockFile($this->path, $type);
  239. }
  240. /**
  241. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  242. */
  243. public function changeLock($type) {
  244. $this->fileView->changeLock($this->path, $type);
  245. }
  246. }