use of java.nio.file.attribute.PosixFilePermission in project alluxio by Alluxio.
the class FileUtils method getLocalFileMode.
/**
* Gets local file's permission mode.
*
* @param filePath the file path
* @return the file mode in short, e.g. 0777
* @throws IOException when fails to get the permission
*/
public static short getLocalFileMode(String filePath) throws IOException {
Set<PosixFilePermission> permission = Files.readAttributes(Paths.get(filePath), PosixFileAttributes.class).permissions();
// Translate posix file permissions to short mode.
int mode = 0;
for (PosixFilePermission action : PosixFilePermission.values()) {
mode = mode << 1;
mode += permission.contains(action) ? 1 : 0;
}
return (short) mode;
}
use of java.nio.file.attribute.PosixFilePermission in project ats-framework by Axway.
the class LocalFileSystemOperations method getPermissions.
private String getPermissions(String sourceFile) {
int ownerPermissions = 0;
int groupPermissions = 0;
int othersPermissions = 0;
try {
Path path = Paths.get(sourceFile);
PosixFileAttributes attr;
attr = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
Set<PosixFilePermission> filePermissions = attr.permissions();
if (filePermissions.contains(PosixFilePermission.OWNER_READ)) {
ownerPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.OWNER_WRITE)) {
ownerPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.OWNER_EXECUTE)) {
ownerPermissions += 1;
}
if (filePermissions.contains(PosixFilePermission.GROUP_READ)) {
groupPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.GROUP_WRITE)) {
groupPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.GROUP_EXECUTE)) {
groupPermissions += 1;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_READ)) {
othersPermissions += 4;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_WRITE)) {
othersPermissions += 2;
}
if (filePermissions.contains(PosixFilePermission.OTHERS_EXECUTE)) {
othersPermissions += 1;
}
} catch (IOException ioe) {
throw new FileSystemOperationException("Could not get permissions for file '" + sourceFile + "'", ioe);
}
return String.valueOf(ownerPermissions) + String.valueOf(groupPermissions) + String.valueOf(othersPermissions);
}
use of java.nio.file.attribute.PosixFilePermission in project ignite by apache.
the class LocalFileSystemUtils method posixAttributesToMap.
/**
* Convert POSIX attributes to property map.
*
* @param attrs Attributes view.
* @return IGFS properties map.
*/
public static Map<String, String> posixAttributesToMap(PosixFileAttributes attrs) {
if (attrs == null)
return null;
Map<String, String> props = U.newHashMap(3);
props.put(IgfsUtils.PROP_USER_NAME, attrs.owner().getName());
props.put(IgfsUtils.PROP_GROUP_NAME, attrs.group().getName());
int perm = 0;
for (PosixFilePermission p : attrs.permissions()) perm |= (1 << 8 - p.ordinal());
props.put(IgfsUtils.PROP_PERMISSION, '0' + Integer.toOctalString(perm));
return props;
}
use of java.nio.file.attribute.PosixFilePermission 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.PosixFilePermission in project jabref by JabRef.
the class FileSaveSession method commit.
@Override
public void commit(Path file) throws SaveException {
if (file == null) {
return;
}
if (backup && Files.exists(file)) {
Path backupFile = FileUtil.addExtension(file, BACKUP_EXTENSION);
FileUtil.copyFile(file, backupFile, true);
}
try {
// Always use a lock file
try {
if (FileBasedLock.createLockFile(file)) {
// Oops, the lock file already existed. Try to wait it out:
if (!FileBasedLock.waitForFileLock(file)) {
throw SaveException.FILE_LOCKED;
}
}
} catch (IOException ex) {
LOGGER.error("Error when creating lock file.", ex);
}
// Try to save file permissions to restore them later (by default: 664)
Set<PosixFilePermission> oldFilePermissions = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_READ);
if (FileUtil.isPosixCompilant && Files.exists(file)) {
try {
oldFilePermissions = Files.getPosixFilePermissions(file);
} catch (IOException exception) {
LOGGER.warn("Error getting file permissions.", exception);
}
}
FileUtil.copyFile(temporaryFile, file, true);
// Restore file permissions
if (FileUtil.isPosixCompilant) {
try {
Files.setPosixFilePermissions(file, oldFilePermissions);
} catch (IOException exception) {
throw new SaveException(exception);
}
}
} finally {
FileBasedLock.deleteLockFile(file);
}
try {
Files.deleteIfExists(temporaryFile);
} catch (IOException e) {
LOGGER.warn("Cannot delete temporary file", e);
}
}
Aggregations