📄 tar.java
字号:
/** * tar a file * @param file the file to tar * @param tOut the output stream * @param vPath the path name of the file to tar * @param tarFileSet the fileset that the file came from. * @throws IOException on error */ protected void tarFile(File file, TarOutputStream tOut, String vPath, TarFileSet tarFileSet) throws IOException { if (file.equals(tarFile)) { // If the archive is built for the first time and it is // matched by a resource collection, then it hasn't been // found in check (it hasn't been there) but will be // included now. // // for some strange reason the old code would simply skip // the entry and not fail, do the same now for backwards // compatibility reasons. Without this, the which4j build // fails in Gump return; } tarResource(new FileResource(file), tOut, vPath, tarFileSet); } /** * tar a resource * @param r the resource to tar * @param tOut the output stream * @param vPath the path name of the file to tar * @param tarFileSet the fileset that the file came from, may be null. * @throws IOException on error * @since Ant 1.7 */ protected void tarResource(Resource r, TarOutputStream tOut, String vPath, TarFileSet tarFileSet) throws IOException { if (!r.isExists()) { return; } if (tarFileSet != null) { String fullpath = tarFileSet.getFullpath(this.getProject()); if (fullpath.length() > 0) { vPath = fullpath; } else { // don't add "" to the archive if (vPath.length() <= 0) { return; } String prefix = tarFileSet.getPrefix(this.getProject()); // '/' is appended for compatibility with the zip task. if (prefix.length() > 0 && !prefix.endsWith("/")) { prefix = prefix + "/"; } vPath = prefix + vPath; } if (vPath.startsWith("/") && !tarFileSet.getPreserveLeadingSlashes()) { int l = vPath.length(); if (l <= 1) { // we would end up adding "" to the archive return; } vPath = vPath.substring(1, l); } } if (r.isDirectory() && !vPath.endsWith("/")) { vPath += "/"; } if (vPath.length() >= TarConstants.NAMELEN) { if (longFileMode.isOmitMode()) { log("Omitting: " + vPath, Project.MSG_INFO); return; } else if (longFileMode.isWarnMode()) { log("Entry: " + vPath + " longer than " + TarConstants.NAMELEN + " characters.", Project.MSG_WARN); if (!longWarningGiven) { log("Resulting tar file can only be processed " + "successfully by GNU compatible tar commands", Project.MSG_WARN); longWarningGiven = true; } } else if (longFileMode.isFailMode()) { throw new BuildException("Entry: " + vPath + " longer than " + TarConstants.NAMELEN + "characters.", getLocation()); } } TarEntry te = new TarEntry(vPath); te.setModTime(r.getLastModified()); // preserve permissions if (r instanceof ArchiveResource) { ArchiveResource ar = (ArchiveResource) r; te.setMode(ar.getMode()); if (r instanceof TarResource) { TarResource tr = (TarResource) r; te.setUserName(tr.getUserName()); te.setUserId(tr.getUid()); te.setGroupName(tr.getGroup()); te.setGroupId(tr.getGid()); } } if (!r.isDirectory()) { if (r.size() > TarConstants.MAXSIZE) { throw new BuildException( "Resource: " + r + " larger than " + TarConstants.MAXSIZE + " bytes."); } te.setSize(r.getSize()); // override permissions if set explicitly if (tarFileSet != null && tarFileSet.hasFileModeBeenSet()) { te.setMode(tarFileSet.getMode()); } } else if (tarFileSet != null && tarFileSet.hasDirModeBeenSet()) { // override permissions if set explicitly te.setMode(tarFileSet.getDirMode(this.getProject())); } if (tarFileSet != null) { // only override permissions if set explicitly if (tarFileSet.hasUserNameBeenSet()) { te.setUserName(tarFileSet.getUserName()); } if (tarFileSet.hasGroupBeenSet()) { te.setGroupName(tarFileSet.getGroup()); } if (tarFileSet.hasUserIdBeenSet()) { te.setUserId(tarFileSet.getUid()); } if (tarFileSet.hasGroupIdBeenSet()) { te.setGroupId(tarFileSet.getGid()); } } InputStream in = null; try { tOut.putNextEntry(te); if (!r.isDirectory()) { in = r.getInputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; do { tOut.write(buffer, 0, count); count = in.read(buffer, 0, buffer.length); } while (count != -1); } tOut.closeEntry(); } finally { FileUtils.close(in); } } /** * Is the archive up to date in relationship to a list of files. * @param files the files to check * @return true if the archive is up to date. * @deprecated since 1.5.x. * use the two-arg version instead. */ protected boolean archiveIsUpToDate(String[] files) { return archiveIsUpToDate(files, baseDir); } /** * Is the archive up to date in relationship to a list of files. * @param files the files to check * @param dir the base directory for the files. * @return true if the archive is up to date. * @since Ant 1.5.2 */ protected boolean archiveIsUpToDate(String[] files, File dir) { SourceFileScanner sfs = new SourceFileScanner(this); MergingMapper mm = new MergingMapper(); mm.setTo(tarFile.getAbsolutePath()); return sfs.restrict(files, dir, null, mm).length == 0; } /** * Is the archive up to date in relationship to a list of files. * @param r the files to check * @return true if the archive is up to date. * @since Ant 1.7 */ protected boolean archiveIsUpToDate(Resource r) { return SelectorUtils.isOutOfDate(new FileResource(tarFile), r, FileUtils.getFileUtils() .getFileTimestampGranularity()); } /** * Whether this task can deal with non-file resources. * * <p>This implementation returns true only if this task is * <tar>. Any subclass of this class that also wants to * support non-file resources needs to override this method. We * need to do so for backwards compatibility reasons since we * can't expect subclasses to support resources.</p> * @return true for this task. * @since Ant 1.7 */ protected boolean supportsNonFileResources() { return getClass().equals(Tar.class); } /** * Checks whether the archive is out-of-date with respect to the resources * of the given collection. * * <p>Also checks that either all collections only contain file * resources or this class supports non-file collections.</p> * * <p>And - in case of file-collections - ensures that the archive won't * contain itself.</p> * * @param rc the resource collection to check * @return whether the archive is up-to-date * @since Ant 1.7 */ protected boolean check(ResourceCollection rc) { boolean upToDate = true; if (isFileFileSet(rc)) { FileSet fs = (FileSet) rc; upToDate = check(fs.getDir(getProject()), getFileNames(fs)); } else if (!rc.isFilesystemOnly() && !supportsNonFileResources()) { throw new BuildException("only filesystem resources are supported"); } else if (rc.isFilesystemOnly()) { HashSet basedirs = new HashSet(); HashMap basedirToFilesMap = new HashMap(); Iterator iter = rc.iterator(); while (iter.hasNext()) { FileResource r = (FileResource) iter.next(); File base = r.getBaseDir(); if (base == null) { base = Copy.NULL_FILE_PLACEHOLDER; } basedirs.add(base); Vector files = (Vector) basedirToFilesMap.get(base); if (files == null) { files = new Vector(); basedirToFilesMap.put(base, new Vector()); } files.add(r.getName()); } iter = basedirs.iterator(); while (iter.hasNext()) { File base = (File) iter.next(); Vector f = (Vector) basedirToFilesMap.get(base); String[] files = (String[]) f.toArray(new String[f.size()]); upToDate &= check(base == Copy.NULL_FILE_PLACEHOLDER ? null : base, files); } } else { // non-file resources Iterator iter = rc.iterator(); while (upToDate && iter.hasNext()) { Resource r = (Resource) iter.next(); upToDate = archiveIsUpToDate(r); } } return upToDate; } /** * Checks whether the archive is out-of-date with respect to the * given files, ensures that the archive won't contain itself.</p> * * @param basedir base directory for file names * @param files array of relative file names * @return whether the archive is up-to-date * @since Ant 1.7 */ protected boolean check(File basedir, String[] files) { boolean upToDate = true; if (!archiveIsUpToDate(files, basedir)) { upToDate = false; } for (int i = 0; i < files.length; ++i) { if (tarFile.equals(new File(basedir, files[i]))) { throw new BuildException("A tar file cannot include " + "itself", getLocation()); } } return upToDate; } /** * Adds the resources contained in this collection to the archive. * * <p>Uses the file based methods for file resources for backwards * compatibility.</p> * * @param rc the collection containing resources to add * @param tOut stream writing to the archive. * @throws IOException on error. * @since Ant 1.7 */ protected void tar(ResourceCollection rc, TarOutputStream tOut) throws IOException { ArchiveFileSet afs = null; if (rc instanceof ArchiveFileSet) { afs = (ArchiveFileSet) rc; } if (afs != null && afs.size() > 1 && afs.getFullpath(this.getProject()).length() > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -