TAR.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. class TAR extends Archive {
  35. const PLAIN = 0;
  36. const GZIP = 1;
  37. const BZIP = 2;
  38. private $fileList;
  39. private $cachedHeaders;
  40. /**
  41. * @var \Archive_Tar tar
  42. */
  43. private $tar = null;
  44. private $path;
  45. /**
  46. * @param string $source
  47. */
  48. function __construct($source) {
  49. $types = array(null, 'gz', 'bz2');
  50. $this->path = $source;
  51. $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]);
  52. }
  53. /**
  54. * try to detect the type of tar compression
  55. *
  56. * @param string $file
  57. * @return integer
  58. */
  59. static public function getTarType($file) {
  60. if (strpos($file, '.')) {
  61. $extension = substr($file, strrpos($file, '.'));
  62. switch ($extension) {
  63. case '.gz':
  64. case '.tgz':
  65. return self::GZIP;
  66. case '.bz':
  67. case '.bz2':
  68. return self::BZIP;
  69. case '.tar':
  70. return self::PLAIN;
  71. default:
  72. return self::PLAIN;
  73. }
  74. } else {
  75. return self::PLAIN;
  76. }
  77. }
  78. /**
  79. * add an empty folder to the archive
  80. *
  81. * @param string $path
  82. * @return bool
  83. */
  84. function addFolder($path) {
  85. $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder();
  86. if (substr($path, -1, 1) != '/') {
  87. $path .= '/';
  88. }
  89. if ($this->fileExists($path)) {
  90. return false;
  91. }
  92. $parts = explode('/', $path);
  93. $folder = $tmpBase;
  94. foreach ($parts as $part) {
  95. $folder .= '/' . $part;
  96. if (!is_dir($folder)) {
  97. mkdir($folder);
  98. }
  99. }
  100. $result = $this->tar->addModify(array($tmpBase . $path), '', $tmpBase);
  101. rmdir($tmpBase . $path);
  102. $this->fileList = false;
  103. $this->cachedHeaders = false;
  104. return $result;
  105. }
  106. /**
  107. * add a file to the archive
  108. *
  109. * @param string $path
  110. * @param string $source either a local file or string data
  111. * @return bool
  112. */
  113. function addFile($path, $source = '') {
  114. if ($this->fileExists($path)) {
  115. $this->remove($path);
  116. }
  117. if ($source and $source[0] == '/' and file_exists($source)) {
  118. $source = file_get_contents($source);
  119. }
  120. $result = $this->tar->addString($path, $source);
  121. $this->fileList = false;
  122. $this->cachedHeaders = false;
  123. return $result;
  124. }
  125. /**
  126. * rename a file or folder in the archive
  127. *
  128. * @param string $source
  129. * @param string $dest
  130. * @return bool
  131. */
  132. function rename($source, $dest) {
  133. //no proper way to delete, rename entire archive, rename file and remake archive
  134. $tmp = \OCP\Files::tmpFolder();
  135. $this->tar->extract($tmp);
  136. rename($tmp . $source, $tmp . $dest);
  137. $this->tar = null;
  138. unlink($this->path);
  139. $types = array(null, 'gz', 'bz');
  140. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  141. $this->tar->createModify(array($tmp), '', $tmp . '/');
  142. $this->fileList = false;
  143. $this->cachedHeaders = false;
  144. return true;
  145. }
  146. /**
  147. * @param string $file
  148. */
  149. private function getHeader($file) {
  150. if (!$this->cachedHeaders) {
  151. $this->cachedHeaders = $this->tar->listContent();
  152. }
  153. foreach ($this->cachedHeaders as $header) {
  154. if ($file == $header['filename']
  155. or $file . '/' == $header['filename']
  156. or '/' . $file . '/' == $header['filename']
  157. or '/' . $file == $header['filename']
  158. ) {
  159. return $header;
  160. }
  161. }
  162. return null;
  163. }
  164. /**
  165. * get the uncompressed size of a file in the archive
  166. *
  167. * @param string $path
  168. * @return int
  169. */
  170. function filesize($path) {
  171. $stat = $this->getHeader($path);
  172. return $stat['size'];
  173. }
  174. /**
  175. * get the last modified time of a file in the archive
  176. *
  177. * @param string $path
  178. * @return int
  179. */
  180. function mtime($path) {
  181. $stat = $this->getHeader($path);
  182. return $stat['mtime'];
  183. }
  184. /**
  185. * get the files in a folder
  186. *
  187. * @param string $path
  188. * @return array
  189. */
  190. function getFolder($path) {
  191. $files = $this->getFiles();
  192. $folderContent = array();
  193. $pathLength = strlen($path);
  194. foreach ($files as $file) {
  195. if ($file[0] == '/') {
  196. $file = substr($file, 1);
  197. }
  198. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  199. $result = substr($file, $pathLength);
  200. if ($pos = strpos($result, '/')) {
  201. $result = substr($result, 0, $pos + 1);
  202. }
  203. if (array_search($result, $folderContent) === false) {
  204. $folderContent[] = $result;
  205. }
  206. }
  207. }
  208. return $folderContent;
  209. }
  210. /**
  211. * get all files in the archive
  212. *
  213. * @return array
  214. */
  215. function getFiles() {
  216. if ($this->fileList) {
  217. return $this->fileList;
  218. }
  219. if (!$this->cachedHeaders) {
  220. $this->cachedHeaders = $this->tar->listContent();
  221. }
  222. $files = array();
  223. foreach ($this->cachedHeaders as $header) {
  224. $files[] = $header['filename'];
  225. }
  226. $this->fileList = $files;
  227. return $files;
  228. }
  229. /**
  230. * get the content of a file
  231. *
  232. * @param string $path
  233. * @return string
  234. */
  235. function getFile($path) {
  236. return $this->tar->extractInString($path);
  237. }
  238. /**
  239. * extract a single file from the archive
  240. *
  241. * @param string $path
  242. * @param string $dest
  243. * @return bool
  244. */
  245. function extractFile($path, $dest) {
  246. $tmp = \OCP\Files::tmpFolder();
  247. if (!$this->fileExists($path)) {
  248. return false;
  249. }
  250. if ($this->fileExists('/' . $path)) {
  251. $success = $this->tar->extractList(array('/' . $path), $tmp);
  252. } else {
  253. $success = $this->tar->extractList(array($path), $tmp);
  254. }
  255. if ($success) {
  256. rename($tmp . $path, $dest);
  257. }
  258. \OCP\Files::rmdirr($tmp);
  259. return $success;
  260. }
  261. /**
  262. * extract the archive
  263. *
  264. * @param string $dest
  265. * @return bool
  266. */
  267. function extract($dest) {
  268. return $this->tar->extract($dest);
  269. }
  270. /**
  271. * check if a file or folder exists in the archive
  272. *
  273. * @param string $path
  274. * @return bool
  275. */
  276. function fileExists($path) {
  277. $files = $this->getFiles();
  278. if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) {
  279. return true;
  280. } else {
  281. $folderPath = $path;
  282. if (substr($folderPath, -1, 1) != '/') {
  283. $folderPath .= '/';
  284. }
  285. $pathLength = strlen($folderPath);
  286. foreach ($files as $file) {
  287. if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
  288. return true;
  289. }
  290. }
  291. }
  292. if ($path[0] != '/') { //not all programs agree on the use of a leading /
  293. return $this->fileExists('/' . $path);
  294. } else {
  295. return false;
  296. }
  297. }
  298. /**
  299. * remove a file or folder from the archive
  300. *
  301. * @param string $path
  302. * @return bool
  303. */
  304. function remove($path) {
  305. if (!$this->fileExists($path)) {
  306. return false;
  307. }
  308. $this->fileList = false;
  309. $this->cachedHeaders = false;
  310. //no proper way to delete, extract entire archive, delete file and remake archive
  311. $tmp = \OCP\Files::tmpFolder();
  312. $this->tar->extract($tmp);
  313. \OCP\Files::rmdirr($tmp . $path);
  314. $this->tar = null;
  315. unlink($this->path);
  316. $this->reopen();
  317. $this->tar->createModify(array($tmp), '', $tmp);
  318. return true;
  319. }
  320. /**
  321. * get a file handler
  322. *
  323. * @param string $path
  324. * @param string $mode
  325. * @return resource
  326. */
  327. function getStream($path, $mode) {
  328. if (strrpos($path, '.') !== false) {
  329. $ext = substr($path, strrpos($path, '.'));
  330. } else {
  331. $ext = '';
  332. }
  333. $tmpFile = \OCP\Files::tmpFile($ext);
  334. if ($this->fileExists($path)) {
  335. $this->extractFile($path, $tmpFile);
  336. } elseif ($mode == 'r' or $mode == 'rb') {
  337. return false;
  338. }
  339. if ($mode == 'r' or $mode == 'rb') {
  340. return fopen($tmpFile, $mode);
  341. } else {
  342. \OC\Files\Stream\Close::registerCallback($tmpFile, array($this, 'writeBack'));
  343. self::$tempFiles[$tmpFile] = $path;
  344. return fopen('close://' . $tmpFile, $mode);
  345. }
  346. }
  347. private static $tempFiles = array();
  348. /**
  349. * write back temporary files
  350. */
  351. function writeBack($tmpFile) {
  352. if (isset(self::$tempFiles[$tmpFile])) {
  353. $this->addFile(self::$tempFiles[$tmpFile], $tmpFile);
  354. unlink($tmpFile);
  355. }
  356. }
  357. /**
  358. * reopen the archive to ensure everything is written
  359. */
  360. private function reopen() {
  361. if ($this->tar) {
  362. $this->tar->_close();
  363. $this->tar = null;
  364. }
  365. $types = array(null, 'gz', 'bz');
  366. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  367. }
  368. }