use of java.nio.file.attribute.UserPrincipalLookupService in project ignite by apache.
the class LocalFileSystemUtils method updateProperties.
/**
* Update file properties.
*
* @param file File.
* @param grp Group.
* @param perm Permissions.
*/
public static void updateProperties(File file, String grp, String perm) {
PosixFileAttributeView attrs = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
if (attrs == null)
throw new UnsupportedOperationException("Posix file attributes not available");
if (grp != null) {
try {
UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
GroupPrincipal grp0 = lookupService.lookupPrincipalByGroupName(grp);
attrs.setGroup(grp0);
} catch (IOException e) {
throw new IgfsException("Update the '" + IgfsUtils.PROP_GROUP_NAME + "' property is failed.", e);
}
}
if (perm != null) {
int perm0 = Integer.parseInt(perm, 8);
Set<PosixFilePermission> permSet = new HashSet<>(9);
for (int i = 0; i < LocalFileSystemUtils.POSIX_PERMISSIONS.length; ++i) {
if ((perm0 & (1 << i)) != 0)
permSet.add(LocalFileSystemUtils.POSIX_PERMISSIONS[i]);
}
try {
attrs.setPermissions(permSet);
} catch (IOException e) {
throw new IgfsException("Update the '" + IgfsUtils.PROP_PERMISSION + "' property is failed.", e);
}
}
}
use of java.nio.file.attribute.UserPrincipalLookupService in project nifi by apache.
the class PutFile method onTrigger.
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
FlowFile flowFile = session.get();
if (flowFile == null) {
return;
}
final StopWatch stopWatch = new StopWatch(true);
final Path configuredRootDirPath = Paths.get(context.getProperty(DIRECTORY).evaluateAttributeExpressions(flowFile).getValue());
final String conflictResponse = context.getProperty(CONFLICT_RESOLUTION).getValue();
final Integer maxDestinationFiles = context.getProperty(MAX_DESTINATION_FILES).asInteger();
final ComponentLog logger = getLogger();
Path tempDotCopyFile = null;
try {
final Path rootDirPath = configuredRootDirPath;
final Path tempCopyFile = rootDirPath.resolve("." + flowFile.getAttribute(CoreAttributes.FILENAME.key()));
final Path copyFile = rootDirPath.resolve(flowFile.getAttribute(CoreAttributes.FILENAME.key()));
if (!Files.exists(rootDirPath)) {
if (context.getProperty(CREATE_DIRS).asBoolean()) {
Files.createDirectories(rootDirPath);
} else {
flowFile = session.penalize(flowFile);
session.transfer(flowFile, REL_FAILURE);
logger.error("Penalizing {} and routing to 'failure' because the output directory {} does not exist and Processor is " + "configured not to create missing directories", new Object[] { flowFile, rootDirPath });
return;
}
}
final Path dotCopyFile = tempCopyFile;
tempDotCopyFile = dotCopyFile;
Path finalCopyFile = copyFile;
final Path finalCopyFileDir = finalCopyFile.getParent();
if (Files.exists(finalCopyFileDir) && maxDestinationFiles != null) {
// check if too many files already
final int numFiles = finalCopyFileDir.toFile().list().length;
if (numFiles >= maxDestinationFiles) {
flowFile = session.penalize(flowFile);
logger.warn("Penalizing {} and routing to 'failure' because the output directory {} has {} files, which exceeds the " + "configured maximum number of files", new Object[] { flowFile, finalCopyFileDir, numFiles });
session.transfer(flowFile, REL_FAILURE);
return;
}
}
if (Files.exists(finalCopyFile)) {
switch(conflictResponse) {
case REPLACE_RESOLUTION:
Files.delete(finalCopyFile);
logger.info("Deleted {} as configured in order to replace with the contents of {}", new Object[] { finalCopyFile, flowFile });
break;
case IGNORE_RESOLUTION:
session.transfer(flowFile, REL_SUCCESS);
logger.info("Transferring {} to success because file with same name already exists", new Object[] { flowFile });
return;
case FAIL_RESOLUTION:
flowFile = session.penalize(flowFile);
logger.warn("Penalizing {} and routing to failure as configured because file with the same name already exists", new Object[] { flowFile });
session.transfer(flowFile, REL_FAILURE);
return;
default:
break;
}
}
session.exportTo(flowFile, dotCopyFile, false);
final String lastModifiedTime = context.getProperty(CHANGE_LAST_MODIFIED_TIME).evaluateAttributeExpressions(flowFile).getValue();
if (lastModifiedTime != null && !lastModifiedTime.trim().isEmpty()) {
try {
final DateFormat formatter = new SimpleDateFormat(FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US);
final Date fileModifyTime = formatter.parse(lastModifiedTime);
dotCopyFile.toFile().setLastModified(fileModifyTime.getTime());
} catch (Exception e) {
logger.warn("Could not set file lastModifiedTime to {} because {}", new Object[] { lastModifiedTime, e });
}
}
final String permissions = context.getProperty(CHANGE_PERMISSIONS).evaluateAttributeExpressions(flowFile).getValue();
if (permissions != null && !permissions.trim().isEmpty()) {
try {
String perms = stringPermissions(permissions);
if (!perms.isEmpty()) {
Files.setPosixFilePermissions(dotCopyFile, PosixFilePermissions.fromString(perms));
}
} catch (Exception e) {
logger.warn("Could not set file permissions to {} because {}", new Object[] { permissions, e });
}
}
final String owner = context.getProperty(CHANGE_OWNER).evaluateAttributeExpressions(flowFile).getValue();
if (owner != null && !owner.trim().isEmpty()) {
try {
UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService();
Files.setOwner(dotCopyFile, lookupService.lookupPrincipalByName(owner));
} catch (Exception e) {
logger.warn("Could not set file owner to {} because {}", new Object[] { owner, e });
}
}
final String group = context.getProperty(CHANGE_GROUP).evaluateAttributeExpressions(flowFile).getValue();
if (group != null && !group.trim().isEmpty()) {
try {
UserPrincipalLookupService lookupService = dotCopyFile.getFileSystem().getUserPrincipalLookupService();
PosixFileAttributeView view = Files.getFileAttributeView(dotCopyFile, PosixFileAttributeView.class);
view.setGroup(lookupService.lookupPrincipalByGroupName(group));
} catch (Exception e) {
logger.warn("Could not set file group to {} because {}", new Object[] { group, e });
}
}
boolean renamed = false;
for (int i = 0; i < 10; i++) {
// try rename up to 10 times.
if (dotCopyFile.toFile().renameTo(finalCopyFile.toFile())) {
renamed = true;
// rename was successful
break;
}
// try waiting a few ms to let whatever might cause rename failure to resolve
Thread.sleep(100L);
}
if (!renamed) {
if (Files.exists(dotCopyFile) && dotCopyFile.toFile().delete()) {
logger.debug("Deleted dot copy file {}", new Object[] { dotCopyFile });
}
throw new ProcessException("Could not rename: " + dotCopyFile);
} else {
logger.info("Produced copy of {} at location {}", new Object[] { flowFile, finalCopyFile });
}
session.getProvenanceReporter().send(flowFile, finalCopyFile.toFile().toURI().toString(), stopWatch.getElapsed(TimeUnit.MILLISECONDS));
session.transfer(flowFile, REL_SUCCESS);
} catch (final Throwable t) {
if (tempDotCopyFile != null) {
try {
Files.deleteIfExists(tempDotCopyFile);
} catch (final Exception e) {
logger.error("Unable to remove temporary file {} due to {}", new Object[] { tempDotCopyFile, e });
}
}
flowFile = session.penalize(flowFile);
logger.error("Penalizing {} and transferring to failure due to {}", new Object[] { flowFile, t });
session.transfer(flowFile, REL_FAILURE);
}
}
use of java.nio.file.attribute.UserPrincipalLookupService in project coprhd-controller by CoprHD.
the class Main method changeLogFileOwner.
private static void changeLogFileOwner() throws Exception {
File f = new File(LOG_FILE_PATH);
if (f.exists()) {
PosixFileAttributeView dbutilsLogFile = Files.getFileAttributeView(f.toPath(), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
UserPrincipal storageosUser = lookupService.lookupPrincipalByName(STORAGEOS_USER);
GroupPrincipal storageosGroup = lookupService.lookupPrincipalByGroupName(STORAGEOS_GROUP);
dbutilsLogFile.setOwner(storageosUser);
dbutilsLogFile.setGroup(storageosGroup);
}
}
use of java.nio.file.attribute.UserPrincipalLookupService in project vert.x by eclipse.
the class FileSystemImpl method chownInternal.
protected BlockingAction<Void> chownInternal(String path, String user, String group) {
Objects.requireNonNull(path);
return new BlockingAction<Void>() {
public Void perform() {
try {
Path target = vertx.resolveFile(path).toPath();
UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService();
UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user);
GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group);
if (groupPrincipal != null) {
PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (view == null) {
throw new FileSystemException("Change group of file not supported");
}
view.setGroup(groupPrincipal);
}
if (userPrincipal != null) {
Files.setOwner(target, userPrincipal);
}
} catch (SecurityException e) {
throw new FileSystemException("Accessed denied for chown on " + path);
} catch (IOException e) {
throw new FileSystemException(getFileAccessErrorMessage("crown", path), e);
}
return null;
}
};
}
use of java.nio.file.attribute.UserPrincipalLookupService in project vert.x by eclipse.
the class FileSystemImpl method chownInternal.
protected BlockingAction<Void> chownInternal(String path, String user, String group, Handler<AsyncResult<Void>> handler) {
Objects.requireNonNull(path);
return new BlockingAction<Void>(handler) {
public Void perform() {
try {
Path target = vertx.resolveFile(path).toPath();
UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService();
UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user);
GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group);
if (groupPrincipal != null) {
PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
if (view == null) {
throw new FileSystemException("Change group of file not supported");
}
view.setGroup(groupPrincipal);
}
if (userPrincipal != null) {
Files.setOwner(target, userPrincipal);
}
} catch (SecurityException e) {
throw new FileSystemException("Accessed denied for chown on " + path);
} catch (IOException e) {
throw new FileSystemException(e);
}
return null;
}
};
}
Aggregations