use of java.nio.file.attribute.PosixFilePermission in project copybara by google.
the class FileUtil method addPermissions.
/**
* Tries to add the Posix permissions if the file belongs to a Posix filesystem. This is an
* addition, which means that no permissions are removed.
*
* <p>For Windows type filesystems, it uses setReadable/setWritable/setExecutable, which is only
* supported for the owner, and ignores the rest of permissions.
*/
public static void addPermissions(Path path, Set<PosixFilePermission> permissionsToAdd) throws IOException {
if (path.getFileSystem().supportedFileAttributeViews().contains("posix")) {
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(path);
permissions.addAll(permissionsToAdd);
Files.setPosixFilePermissions(path, permissions);
} else {
File file = path.toFile();
if (permissionsToAdd.contains(PosixFilePermission.OWNER_READ)) {
if (!file.setReadable(true)) {
throw new IOException("Could not set 'readable' permission for file: " + path);
}
}
if (permissionsToAdd.contains(PosixFilePermission.OWNER_WRITE)) {
if (!file.setWritable(true)) {
throw new IOException("Could not set 'writable' permission for file: " + path);
}
}
if (permissionsToAdd.contains(PosixFilePermission.OWNER_EXECUTE)) {
if (!file.setExecutable(true)) {
throw new IOException("Could not set 'executable' permission for file: " + path);
}
}
}
}
use of java.nio.file.attribute.PosixFilePermission in project spring-integration by spring-projects.
the class FileWritingMessageHandler method setChmod.
/**
* Set the file permissions after uploading, e.g. 0600 for
* owner read/write. Only applies to file systems that support posix
* file permissions.
* @param chmod the permissions.
* @throws IllegalArgumentException if the value is higher than 0777.
* @since 5.0
*/
public void setChmod(int chmod) {
Assert.isTrue(chmod >= 0 && chmod <= 0777, "'chmod' must be between 0 and 0777 (octal)");
if (!FileUtils.IS_POSIX) {
this.logger.error("'chmod' setting ignored - the file system does not support Posix attributes");
return;
}
/*
* Bitset.valueOf(byte[]) takes a little-endian array of bytes to create a BitSet.
* Since we are interested in 9 bits, we construct an array with the low-order byte
* (bits 0-7) followed by the second order byte (bit 8).
* BitSet.stream() returns a stream of ints representing those bits that are set.
* We use that stream with a switch to create the set of PosixFilePermissions
* representing the bits that were set in the chmod value.
*/
BitSet bits = BitSet.valueOf(new byte[] { (byte) chmod, (byte) (chmod >> 8) });
final Set<PosixFilePermission> permissions = new HashSet<>();
bits.stream().forEach(b -> {
switch(b) {
case 0:
permissions.add(PosixFilePermission.OTHERS_EXECUTE);
break;
case 1:
permissions.add(PosixFilePermission.OTHERS_WRITE);
break;
case 2:
permissions.add(PosixFilePermission.OTHERS_READ);
break;
case 3:
permissions.add(PosixFilePermission.GROUP_EXECUTE);
break;
case 4:
permissions.add(PosixFilePermission.GROUP_WRITE);
break;
case 5:
permissions.add(PosixFilePermission.GROUP_READ);
break;
case 6:
permissions.add(PosixFilePermission.OWNER_EXECUTE);
break;
case 7:
permissions.add(PosixFilePermission.OWNER_WRITE);
break;
case 8:
permissions.add(PosixFilePermission.OWNER_READ);
break;
}
});
this.permissions = permissions;
}
use of java.nio.file.attribute.PosixFilePermission in project spring-integration by spring-projects.
the class FileWritingMessageHandlerTests method supportedTypeAndPermissions.
@Test
public void supportedTypeAndPermissions() throws Exception {
if (FileUtils.IS_POSIX) {
handler.setChmod(0777);
}
handler.setOutputChannel(new NullChannel());
handler.handleMessage(new GenericMessage<String>("test"));
File[] output = outputDirectory.listFiles();
assertThat(output.length, equalTo(1));
assertThat(output[0], notNullValue());
if (FileUtils.IS_POSIX) {
Set<PosixFilePermission> permissions = Files.getPosixFilePermissions(output[0].toPath());
assertThat(permissions.size(), equalTo(9));
}
}
use of java.nio.file.attribute.PosixFilePermission in project cryptofs by cryptomator.
the class CryptoFileSystemImpl method checkAccess.
void checkAccess(CryptoPath cleartextPath, AccessMode... modes) throws IOException {
if (fileStore.supportsFileAttributeView(PosixFileAttributeView.class)) {
Set<PosixFilePermission> permissions = readAttributes(cleartextPath, PosixFileAttributes.class).permissions();
boolean accessDenied = Arrays.stream(modes).anyMatch(accessMode -> !hasAccess(permissions, accessMode));
if (accessDenied) {
throw new AccessDeniedException(cleartextPath.toString());
}
} else if (fileStore.supportsFileAttributeView(DosFileAttributeView.class)) {
DosFileAttributes attrs = readAttributes(cleartextPath, DosFileAttributes.class);
if (ArrayUtils.contains(modes, AccessMode.WRITE) && attrs.isReadOnly()) {
throw new AccessDeniedException(cleartextPath.toString(), null, "read only file");
}
} else {
// read attributes to check for file existence / throws IOException if file does not exist
readAttributes(cleartextPath, BasicFileAttributes.class);
}
}
use of java.nio.file.attribute.PosixFilePermission in project ant by apache.
the class PermissionUtilsTest method getSetPermissionsWorksForZipResources.
@Test
public void getSetPermissionsWorksForZipResources() throws IOException {
File f = File.createTempFile("ant", ".zip");
f.deleteOnExit();
try (ZipOutputStream os = new ZipOutputStream(f)) {
ZipEntry e = new ZipEntry("foo");
os.putNextEntry(e);
os.closeEntry();
}
ZipResource r = new ZipResource();
r.setName("foo");
r.setArchive(f);
Set<PosixFilePermission> s = EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ);
PermissionUtils.setPermissions(r, s, null);
assertEquals(s, PermissionUtils.getPermissions(r, null));
}
Aggregations