Search in sources :

Example 1 with GroupPrincipal

use of java.nio.file.attribute.GroupPrincipal 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);
        }
    }
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) IgfsException(org.apache.ignite.igfs.IgfsException) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) HashSet(java.util.HashSet)

Example 2 with GroupPrincipal

use of java.nio.file.attribute.GroupPrincipal 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;
        }
    };
}
Also used : Path(java.nio.file.Path) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) FileSystemException(io.vertx.core.file.FileSystemException) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) IOException(java.io.IOException) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 3 with GroupPrincipal

use of java.nio.file.attribute.GroupPrincipal in project vert.x by eclipse.

the class FileSystemTest method testChownToOwnGroup.

@Test
public void testChownToOwnGroup() throws Exception {
    // Not supported in WindowsFileSystemProvider
    Assume.assumeFalse(Utils.isWindows());
    String file1 = "some-file.dat";
    createFileWithJunk(file1, 100);
    String fullPath = testDir + pathSep + file1;
    Path path = Paths.get(fullPath);
    GroupPrincipal group = Files.readAttributes(path, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS).group();
    vertx.fileSystem().chown(fullPath, null, group.getName(), ar -> {
        deleteFile(file1);
        assertTrue(ar.succeeded());
        testComplete();
    });
    await();
}
Also used : Path(java.nio.file.Path) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) Test(org.junit.Test)

Example 4 with GroupPrincipal

use of java.nio.file.attribute.GroupPrincipal in project elasticsearch by elastic.

the class InstallPluginCommandTests method assertPlugin.

void assertPlugin(String name, Path original, Environment env) throws IOException {
    Path got = env.pluginsFile().resolve(name);
    assertTrue("dir " + name + " exists", Files.exists(got));
    if (isPosix) {
        Set<PosixFilePermission> perms = Files.getPosixFilePermissions(got);
        assertThat(perms, containsInAnyOrder(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE));
    }
    assertTrue("jar was copied", Files.exists(got.resolve("plugin.jar")));
    assertFalse("bin was not copied", Files.exists(got.resolve("bin")));
    assertFalse("config was not copied", Files.exists(got.resolve("config")));
    if (Files.exists(original.resolve("bin"))) {
        Path binDir = env.binFile().resolve(name);
        assertTrue("bin dir exists", Files.exists(binDir));
        assertTrue("bin is a dir", Files.isDirectory(binDir));
        PosixFileAttributes binAttributes = null;
        if (isPosix) {
            binAttributes = Files.readAttributes(env.binFile(), PosixFileAttributes.class);
        }
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(binDir)) {
            for (Path file : stream) {
                assertFalse("not a dir", Files.isDirectory(file));
                if (isPosix) {
                    PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
                    assertEquals(InstallPluginCommand.BIN_FILES_PERMS, attributes.permissions());
                }
            }
        }
    }
    if (Files.exists(original.resolve("config"))) {
        Path configDir = env.configFile().resolve(name);
        assertTrue("config dir exists", Files.exists(configDir));
        assertTrue("config is a dir", Files.isDirectory(configDir));
        UserPrincipal user = null;
        GroupPrincipal group = null;
        if (isPosix) {
            PosixFileAttributes configAttributes = Files.getFileAttributeView(env.configFile(), PosixFileAttributeView.class).readAttributes();
            user = configAttributes.owner();
            group = configAttributes.group();
            PosixFileAttributes attributes = Files.getFileAttributeView(configDir, PosixFileAttributeView.class).readAttributes();
            assertThat(attributes.owner(), equalTo(user));
            assertThat(attributes.group(), equalTo(group));
        }
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) {
            for (Path file : stream) {
                assertFalse("not a dir", Files.isDirectory(file));
                if (isPosix) {
                    PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
                    if (user != null) {
                        assertThat(attributes.owner(), equalTo(user));
                    }
                    if (group != null) {
                        assertThat(attributes.group(), equalTo(group));
                    }
                }
            }
        }
    }
    assertInstallCleaned(env);
}
Also used : Path(java.nio.file.Path) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 5 with GroupPrincipal

use of java.nio.file.attribute.GroupPrincipal in project jimfs by google.

the class UserLookupServiceTest method testUserLookupService.

@Test
public void testUserLookupService() throws IOException {
    UserPrincipalLookupService service = new UserLookupService(true);
    UserPrincipal bob1 = service.lookupPrincipalByName("bob");
    UserPrincipal bob2 = service.lookupPrincipalByName("bob");
    UserPrincipal alice = service.lookupPrincipalByName("alice");
    assertThat(bob1).isEqualTo(bob2);
    assertThat(bob1).isNotEqualTo(alice);
    GroupPrincipal group1 = service.lookupPrincipalByGroupName("group");
    GroupPrincipal group2 = service.lookupPrincipalByGroupName("group");
    GroupPrincipal foo = service.lookupPrincipalByGroupName("foo");
    assertThat(group1).isEqualTo(group2);
    assertThat(group1).isNotEqualTo(foo);
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Aggregations

GroupPrincipal (java.nio.file.attribute.GroupPrincipal)7 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)4 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)4 Path (java.nio.file.Path)3 UserPrincipal (java.nio.file.attribute.UserPrincipal)3 IOException (java.io.IOException)2 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 Test (org.junit.Test)2 UserLookupService.createGroupPrincipal (com.google.common.jimfs.UserLookupService.createGroupPrincipal)1 FileSystemException (io.vertx.core.file.FileSystemException)1 HashSet (java.util.HashSet)1 IgfsException (org.apache.ignite.igfs.IgfsException)1