TAR.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.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. public const PLAIN = 0;
  37. public const GZIP = 1;
  38. public const BZIP = 2;
  39. /**
  40. * @var string[]|false
  41. */
  42. private $fileList = false;
  43. /**
  44. * @var array|false
  45. */
  46. private $cachedHeaders = false;
  47. /**
  48. * @var \Archive_Tar
  49. */
  50. private $tar = null;
  51. /**
  52. * @var string
  53. */
  54. private $path;
  55. public function __construct(string $source) {
  56. $types = [null, 'gz', 'bz2'];
  57. $this->path = $source;
  58. $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]);
  59. }
  60. /**
  61. * try to detect the type of tar compression
  62. */
  63. public static function getTarType(string $file): int {
  64. if (strpos($file, '.')) {
  65. $extension = substr($file, strrpos($file, '.'));
  66. switch ($extension) {
  67. case '.gz':
  68. case '.tgz':
  69. return self::GZIP;
  70. case '.bz':
  71. case '.bz2':
  72. return self::BZIP;
  73. case '.tar':
  74. return self::PLAIN;
  75. default:
  76. return self::PLAIN;
  77. }
  78. } else {
  79. return self::PLAIN;
  80. }
  81. }
  82. /**
  83. * add an empty folder to the archive
  84. */
  85. public function addFolder(string $path): bool {
  86. $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder();
  87. $path = rtrim($path, '/') . '/';
  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([$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 $source either a local file or string data
  109. */
  110. public function addFile(string $path, string $source = ''): bool {
  111. if ($this->fileExists($path)) {
  112. $this->remove($path);
  113. }
  114. if ($source and $source[0] == '/' and file_exists($source)) {
  115. $source = file_get_contents($source);
  116. }
  117. $result = $this->tar->addString($path, $source);
  118. $this->fileList = false;
  119. $this->cachedHeaders = false;
  120. return $result;
  121. }
  122. /**
  123. * rename a file or folder in the archive
  124. */
  125. public function rename(string $source, string $dest): bool {
  126. //no proper way to delete, rename entire archive, rename file and remake archive
  127. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  128. $this->tar->extract($tmp);
  129. rename($tmp . $source, $tmp . $dest);
  130. $this->tar = null;
  131. unlink($this->path);
  132. $types = [null, 'gz', 'bz'];
  133. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  134. $this->tar->createModify([$tmp], '', $tmp . '/');
  135. $this->fileList = false;
  136. $this->cachedHeaders = false;
  137. return true;
  138. }
  139. private function getHeader(string $file): ?array {
  140. if (!$this->cachedHeaders) {
  141. $this->cachedHeaders = $this->tar->listContent();
  142. }
  143. foreach ($this->cachedHeaders as $header) {
  144. if ($file == $header['filename']
  145. or $file . '/' == $header['filename']
  146. or '/' . $file . '/' == $header['filename']
  147. or '/' . $file == $header['filename']
  148. ) {
  149. return $header;
  150. }
  151. }
  152. return null;
  153. }
  154. /**
  155. * get the uncompressed size of a file in the archive
  156. *
  157. * @return int|false
  158. */
  159. public function filesize(string $path) {
  160. $stat = $this->getHeader($path);
  161. return $stat['size'] ?? false;
  162. }
  163. /**
  164. * get the last modified time of a file in the archive
  165. *
  166. * @return int|false
  167. */
  168. public function mtime(string $path) {
  169. $stat = $this->getHeader($path);
  170. return $stat['mtime'] ?? false;
  171. }
  172. /**
  173. * get the files in a folder
  174. */
  175. public function getFolder(string $path): array {
  176. $files = $this->getFiles();
  177. $folderContent = [];
  178. $pathLength = strlen($path);
  179. foreach ($files as $file) {
  180. if ($file[0] == '/') {
  181. $file = substr($file, 1);
  182. }
  183. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  184. $result = substr($file, $pathLength);
  185. if ($pos = strpos($result, '/')) {
  186. $result = substr($result, 0, $pos + 1);
  187. }
  188. if (array_search($result, $folderContent) === false) {
  189. $folderContent[] = $result;
  190. }
  191. }
  192. }
  193. return $folderContent;
  194. }
  195. /**
  196. * get all files in the archive
  197. */
  198. public function getFiles(): array {
  199. if ($this->fileList) {
  200. return $this->fileList;
  201. }
  202. if (!$this->cachedHeaders) {
  203. $this->cachedHeaders = $this->tar->listContent();
  204. }
  205. $files = [];
  206. foreach ($this->cachedHeaders as $header) {
  207. $files[] = $header['filename'];
  208. }
  209. $this->fileList = $files;
  210. return $files;
  211. }
  212. /**
  213. * get the content of a file
  214. *
  215. * @return string|false
  216. */
  217. public function getFile(string $path) {
  218. $string = $this->tar->extractInString($path);
  219. if (is_string($string)) {
  220. return $string;
  221. } else {
  222. return false;
  223. }
  224. }
  225. /**
  226. * extract a single file from the archive
  227. */
  228. public function extractFile(string $path, string $dest): bool {
  229. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  230. if (!$this->fileExists($path)) {
  231. return false;
  232. }
  233. if ($this->fileExists('/' . $path)) {
  234. $success = $this->tar->extractList(['/' . $path], $tmp);
  235. } else {
  236. $success = $this->tar->extractList([$path], $tmp);
  237. }
  238. if ($success) {
  239. rename($tmp . $path, $dest);
  240. }
  241. \OCP\Files::rmdirr($tmp);
  242. return $success;
  243. }
  244. /**
  245. * extract the archive
  246. */
  247. public function extract(string $dest): bool {
  248. return $this->tar->extract($dest);
  249. }
  250. /**
  251. * check if a file or folder exists in the archive
  252. */
  253. public function fileExists(string $path): bool {
  254. $files = $this->getFiles();
  255. if ((array_search($path, $files) !== false) or (array_search($path . '/', $files) !== false)) {
  256. return true;
  257. } else {
  258. $folderPath = rtrim($path, '/') . '/';
  259. $pathLength = strlen($folderPath);
  260. foreach ($files as $file) {
  261. if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
  262. return true;
  263. }
  264. }
  265. }
  266. if ($path[0] != '/') { //not all programs agree on the use of a leading /
  267. return $this->fileExists('/' . $path);
  268. } else {
  269. return false;
  270. }
  271. }
  272. /**
  273. * remove a file or folder from the archive
  274. */
  275. public function remove(string $path): bool {
  276. if (!$this->fileExists($path)) {
  277. return false;
  278. }
  279. $this->fileList = false;
  280. $this->cachedHeaders = false;
  281. //no proper way to delete, extract entire archive, delete file and remake archive
  282. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  283. $this->tar->extract($tmp);
  284. \OCP\Files::rmdirr($tmp . $path);
  285. $this->tar = null;
  286. unlink($this->path);
  287. $this->reopen();
  288. $this->tar->createModify([$tmp], '', $tmp);
  289. return true;
  290. }
  291. /**
  292. * get a file handler
  293. *
  294. * @return bool|resource
  295. */
  296. public function getStream(string $path, string $mode) {
  297. $lastPoint = strrpos($path, '.');
  298. if ($lastPoint !== false) {
  299. $ext = substr($path, $lastPoint);
  300. } else {
  301. $ext = '';
  302. }
  303. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  304. if ($this->fileExists($path)) {
  305. $this->extractFile($path, $tmpFile);
  306. } elseif ($mode == 'r' or $mode == 'rb') {
  307. return false;
  308. }
  309. if ($mode == 'r' or $mode == 'rb') {
  310. return fopen($tmpFile, $mode);
  311. } else {
  312. $handle = fopen($tmpFile, $mode);
  313. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  314. $this->writeBack($tmpFile, $path);
  315. });
  316. }
  317. }
  318. /**
  319. * write back temporary files
  320. */
  321. public function writeBack(string $tmpFile, string $path): void {
  322. $this->addFile($path, $tmpFile);
  323. unlink($tmpFile);
  324. }
  325. /**
  326. * reopen the archive to ensure everything is written
  327. */
  328. private function reopen(): void {
  329. if ($this->tar) {
  330. $this->tar->_close();
  331. $this->tar = null;
  332. }
  333. $types = [null, 'gz', 'bz'];
  334. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  335. }
  336. /**
  337. * Get error object from archive_tar.
  338. */
  339. public function getError(): ?\PEAR_Error {
  340. if ($this->tar instanceof \Archive_Tar && $this->tar->error_object instanceof \PEAR_Error) {
  341. return $this->tar->error_object;
  342. }
  343. return null;
  344. }
  345. }