TAR.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Archive;
  8. use Icewind\Streams\CallbackWrapper;
  9. class TAR extends Archive {
  10. public const PLAIN = 0;
  11. public const GZIP = 1;
  12. public const BZIP = 2;
  13. /**
  14. * @var string[]|false
  15. */
  16. private $fileList = false;
  17. /**
  18. * @var array|false
  19. */
  20. private $cachedHeaders = false;
  21. private \Archive_Tar $tar;
  22. private string $path;
  23. public function __construct(string $source) {
  24. $types = [null, 'gz', 'bz2'];
  25. $this->path = $source;
  26. $this->tar = new \Archive_Tar($source, $types[self::getTarType($source)]);
  27. }
  28. /**
  29. * try to detect the type of tar compression
  30. */
  31. public static function getTarType(string $file): int {
  32. if (strpos($file, '.')) {
  33. $extension = substr($file, strrpos($file, '.'));
  34. switch ($extension) {
  35. case '.gz':
  36. case '.tgz':
  37. return self::GZIP;
  38. case '.bz':
  39. case '.bz2':
  40. return self::BZIP;
  41. case '.tar':
  42. return self::PLAIN;
  43. default:
  44. return self::PLAIN;
  45. }
  46. } else {
  47. return self::PLAIN;
  48. }
  49. }
  50. /**
  51. * add an empty folder to the archive
  52. */
  53. public function addFolder(string $path): bool {
  54. $tmpBase = \OC::$server->getTempManager()->getTemporaryFolder();
  55. $path = rtrim($path, '/') . '/';
  56. if ($this->fileExists($path)) {
  57. return false;
  58. }
  59. $parts = explode('/', $path);
  60. $folder = $tmpBase;
  61. foreach ($parts as $part) {
  62. $folder .= '/' . $part;
  63. if (!is_dir($folder)) {
  64. mkdir($folder);
  65. }
  66. }
  67. $result = $this->tar->addModify([$tmpBase . $path], '', $tmpBase);
  68. rmdir($tmpBase . $path);
  69. $this->fileList = false;
  70. $this->cachedHeaders = false;
  71. return $result;
  72. }
  73. /**
  74. * add a file to the archive
  75. *
  76. * @param string $source either a local file or string data
  77. */
  78. public function addFile(string $path, string $source = ''): bool {
  79. if ($this->fileExists($path)) {
  80. $this->remove($path);
  81. }
  82. if ($source and $source[0] == '/' and file_exists($source)) {
  83. $source = file_get_contents($source);
  84. }
  85. $result = $this->tar->addString($path, $source);
  86. $this->fileList = false;
  87. $this->cachedHeaders = false;
  88. return $result;
  89. }
  90. /**
  91. * rename a file or folder in the archive
  92. */
  93. public function rename(string $source, string $dest): bool {
  94. //no proper way to delete, rename entire archive, rename file and remake archive
  95. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  96. $this->tar->extract($tmp);
  97. rename($tmp . $source, $tmp . $dest);
  98. $this->tar->_close();
  99. unlink($this->path);
  100. $types = [null, 'gz', 'bz'];
  101. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  102. $this->tar->createModify([$tmp], '', $tmp . '/');
  103. $this->fileList = false;
  104. $this->cachedHeaders = false;
  105. return true;
  106. }
  107. private function getHeader(string $file): ?array {
  108. if (!$this->cachedHeaders) {
  109. $this->cachedHeaders = $this->tar->listContent();
  110. }
  111. foreach ($this->cachedHeaders as $header) {
  112. if ($file == $header['filename']
  113. or $file . '/' == $header['filename']
  114. or '/' . $file . '/' == $header['filename']
  115. or '/' . $file == $header['filename']
  116. ) {
  117. return $header;
  118. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * get the uncompressed size of a file in the archive
  124. */
  125. public function filesize(string $path): false|int|float {
  126. $stat = $this->getHeader($path);
  127. return $stat['size'] ?? false;
  128. }
  129. /**
  130. * get the last modified time of a file in the archive
  131. *
  132. * @return int|false
  133. */
  134. public function mtime(string $path) {
  135. $stat = $this->getHeader($path);
  136. return $stat['mtime'] ?? false;
  137. }
  138. /**
  139. * get the files in a folder
  140. */
  141. public function getFolder(string $path): array {
  142. $files = $this->getFiles();
  143. $folderContent = [];
  144. $pathLength = strlen($path);
  145. foreach ($files as $file) {
  146. if ($file[0] == '/') {
  147. $file = substr($file, 1);
  148. }
  149. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  150. $result = substr($file, $pathLength);
  151. if ($pos = strpos($result, '/')) {
  152. $result = substr($result, 0, $pos + 1);
  153. }
  154. if (!in_array($result, $folderContent)) {
  155. $folderContent[] = $result;
  156. }
  157. }
  158. }
  159. return $folderContent;
  160. }
  161. /**
  162. * get all files in the archive
  163. */
  164. public function getFiles(): array {
  165. if ($this->fileList !== false) {
  166. return $this->fileList;
  167. }
  168. if ($this->cachedHeaders === false) {
  169. $headers = $this->tar->listContent();
  170. if (is_array($headers)) {
  171. $this->cachedHeaders = $headers;
  172. } else {
  173. return [];
  174. }
  175. }
  176. $files = [];
  177. foreach ($this->cachedHeaders as $header) {
  178. $files[] = $header['filename'];
  179. }
  180. $this->fileList = $files;
  181. return $files;
  182. }
  183. /**
  184. * get the content of a file
  185. *
  186. * @return string|false
  187. */
  188. public function getFile(string $path) {
  189. $string = $this->tar->extractInString($path);
  190. /** @var ?string $string */
  191. if (is_string($string)) {
  192. return $string;
  193. } else {
  194. return false;
  195. }
  196. }
  197. /**
  198. * extract a single file from the archive
  199. */
  200. public function extractFile(string $path, string $dest): bool {
  201. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  202. if (!$this->fileExists($path)) {
  203. return false;
  204. }
  205. if ($this->fileExists('/' . $path)) {
  206. $success = $this->tar->extractList(['/' . $path], $tmp);
  207. } else {
  208. $success = $this->tar->extractList([$path], $tmp);
  209. }
  210. if ($success) {
  211. rename($tmp . $path, $dest);
  212. }
  213. \OCP\Files::rmdirr($tmp);
  214. return $success;
  215. }
  216. /**
  217. * extract the archive
  218. */
  219. public function extract(string $dest): bool {
  220. return $this->tar->extract($dest);
  221. }
  222. /**
  223. * check if a file or folder exists in the archive
  224. */
  225. public function fileExists(string $path): bool {
  226. $files = $this->getFiles();
  227. if ((in_array($path, $files)) or (in_array($path . '/', $files))) {
  228. return true;
  229. } else {
  230. $folderPath = rtrim($path, '/') . '/';
  231. $pathLength = strlen($folderPath);
  232. foreach ($files as $file) {
  233. if (strlen($file) > $pathLength and substr($file, 0, $pathLength) == $folderPath) {
  234. return true;
  235. }
  236. }
  237. }
  238. if ($path[0] != '/') { //not all programs agree on the use of a leading /
  239. return $this->fileExists('/' . $path);
  240. } else {
  241. return false;
  242. }
  243. }
  244. /**
  245. * remove a file or folder from the archive
  246. */
  247. public function remove(string $path): bool {
  248. if (!$this->fileExists($path)) {
  249. return false;
  250. }
  251. $this->fileList = false;
  252. $this->cachedHeaders = false;
  253. //no proper way to delete, extract entire archive, delete file and remake archive
  254. $tmp = \OC::$server->getTempManager()->getTemporaryFolder();
  255. $this->tar->extract($tmp);
  256. \OCP\Files::rmdirr($tmp . $path);
  257. unlink($this->path);
  258. $this->reopen();
  259. $this->tar->createModify([$tmp], '', $tmp);
  260. return true;
  261. }
  262. /**
  263. * get a file handler
  264. *
  265. * @return bool|resource
  266. */
  267. public function getStream(string $path, string $mode) {
  268. $lastPoint = strrpos($path, '.');
  269. if ($lastPoint !== false) {
  270. $ext = substr($path, $lastPoint);
  271. } else {
  272. $ext = '';
  273. }
  274. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  275. if ($this->fileExists($path)) {
  276. $this->extractFile($path, $tmpFile);
  277. } elseif ($mode == 'r' or $mode == 'rb') {
  278. return false;
  279. }
  280. if ($mode == 'r' or $mode == 'rb') {
  281. return fopen($tmpFile, $mode);
  282. } else {
  283. $handle = fopen($tmpFile, $mode);
  284. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  285. $this->writeBack($tmpFile, $path);
  286. });
  287. }
  288. }
  289. /**
  290. * write back temporary files
  291. */
  292. public function writeBack(string $tmpFile, string $path): void {
  293. $this->addFile($path, $tmpFile);
  294. unlink($tmpFile);
  295. }
  296. /**
  297. * reopen the archive to ensure everything is written
  298. */
  299. private function reopen(): void {
  300. $this->tar->_close();
  301. $types = [null, 'gz', 'bz'];
  302. $this->tar = new \Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  303. }
  304. /**
  305. * Get error object from archive_tar.
  306. */
  307. public function getError(): ?\PEAR_Error {
  308. if ($this->tar->error_object instanceof \PEAR_Error) {
  309. return $this->tar->error_object;
  310. }
  311. return null;
  312. }
  313. }