Local.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Brice Maron <brice@bmaron.net>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Klaas Freitag <freitag@owncloud.com>
  10. * @author Martin Mattel <martin.mattel@diemattels.at>
  11. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Sjors van der Pluijm <sjors@desjors.nl>
  15. * @author Stefan Weil <sw@weilnetz.de>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  18. * @author Vincent Petry <pvince81@owncloud.com>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OC\Files\Storage;
  36. use OCP\Files\ForbiddenException;
  37. /**
  38. * for local filestore, we only have to map the paths
  39. */
  40. class Local extends \OC\Files\Storage\Common {
  41. protected $datadir;
  42. protected $dataDirLength;
  43. protected $allowSymlinks = false;
  44. protected $realDataDir;
  45. public function __construct($arguments) {
  46. if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
  47. throw new \InvalidArgumentException('No data directory set for local storage');
  48. }
  49. $this->datadir = $arguments['datadir'];
  50. // some crazy code uses a local storage on root...
  51. if ($this->datadir === '/') {
  52. $this->realDataDir = $this->datadir;
  53. } else {
  54. $this->realDataDir = rtrim(realpath($this->datadir), '/') . '/';
  55. }
  56. if (substr($this->datadir, -1) !== '/') {
  57. $this->datadir .= '/';
  58. }
  59. $this->dataDirLength = strlen($this->realDataDir);
  60. }
  61. public function __destruct() {
  62. }
  63. public function getId() {
  64. return 'local::' . $this->datadir;
  65. }
  66. public function mkdir($path) {
  67. return @mkdir($this->getSourcePath($path), 0777, true);
  68. }
  69. public function rmdir($path) {
  70. if (!$this->isDeletable($path)) {
  71. return false;
  72. }
  73. try {
  74. $it = new \RecursiveIteratorIterator(
  75. new \RecursiveDirectoryIterator($this->getSourcePath($path)),
  76. \RecursiveIteratorIterator::CHILD_FIRST
  77. );
  78. /**
  79. * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach
  80. * This bug is fixed in PHP 5.5.9 or before
  81. * See #8376
  82. */
  83. $it->rewind();
  84. while ($it->valid()) {
  85. /**
  86. * @var \SplFileInfo $file
  87. */
  88. $file = $it->current();
  89. if (in_array($file->getBasename(), array('.', '..'))) {
  90. $it->next();
  91. continue;
  92. } elseif ($file->isDir()) {
  93. rmdir($file->getPathname());
  94. } elseif ($file->isFile() || $file->isLink()) {
  95. unlink($file->getPathname());
  96. }
  97. $it->next();
  98. }
  99. return rmdir($this->getSourcePath($path));
  100. } catch (\UnexpectedValueException $e) {
  101. return false;
  102. }
  103. }
  104. public function opendir($path) {
  105. return opendir($this->getSourcePath($path));
  106. }
  107. public function is_dir($path) {
  108. if (substr($path, -1) == '/') {
  109. $path = substr($path, 0, -1);
  110. }
  111. return is_dir($this->getSourcePath($path));
  112. }
  113. public function is_file($path) {
  114. return is_file($this->getSourcePath($path));
  115. }
  116. public function stat($path) {
  117. clearstatcache();
  118. $fullPath = $this->getSourcePath($path);
  119. $statResult = stat($fullPath);
  120. if (PHP_INT_SIZE === 4 && !$this->is_dir($path)) {
  121. $filesize = $this->filesize($path);
  122. $statResult['size'] = $filesize;
  123. $statResult[7] = $filesize;
  124. }
  125. return $statResult;
  126. }
  127. public function filetype($path) {
  128. $filetype = filetype($this->getSourcePath($path));
  129. if ($filetype == 'link') {
  130. $filetype = filetype(realpath($this->getSourcePath($path)));
  131. }
  132. return $filetype;
  133. }
  134. public function filesize($path) {
  135. if ($this->is_dir($path)) {
  136. return 0;
  137. }
  138. $fullPath = $this->getSourcePath($path);
  139. if (PHP_INT_SIZE === 4) {
  140. $helper = new \OC\LargeFileHelper;
  141. return $helper->getFileSize($fullPath);
  142. }
  143. return filesize($fullPath);
  144. }
  145. public function isReadable($path) {
  146. return is_readable($this->getSourcePath($path));
  147. }
  148. public function isUpdatable($path) {
  149. return is_writable($this->getSourcePath($path));
  150. }
  151. public function file_exists($path) {
  152. return file_exists($this->getSourcePath($path));
  153. }
  154. public function filemtime($path) {
  155. $fullPath = $this->getSourcePath($path);
  156. clearstatcache($fullPath);
  157. if (!$this->file_exists($path)) {
  158. return false;
  159. }
  160. if (PHP_INT_SIZE === 4) {
  161. $helper = new \OC\LargeFileHelper();
  162. return $helper->getFileMtime($fullPath);
  163. }
  164. return filemtime($fullPath);
  165. }
  166. public function touch($path, $mtime = null) {
  167. // sets the modification time of the file to the given value.
  168. // If mtime is nil the current time is set.
  169. // note that the access time of the file always changes to the current time.
  170. if ($this->file_exists($path) and !$this->isUpdatable($path)) {
  171. return false;
  172. }
  173. if (!is_null($mtime)) {
  174. $result = touch($this->getSourcePath($path), $mtime);
  175. } else {
  176. $result = touch($this->getSourcePath($path));
  177. }
  178. if ($result) {
  179. clearstatcache(true, $this->getSourcePath($path));
  180. }
  181. return $result;
  182. }
  183. public function file_get_contents($path) {
  184. return file_get_contents($this->getSourcePath($path));
  185. }
  186. public function file_put_contents($path, $data) {
  187. return file_put_contents($this->getSourcePath($path), $data);
  188. }
  189. public function unlink($path) {
  190. if ($this->is_dir($path)) {
  191. return $this->rmdir($path);
  192. } else if ($this->is_file($path)) {
  193. return unlink($this->getSourcePath($path));
  194. } else {
  195. return false;
  196. }
  197. }
  198. public function rename($path1, $path2) {
  199. $srcParent = dirname($path1);
  200. $dstParent = dirname($path2);
  201. if (!$this->isUpdatable($srcParent)) {
  202. \OCP\Util::writeLog('core', 'unable to rename, source directory is not writable : ' . $srcParent, \OCP\Util::ERROR);
  203. return false;
  204. }
  205. if (!$this->isUpdatable($dstParent)) {
  206. \OCP\Util::writeLog('core', 'unable to rename, destination directory is not writable : ' . $dstParent, \OCP\Util::ERROR);
  207. return false;
  208. }
  209. if (!$this->file_exists($path1)) {
  210. \OCP\Util::writeLog('core', 'unable to rename, file does not exists : ' . $path1, \OCP\Util::ERROR);
  211. return false;
  212. }
  213. if ($this->is_dir($path2)) {
  214. $this->rmdir($path2);
  215. } else if ($this->is_file($path2)) {
  216. $this->unlink($path2);
  217. }
  218. if ($this->is_dir($path1)) {
  219. // we can't move folders across devices, use copy instead
  220. $stat1 = stat(dirname($this->getSourcePath($path1)));
  221. $stat2 = stat(dirname($this->getSourcePath($path2)));
  222. if ($stat1['dev'] !== $stat2['dev']) {
  223. $result = $this->copy($path1, $path2);
  224. if ($result) {
  225. $result &= $this->rmdir($path1);
  226. }
  227. return $result;
  228. }
  229. }
  230. return rename($this->getSourcePath($path1), $this->getSourcePath($path2));
  231. }
  232. public function copy($path1, $path2) {
  233. if ($this->is_dir($path1)) {
  234. return parent::copy($path1, $path2);
  235. } else {
  236. return copy($this->getSourcePath($path1), $this->getSourcePath($path2));
  237. }
  238. }
  239. public function fopen($path, $mode) {
  240. return fopen($this->getSourcePath($path), $mode);
  241. }
  242. public function hash($type, $path, $raw = false) {
  243. return hash_file($type, $this->getSourcePath($path), $raw);
  244. }
  245. public function free_space($path) {
  246. $sourcePath = $this->getSourcePath($path);
  247. // using !is_dir because $sourcePath might be a part file or
  248. // non-existing file, so we'd still want to use the parent dir
  249. // in such cases
  250. if (!is_dir($sourcePath)) {
  251. // disk_free_space doesn't work on files
  252. $sourcePath = dirname($sourcePath);
  253. }
  254. $space = @disk_free_space($sourcePath);
  255. if ($space === false || is_null($space)) {
  256. return \OCP\Files\FileInfo::SPACE_UNKNOWN;
  257. }
  258. return $space;
  259. }
  260. public function search($query) {
  261. return $this->searchInDir($query);
  262. }
  263. public function getLocalFile($path) {
  264. return $this->getSourcePath($path);
  265. }
  266. public function getLocalFolder($path) {
  267. return $this->getSourcePath($path);
  268. }
  269. /**
  270. * @param string $query
  271. * @param string $dir
  272. * @return array
  273. */
  274. protected function searchInDir($query, $dir = '') {
  275. $files = array();
  276. $physicalDir = $this->getSourcePath($dir);
  277. foreach (scandir($physicalDir) as $item) {
  278. if (\OC\Files\Filesystem::isIgnoredDir($item))
  279. continue;
  280. $physicalItem = $physicalDir . '/' . $item;
  281. if (strstr(strtolower($item), strtolower($query)) !== false) {
  282. $files[] = $dir . '/' . $item;
  283. }
  284. if (is_dir($physicalItem)) {
  285. $files = array_merge($files, $this->searchInDir($query, $dir . '/' . $item));
  286. }
  287. }
  288. return $files;
  289. }
  290. /**
  291. * check if a file or folder has been updated since $time
  292. *
  293. * @param string $path
  294. * @param int $time
  295. * @return bool
  296. */
  297. public function hasUpdated($path, $time) {
  298. if ($this->file_exists($path)) {
  299. return $this->filemtime($path) > $time;
  300. } else {
  301. return true;
  302. }
  303. }
  304. /**
  305. * Get the source path (on disk) of a given path
  306. *
  307. * @param string $path
  308. * @return string
  309. * @throws ForbiddenException
  310. */
  311. public function getSourcePath($path) {
  312. $fullPath = $this->datadir . $path;
  313. if ($this->allowSymlinks || $path === '') {
  314. return $fullPath;
  315. }
  316. $pathToResolve = $fullPath;
  317. $realPath = realpath($pathToResolve);
  318. while ($realPath === false) { // for non existing files check the parent directory
  319. $pathToResolve = dirname($pathToResolve);
  320. $realPath = realpath($pathToResolve);
  321. }
  322. if ($realPath) {
  323. $realPath = $realPath . '/';
  324. }
  325. if (substr($realPath, 0, $this->dataDirLength) === $this->realDataDir) {
  326. return $fullPath;
  327. }
  328. \OCP\Util::writeLog('core', "Following symlinks is not allowed ('$fullPath' -> '$realPath' not inside '{$this->realDataDir}')", \OCP\Util::ERROR);
  329. throw new ForbiddenException('Following symlinks is not allowed', false);
  330. }
  331. /**
  332. * {@inheritdoc}
  333. */
  334. public function isLocal() {
  335. return true;
  336. }
  337. /**
  338. * get the ETag for a file or folder
  339. *
  340. * @param string $path
  341. * @return string
  342. */
  343. public function getETag($path) {
  344. if ($this->is_file($path)) {
  345. $stat = $this->stat($path);
  346. return md5(
  347. $stat['mtime'] .
  348. $stat['ino'] .
  349. $stat['dev'] .
  350. $stat['size']
  351. );
  352. } else {
  353. return parent::getETag($path);
  354. }
  355. }
  356. /**
  357. * @param \OCP\Files\Storage $sourceStorage
  358. * @param string $sourceInternalPath
  359. * @param string $targetInternalPath
  360. * @return bool
  361. */
  362. public function copyFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  363. if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')) {
  364. /**
  365. * @var \OC\Files\Storage\Local $sourceStorage
  366. */
  367. $rootStorage = new Local(['datadir' => '/']);
  368. return $rootStorage->copy($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  369. } else {
  370. return parent::copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  371. }
  372. }
  373. /**
  374. * @param \OCP\Files\Storage $sourceStorage
  375. * @param string $sourceInternalPath
  376. * @param string $targetInternalPath
  377. * @return bool
  378. */
  379. public function moveFromStorage(\OCP\Files\Storage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
  380. if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Local')) {
  381. /**
  382. * @var \OC\Files\Storage\Local $sourceStorage
  383. */
  384. $rootStorage = new Local(['datadir' => '/']);
  385. return $rootStorage->rename($sourceStorage->getSourcePath($sourceInternalPath), $this->getSourcePath($targetInternalPath));
  386. } else {
  387. return parent::moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
  388. }
  389. }
  390. }