java.nio.file.attribute.FileAttribute Java Examples

The following examples show how to use java.nio.file.attribute.FileAttribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaConsumerGroupClientImpl.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 7 votes vote down vote up
private File flushPropertiesToTempFile(Map<String, Object> configProps) throws IOException {
  FileAttribute<Set<PosixFilePermission>> attributes
      = PosixFilePermissions.asFileAttribute(new HashSet<>(
          Arrays.asList(PosixFilePermission.OWNER_WRITE,
                        PosixFilePermission.OWNER_READ)));
  File configFile = Files.createTempFile("ksqlclient", "properties", attributes).toFile();
  configFile.deleteOnExit();

  try (FileOutputStream outputStream = new FileOutputStream(configFile)) {
    Properties clientProps = new Properties();
    for (Map.Entry<String, Object> property
        : configProps.entrySet()) {
      clientProps.put(property.getKey(), property.getValue());
    }
    clientProps.store(outputStream, "Configuration properties of KSQL AdminClient");
  }
  return configFile;
}
 
Example #2
Source File: Aion.java    From aion with MIT License 7 votes vote down vote up
private static void writeKeyToFile(final String path, final String fileName, final String key)
        throws IOException {
    Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-----");
    FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);

    Path p = Paths.get(path).resolve(fileName);
    Path keyFile;
    if (!java.nio.file.Files.exists(p)) {
        keyFile = java.nio.file.Files.createFile(p, attr);
    } else {
        keyFile = p;
    }

    FileOutputStream fos = new FileOutputStream(keyFile.toString());
    fos.write(key.getBytes());
    fos.close();
}
 
Example #3
Source File: Standard.java    From presto with Apache License 2.0 6 votes vote down vote up
public static void enablePrestoJavaDebugger(DockerContainer container, int debugPort)
{
    try {
        FileAttribute<Set<PosixFilePermission>> rwx = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxrwxrwx"));
        Path script = Files.createTempFile("enable-java-debugger", ".sh", rwx);
        Files.writeString(
                script,
                format(
                        "#!/bin/bash\n" +
                                "printf '%%s\\n' '%s' >> '%s'\n",
                        "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:" + debugPort,
                        CONTAINER_PRESTO_JVM_CONFIG),
                UTF_8);
        container.withCopyFileToContainer(MountableFile.forHostPath(script), "/docker/presto-init.d/enable-java-debugger.sh");

        // expose debug port unconditionally when debug is enabled
        exposePort(container, debugPort);
    }
    catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example #4
Source File: FileUtils.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that the given path is an existing directory, creating it if required.
 *
 * @param path The path.
 * @param attrs The attributes.
 * @return The normalized, absolute directory path.
 */
public static Path ensureDirectory(Path path, FileAttribute<?>... attrs) {
    if (Files.exists(requireNonNull(path))) {
        return assertDir(path);
    } else {
        try {
            return Files.createDirectories(path, attrs);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
}
 
Example #5
Source File: Files.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Used by createDirectories to attempt to create a directory. A no-op
 * if the directory already exists.
 */
private static void createAndCheckIsDirectory(Path dir,
                                              FileAttribute<?>... attrs)
    throws IOException
{
    try {
        createDirectory(dir, attrs);
    } catch (FileAlreadyExistsException x) {
        if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
            throw x;
    }
}
 
Example #6
Source File: TempFileHelper.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Creates a temporary directory in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempDirectory(Path dir,
                                String prefix,
                                FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, null, true, attrs);
}
 
Example #7
Source File: TestDirResourceSet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@BeforeClass
public static void before() throws IOException {
    tempDir = Files.createTempDirectory("test", new FileAttribute[0]);
    dir1 = new File(tempDir.toFile(), "dir1");
    TomcatBaseTest.recursiveCopy(new File("test/webresources/dir1").toPath(), dir1.toPath());
}
 
Example #8
Source File: DelegatingFileSystemProvider.java    From tessera with Apache License 2.0 4 votes vote down vote up
@Override
public FileChannel newFileChannel(final Path path,
                                  final Set<? extends OpenOption> options,
                                  final FileAttribute<?>... attrs) throws IOException {
    return delegate.newFileChannel(path, options, attrs);
}
 
Example #9
Source File: FaultyFileSystem.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(target, "createSymbolicLink");
    Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
 
Example #10
Source File: FaultyFileSystem.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(dir, "createDirectory");
    Files.createDirectory(unwrap(dir), attrs);
}
 
Example #11
Source File: FaultyFileSystem.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(file, "newByteChannel");
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example #12
Source File: CustomOptions.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path path,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    if (options.contains(CustomOption.IGNORE)) {
        ignoreCount++;
        options.remove(CustomOption.IGNORE);
    }
    return super.newByteChannel(path, options, attrs);
}
 
