1
0

tar.php 9.0 KB

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