TarTask.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. /*
  3. * $Id: TarTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/tasks/system/MatchingTask.php';
  22. include_once 'phing/util/SourceFileScanner.php';
  23. include_once 'phing/mappers/MergeMapper.php';
  24. include_once 'phing/util/StringHelper.php';
  25. /**
  26. * Creates a tar archive using PEAR Archive_Tar.
  27. *
  28. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  29. * @author Stefano Mazzocchi <stefano@apache.org> (Ant)
  30. * @author Stefan Bodewig <stefan.bodewig@epost.de> (Ant)
  31. * @author Magesh Umasankar
  32. * @version $Id: TarTask.php 905 2010-10-05 16:28:03Z mrook $
  33. * @package phing.tasks.ext
  34. */
  35. class TarTask extends MatchingTask {
  36. const TAR_NAMELEN = 100;
  37. const WARN = "warn";
  38. const FAIL = "fail";
  39. const OMIT = "omit";
  40. private $tarFile;
  41. private $baseDir;
  42. private $includeEmpty = true; // Whether to include empty dirs in the TAR
  43. private $longFileMode = "warn";
  44. private $filesets = array();
  45. private $fileSetFiles = array();
  46. /**
  47. * Indicates whether the user has been warned about long files already.
  48. */
  49. private $longWarningGiven = false;
  50. /**
  51. * Compression mode. Available options "gzip", "bzip2", "none" (null).
  52. */
  53. private $compression = null;
  54. /**
  55. * File path prefix in the tar archive
  56. *
  57. * @var string
  58. */
  59. private $prefix = null;
  60. /**
  61. * Ensures that PEAR lib exists.
  62. */
  63. public function init() {
  64. include_once 'Archive/Tar.php';
  65. if (!class_exists('Archive_Tar')) {
  66. throw new BuildException("You must have installed the PEAR Archive_Tar class in order to use TarTask.");
  67. }
  68. }
  69. /**
  70. * Add a new fileset
  71. * @return FileSet
  72. */
  73. public function createTarFileSet() {
  74. $this->fileset = new TarFileSet();
  75. $this->filesets[] = $this->fileset;
  76. return $this->fileset;
  77. }
  78. /**
  79. * Add a new fileset. Alias to createTarFileSet() for backwards compatibility.
  80. * @return FileSet
  81. * @see createTarFileSet()
  82. */
  83. public function createFileSet() {
  84. $this->fileset = new TarFileSet();
  85. $this->filesets[] = $this->fileset;
  86. return $this->fileset;
  87. }
  88. /**
  89. * Set is the name/location of where to create the tar file.
  90. * @param PhingFile $destFile The output of the tar
  91. */
  92. public function setDestFile(PhingFile $destFile) {
  93. $this->tarFile = $destFile;
  94. }
  95. /**
  96. * This is the base directory to look in for things to tar.
  97. * @param PhingFile $baseDir
  98. */
  99. public function setBasedir(PhingFile $baseDir) {
  100. $this->baseDir = $baseDir;
  101. }
  102. /**
  103. * Set the include empty dirs flag.
  104. * @param boolean Flag if empty dirs should be tarred too
  105. * @return void
  106. * @access public
  107. */
  108. function setIncludeEmptyDirs($bool) {
  109. $this->includeEmpty = (boolean) $bool;
  110. }
  111. /**
  112. * Set how to handle long files, those with a path&gt;100 chars.
  113. * Optional, default=warn.
  114. * <p>
  115. * Allowable values are
  116. * <ul>
  117. * <li> truncate - paths are truncated to the maximum length
  118. * <li> fail - paths greater than the maximim cause a build exception
  119. * <li> warn - paths greater than the maximum cause a warning and GNU is used
  120. * <li> gnu - GNU extensions are used for any paths greater than the maximum.
  121. * <li> omit - paths greater than the maximum are omitted from the archive
  122. * </ul>
  123. */
  124. public function setLongfile($mode) {
  125. $this->longFileMode = $mode;
  126. }
  127. /**
  128. * Set compression method.
  129. * Allowable values are
  130. * <ul>
  131. * <li> none - no compression
  132. * <li> gzip - Gzip compression
  133. * <li> bzip2 - Bzip2 compression
  134. * </ul>
  135. */
  136. public function setCompression($mode) {
  137. switch($mode) {
  138. case "gzip":
  139. $this->compression = "gz";
  140. break;
  141. case "bzip2":
  142. $this->compression = "bz2";
  143. break;
  144. case "none":
  145. $this->compression = null;
  146. break;
  147. default:
  148. $this->log("Ignoring unknown compression mode: ".$mode, Project::MSG_WARN);
  149. $this->compression = null;
  150. }
  151. }
  152. /**
  153. * Sets the file path prefix for file in the tar file.
  154. *
  155. * @param string $prefix Prefix
  156. *
  157. * @return void
  158. */
  159. public function setPrefix($prefix) {
  160. $this->prefix = $prefix;
  161. }
  162. /**
  163. * do the work
  164. * @throws BuildException
  165. */
  166. public function main() {
  167. if ($this->tarFile === null) {
  168. throw new BuildException("tarfile attribute must be set!", $this->getLocation());
  169. }
  170. if ($this->tarFile->exists() && $this->tarFile->isDirectory()) {
  171. throw new BuildException("tarfile is a directory!", $this->getLocation());
  172. }
  173. if ($this->tarFile->exists() && !$this->tarFile->canWrite()) {
  174. throw new BuildException("Can not write to the specified tarfile!", $this->getLocation());
  175. }
  176. // shouldn't need to clone, since the entries in filesets
  177. // themselves won't be modified -- only elements will be added
  178. $savedFileSets = $this->filesets;
  179. try {
  180. if ($this->baseDir !== null) {
  181. if (!$this->baseDir->exists()) {
  182. throw new BuildException("basedir does not exist!", $this->getLocation());
  183. }
  184. if (empty($this->filesets)) { // if there weren't any explicit filesets specivied, then
  185. // create a default, all-inclusive fileset using the specified basedir.
  186. $mainFileSet = new TarFileSet($this->fileset);
  187. $mainFileSet->setDir($this->baseDir);
  188. $this->filesets[] = $mainFileSet;
  189. }
  190. }
  191. if (empty($this->filesets)) {
  192. throw new BuildException("You must supply either a basedir "
  193. . "attribute or some nested filesets.",
  194. $this->getLocation());
  195. }
  196. // check if tar is out of date with respect to each fileset
  197. if($this->tarFile->exists()) {
  198. $upToDate = true;
  199. foreach($this->filesets as $fs) {
  200. $files = $fs->getFiles($this->project, $this->includeEmpty);
  201. if (!$this->archiveIsUpToDate($files, $fs->getDir($this->project))) {
  202. $upToDate = false;
  203. }
  204. for ($i=0, $fcount=count($files); $i < $fcount; $i++) {
  205. if ($this->tarFile->equals(new PhingFile($fs->getDir($this->project), $files[$i]))) {
  206. throw new BuildException("A tar file cannot include itself", $this->getLocation());
  207. }
  208. }
  209. }
  210. if ($upToDate) {
  211. $this->log("Nothing to do: " . $this->tarFile->__toString() . " is up to date.", Project::MSG_INFO);
  212. return;
  213. }
  214. }
  215. $this->log("Building tar: " . $this->tarFile->__toString(), Project::MSG_INFO);
  216. $tar = new Archive_Tar($this->tarFile->getAbsolutePath(), $this->compression);
  217. // print errors
  218. $tar->setErrorHandling(PEAR_ERROR_PRINT);
  219. foreach($this->filesets as $fs) {
  220. $files = $fs->getFiles($this->project, $this->includeEmpty);
  221. if (count($files) > 1 && strlen($fs->getFullpath()) > 0) {
  222. throw new BuildException("fullpath attribute may only "
  223. . "be specified for "
  224. . "filesets that specify a "
  225. . "single file.");
  226. }
  227. $fsBasedir = $fs->getDir($this->project);
  228. $filesToTar = array();
  229. for ($i=0, $fcount=count($files); $i < $fcount; $i++) {
  230. $f = new PhingFile($fsBasedir, $files[$i]);
  231. $filesToTar[] = $f->getAbsolutePath();
  232. $this->log("Adding file " . $f->getPath() . " to archive.", Project::MSG_VERBOSE);
  233. }
  234. $tar->addModify($filesToTar, $this->prefix, $fsBasedir->getAbsolutePath());
  235. }
  236. } catch (IOException $ioe) {
  237. $msg = "Problem creating TAR: " . $ioe->getMessage();
  238. $this->filesets = $savedFileSets;
  239. throw new BuildException($msg, $ioe, $this->getLocation());
  240. }
  241. $this->filesets = $savedFileSets;
  242. }
  243. /**
  244. * @param array $files array of filenames
  245. * @param PhingFile $dir
  246. * @return boolean
  247. */
  248. protected function archiveIsUpToDate($files, $dir) {
  249. $sfs = new SourceFileScanner($this);
  250. $mm = new MergeMapper();
  251. $mm->setTo($this->tarFile->getAbsolutePath());
  252. return count($sfs->restrict($files, $dir, null, $mm)) == 0;
  253. }
  254. }
  255. /**
  256. * This is a FileSet with the option to specify permissions.
  257. *
  258. * Permissions are currently not implemented by PEAR Archive_Tar,
  259. * but hopefully they will be in the future.
  260. *
  261. */
  262. class TarFileSet extends FileSet {
  263. private $files = null;
  264. private $mode = 0100644;
  265. private $userName = "";
  266. private $groupName = "";
  267. private $prefix = "";
  268. private $fullpath = "";
  269. private $preserveLeadingSlashes = false;
  270. /**
  271. * Get a list of files and directories specified in the fileset.
  272. * @return array a list of file and directory names, relative to
  273. * the baseDir for the project.
  274. */
  275. public function getFiles(Project $p, $includeEmpty = true) {
  276. if ($this->files === null) {
  277. $ds = $this->getDirectoryScanner($p);
  278. $this->files = $ds->getIncludedFiles();
  279. if ($includeEmpty) {
  280. // first any empty directories that will not be implicitly added by any of the files
  281. $implicitDirs = array();
  282. foreach($this->files as $file) {
  283. $implicitDirs[] = dirname($file);
  284. }
  285. $incDirs = $ds->getIncludedDirectories();
  286. // we'll need to add to that list of implicit dirs any directories
  287. // that contain other *directories* (and not files), since otherwise
  288. // we get duplicate directories in the resulting tar
  289. foreach($incDirs as $dir) {
  290. foreach($incDirs as $dircheck) {
  291. if (!empty($dir) && $dir == dirname($dircheck)) {
  292. $implicitDirs[] = $dir;
  293. }
  294. }
  295. }
  296. $implicitDirs = array_unique($implicitDirs);
  297. // Now add any empty dirs (dirs not covered by the implicit dirs)
  298. // to the files array.
  299. foreach($incDirs as $dir) { // we cannot simply use array_diff() since we want to disregard empty/. dirs
  300. if ($dir != "" && $dir != "." && !in_array($dir, $implicitDirs)) {
  301. // it's an empty dir, so we'll add it.
  302. $this->files[] = $dir;
  303. }
  304. }
  305. } // if $includeEmpty
  306. } // if ($this->files===null)
  307. return $this->files;
  308. }
  309. /**
  310. * A 3 digit octal string, specify the user, group and
  311. * other modes in the standard Unix fashion;
  312. * optional, default=0644
  313. * @param string $octalString
  314. */
  315. public function setMode($octalString) {
  316. $octal = (int) $octalString;
  317. $this->mode = 0100000 | $octal;
  318. }
  319. public function getMode() {
  320. return $this->mode;
  321. }
  322. /**
  323. * The username for the tar entry
  324. * This is not the same as the UID, which is
  325. * not currently set by the task.
  326. */
  327. public function setUserName($userName) {
  328. $this->userName = $userName;
  329. }
  330. public function getUserName() {
  331. return $this->userName;
  332. }
  333. /**
  334. * The groupname for the tar entry; optional, default=""
  335. * This is not the same as the GID, which is
  336. * not currently set by the task.
  337. */
  338. public function setGroup($groupName) {
  339. $this->groupName = $groupName;
  340. }
  341. public function getGroup() {
  342. return $this->groupName;
  343. }
  344. /**
  345. * If the prefix attribute is set, all files in the fileset
  346. * are prefixed with that path in the archive.
  347. * optional.
  348. */
  349. public function setPrefix($prefix) {
  350. $this->prefix = $prefix;
  351. }
  352. public function getPrefix() {
  353. return $this->prefix;
  354. }
  355. /**
  356. * If the fullpath attribute is set, the file in the fileset
  357. * is written with that path in the archive. The prefix attribute,
  358. * if specified, is ignored. It is an error to have more than one file specified in
  359. * such a fileset.
  360. */
  361. public function setFullpath($fullpath) {
  362. $this->fullpath = $fullpath;
  363. }
  364. public function getFullpath() {
  365. return $this->fullpath;
  366. }
  367. /**
  368. * Flag to indicates whether leading `/'s should
  369. * be preserved in the file names.
  370. * Optional, default is <code>false</code>.
  371. * @return void
  372. */
  373. public function setPreserveLeadingSlashes($b) {
  374. $this->preserveLeadingSlashes = (boolean) $b;
  375. }
  376. public function getPreserveLeadingSlashes() {
  377. return $this->preserveLeadingSlashes;
  378. }
  379. }