All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.tools.ant.util.PermissionUtils Maven / Gradle / Ivy

There is a newer version: 1.10.15
Show newest version
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package org.apache.tools.ant.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFilePermission;
import java.util.EnumSet;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;

import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.ArchiveResource;
import org.apache.tools.ant.types.resources.FileProvider;

/**
 * Contains helper methods for dealing with {@link
 * PosixFilePermission} or the traditional Unix mode representation of
 * permissions.
 *
 * @since Ant 1.10.0
 */
public class PermissionUtils {

    private PermissionUtils() { }

    /**
     * Translates a set of permissions into a Unix stat(2) {@code
     * st_mode} result.
     * @param permissions the permissions
     * @param type the file type
     * @return the "mode"
     */
    public static int modeFromPermissions(Set permissions,
                                          FileType type) {
        int mode;
        switch (type) {
        case SYMLINK:
            mode = 012;
            break;
        case REGULAR_FILE:
            mode = 010;
            break;
        case DIR:
            mode = 004;
            break;
        default:
            // OTHER could be a character or block device, a socket or a FIFO - so don't set anything
            mode = 0;
            break;
        }
        mode <<= 3;
        mode <<= 3; // we don't support sticky, setuid, setgid
        mode |= modeFromPermissions(permissions, "OWNER");
        mode <<= 3;
        mode |= modeFromPermissions(permissions, "GROUP");
        mode <<= 3;
        mode |= modeFromPermissions(permissions, "OTHERS");
        return mode;
    }

    /**
     * Translates a Unix stat(2) {@code st_mode} compatible value into
     * a set of permissions.
     * @param mode the "mode"
     * @return set of permissions
     */
    public static Set permissionsFromMode(int mode) {
        Set permissions = EnumSet.noneOf(PosixFilePermission.class);
        addPermissions(permissions, "OTHERS", mode);
        addPermissions(permissions, "GROUP", mode >> 3);
        addPermissions(permissions, "OWNER", mode >> 6);
        return permissions;
    }

    /**
     * Sets permissions on a {@link Resource} - doesn't do anything
     * for unsupported resource types.
     *
     * 

Supported types are:

*
    *
  • any {@link FileProvider}
  • *
  • {@link ArchiveResource}
  • *
* * @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 permissions, Consumer 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))); } } /** * 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.. * *

Supported types are:

*
    *
  • any {@link FileProvider}
  • *
  • {@link ArchiveResource}
  • *
* * @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 getPermissions(Resource r, Function> 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); } private static long modeFromPermissions(Set permissions, String prefix) { long mode = 0; if (permissions.contains(PosixFilePermission.valueOf(prefix + "_READ"))) { mode |= 4; } if (permissions.contains(PosixFilePermission.valueOf(prefix + "_WRITE"))) { mode |= 2; } if (permissions.contains(PosixFilePermission.valueOf(prefix + "_EXECUTE"))) { mode |= 1; } return mode; } private static void addPermissions(Set permissions, String prefix, long mode) { if ((mode & 1) == 1) { permissions.add(PosixFilePermission.valueOf(prefix + "_EXECUTE")); } if ((mode & 2) == 2) { permissions.add(PosixFilePermission.valueOf(prefix + "_WRITE")); } if ((mode & 4) == 4) { permissions.add(PosixFilePermission.valueOf(prefix + "_READ")); } } /** * The supported types of files, maps to the {@code isFoo} methods * in {@link java.nio.file.attribute.BasicFileAttributes}. */ public enum FileType { /** A regular file. */ REGULAR_FILE, /** A directory. */ DIR, /** A symbolic link. */ SYMLINK, /** Something that is neither a regular file nor a directory nor a symbolic link. */ OTHER; /** * Determines the file type of a {@link Path}. * * @param p Path * @return FileType * @throws IOException if file attributes cannot be read */ public static FileType of(Path p) throws IOException { BasicFileAttributes attrs = Files.readAttributes(p, BasicFileAttributes.class); if (attrs.isRegularFile()) { return FileType.REGULAR_FILE; } else if (attrs.isDirectory()) { return FileType.DIR; } else if (attrs.isSymbolicLink()) { return FileType.SYMLINK; } return FileType.OTHER; } /** * Determines the file type of a {@link Resource}. * * @param r Resource * @return FileType */ public static FileType of(Resource r) { if (r.isDirectory()) { return FileType.DIR; } return FileType.REGULAR_FILE; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy