Search in sources :

Example 16 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project ha-bridge by bwssytems.

the class BridgeSettings method configWriter.

private synchronized void configWriter(String content, Path filePath) {
    if (Files.exists(filePath) && !Files.isWritable(filePath)) {
        log.error("Error file is not writable: " + filePath);
        return;
    }
    if (Files.notExists(filePath.getParent())) {
        try {
            Files.createDirectories(filePath.getParent());
        } catch (IOException e) {
            log.error("Error creating the directory: " + filePath + " message: " + e.getMessage(), e);
        }
    }
    try {
        Path target = null;
        if (Files.exists(filePath)) {
            target = FileSystems.getDefault().getPath(filePath.getParent().toString(), "habridge.config.old." + getCurrentDate());
            Files.move(filePath, target);
        }
        Files.write(filePath, content.getBytes(), StandardOpenOption.CREATE);
        // set attributes to be for user only
        // using PosixFilePermission to set file permissions
        Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
        // add owners permission 
        perms.add(PosixFilePermission.OWNER_READ);
        perms.add(PosixFilePermission.OWNER_WRITE);
        try {
            String osName = System.getProperty("os.name");
            if (osName.toLowerCase().indexOf("win") < 0)
                Files.setPosixFilePermissions(filePath, perms);
        } catch (UnsupportedOperationException e) {
            log.info("Cannot set permissions for config file on this system as it is not supported. Continuing");
        }
        if (target != null)
            Files.delete(target);
    } catch (IOException e) {
        log.error("Error writing the file: " + filePath + " message: " + e.getMessage(), e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Example 17 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project zeppelin by apache.

the class InterpreterSettingManager method saveToFile.

public void saveToFile() throws IOException {
    String jsonString;
    synchronized (interpreterSettings) {
        InterpreterInfoSaving info = new InterpreterInfoSaving();
        info.interpreterBindings = interpreterBindings;
        info.interpreterSettings = interpreterSettings;
        info.interpreterRepositories = interpreterRepositories;
        jsonString = gson.toJson(info);
    }
    if (!Files.exists(interpreterBindingPath)) {
        Files.createFile(interpreterBindingPath);
        Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
        Files.setPosixFilePermissions(interpreterBindingPath, permissions);
    }
    FileOutputStream fos = new FileOutputStream(interpreterBindingPath.toFile(), false);
    OutputStreamWriter out = new OutputStreamWriter(fos);
    out.append(jsonString);
    out.close();
    fos.close();
}
Also used : FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 18 with PosixFilePermission

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

the class FileSystemImpl method chmodInternal.

protected BlockingAction<Void> chmodInternal(String path, String perms, String dirPerms, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(path);
    Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perms);
    Set<PosixFilePermission> dirPermissions = dirPerms == null ? null : PosixFilePermissions.fromString(dirPerms);
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path target = vertx.resolveFile(path).toPath();
                if (dirPermissions != null) {
                    Files.walkFileTree(target, new SimpleFileVisitor<Path>() {

                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                            //The directory entries typically have different permissions to the files, e.g. execute permission
                            //or can't cd into it
                            Files.setPosixFilePermissions(dir, dirPermissions);
                            return FileVisitResult.CONTINUE;
                        }

                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            Files.setPosixFilePermissions(file, permissions);
                            return FileVisitResult.CONTINUE;
                        }
                    });
                } else {
                    Files.setPosixFilePermissions(target, permissions);
                }
            } catch (SecurityException e) {
                throw new FileSystemException("Accessed denied for chmod on " + path);
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileSystemException(io.vertx.core.file.FileSystemException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 19 with PosixFilePermission

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

use of java.nio.file.attribute.PosixFilePermission in project che by eclipse.

the class JGitConnection method writePrivateKeyFile.

/**
     * Writes private SSH key into file.
     *
     * @return file that contains SSH key
     * @throws GitException
     *         if other error occurs
     */
private File writePrivateKeyFile(String url, File keyDirectory) throws GitException {
    final File keyFile = new File(keyDirectory, "identity");
    try (FileOutputStream fos = new FileOutputStream(keyFile)) {
        byte[] sshKey = sshKeyProvider.getPrivateKey(url);
        fos.write(sshKey);
    } catch (IOException | ServerException exception) {
        String errorMessage = "Can't store ssh key. ".concat(exception.getMessage());
        LOG.error(errorMessage, exception);
        throw new GitException(errorMessage, ErrorCodes.UNABLE_GET_PRIVATE_SSH_KEY);
    }
    Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
    try {
        java.nio.file.Files.setPosixFilePermissions(keyFile.toPath(), permissions);
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
    return keyFile;
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) GitException(org.eclipse.che.api.git.exception.GitException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) DiffCommitFile(org.eclipse.che.api.git.shared.DiffCommitFile) File(java.io.File)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)33 Path (java.nio.file.Path)18 IOException (java.io.IOException)10 Test (org.junit.Test)7 File (java.io.File)6 HashSet (java.util.HashSet)6 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)5 Set (java.util.Set)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 TransferSettings (org.syncany.plugins.transfer.TransferSettings)3 TestClient (org.syncany.tests.util.TestClient)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 RandomAccessFile (java.io.RandomAccessFile)2 UserPrincipal (java.nio.file.attribute.UserPrincipal)2 Date (java.util.Date)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 ZipArchiveEntry (org.apache.commons.compress.archivers.zip.ZipArchiveEntry)2