use of org.apache.tools.ant.types.resources.TarResource in project ant by apache.
the class PermissionUtilsTest method getSetPermissionsWorksForTarResources.
@Test
public void getSetPermissionsWorksForTarResources() throws IOException {
File f = File.createTempFile("ant", ".zip");
f.deleteOnExit();
try (TarOutputStream os = new TarOutputStream(new FileOutputStream(f))) {
TarEntry e = new TarEntry("foo");
os.putNextEntry(e);
os.closeEntry();
}
TarResource r = new TarResource();
r.setName("foo");
r.setArchive(f);
Set<PosixFilePermission> s = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ);
PermissionUtils.setPermissions(r, s, null);
assertEquals(s, PermissionUtils.getPermissions(r, null));
}
use of org.apache.tools.ant.types.resources.TarResource in project ant by apache.
the class TarScanner method fillMapsFromArchive.
/**
* Fills the file and directory maps with resources read from the
* archive.
*
* @param src the archive to scan.
* @param encoding encoding used to encode file names inside the archive.
* @param fileEntries Map (name to resource) of non-directory
* resources found inside the archive.
* @param matchFileEntries Map (name to resource) of non-directory
* resources found inside the archive that matched all include
* patterns and didn't match any exclude patterns.
* @param dirEntries Map (name to resource) of directory
* resources found inside the archive.
* @param matchDirEntries Map (name to resource) of directory
* resources found inside the archive that matched all include
* patterns and didn't match any exclude patterns.
*/
protected void fillMapsFromArchive(Resource src, String encoding, Map<String, Resource> fileEntries, Map<String, Resource> matchFileEntries, Map<String, Resource> dirEntries, Map<String, Resource> matchDirEntries) {
try (TarInputStream ti = new TarInputStream(src.getInputStream(), encoding)) {
try {
TarEntry entry = null;
while ((entry = ti.getNextEntry()) != null) {
Resource r = new TarResource(src, entry);
String name = entry.getName();
if (entry.isDirectory()) {
name = trimSeparator(name);
dirEntries.put(name, r);
if (match(name)) {
matchDirEntries.put(name, r);
}
} else {
fileEntries.put(name, r);
if (match(name)) {
matchFileEntries.put(name, r);
}
}
}
} catch (IOException ex) {
throw new BuildException("problem reading " + srcFile, ex);
}
} catch (IOException ex) {
throw new BuildException("problem opening " + srcFile, ex);
}
}
use of org.apache.tools.ant.types.resources.TarResource in project ant by apache.
the class Tar method tar.
/**
* 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(final ResourceCollection rc, final TarOutputStream tOut) throws IOException {
ArchiveFileSet afs = null;
if (rc instanceof ArchiveFileSet) {
afs = (ArchiveFileSet) rc;
}
if (afs != null && afs.size() > 1 && !afs.getFullpath(this.getProject()).isEmpty()) {
throw new BuildException("fullpath attribute may only be specified for filesets that specify a single file.");
}
final TarFileSet tfs = asTarFileSet(afs);
if (isFileFileSet(rc)) {
final FileSet fs = (FileSet) rc;
for (String file : getFileNames(fs)) {
final File f = new File(fs.getDir(getProject()), file);
final String name = file.replace(File.separatorChar, '/');
tarFile(f, tOut, name, tfs);
}
} else if (rc.isFilesystemOnly()) {
for (final Resource r : rc) {
final File f = r.as(FileProvider.class).getFile();
tarFile(f, tOut, f.getName(), tfs);
}
} else {
// non-file resources
for (final Resource r : rc) {
tarResource(r, tOut, r.getName(), tfs);
}
}
}
use of org.apache.tools.ant.types.resources.TarResource in project ant by apache.
the class Tar method tarResource.
/**
* 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(final Resource r, final TarOutputStream tOut, String vPath, final TarFileSet tarFileSet) throws IOException {
if (!r.isExists()) {
return;
}
boolean preserveLeadingSlashes = false;
if (tarFileSet != null) {
final 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;
}
preserveLeadingSlashes = tarFileSet.getPreserveLeadingSlashes();
if (vPath.startsWith("/") && !preserveLeadingSlashes) {
final 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());
}
}
final TarEntry te = new TarEntry(vPath, preserveLeadingSlashes);
te.setModTime(r.getLastModified());
// preserve permissions
if (r instanceof ArchiveResource) {
final ArchiveResource ar = (ArchiveResource) r;
te.setMode(ar.getMode());
if (r instanceof TarResource) {
final 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();
final 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);
}
}
Aggregations