Search in sources :

Example 1 with CrashableFunction

use of com.aws.greengrass.util.CrashableFunction in project aws-greengrass-nucleus by aws-greengrass.

the class Platform method setPermissions.

/**
 * Set permission on a path.
 *
 * @param permission permissions to set
 * @param path path to apply to
 * @param options options for how to apply the permission to the path
 * @throws IOException if any exception occurs while changing permissions
 */
protected void setPermissions(FileSystemPermission permission, Path path, EnumSet<Option> options) throws IOException {
    // noop function that does not set owner
    CrashableFunction<Path, Void, IOException> setOwner = (p) -> null;
    if (options.contains(Option.SetOwner)) {
        if (Utils.isEmpty(permission.getOwnerUser())) {
            logger.atTrace().setEventType(SET_PERMISSIONS_EVENT).kv(PATH_LOG_KEY, path).log("No owner to set for path");
        } else {
            UserPrincipalLookupService lookupService = path.getFileSystem().getUserPrincipalLookupService();
            UserPrincipal userPrincipal = this.lookupUserByName(path, permission.getOwnerUser());
            GroupPrincipal groupPrincipal = Utils.isEmpty(permission.getOwnerGroup()) ? null : lookupService.lookupPrincipalByGroupName(permission.getOwnerGroup());
            setOwner = (p) -> {
                this.setOwner(userPrincipal, groupPrincipal, p);
                return null;
            };
        }
    }
    // noop function that does not change the file mode
    CrashableFunction<Path, Void, IOException> setMode = (p) -> null;
    if (options.contains(Option.SetMode)) {
        FileSystemPermissionView view = getFileSystemPermissionView(permission, path);
        setMode = (p) -> {
            this.setMode(view, p);
            return null;
        };
    }
    final CrashableFunction<Path, Void, IOException> setModeFunc = setMode;
    final CrashableFunction<Path, Void, IOException> setOwnerFunc = setOwner;
    if (options.contains(Option.Recurse)) {
        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                setModeFunc.apply(dir);
                setOwnerFunc.apply(dir);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                setModeFunc.apply(file);
                setOwnerFunc.apply(file);
                return FileVisitResult.CONTINUE;
            }
        });
    } else {
        setModeFunc.apply(path);
        setOwnerFunc.apply(path);
    }
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) OS_LINUX(com.aws.greengrass.config.PlatformResolver.OS_LINUX) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) Option(com.aws.greengrass.util.FileSystemPermission.Option) LinuxPlatform(com.aws.greengrass.util.platforms.unix.linux.LinuxPlatform) UserPrincipal(java.nio.file.attribute.UserPrincipal) LogManager(com.aws.greengrass.logging.impl.LogManager) Path(java.nio.file.Path) PlatformResolver(com.aws.greengrass.config.PlatformResolver) EnumSet(java.util.EnumSet) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) FileSystemPermission(com.aws.greengrass.util.FileSystemPermission) Exec(com.aws.greengrass.util.Exec) Files(java.nio.file.Files) DarwinPlatform(com.aws.greengrass.util.platforms.unix.DarwinPlatform) QNXPlatform(com.aws.greengrass.util.platforms.unix.QNXPlatform) Set(java.util.Set) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) CrashableFunction(com.aws.greengrass.util.CrashableFunction) UnixPlatform(com.aws.greengrass.util.platforms.unix.UnixPlatform) FileVisitResult(java.nio.file.FileVisitResult) Utils(com.aws.greengrass.util.Utils) OS_DARWIN(com.aws.greengrass.config.PlatformResolver.OS_DARWIN) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) WindowsPlatform(com.aws.greengrass.util.platforms.windows.WindowsPlatform) Logger(com.aws.greengrass.logging.api.Logger) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) UserPrincipal(java.nio.file.attribute.UserPrincipal) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

PlatformResolver (com.aws.greengrass.config.PlatformResolver)1 OS_DARWIN (com.aws.greengrass.config.PlatformResolver.OS_DARWIN)1 OS_LINUX (com.aws.greengrass.config.PlatformResolver.OS_LINUX)1 Logger (com.aws.greengrass.logging.api.Logger)1 LogManager (com.aws.greengrass.logging.impl.LogManager)1 CrashableFunction (com.aws.greengrass.util.CrashableFunction)1 Exec (com.aws.greengrass.util.Exec)1 FileSystemPermission (com.aws.greengrass.util.FileSystemPermission)1 Option (com.aws.greengrass.util.FileSystemPermission.Option)1 Utils (com.aws.greengrass.util.Utils)1 DarwinPlatform (com.aws.greengrass.util.platforms.unix.DarwinPlatform)1 QNXPlatform (com.aws.greengrass.util.platforms.unix.QNXPlatform)1 UnixPlatform (com.aws.greengrass.util.platforms.unix.UnixPlatform)1 LinuxPlatform (com.aws.greengrass.util.platforms.unix.linux.LinuxPlatform)1 WindowsPlatform (com.aws.greengrass.util.platforms.windows.WindowsPlatform)1 IOException (java.io.IOException)1 FileVisitResult (java.nio.file.FileVisitResult)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)1