1
0

Local.php 13 KB

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