TAR.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. public function filesize(string $path): false|int|float {
  158. $stat = $this->getHeader($path);
  159. return $stat['size'] ?? false;
  160. }
  161. /**
  162. * get the last modified time of a file in the archive
  163. *
  164. * @return int|false
  165. */
  166. public function mtime(string $path) {
  167. $stat = $this->getHeader($path);
  168. return $stat['mtime'] ?? false;
  169. }
  170. /**
  171. * get the files in a folder
  172. */
  173. public function getFolder(string $path): array {
  174. $files = $this->getFiles();
  175. $folderContent = [];
  176. $pathLength = strlen($path);
  177. foreach ($files as $file) {
  178. if ($file[0] == '/') {
  179. $file = substr($file, 1);
  180. }
  181. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  182. $result = substr($file, $pathLength);
  183. if ($pos = strpos($result, '/')) {
  184. $result = substr($result, 0, $pos + 1);
  185. }
  186. if (!in_array($result, $folderContent)) {
  187. $folderContent[] = $result;
  188. }
  189. }
  190. }
  191. return $folderContent;
  192. }
  193. /**
  194. * get all files in the archive
  195. */
  196. public function getFiles(): array {
  197. if ($this->fileList) {
  198. return $this->fileList;
  199. }
  200. if (!$this->cachedHeaders) {
  201. $this->cachedHeaders = $this->tar->listContent();
  202. }
  203. $files = [];
  204. foreach ($this->cachedHeaders as $header) {
  205. $files[] = $header['filename'];
  206. }
  207. $this->fileList = $files;
  208. return $files;
  209. }
  210. /**
  211. * get the content of a file
  212. *
  213. * @return string|false
  214. */
  215. public function getFile(string $path) {
  216. $string = $this->tar->extractInString($path);
  217. if (is_string($string)) {
  218. return $string;
  219. } else {
  220. return false;
  221. }
  222. }
  223. /**
  224. * extract a single file from the archive
  225. */
  226. public function extractFile(string $path, string $dest): bool {
  227. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  228. if (!$this->fileExists($path)) {
  229. return false;
  230. }
  231. if ($this->fileExists('/' . $path)) {
  232. $success = $this->tar->extractList(['/' . $path], $tmp);
  233. } else {
  234. $success = $this->tar->extractList([$path], $tmp);
  235. }
  236. if ($success) {
  237. rename($tmp . $path, $dest);
  238. }
  239. \OCP\Files::rmdirr($tmp);
  240. return $success;
  241. }
  242. /**
  243. * extract the archive
  244. */
  245. public function extract(string $dest): bool {
  246. return $this->tar->extract($dest);
  247. }
  248. /**
  249. * check if a file or folder exists in the archive
  250. */
  251. public function fileExists(string $path): bool {
  252. $files = $this->getFiles();
  253. if ((in_array($path, $files)) or (in_array($path . '/', $files))) {
  254. return true;
  255. } else {
  256. $folderPath = rtrim($path, '/') . '/';
  257. $pathLength = strlen($folderPath);
  258. foreach ($files as $file) {
  259. if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
  260. return true;
  261. }
  262. }
  263. }
  264. if ($path[0] != '/') { //not all programs agree on the use of a leading /
  265. return $this->fileExists('/' . $path);
  266. } else {
  267. return false;
  268. }
  269. }
  270. /**
  271. * remove a file or folder from the archive
  272. */
  273. public function remove(string $path): bool {
  274. if (!$this->fileExists($path)) {
  275. return false;
  276. }
  277. $this->fileList = false;
  278. $this->cachedHeaders = false;
  279. //no proper way to delete, extract entire archive, delete file and remake archive
  280. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  281. $this->tar->extract($tmp);
  282. \OCP\Files::rmdirr($tmp . $path);
  283. $this->tar = null;
  284. unlink($this->path);
  285. $this->reopen();
  286. $this->tar->createModify([$tmp], '', $tmp);
  287. return true;
  288. }
  289. /**
  290. * get a file handler
  291. *
  292. * @return bool|resource
  293. */
  294. public function getStream(string $path, string $mode) {
  295. $lastPoint = strrpos($path, '.');
  296. if ($lastPoint !== false) {
  297. $ext = substr($path, $lastPoint);
  298. } else {
  299. $ext = '';
  300. }
  301. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  302. if ($this->fileExists($path)) {
  303. $this->extractFile($path, $tmpFile);
  304. } elseif ($mode == 'r' or $mode == 'rb') {
  305. return false;
  306. }
  307. if ($mode == 'r' or $mode == 'rb') {
  308. return fopen($tmpFile, $mode);
  309. } else {
  310. $handle = fopen($tmpFile, $mode);
  311. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  312. $this->writeBack($tmpFile, $path);
  313. });
  314. }
  315. }
  316. /**
  317. * write back temporary files
  318. */
  319. public function writeBack(string $tmpFile, string $path): void {
  320. $this->addFile($path, $tmpFile);
  321. unlink($tmpFile);
  322. }
  323. /**
  324. * reopen the archive to ensure everything is written
  325. */
  326. private function reopen(): void {
  327. if ($this->tar) {
  328. $this->tar->_close();
  329. $this->tar = null;
  330. }
  331. $types = [null, 'gz', 'bz'];
  332. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  333. }
  334. /**
  335. * Get error object from archive_tar.
  336. */
  337. public function getError(): ?\PEAR_Error {
  338. if ($this->tar instanceof \Archive_Tar && $this->tar->error_object instanceof \PEAR_Error) {
  339. return $this->tar->error_object;
  340. }
  341. return null;
  342. }
  343. }