Search in sources :

Example 61 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project buck by facebook.

the class ProjectFilesystemTest method testCreateZipPreservesExecutablePermissions.

@Test
public void testCreateZipPreservesExecutablePermissions() throws IOException {
    // Create a empty executable file.
    Path exe = tmp.newFile("test.exe");
    MoreFiles.makeExecutable(exe);
    // Archive it into a zipfile using `ProjectFileSystem.createZip`.
    Path zipFile = tmp.getRoot().resolve("test.zip");
    filesystem.createZip(ImmutableList.of(exe), zipFile);
    // permissions.
    try (ZipFile zip = new ZipFile(zipFile.toFile())) {
        Enumeration<ZipArchiveEntry> entries = zip.getEntries();
        assertTrue(entries.hasMoreElements());
        ZipArchiveEntry entry = entries.nextElement();
        Set<PosixFilePermission> permissions = MorePosixFilePermissions.fromMode(entry.getExternalAttributes() >> 16);
        assertTrue(permissions.contains(PosixFilePermission.OWNER_EXECUTE));
        assertFalse(entries.hasMoreElements());
    }
}
Also used : Path(java.nio.file.Path) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Example 62 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project buck by facebook.

the class ZipStepTest method zipMaintainsExecutablePermissions.

@Test
public void zipMaintainsExecutablePermissions() throws IOException {
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    Path parent = tmp.newFolder("zipstep");
    Path toZip = tmp.newFolder("zipdir");
    Path file = toZip.resolve("foo.sh");
    ImmutableSet<PosixFilePermission> filePermissions = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ);
    Files.createFile(file, PosixFilePermissions.asFileAttribute(filePermissions));
    Path outputZip = parent.resolve("output.zip");
    ZipStep step = new ZipStep(filesystem, outputZip, ImmutableSet.of(), false, ZipCompressionLevel.MIN_COMPRESSION_LEVEL, Paths.get("zipdir"));
    assertEquals(0, step.execute(TestExecutionContext.newInstance()).getExitCode());
    Path destination = tmp.newFolder("output");
    Unzip.extractZipFile(outputZip, destination, Unzip.ExistingFileMode.OVERWRITE);
    assertTrue(Files.isExecutable(destination.resolve("foo.sh")));
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) Test(org.junit.Test)

Example 63 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 64 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 65 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)

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