scanner.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Cache;
  9. use OC\Files\Filesystem;
  10. use OC\Hooks\BasicEmitter;
  11. /**
  12. * Class Scanner
  13. *
  14. * Hooks available in scope \OC\Files\Cache\Scanner:
  15. * - scanFile(string $path, string $storageId)
  16. * - scanFolder(string $path, string $storageId)
  17. *
  18. * @package OC\Files\Cache
  19. */
  20. class Scanner extends BasicEmitter {
  21. /**
  22. * @var \OC\Files\Storage\Storage $storage
  23. */
  24. private $storage;
  25. /**
  26. * @var string $storageId
  27. */
  28. private $storageId;
  29. /**
  30. * @var \OC\Files\Cache\Cache $cache
  31. */
  32. private $cache;
  33. const SCAN_RECURSIVE = true;
  34. const SCAN_SHALLOW = false;
  35. const REUSE_ETAG = 1;
  36. const REUSE_SIZE = 2;
  37. public function __construct(\OC\Files\Storage\Storage $storage) {
  38. $this->storage = $storage;
  39. $this->storageId = $this->storage->getId();
  40. $this->cache = $storage->getCache();
  41. }
  42. /**
  43. * get all the metadata of a file or folder
  44. * *
  45. *
  46. * @param string $path
  47. * @return array with metadata of the file
  48. */
  49. public function getData($path) {
  50. $data = array();
  51. if (!$this->storage->isReadable($path)) return null; //cant read, nothing we can do
  52. $data['mimetype'] = $this->storage->getMimeType($path);
  53. $data['mtime'] = $this->storage->filemtime($path);
  54. if ($data['mimetype'] == 'httpd/unix-directory') {
  55. $data['size'] = -1; //unknown
  56. } else {
  57. $data['size'] = $this->storage->filesize($path);
  58. }
  59. $data['etag'] = $this->storage->getETag($path);
  60. $data['storage_mtime'] = $data['mtime'];
  61. return $data;
  62. }
  63. /**
  64. * scan a single file and store it in the cache
  65. *
  66. * @param string $file
  67. * @param int $reuseExisting
  68. * @param bool $parentExistsInCache
  69. * @return array with metadata of the scanned file
  70. */
  71. public function scanFile($file, $reuseExisting = 0, $parentExistsInCache = false) {
  72. if (!self::isPartialFile($file)
  73. and !Filesystem::isFileBlacklisted($file)
  74. ) {
  75. $this->emit('\OC\Files\Cache\Scanner', 'scanFile', array($file, $this->storageId));
  76. \OC_Hook::emit('\OC\Files\Cache\Scanner', 'scan_file', array('path' => $file, 'storage' => $this->storageId));
  77. $data = $this->getData($file);
  78. if ($data) {
  79. if ($file and !$parentExistsInCache) {
  80. $parent = dirname($file);
  81. if ($parent === '.' or $parent === '/') {
  82. $parent = '';
  83. }
  84. if (!$this->cache->inCache($parent)) {
  85. $this->scanFile($parent);
  86. }
  87. }
  88. $newData = $data;
  89. if ($reuseExisting and $cacheData = $this->cache->get($file)) {
  90. // only reuse data if the file hasn't explicitly changed
  91. if (isset($data['mtime']) && isset($cacheData['mtime']) && $data['mtime'] === $cacheData['mtime']) {
  92. if (($reuseExisting & self::REUSE_SIZE) && ($data['size'] === -1)) {
  93. $data['size'] = $cacheData['size'];
  94. }
  95. if ($reuseExisting & self::REUSE_ETAG) {
  96. $data['etag'] = $cacheData['etag'];
  97. }
  98. }
  99. // Only update metadata that has changed
  100. $newData = array_diff($data, $cacheData);
  101. }
  102. if (!empty($newData)) {
  103. $this->cache->put($file, $newData);
  104. }
  105. }
  106. return $data;
  107. }
  108. return null;
  109. }
  110. /**
  111. * scan a folder and all it's children
  112. *
  113. * @param string $path
  114. * @param bool $recursive
  115. * @param int $reuse
  116. * @return int the size of the scanned folder or -1 if the size is unknown at this stage
  117. */
  118. public function scan($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1) {
  119. if ($reuse === -1) {
  120. $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : 0;
  121. }
  122. $this->scanFile($path, $reuse);
  123. return $this->scanChildren($path, $recursive, $reuse);
  124. }
  125. /**
  126. * scan all the files and folders in a folder
  127. *
  128. * @param string $path
  129. * @param bool $recursive
  130. * @param int $reuse
  131. * @return int the size of the scanned folder or -1 if the size is unknown at this stage
  132. */
  133. public function scanChildren($path, $recursive = self::SCAN_RECURSIVE, $reuse = -1) {
  134. if ($reuse === -1) {
  135. $reuse = ($recursive === self::SCAN_SHALLOW) ? self::REUSE_ETAG | self::REUSE_SIZE : 0;
  136. }
  137. $this->emit('\OC\Files\Cache\Scanner', 'scanFolder', array($path, $this->storageId));
  138. $size = 0;
  139. $childQueue = array();
  140. $existingChildren = array();
  141. if ($this->cache->inCache($path)) {
  142. $children = $this->cache->getFolderContents($path);
  143. foreach ($children as $child) {
  144. $existingChildren[] = $child['name'];
  145. }
  146. }
  147. $newChildren = array();
  148. if ($this->storage->is_dir($path) && ($dh = $this->storage->opendir($path))) {
  149. \OC_DB::beginTransaction();
  150. while ($file = readdir($dh)) {
  151. $child = ($path) ? $path . '/' . $file : $file;
  152. if (!Filesystem::isIgnoredDir($file)) {
  153. $newChildren[] = $file;
  154. $data = $this->scanFile($child, $reuse, true);
  155. if ($data) {
  156. if ($data['size'] === -1) {
  157. if ($recursive === self::SCAN_RECURSIVE) {
  158. $childQueue[] = $child;
  159. } else {
  160. $size = -1;
  161. }
  162. } else if ($size !== -1) {
  163. $size += $data['size'];
  164. }
  165. }
  166. }
  167. }
  168. $removedChildren = \array_diff($existingChildren, $newChildren);
  169. foreach ($removedChildren as $childName) {
  170. $child = ($path) ? $path . '/' . $childName : $childName;
  171. $this->cache->remove($child);
  172. }
  173. \OC_DB::commit();
  174. foreach ($childQueue as $child) {
  175. $childSize = $this->scanChildren($child, self::SCAN_RECURSIVE, $reuse);
  176. if ($childSize === -1) {
  177. $size = -1;
  178. } else {
  179. $size += $childSize;
  180. }
  181. }
  182. $this->cache->put($path, array('size' => $size));
  183. }
  184. return $size;
  185. }
  186. /**
  187. * @brief check if the file should be ignored when scanning
  188. * NOTE: files with a '.part' extension are ignored as well!
  189. * prevents unfinished put requests to be scanned
  190. * @param String $file
  191. * @return boolean
  192. */
  193. public static function isPartialFile($file) {
  194. if (pathinfo($file, PATHINFO_EXTENSION) === 'part') {
  195. return true;
  196. }
  197. return false;
  198. }
  199. /**
  200. * walk over any folders that are not fully scanned yet and scan them
  201. */
  202. public function backgroundScan() {
  203. $lastPath = null;
  204. while (($path = $this->cache->getIncomplete()) !== false && $path !== $lastPath) {
  205. $this->scan($path);
  206. $this->cache->correctFolderSize($path);
  207. $lastPath = $path;
  208. }
  209. }
  210. }