tar.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. {
  126. return $header;
  127. }
  128. }
  129. return null;
  130. }
  131. /**
  132. * get the uncompressed size of a file in the archive
  133. * @param string path
  134. * @return int
  135. */
  136. function filesize($path) {
  137. $stat=$this->getHeader($path);
  138. return $stat['size'];
  139. }
  140. /**
  141. * get the last modified time of a file in the archive
  142. * @param string path
  143. * @return int
  144. */
  145. function mtime($path) {
  146. $stat=$this->getHeader($path);
  147. return $stat['mtime'];
  148. }
  149. /**
  150. * get the files in a folder
  151. * @param path
  152. * @return array
  153. */
  154. function getFolder($path) {
  155. $files=$this->getFiles();
  156. $folderContent=array();
  157. $pathLength=strlen($path);
  158. foreach($files as $file) {
  159. if($file[0]=='/') {
  160. $file=substr($file, 1);
  161. }
  162. if(substr($file, 0, $pathLength)==$path and $file!=$path) {
  163. $result=substr($file, $pathLength);
  164. if($pos=strpos($result, '/')) {
  165. $result=substr($result, 0, $pos+1);
  166. }
  167. if(array_search($result, $folderContent)===false) {
  168. $folderContent[]=$result;
  169. }
  170. }
  171. }
  172. return $folderContent;
  173. }
  174. /**
  175. *get all files in the archive
  176. * @return array
  177. */
  178. function getFiles() {
  179. if($this->fileList) {
  180. return $this->fileList;
  181. }
  182. if ( ! $this->cachedHeaders ) {
  183. $this->cachedHeaders = $this->tar->listContent();
  184. }
  185. $files=array();
  186. foreach($this->cachedHeaders as $header) {
  187. $files[]=$header['filename'];
  188. }
  189. $this->fileList=$files;
  190. return $files;
  191. }
  192. /**
  193. * get the content of a file
  194. * @param string path
  195. * @return string
  196. */
  197. function getFile($path) {
  198. return $this->tar->extractInString($path);
  199. }
  200. /**
  201. * extract a single file from the archive
  202. * @param string path
  203. * @param string dest
  204. * @return bool
  205. */
  206. function extractFile($path,$dest) {
  207. $tmp=OCP\Files::tmpFolder();
  208. if(!$this->fileExists($path)) {
  209. return false;
  210. }
  211. if($this->fileExists('/'.$path)) {
  212. $success=$this->tar->extractList(array('/'.$path), $tmp);
  213. }else{
  214. $success=$this->tar->extractList(array($path), $tmp);
  215. }
  216. if($success) {
  217. rename($tmp.$path, $dest);
  218. }
  219. OCP\Files::rmdirr($tmp);
  220. return $success;
  221. }
  222. /**
  223. * extract the archive
  224. * @param string path
  225. * @param string dest
  226. * @return bool
  227. */
  228. function extract($dest) {
  229. return $this->tar->extract($dest);
  230. }
  231. /**
  232. * check if a file or folder exists in the archive
  233. * @param string path
  234. * @return bool
  235. */
  236. function fileExists($path) {
  237. $files=$this->getFiles();
  238. if((array_search($path, $files)!==false) or (array_search($path.'/', $files)!==false)) {
  239. return true;
  240. }else{
  241. $folderPath=$path;
  242. if(substr($folderPath, -1, 1)!='/') {
  243. $folderPath.='/';
  244. }
  245. $pathLength=strlen($folderPath);
  246. foreach($files as $file) {
  247. if(strlen($file)>$pathLength and substr($file, 0, $pathLength)==$folderPath) {
  248. return true;
  249. }
  250. }
  251. }
  252. if($path[0]!='/') {//not all programs agree on the use of a leading /
  253. return $this->fileExists('/'.$path);
  254. }else{
  255. return false;
  256. }
  257. }
  258. /**
  259. * remove a file or folder from the archive
  260. * @param string path
  261. * @return bool
  262. */
  263. function remove($path) {
  264. if(!$this->fileExists($path)) {
  265. return false;
  266. }
  267. $this->fileList=false;
  268. $this->cachedHeaders=false;
  269. //no proper way to delete, extract entire archive, delete file and remake archive
  270. $tmp=OCP\Files::tmpFolder();
  271. $this->tar->extract($tmp);
  272. OCP\Files::rmdirr($tmp.$path);
  273. $this->tar=null;
  274. unlink($this->path);
  275. $this->reopen();
  276. $this->tar->createModify(array($tmp), '', $tmp);
  277. return true;
  278. }
  279. /**
  280. * get a file handler
  281. * @param string path
  282. * @param string mode
  283. * @return resource
  284. */
  285. function getStream($path,$mode) {
  286. if(strrpos($path, '.')!==false) {
  287. $ext=substr($path, strrpos($path, '.'));
  288. }else{
  289. $ext='';
  290. }
  291. $tmpFile=OCP\Files::tmpFile($ext);
  292. if($this->fileExists($path)) {
  293. $this->extractFile($path, $tmpFile);
  294. }elseif($mode=='r' or $mode=='rb') {
  295. return false;
  296. }
  297. if($mode=='r' or $mode=='rb') {
  298. return fopen($tmpFile, $mode);
  299. }else{
  300. OC_CloseStreamWrapper::$callBacks[$tmpFile]=array($this,'writeBack');
  301. self::$tempFiles[$tmpFile]=$path;
  302. return fopen('close://'.$tmpFile, $mode);
  303. }
  304. }
  305. private static $tempFiles=array();
  306. /**
  307. * write back temporary files
  308. */
  309. function writeBack($tmpFile) {
  310. if(isset(self::$tempFiles[$tmpFile])) {
  311. $this->addFile(self::$tempFiles[$tmpFile], $tmpFile);
  312. unlink($tmpFile);
  313. }
  314. }
  315. /**
  316. * reopen the archive to ensure everything is written
  317. */
  318. private function reopen() {
  319. if($this->tar) {
  320. $this->tar->_close();
  321. $this->tar=null;
  322. }
  323. $types=array(null,'gz','bz');
  324. $this->tar=new Archive_Tar($this->path, $types[self::getTarType($this->path)]);
  325. }
  326. }