Example #13
Source File: TempFileHelper.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}
 
Example #14
Source File: FaultyFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(target, "createSymbolicLink");
    Files.createSymbolicLink(unwrap(link), unwrap(target), attrs);
}
 
Example #15
Source File: FaultyFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(dir, "createDirectory");
    Files.createDirectory(unwrap(dir), attrs);
}
 
Example #16
Source File: FaultyFileSystem.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(file, "newByteChannel");
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example #17
Source File: CustomOptions.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path path,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    if (options.contains(CustomOption.IGNORE)) {
        ignoreCount++;
        options.remove(CustomOption.IGNORE);
    }
    return super.newByteChannel(path, options, attrs);
}
 
Example #18
Source File: TempFileHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a temporary directory in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempDirectory(Path dir,
                                String prefix,
                                FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, null, true, attrs);
}
 
Example #19
Source File: DelegatingFileSystemProvider.java    From tessera with Apache License 2.0 4 votes vote down vote up
@Override
public void createDirectory(final Path dir, final FileAttribute<?>... attrs) throws IOException {
    delegate.createDirectory(dir, attrs);
}
 
Example #20
Source File: SaltServerActionService.java    From uyuni with GNU General Public License v2.0 4 votes vote down vote up
private Map<LocalCall<?>, List<MinionSummary>> remoteCommandAction(
        List<MinionSummary> minions, ScriptAction scriptAction) {
    String script = scriptAction.getScriptActionDetails().getScriptContents();

    Map<LocalCall<?>, List<MinionSummary>> ret = new HashMap<>();
    // write script to /srv/susemanager/salt/scripts/script_<action_id>.sh
    Path scriptFile = saltUtils.getScriptPath(scriptAction.getId());
    try {
        if (!Files.exists(scriptFile)) {
            // make sure parent dir exists
            if (!Files.exists(scriptFile.getParent())) {
                FileAttribute<Set<PosixFilePermission>> dirAttributes =
                        PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxr-xr-x"));

                Files.createDirectory(scriptFile.getParent(), dirAttributes);
                // make sure correct user is set
            }
        }

        if (!skipCommandScriptPerms) {
            setFileOwner(scriptFile.getParent());
        }

        // In case of action retry, the files script files will be already created.
        if (!Files.exists(scriptFile)) {
            FileAttribute<Set<PosixFilePermission>> fileAttributes =
                    PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-r--r--"));
            Files.createFile(scriptFile, fileAttributes);
            FileUtils.writeStringToFile(scriptFile.toFile(),
                    script.replaceAll("\r\n", "\n"));
        }

        if (!skipCommandScriptPerms) {
            setFileOwner(scriptFile);
        }

        // state.apply remotecommands
        Map<String, Object> pillar = new HashMap<>();
        pillar.put("mgr_remote_cmd_script", SALT_FS_PREFIX + SCRIPTS_DIR + "/" + scriptFile.getFileName());
        pillar.put("mgr_remote_cmd_runas", scriptAction.getScriptActionDetails().getUsername());
        pillar.put("mgr_remote_cmd_timeout", scriptAction.getScriptActionDetails().getTimeout());
        ret.put(State.apply(Arrays.asList(REMOTE_COMMANDS), Optional.of(pillar)), minions);
    }
    catch (IOException e) {
        String errorMsg = "Could not write script to file " + scriptFile + " - " + e;
        LOG.error(errorMsg);
        scriptAction.getServerActions().stream()
                .filter(entry -> minions.contains(entry.getServer()))
                .forEach(sa -> {
                    sa.fail("Error scheduling the action: " + errorMsg);
                    ActionFactory.save(sa);
        });
    }
    return ret;
}
 
