smb.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Philipp Kapfer <philipp.kapfer@gmx.at>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Robin McCorkell <robin@mccorkell.me.uk>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2016, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Storage;
  30. use Icewind\SMB\Exception\Exception;
  31. use Icewind\SMB\Exception\ForbiddenException;
  32. use Icewind\SMB\Exception\NotFoundException;
  33. use Icewind\SMB\NativeServer;
  34. use Icewind\SMB\Server;
  35. use Icewind\Streams\CallbackWrapper;
  36. use Icewind\Streams\IteratorDirectory;
  37. use OC\Cache\CappedMemoryCache;
  38. use OC\Files\Filesystem;
  39. class SMB extends Common {
  40. /**
  41. * @var \Icewind\SMB\Server
  42. */
  43. protected $server;
  44. /**
  45. * @var \Icewind\SMB\Share
  46. */
  47. protected $share;
  48. /**
  49. * @var string
  50. */
  51. protected $root;
  52. /**
  53. * @var \Icewind\SMB\FileInfo[]
  54. */
  55. protected $statCache;
  56. public function __construct($params) {
  57. if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) {
  58. if (Server::NativeAvailable()) {
  59. $this->server = new NativeServer($params['host'], $params['user'], $params['password']);
  60. } else {
  61. $this->server = new Server($params['host'], $params['user'], $params['password']);
  62. }
  63. $this->share = $this->server->getShare(trim($params['share'], '/'));
  64. $this->root = isset($params['root']) ? $params['root'] : '/';
  65. if (!$this->root || $this->root[0] != '/') {
  66. $this->root = '/' . $this->root;
  67. }
  68. if (substr($this->root, -1, 1) != '/') {
  69. $this->root .= '/';
  70. }
  71. } else {
  72. throw new \Exception('Invalid configuration');
  73. }
  74. $this->statCache = new CappedMemoryCache();
  75. }
  76. /**
  77. * @return string
  78. */
  79. public function getId() {
  80. // FIXME: double slash to keep compatible with the old storage ids,
  81. // failure to do so will lead to creation of a new storage id and
  82. // loss of shares from the storage
  83. return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root;
  84. }
  85. /**
  86. * @param string $path
  87. * @return string
  88. */
  89. protected function buildPath($path) {
  90. return Filesystem::normalizePath($this->root . '/' . $path);
  91. }
  92. /**
  93. * @param string $path
  94. * @return \Icewind\SMB\IFileInfo
  95. */
  96. protected function getFileInfo($path) {
  97. $path = $this->buildPath($path);
  98. if (!isset($this->statCache[$path])) {
  99. $this->statCache[$path] = $this->share->stat($path);
  100. }
  101. return $this->statCache[$path];
  102. }
  103. /**
  104. * @param string $path
  105. * @return \Icewind\SMB\IFileInfo[]
  106. */
  107. protected function getFolderContents($path) {
  108. $path = $this->buildPath($path);
  109. $files = $this->share->dir($path);
  110. foreach ($files as $file) {
  111. $this->statCache[$path . '/' . $file->getName()] = $file;
  112. }
  113. return $files;
  114. }
  115. /**
  116. * @param \Icewind\SMB\IFileInfo $info
  117. * @return array
  118. */
  119. protected function formatInfo($info) {
  120. return array(
  121. 'size' => $info->getSize(),
  122. 'mtime' => $info->getMTime()
  123. );
  124. }
  125. /**
  126. * @param string $path
  127. * @return array
  128. */
  129. public function stat($path) {
  130. return $this->formatInfo($this->getFileInfo($path));
  131. }
  132. /**
  133. * @param string $path
  134. * @return bool
  135. */
  136. public function unlink($path) {
  137. try {
  138. if ($this->is_dir($path)) {
  139. return $this->rmdir($path);
  140. } else {
  141. $path = $this->buildPath($path);
  142. unset($this->statCache[$path]);
  143. $this->share->del($path);
  144. return true;
  145. }
  146. } catch (NotFoundException $e) {
  147. return false;
  148. } catch (ForbiddenException $e) {
  149. return false;
  150. }
  151. }
  152. /**
  153. * check if a file or folder has been updated since $time
  154. *
  155. * @param string $path
  156. * @param int $time
  157. * @return bool
  158. */
  159. public function hasUpdated($path, $time) {
  160. if (!$path and $this->root == '/') {
  161. // mtime doesn't work for shares, but giving the nature of the backend,
  162. // doing a full update is still just fast enough
  163. return true;
  164. } else {
  165. $actualTime = $this->filemtime($path);
  166. return $actualTime > $time;
  167. }
  168. }
  169. /**
  170. * @param string $path
  171. * @param string $mode
  172. * @return resource
  173. */
  174. public function fopen($path, $mode) {
  175. $fullPath = $this->buildPath($path);
  176. try {
  177. switch ($mode) {
  178. case 'r':
  179. case 'rb':
  180. if (!$this->file_exists($path)) {
  181. return false;
  182. }
  183. return $this->share->read($fullPath);
  184. case 'w':
  185. case 'wb':
  186. $source = $this->share->write($fullPath);
  187. return CallBackWrapper::wrap($source, null, null, function () use ($fullPath) {
  188. unset($this->statCache[$fullPath]);
  189. });
  190. case 'a':
  191. case 'ab':
  192. case 'r+':
  193. case 'w+':
  194. case 'wb+':
  195. case 'a+':
  196. case 'x':
  197. case 'x+':
  198. case 'c':
  199. case 'c+':
  200. //emulate these
  201. if (strrpos($path, '.') !== false) {
  202. $ext = substr($path, strrpos($path, '.'));
  203. } else {
  204. $ext = '';
  205. }
  206. if ($this->file_exists($path)) {
  207. if (!$this->isUpdatable($path)) {
  208. return false;
  209. }
  210. $tmpFile = $this->getCachedFile($path);
  211. } else {
  212. if (!$this->isCreatable(dirname($path))) {
  213. return false;
  214. }
  215. $tmpFile = \OCP\Files::tmpFile($ext);
  216. }
  217. $source = fopen($tmpFile, $mode);
  218. $share = $this->share;
  219. return CallbackWrapper::wrap($source, null, null, function () use ($tmpFile, $fullPath, $share) {
  220. unset($this->statCache[$fullPath]);
  221. $share->put($tmpFile, $fullPath);
  222. unlink($tmpFile);
  223. });
  224. }
  225. return false;
  226. } catch (NotFoundException $e) {
  227. return false;
  228. } catch (ForbiddenException $e) {
  229. return false;
  230. }
  231. }
  232. public function rmdir($path) {
  233. try {
  234. $this->statCache = array();
  235. $content = $this->share->dir($this->buildPath($path));
  236. foreach ($content as $file) {
  237. if ($file->isDirectory()) {
  238. $this->rmdir($path . '/' . $file->getName());
  239. } else {
  240. $this->share->del($file->getPath());
  241. }
  242. }
  243. $this->share->rmdir($this->buildPath($path));
  244. return true;
  245. } catch (NotFoundException $e) {
  246. return false;
  247. } catch (ForbiddenException $e) {
  248. return false;
  249. }
  250. }
  251. public function touch($path, $time = null) {
  252. if (!$this->file_exists($path)) {
  253. $fh = $this->share->write($this->buildPath($path));
  254. fclose($fh);
  255. return true;
  256. }
  257. return false;
  258. }
  259. public function opendir($path) {
  260. try {
  261. $files = $this->getFolderContents($path);
  262. } catch (NotFoundException $e) {
  263. return false;
  264. } catch (ForbiddenException $e) {
  265. return false;
  266. }
  267. $names = array_map(function ($info) {
  268. /** @var \Icewind\SMB\IFileInfo $info */
  269. return $info->getName();
  270. }, $files);
  271. return IteratorDirectory::wrap($names);
  272. }
  273. public function filetype($path) {
  274. try {
  275. return $this->getFileInfo($path)->isDirectory() ? 'dir' : 'file';
  276. } catch (NotFoundException $e) {
  277. return false;
  278. } catch (ForbiddenException $e) {
  279. return false;
  280. }
  281. }
  282. public function mkdir($path) {
  283. $path = $this->buildPath($path);
  284. try {
  285. $this->share->mkdir($path);
  286. return true;
  287. } catch (Exception $e) {
  288. return false;
  289. }
  290. }
  291. public function file_exists($path) {
  292. try {
  293. $this->getFileInfo($path);
  294. return true;
  295. } catch (NotFoundException $e) {
  296. return false;
  297. } catch (ForbiddenException $e) {
  298. return false;
  299. }
  300. }
  301. /**
  302. * check if smbclient is installed
  303. */
  304. public static function checkDependencies() {
  305. return (
  306. (bool)\OC_Helper::findBinaryPath('smbclient')
  307. || Server::NativeAvailable()
  308. ) ? true : ['smbclient'];
  309. }
  310. /**
  311. * Test a storage for availability
  312. *
  313. * @return bool
  314. */
  315. public function test() {
  316. try {
  317. return parent::test();
  318. } catch (Exception $e) {
  319. return false;
  320. }
  321. }
  322. }