Search in sources :

Example 91 with PosixFilePermission

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);
            }
        }
    }
}
Also used : IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File)

Example 92 with PosixFilePermission

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;
}
Also used : BitSet(java.util.BitSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Example 93 with PosixFilePermission

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));
    }
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) NullChannel(org.springframework.integration.channel.NullChannel) File(java.io.File) Test(org.junit.Test)

Example 94 with PosixFilePermission

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);
    }
}
Also used : AccessDeniedException(java.nio.file.AccessDeniedException) DosFileAttributeView(java.nio.file.attribute.DosFileAttributeView) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 95 with PosixFilePermission

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));
}
Also used : ZipResource(org.apache.tools.ant.types.resources.ZipResource) ZipOutputStream(org.apache.tools.zip.ZipOutputStream) ZipEntry(org.apache.tools.zip.ZipEntry) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) File(java.io.File) Test(org.junit.Test)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)139 Path (java.nio.file.Path)69 IOException (java.io.IOException)41 File (java.io.File)40 HashSet (java.util.HashSet)40 Test (org.junit.Test)35 Set (java.util.Set)22 FileOutputStream (java.io.FileOutputStream)17 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)14 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)11 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)7 RandomAccessFile (java.io.RandomAccessFile)6 UserPrincipal (java.nio.file.attribute.UserPrincipal)6 OutputStream (java.io.OutputStream)5 SimpleDateFormat (java.text.SimpleDateFormat)5 BufferedWriter (java.io.BufferedWriter)4 InputStream (java.io.InputStream)4 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4