local.php 11 KB

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