Search in sources :

Example 1 with ArchiveResource

use of org.apache.tools.ant.types.resources.ArchiveResource in project ant by apache.

the class PermissionUtils method getPermissions.

/**
 * Sets permissions of a {@link Resource} - returns an empty set
 * for unsupported resource types or file systems that don't
 * support PosixFilePermissions and no fallback has been
 * provided..
 *
 * <p>Supported types are:</p>
 * <ul>
 *  <li>any {@link FileProvider}</li>
 *  <li>{@link ArchiveResource}</li>
 * </ul>
 *
 * @param r the resource to read permissions from
 * @param posixNotSupportedFallback optional fallback function to provide
 * permissions for file system that don't support
 * PosixFilePermissions. The Path corresponding to the file is
 * passed to the callback.
 * @return the permissions
 * @throws IOException if something goes wrong
 */
public static Set<PosixFilePermission> getPermissions(Resource r, Function<Path, Set<PosixFilePermission>> posixNotSupportedFallback) throws IOException {
    FileProvider f = r.as(FileProvider.class);
    if (f != null) {
        Path p = f.getFile().toPath();
        PosixFileAttributeView view = Files.getFileAttributeView(p, PosixFileAttributeView.class);
        if (view != null) {
            return view.readAttributes().permissions();
        } else if (posixNotSupportedFallback != null) {
            return posixNotSupportedFallback.apply(p);
        }
    } else if (r instanceof ArchiveResource) {
        return permissionsFromMode(((ArchiveResource) r).getMode());
    }
    return EnumSet.noneOf(PosixFilePermission.class);
}
Also used : Path(java.nio.file.Path) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 2 with ArchiveResource

use of org.apache.tools.ant.types.resources.ArchiveResource in project ant by apache.

the class PermissionUtils method setPermissions.

/**
 * Sets permissions on a {@link Resource} - doesn't do anything
 * for unsupported resource types.
 *
 * <p>Supported types are:</p>
 * <ul>
 *  <li>any {@link FileProvider}</li>
 *  <li>{@link ArchiveResource}</li>
 * </ul>
 *
 * @param r the resource to set permissions for
 * @param permissions the permissions
 * @param posixNotSupportedCallback optional callback that is
 * invoked for a file provider resource if the file-system holding
 * the file doesn't support PosixFilePermissions. The Path
 * corresponding to the file is passed to the callback.
 * @throws IOException if something goes wrong
 */
public static void setPermissions(Resource r, Set<PosixFilePermission> permissions, Consumer<Path> posixNotSupportedCallback) throws IOException {
    FileProvider f = r.as(FileProvider.class);
    if (f != null) {
        Path p = f.getFile().toPath();
        PosixFileAttributeView view = Files.getFileAttributeView(p, PosixFileAttributeView.class);
        if (view != null) {
            view.setPermissions(permissions);
        } else if (posixNotSupportedCallback != null) {
            posixNotSupportedCallback.accept(p);
        }
    } else if (r instanceof ArchiveResource) {
        ((ArchiveResource) r).setMode(modeFromPermissions(permissions, FileType.of(r)));
    }
}
Also used : Path(java.nio.file.Path) FileProvider(org.apache.tools.ant.types.resources.FileProvider) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 3 with ArchiveResource

use of org.apache.tools.ant.types.resources.ArchiveResource in project ant by apache.

the class Zip method getUnixMode.

/**
 * Determine a Resource's Unix mode or return the given default
 * value if not available.
 */
private int getUnixMode(final Resource r, final ZipFile zf, final int defaultMode) {
    int unixMode = defaultMode;
    if (zf != null) {
        final ZipEntry ze = zf.getEntry(r.getName());
        unixMode = ze.getUnixMode();
        if ((unixMode == 0 || unixMode == UnixStat.DIR_FLAG) && !preserve0Permissions) {
            unixMode = defaultMode;
        }
    } else if (r instanceof ArchiveResource) {
        unixMode = ((ArchiveResource) r).getMode();
    }
    return unixMode;
}
Also used : ZipEntry(org.apache.tools.zip.ZipEntry) ArchiveResource(org.apache.tools.ant.types.resources.ArchiveResource)

Example 4 with ArchiveResource

use of org.apache.tools.ant.types.resources.ArchiveResource 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

ArchiveResource (org.apache.tools.ant.types.resources.ArchiveResource)4 Path (java.nio.file.Path)2 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)2 FileProvider (org.apache.tools.ant.types.resources.FileProvider)2 InputStream (java.io.InputStream)1 BuildException (org.apache.tools.ant.BuildException)1 TarResource (org.apache.tools.ant.types.resources.TarResource)1 TarEntry (org.apache.tools.tar.TarEntry)1 ZipEntry (org.apache.tools.zip.ZipEntry)1