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);
}
}
Aggregations