Search in sources :

Example 1 with PosixFileAttributes

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

the class InstallPluginCommand method installConfig.

/**
     * Copies the files from {@code tmpConfigDir} into {@code destConfigDir}.
     * Any files existing in both the source and destination will be skipped.
     */
private void installConfig(PluginInfo info, Path tmpConfigDir, Path destConfigDir) throws Exception {
    if (Files.isDirectory(tmpConfigDir) == false) {
        throw new UserException(ExitCodes.IO_ERROR, "config in plugin " + info.getName() + " is not a directory");
    }
    Files.createDirectories(destConfigDir);
    setFileAttributes(destConfigDir, CONFIG_DIR_PERMS);
    final PosixFileAttributeView destConfigDirAttributesView = Files.getFileAttributeView(destConfigDir.getParent(), PosixFileAttributeView.class);
    final PosixFileAttributes destConfigDirAttributes = destConfigDirAttributesView != null ? destConfigDirAttributesView.readAttributes() : null;
    if (destConfigDirAttributes != null) {
        setOwnerGroup(destConfigDir, destConfigDirAttributes);
    }
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(tmpConfigDir)) {
        for (Path srcFile : stream) {
            if (Files.isDirectory(srcFile)) {
                throw new UserException(ExitCodes.DATA_ERROR, "Directories not allowed in config dir for plugin " + info.getName());
            }
            Path destFile = destConfigDir.resolve(tmpConfigDir.relativize(srcFile));
            if (Files.exists(destFile) == false) {
                Files.copy(srcFile, destFile);
                setFileAttributes(destFile, CONFIG_FILES_PERMS);
                if (destConfigDirAttributes != null) {
                    setOwnerGroup(destFile, destConfigDirAttributes);
                }
            }
        }
    }
    // clean up what we just copied
    IOUtils.rm(tmpConfigDir);
}
Also used : Path(java.nio.file.Path) UserException(org.elasticsearch.cli.UserException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 2 with PosixFileAttributes

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

the class ProjectFilesystemTest method testCreateReadOnlyFileSetsPermissions.

@Test
public void testCreateReadOnlyFileSetsPermissions() throws IOException {
    Assume.assumeTrue(FileSystems.getDefault().supportedFileAttributeViews().contains("posix"));
    Path path = Paths.get("hello.txt");
    ImmutableSet<PosixFilePermission> permissions = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ);
    filesystem.writeContentsToPath("hello world", path, PosixFilePermissions.asFileAttribute(permissions));
    // The umask may restrict the actual permissions on the filesystem:
    // https://fburl.com/26569549
    // So the best we can do is to check that the actual permissions are a
    // strict subset of the expected permissions.
    PosixFileAttributes attrs = filesystem.readAttributes(path, PosixFileAttributes.class);
    assertTrue(permissions.containsAll(attrs.permissions()));
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) Test(org.junit.Test)

Example 3 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project java-chassis by ServiceComb.

the class FilePerm method setFilePerm.

public static void setFilePerm(File file, String perm) {
    if (filePermSupported()) {
        try {
            Set<PosixFilePermission> perms = PosixFilePermissions.fromString(perm);
            PosixFileAttributes attr = Files.readAttributes(file.toPath(), PosixFileAttributes.class);
            attr.permissions().clear();
            Files.setPosixFilePermissions(file.toPath(), perms);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes)

Example 4 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project ignite by apache.

the class LocalIgfsSecondaryFileSystem method info.

/**
 * {@inheritDoc}
 */
@Override
public IgfsFile info(final IgfsPath path) {
    File file = fileForPath(path);
    if (!file.exists())
        return null;
    boolean isDir = file.isDirectory();
    PosixFileAttributes attrs = LocalFileSystemUtils.posixAttributes(file);
    Map<String, String> props = LocalFileSystemUtils.posixAttributesToMap(attrs);
    BasicFileAttributes basicAttrs = LocalFileSystemUtils.basicAttributes(file);
    if (isDir) {
        return new LocalFileSystemIgfsFile(path, false, true, 0, basicAttrs.lastAccessTime().toMillis(), basicAttrs.lastModifiedTime().toMillis(), 0, props);
    } else {
        return new LocalFileSystemIgfsFile(path, file.isFile(), false, 0, basicAttrs.lastAccessTime().toMillis(), basicAttrs.lastModifiedTime().toMillis(), file.length(), props);
    }
}
Also used : LocalFileSystemIgfsFile(org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemIgfsFile) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) IgfsFile(org.apache.ignite.igfs.IgfsFile) LocalFileSystemIgfsFile(org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemIgfsFile) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 5 with PosixFileAttributes

use of java.nio.file.attribute.PosixFileAttributes in project incubator-servicecomb-java-chassis by apache.

the class FilePerm method setFilePerm.

/**
 * 设置文件权限。前提:必须支持PosixFileAttributeView.
 */
public static void setFilePerm(File file, String perm) {
    if (filePermSupported()) {
        try {
            Set<PosixFilePermission> perms = PosixFilePermissions.fromString(perm);
            PosixFileAttributes attr = Files.readAttributes(file.toPath(), PosixFileAttributes.class);
            attr.permissions().clear();
            Files.setPosixFilePermissions(file.toPath(), perms);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes)

Aggregations

PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)36 Path (java.nio.file.Path)16 IOException (java.io.IOException)13 Test (org.junit.Test)12 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)11 File (java.io.File)8 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)8 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)6 FileTime (java.nio.file.attribute.FileTime)5 HashMap (java.util.HashMap)5 InputStream (java.io.InputStream)4 OutputStream (java.io.OutputStream)4 DosFileAttributes (java.nio.file.attribute.DosFileAttributes)4 HashSet (java.util.HashSet)4 Map (java.util.Map)4 Set (java.util.Set)4 UfsDirectoryStatus (alluxio.underfs.UfsDirectoryStatus)3 UfsFileStatus (alluxio.underfs.UfsFileStatus)3 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3