Search in sources :

Example 6 with PosixFileAttributeView

use of java.nio.file.attribute.PosixFileAttributeView 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 7 with PosixFileAttributeView

use of java.nio.file.attribute.PosixFileAttributeView in project gradle by gradle.

the class PosixJdk7FilePermissionHandler method chmod.

@Override
public void chmod(File f, int mode) throws IOException {
    PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(f.toPath(), PosixFileAttributeView.class);
    fileAttributeView.setPermissions(convertToPermissionsSet(mode));
}
Also used : PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 8 with PosixFileAttributeView

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

the class PosixAttributeProviderTest method testView.

@Test
public void testView() throws IOException {
    file.setAttribute("owner", "owner", createUserPrincipal("user"));
    PosixFileAttributeView view = provider.view(fileLookup(), ImmutableMap.of("basic", new BasicAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS), "owner", new OwnerAttributeProvider().view(fileLookup(), NO_INHERITED_VIEWS)));
    assertNotNull(view);
    assertThat(view.name()).isEqualTo("posix");
    assertThat(view.getOwner()).isEqualTo(createUserPrincipal("user"));
    PosixFileAttributes attrs = view.readAttributes();
    assertThat(attrs.fileKey()).isEqualTo(0);
    assertThat(attrs.owner()).isEqualTo(createUserPrincipal("user"));
    assertThat(attrs.group()).isEqualTo(createGroupPrincipal("group"));
    assertThat(attrs.permissions()).isEqualTo(PosixFilePermissions.fromString("rw-r--r--"));
    view.setOwner(createUserPrincipal("root"));
    assertThat(view.getOwner()).isEqualTo(createUserPrincipal("root"));
    assertThat(file.getAttribute("owner", "owner")).isEqualTo(createUserPrincipal("root"));
    view.setGroup(createGroupPrincipal("root"));
    assertThat(view.readAttributes().group()).isEqualTo(createGroupPrincipal("root"));
    assertThat(file.getAttribute("posix", "group")).isEqualTo(createGroupPrincipal("root"));
    view.setPermissions(PosixFilePermissions.fromString("rwx------"));
    assertThat(view.readAttributes().permissions()).isEqualTo(PosixFilePermissions.fromString("rwx------"));
    assertThat(file.getAttribute("posix", "permissions")).isEqualTo(PosixFilePermissions.fromString("rwx------"));
}
Also used : PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) Test(org.junit.Test)

Example 9 with PosixFileAttributeView

use of java.nio.file.attribute.PosixFileAttributeView in project intellij-community by JetBrains.

the class SocketLock method lock.

@NotNull
public ActivateStatus lock(@NotNull String[] args) throws Exception {
    log("enter: lock(config=%s system=%s)", myConfigPath, mySystemPath);
    return underLocks(() -> {
        File portMarkerC = new File(myConfigPath, PORT_FILE);
        File portMarkerS = new File(mySystemPath, PORT_FILE);
        MultiMap<Integer, String> portToPath = MultiMap.createSmart();
        addExistingPort(portMarkerC, myConfigPath, portToPath);
        addExistingPort(portMarkerS, mySystemPath, portToPath);
        if (!portToPath.isEmpty()) {
            for (Map.Entry<Integer, Collection<String>> entry : portToPath.entrySet()) {
                ActivateStatus status = tryActivate(entry.getKey(), entry.getValue(), args);
                if (status != ActivateStatus.NO_INSTANCE) {
                    log("exit: lock(): " + status);
                    return status;
                }
            }
        }
        if (isShutdownCommand()) {
            System.exit(0);
        }
        myToken = UUID.randomUUID().toString();
        String[] lockedPaths = { myConfigPath, mySystemPath };
        int workerCount = PlatformUtils.isIdeaCommunity() || PlatformUtils.isDatabaseIDE() || PlatformUtils.isCidr() ? 1 : 2;
        NotNullProducer<ChannelHandler> handler = () -> new MyChannelInboundHandler(lockedPaths, myActivateListener, myToken);
        myServer = BuiltInServer.startNioOrOio(workerCount, 6942, 50, false, handler);
        byte[] portBytes = Integer.toString(myServer.getPort()).getBytes(CharsetToolkit.UTF8_CHARSET);
        FileUtil.writeToFile(portMarkerC, portBytes);
        FileUtil.writeToFile(portMarkerS, portBytes);
        File tokenFile = new File(mySystemPath, TOKEN_FILE);
        FileUtil.writeToFile(tokenFile, myToken.getBytes(CharsetToolkit.UTF8_CHARSET));
        PosixFileAttributeView view = Files.getFileAttributeView(tokenFile.toPath(), PosixFileAttributeView.class);
        if (view != null) {
            try {
                view.setPermissions(ContainerUtil.newHashSet(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE));
            } catch (IOException e) {
                log(e);
            }
        }
        log("exit: lock(): succeed");
        return ActivateStatus.NO_INSTANCE;
    });
}
Also used : ChannelHandler(io.netty.channel.ChannelHandler) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) Collection(java.util.Collection) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with PosixFileAttributeView

use of java.nio.file.attribute.PosixFileAttributeView in project alluxio by Alluxio.

the class FileUtils method changeLocalFileUser.

/**
   * Changes the local file's user.
   *
   * @param path that will change owner
   * @param user the new user
   * @throws IOException if the group is unable to be changed
   */
public static void changeLocalFileUser(String path, String user) throws IOException {
    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    PosixFileAttributeView view = Files.getFileAttributeView(Paths.get(path), PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(user);
    view.setOwner(userPrincipal);
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Aggregations

PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)13 Path (java.nio.file.Path)5 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)3 IOException (java.io.IOException)2 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 UserPrincipal (java.nio.file.attribute.UserPrincipal)2 MultiMap (com.intellij.util.containers.MultiMap)1 ChannelHandler (io.netty.channel.ChannelHandler)1 FileSystemException (io.vertx.core.file.FileSystemException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 IgfsException (org.apache.ignite.igfs.IgfsException)1 IndexOutput (org.apache.lucene.store.IndexOutput)1 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)1 UserException (org.elasticsearch.cli.UserException)1 NotNull (org.jetbrains.annotations.NotNull)1