Search in sources :

Example 6 with PosixFileAttributes

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

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

the class PosixAttributeProviderTest method testAttributes.

@Test
public void testAttributes() {
    PosixFileAttributes attrs = provider.readAttributes(file);
    assertThat(attrs.permissions()).isEqualTo(PosixFilePermissions.fromString("rw-r--r--"));
    assertThat(attrs.group()).isEqualTo(createGroupPrincipal("group"));
    assertThat(attrs.fileKey()).isEqualTo(0);
}
Also used : PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) Test(org.junit.Test)

Example 8 with PosixFileAttributes

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

use of java.nio.file.attribute.PosixFileAttributes in project neo4j by neo4j.

the class SelfSignedCertificatesIT method createSelfSignedCertificateWithCorrectPermissions.

@Test
public void createSelfSignedCertificateWithCorrectPermissions() throws Exception {
    assumeTrue(!SystemUtils.IS_OS_WINDOWS);
    Certificates certificates = new Certificates();
    certificates.createSelfSignedCertificate(testDirectory.file("certificate"), testDirectory.file("privateKey"), "localhost");
    PosixFileAttributes certificateAttributes = Files.getFileAttributeView(testDirectory.file("certificate").toPath(), PosixFileAttributeView.class).readAttributes();
    assertTrue(certificateAttributes.permissions().contains(PosixFilePermission.OWNER_READ));
    assertTrue(certificateAttributes.permissions().contains(PosixFilePermission.OWNER_WRITE));
    assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OWNER_EXECUTE));
    assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.GROUP_READ));
    assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.GROUP_WRITE));
    assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.GROUP_EXECUTE));
    assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OTHERS_READ));
    assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OTHERS_WRITE));
    assertFalse(certificateAttributes.permissions().contains(PosixFilePermission.OTHERS_EXECUTE));
    PosixFileAttributes privateKey = Files.getFileAttributeView(testDirectory.file("privateKey").toPath(), PosixFileAttributeView.class).readAttributes();
    assertTrue(privateKey.permissions().contains(PosixFilePermission.OWNER_READ));
    assertTrue(privateKey.permissions().contains(PosixFilePermission.OWNER_WRITE));
    assertFalse(privateKey.permissions().contains(PosixFilePermission.OWNER_EXECUTE));
    assertFalse(privateKey.permissions().contains(PosixFilePermission.GROUP_READ));
    assertFalse(privateKey.permissions().contains(PosixFilePermission.GROUP_WRITE));
    assertFalse(privateKey.permissions().contains(PosixFilePermission.GROUP_EXECUTE));
    assertFalse(privateKey.permissions().contains(PosixFilePermission.OTHERS_READ));
    assertFalse(privateKey.permissions().contains(PosixFilePermission.OTHERS_WRITE));
    assertFalse(privateKey.permissions().contains(PosixFilePermission.OTHERS_EXECUTE));
}
Also used : Certificates(org.neo4j.bolt.security.ssl.Certificates) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) Test(org.junit.Test)

Example 10 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project syncany by syncany.

the class FileVersionComparator method captureFileProperties.

public FileProperties captureFileProperties(File file, FileChecksum knownChecksum, boolean forceChecksum) {
    FileProperties fileProperties = new FileProperties();
    fileProperties.relativePath = FileUtil.getRelativeDatabasePath(rootFolder, file);
    Path filePath = null;
    try {
        filePath = Paths.get(file.getAbsolutePath());
        fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
    } catch (InvalidPathException e) {
        // This throws an exception if the filename is invalid,
        // e.g. colon in filename on windows "file:name"
        logger.log(Level.FINE, "InvalidPath", e);
        logger.log(Level.WARNING, "- Path '{0}' is invalid on this file system. It cannot exist. ", file.getAbsolutePath());
        fileProperties.exists = false;
        return fileProperties;
    }
    if (!fileProperties.exists) {
        return fileProperties;
    }
    try {
        // Read operating system dependent file attributes
        BasicFileAttributes fileAttributes = null;
        if (EnvironmentUtil.isWindows()) {
            DosFileAttributes dosAttrs = Files.readAttributes(filePath, DosFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
            fileProperties.dosAttributes = FileUtil.dosAttrsToString(dosAttrs);
            fileAttributes = dosAttrs;
        } else if (EnvironmentUtil.isUnixLikeOperatingSystem()) {
            PosixFileAttributes posixAttrs = Files.readAttributes(filePath, PosixFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
            fileProperties.posixPermissions = PosixFilePermissions.toString(posixAttrs.permissions());
            fileAttributes = posixAttrs;
        } else {
            fileAttributes = Files.readAttributes(filePath, BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
        }
        fileProperties.lastModified = fileAttributes.lastModifiedTime().toMillis();
        fileProperties.size = fileAttributes.size();
        // Type
        if (fileAttributes.isSymbolicLink()) {
            fileProperties.type = FileType.SYMLINK;
            fileProperties.linkTarget = FileUtil.readSymlinkTarget(file);
        } else if (fileAttributes.isDirectory()) {
            fileProperties.type = FileType.FOLDER;
            fileProperties.linkTarget = null;
        } else {
            fileProperties.type = FileType.FILE;
            fileProperties.linkTarget = null;
        }
        // Checksum
        if (knownChecksum != null) {
            fileProperties.checksum = knownChecksum;
        } else {
            if (fileProperties.type == FileType.FILE && forceChecksum) {
                try {
                    if (fileProperties.size > 0) {
                        fileProperties.checksum = new FileChecksum(FileUtil.createChecksum(file, checksumAlgorithm));
                    } else {
                        fileProperties.checksum = null;
                    }
                } catch (NoSuchAlgorithmException | IOException e) {
                    logger.log(Level.FINE, "Failed create checksum", e);
                    logger.log(Level.SEVERE, "SEVERE: Unable to create checksum for file {0}", file);
                    fileProperties.checksum = null;
                }
            } else {
                fileProperties.checksum = null;
            }
        }
        // Must be last (!), used for vanish-test later
        fileProperties.exists = Files.exists(filePath, LinkOption.NOFOLLOW_LINKS);
        fileProperties.locked = fileProperties.exists && FileUtil.isFileLocked(file);
        return fileProperties;
    } catch (IOException e) {
        logger.log(Level.FINE, "Failed to read file", e);
        logger.log(Level.SEVERE, "SEVERE: Cannot read file {0}. Assuming file is locked.", file);
        fileProperties.exists = true;
        fileProperties.locked = true;
        return fileProperties;
    }
}
Also used : Path(java.nio.file.Path) DosFileAttributes(java.nio.file.attribute.DosFileAttributes) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) InvalidPathException(java.nio.file.InvalidPathException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) FileChecksum(org.syncany.database.FileContent.FileChecksum)

Aggregations

PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)11 Path (java.nio.file.Path)6 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)5 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)1 File (java.io.File)1 InvalidPathException (java.nio.file.InvalidPathException)1 DosFileAttributes (java.nio.file.attribute.DosFileAttributes)1 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)1 UserPrincipal (java.nio.file.attribute.UserPrincipal)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 HashMap (java.util.HashMap)1 IgfsFile (org.apache.ignite.igfs.IgfsFile)1 LocalFileSystemIgfsFile (org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemIgfsFile)1 UserException (org.elasticsearch.cli.UserException)1 Certificates (org.neo4j.bolt.security.ssl.Certificates)1 FileChecksum (org.syncany.database.FileContent.FileChecksum)1