Example #21
Source File: TempFileHelper.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}
 
Example #22
Source File: TempFileHelper.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a temporary directory in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempDirectory(Path dir,
                                String prefix,
                                FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, null, true, attrs);
}
 
Example #23
Source File: Files.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Used by createDirectories to attempt to create a directory. A no-op
 * if the directory already exists.
 */
private static void createAndCheckIsDirectory(Path dir,
                                              FileAttribute<?>... attrs)
    throws IOException
{
    try {
        createDirectory(dir, attrs);
    } catch (FileAlreadyExistsException x) {
        if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
            throw x;
    }
}
 
Example #24
Source File: CustomOptions.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path path,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    if (options.contains(CustomOption.IGNORE)) {
        ignoreCount++;
        options.remove(CustomOption.IGNORE);
    }
    return super.newByteChannel(path, options, attrs);
}
 
Example #25
Source File: FaultyFileSystem.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SeekableByteChannel newByteChannel(Path file,
                                          Set<? extends OpenOption> options,
                                          FileAttribute<?>... attrs)
    throws IOException
{
    triggerEx(file, "newByteChannel");
    return Files.newByteChannel(unwrap(file), options, attrs);
}
 
Example #26
Source File: DelegatingFileSystemProvider.java    From tessera with Apache License 2.0 4 votes vote down vote up
@Override
public void createSymbolicLink(final Path link,
                               final Path target,
                               final FileAttribute<?>... attrs) throws IOException {
    delegate.createSymbolicLink(link, target, attrs);
}
 
Example #27
Source File: FileUtils.java    From embedded-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new and empty file, if the file does not exist.
 *
 * @param file the path to the file to create
 * @param attributes an optional list of file attributes to set atomically when creating the file
 * @return the file
 * @throws IOException if an I/O error occurs or the parent directory does not exist
 * @see Files#createFile(Path, FileAttribute[])
 */
public static Path createIfNotExists(Path file, FileAttribute<?>... attributes) throws IOException {
	Objects.requireNonNull(file, "'file' must not be null");
	Objects.requireNonNull(attributes, "'attributes' must not be null");
	try {
		return Files.createFile(file, attributes);
	}
	catch (FileAlreadyExistsException ex) {
		return file;
	}
}
 
Example #28
Source File: Files.java    From cava with Apache License 2.0 4 votes vote down vote up
/**
 * Create a file, if it does not already exist.
 *
 * @param path The path to the file to create.
 * @param attrs An optional list of file attributes to set atomically when creating the file.
 * @return {@code true} if the file was created.
 * @throws IOException If an I/O error occurs or the parent directory does not exist.
 */
public static boolean createFileIfMissing(Path path, FileAttribute<?>... attrs) throws IOException {
  requireNonNull(path);
  try {
    java.nio.file.Files.createFile(path, attrs);
  } catch (FileAlreadyExistsException e) {
    return false;
  }
  return true;
}
 
Example #29
Source File: TempFileHelper.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Creates a temporary file in the given directory, or in in the
 * temporary directory if dir is {@code null}.
 */
static Path createTempFile(Path dir,
                           String prefix,
                           String suffix,
                           FileAttribute<?>[] attrs)
    throws IOException
{
    return create(dir, prefix, suffix, false, attrs);
}
 
Example #30
Source File: AcmeFileSystemProvider.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
}