Search in sources :

Example 1 with TarResource

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));
}
Also used : TarOutputStream(org.apache.tools.tar.TarOutputStream) FileOutputStream(java.io.FileOutputStream) TarEntry(org.apache.tools.tar.TarEntry) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) TarResource(org.apache.tools.ant.types.resources.TarResource) Test(org.junit.Test)

Example 2 with TarResource

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);
    }
}
Also used : TarInputStream(org.apache.tools.tar.TarInputStream) TarResource(org.apache.tools.ant.types.resources.TarResource) TarEntry(org.apache.tools.tar.TarEntry) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) TarResource(org.apache.tools.ant.types.resources.TarResource)

Example 3 with TarResource

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);
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) FileResource(org.apache.tools.ant.types.resources.FileResource) TarResource(org.apache.tools.ant.types.resources.TarResource) Resource(org.apache.tools.ant.types.Resource) BuildException(org.apache.tools.ant.BuildException) ArchiveFileSet(org.apache.tools.ant.types.ArchiveFileSet) File(java.io.File)

Example 4 with TarResource

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);
    }
}
Also used : InputStream(java.io.InputStream) TarEntry(org.apache.tools.tar.TarEntry) BuildException(org.apache.tools.ant.BuildException) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) TarResource(org.apache.tools.ant.types.resources.TarResource)

Aggregations

TarResource (org.apache.tools.ant.types.resources.TarResource)4 BuildException (org.apache.tools.ant.BuildException)3 TarEntry (org.apache.tools.tar.TarEntry)3 File (java.io.File)2 ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 ArchiveFileSet (org.apache.tools.ant.types.ArchiveFileSet)1 FileSet (org.apache.tools.ant.types.FileSet)1 Resource (org.apache.tools.ant.types.Resource)1 FileResource (org.apache.tools.ant.types.resources.FileResource)1 TarInputStream (org.apache.tools.tar.TarInputStream)1 TarOutputStream (org.apache.tools.tar.TarOutputStream)1 Test (org.junit.Test)1