TAR.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christian Weiske <cweiske@cweiske.de>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Felix Moeller <mail@felixmoeller.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Remco Brenninkmeijer <requist1@starmail.nl>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Robin McCorkell <robin@mccorkell.me.uk>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. namespace OC\Archive;
  34. use Icewind\Streams\CallbackWrapper;
  35. class TAR extends Archive {
  36. const PLAIN = 0;
  37. const GZIP = 1;
  38. const BZIP = 2;
  39. private $fileList;
  40. private $cachedHeaders;
  41. /**
  42. * @var \Archive_Tar tar
  43. */
  44. private $tar = null;
  45. private $path;
  46. /**
  47. * @param string $source
  48. */
  49. function __construct($source) {
  50. $types = array(null, 'gz', 'bz2');
  51. $this->path = $source;
  52. $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]);
  53. }
  54. /**
  55. * try to detect the type of tar compression
  56. *
  57. * @param string $file
  58. * @return integer
  59. */
  60. static public function getTarType($file) {
  61. if (strpos($file, '.')) {
  62. $extension = substr($file, strrpos($file, '.'));
  63. switch ($extension) {
  64. case '.gz':
  65. case '.tgz':
  66. return self::GZIP;
  67. case '.bz':
  68. case '.bz2':
  69. return self::BZIP;
  70. case '.tar':
  71. return self::PLAIN;
  72. default:
  73. return self::PLAIN;
  74. }
  75. } else {
  76. return self::PLAIN;
  77. }
  78. }
  79. /**
  80. * add an empty folder to the archive
  81. *
  82. * @param string $path
  83. * @return bool
  84. */
  85. function addFolder($path) {
  86. $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder();
  87. if (substr($path, -1, 1) != '/') {
  88. $path .= '/';
  89. }
  90. if ($this->fileExists($path)) {
  91. return false;
  92. }
  93. $parts = explode('/', $path);
  94. $folder = $tmpBase;
  95. foreach ($parts as $part) {
  96. $folder .= '/' . $part;
  97. if (!is_dir($folder)) {
  98. mkdir($folder);
  99. }
  100. }
  101. $result = $this->tar->addModify(array($tmpBase . $path), '', $tmpBase);
  102. rmdir($tmpBase . $path);
  103. $this->fileList = false;
  104. $this->cachedHeaders = false;
  105. return $result;
  106. }
  107. /**
  108. * add a file to the archive
  109. *
  110. * @param string $path
  111. * @param string $source either a local file or string data
  112. * @return bool
  113. */
  114. function addFile($path, $source = '') {
  115. if ($this->fileExists($path)) {
  116. $this->remove($path);
  117. }
  118. if ($source and $source[0] == '/' and file_exists($source)) {
  119. $source = file_get_contents($source);
  120. }
  121. $result = $this->tar->addString($path, $source);
  122. $this->fileList = false;
  123. $this->cachedHeaders = false;
  124. return $result;
  125. }
  126. /**
  127. * rename a file or folder in the archive
  128. *
  129. * @param string $source
  130. * @param string $dest
  131. * @return bool
  132. */
  133. function rename($source, $dest) {
  134. //no proper way to delete, rename entire archive, rename file and remake archive
  135. $tmp = \OCP\Files::tmpFolder();
  136. $this->tar->extract($tmp);
  137. rename($tmp . $source, $tmp . $dest);
  138. $this->tar = null;
  139. unlink($this->path);
  140. $types = array(null, 'gz', 'bz');
  141. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  142. $this->tar->createModify(array($tmp), '', $tmp . '/');
  143. $this->fileList = false;
  144. $this->cachedHeaders = false;
  145. return true;
  146. }
  147. /**
  148. * @param string $file
  149. */
  150. private function getHeader($file) {
  151. if (!$this->cachedHeaders) {
  152. $this->cachedHeaders = $this->tar->listContent();
  153. }
  154. foreach ($this->cachedHeaders as $header) {
  155. if ($file == $header['filename']
  156. or $file . '/' == $header['filename']
  157. or '/' . $file . '/' == $header['filename']
  158. or '/' . $file == $header['filename']
  159. ) {
  160. return $header;
  161. }
  162. }
  163. return null;
  164. }
  165. /**
  166. * get the uncompressed size of a file in the archive
  167. *
  168. * @param string $path
  169. * @return int
  170. */
  171. function filesize($path) {
  172. $stat = $this->getHeader($path);
  173. return $stat['size'];
  174. }
  175. /**
  176. * get the last modified time of a file in the archive
  177. *
  178. * @param string $path
  179. * @return int
  180. */
  181. function mtime($path) {
  182. $stat = $this->getHeader($path);
  183. return $stat['mtime'];
  184. }
  185. /**
  186. * get the files in a folder
  187. *
  188. * @param string $path
  189. * @return array
  190. */
  191. function getFolder($path) {
  192. $files = $this->getFiles();
  193. $folderContent = array();
  194. $pathLength = strlen($path);
  195. foreach ($files as $file) {
  196. if ($file[0] == '/') {
  197. $file = substr($file, 1);
  198. }
  199. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  200. $result = substr($file, $pathLength);
  201. if ($pos = strpos($result, '/')) {
  202. $result = substr($result, 0, $pos + 1);
  203. }
  204. if (array_search($result, $folderContent) === false) {
  205. $folderContent[] = $result;
  206. }
  207. }
  208. }
  209. return $folderContent;
  210. }
  211. /**
  212. * get all files in the archive
  213. *
  214. * @return array
  215. */
  216. function getFiles() {
  217. if ($this->fileList) {
  218. return $this->fileList;
  219. }
  220. if (!$this->cachedHeaders) {
  221. $this->cachedHeaders = $this->tar->listContent();
  222. }
  223. $files = array();
  224. foreach ($this->cachedHeaders as $header) {
  225. $files[] = $header['filename'];
  226. }
  227. $this->fileList = $files;
  228. return $files;
  229. }
  230. /**
  231. * get the content of a file
  232. *
  233. * @param string $path
  234. * @return string
  235. */
  236. function getFile($path) {
  237. return $this->tar->extractInString($path);
  238. }
  239. /**
  240. * extract a single file from the archive
  241. *
  242. * @param string $path
  243. * @param string $dest
  244. * @return bool
  245. */
  246. function extractFile($path, $dest) {
  247. $tmp = \OCP\Files::tmpFolder();
  248. if (!$this->fileExists($path)) {
  249. return false;
  250. }
  251. if ($this->fileExists('/' . $path)) {
  252. $success = $this->tar->extractList(array('/' . $path), $tmp);
  253. } else {
  254. $success = $this->tar->extractList(array($path), $tmp);
  255. }
  256. if ($success) {
  257. rename($tmp . $path, $dest);
  258. }
  259. \OCP\Files::rmdirr($tmp);
  260. return $success;
  261. }
  262. /**
  263. * extract the archive
  264. *
  265. * @param string $dest
  266. * @return bool
  267. */
  268. function extract($dest) {
  269. return $this->tar->extract($dest);
  270. }
  271. /**
  272. * check if a file or folder exists in the archive
  273. *
  274. * @param string $path
  275. * @return bool
  276. */
  277. function fileExists($path) {
  278. $files = $this->getFiles();
  279. if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) {
  280. return true;
  281. } else {
  282. $folderPath = $path;
  283. if (substr($folderPath, -1, 1) != '/') {
  284. $folderPath .= '/';
  285. }
  286. $pathLength = strlen($folderPath);
  287. foreach ($files as $file) {
  288. if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
  289. return true;
  290. }
  291. }
  292. }
  293. if ($path[0] != '/') { //not all programs agree on the use of a leading /
  294. return $this->fileExists('/' . $path);
  295. } else {
  296. return false;
  297. }
  298. }
  299. /**
  300. * remove a file or folder from the archive
  301. *
  302. * @param string $path
  303. * @return bool
  304. */
  305. function remove($path) {
  306. if (!$this->fileExists($path)) {
  307. return false;
  308. }
  309. $this->fileList = false;
  310. $this->cachedHeaders = false;
  311. //no proper way to delete, extract entire archive, delete file and remake archive
  312. $tmp = \OCP\Files::tmpFolder();
  313. $this->tar->extract($tmp);
  314. \OCP\Files::rmdirr($tmp . $path);
  315. $this->tar = null;
  316. unlink($this->path);
  317. $this->reopen();
  318. $this->tar->createModify(array($tmp), '', $tmp);
  319. return true;
  320. }
  321. /**
  322. * get a file handler
  323. *
  324. * @param string $path
  325. * @param string $mode
  326. * @return resource
  327. */
  328. function getStream($path, $mode) {
  329. if (strrpos($path, '.') !== false) {
  330. $ext = substr($path, strrpos($path, '.'));
  331. } else {
  332. $ext = '';
  333. }
  334. $tmpFile = \OCP\Files::tmpFile($ext);
  335. if ($this->fileExists($path)) {
  336. $this->extractFile($path, $tmpFile);
  337. } elseif ($mode == 'r' or $mode == 'rb') {
  338. return false;
  339. }
  340. if ($mode == 'r' or $mode == 'rb') {
  341. return fopen($tmpFile, $mode);
  342. } else {
  343. $handle = fopen($tmpFile, $mode);
  344. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  345. $this->writeBack($tmpFile, $path);
  346. });
  347. }
  348. }
  349. /**
  350. * write back temporary files
  351. */
  352. function writeBack($tmpFile, $path) {
  353. $this->addFile($path, $tmpFile);
  354. unlink($tmpFile);
  355. }
  356. /**
  357. * reopen the archive to ensure everything is written
  358. */
  359. private function reopen() {
  360. if ($this->tar) {
  361. $this->tar->_close();
  362. $this->tar = null;
  363. }
  364. $types = array(null, 'gz', 'bz');
  365. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  366. }
  367. }