tar.php 7.6 KB

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