Search in sources :

Example 1 with FileVisitResult

use of java.nio.file.FileVisitResult in project flink by apache.

the class AbstractContainerOverlay method addPathRecursively.

/**
	 * Add a path recursively to the container specification.
	 *
	 * If the path is a directory, the directory itself (not just its contents) is added to the target path.
	 *
	 * The execute bit is preserved; permissions aren't.
	 *
	 * @param sourcePath the path to add.
	 * @param targetPath the target path.
	 * @param env the specification to mutate.
     * @throws IOException
     */
protected void addPathRecursively(final File sourcePath, final Path targetPath, final ContainerSpecification env) throws IOException {
    final java.nio.file.Path sourceRoot = sourcePath.toPath().getParent();
    Files.walkFileTree(sourcePath.toPath(), new SimpleFileVisitor<java.nio.file.Path>() {

        @Override
        public FileVisitResult visitFile(java.nio.file.Path file, BasicFileAttributes attrs) throws IOException {
            java.nio.file.Path relativePath = sourceRoot.relativize(file);
            ContainerSpecification.Artifact.Builder artifact = ContainerSpecification.Artifact.newBuilder().setSource(new Path(file.toUri())).setDest(new Path(targetPath, relativePath.toString())).setExecutable(Files.isExecutable(file)).setCachable(true).setExtract(false);
            env.getArtifacts().add(artifact.build());
            return super.visitFile(file, attrs);
        }
    });
}
Also used : Path(org.apache.flink.core.fs.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) ContainerSpecification(org.apache.flink.runtime.clusterframework.ContainerSpecification) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with FileVisitResult

use of java.nio.file.FileVisitResult in project hadoop by apache.

the class NNUpgradeUtil method doPreUpgrade.

/**
   * Perform any steps that must succeed across all storage dirs/JournalManagers
   * involved in an upgrade before proceeding onto the actual upgrade stage. If
   * a call to any JM's or local storage dir's doPreUpgrade method fails, then
   * doUpgrade will not be called for any JM. The existing current dir is
   * renamed to previous.tmp, and then a new, empty current dir is created.
   *
   * @param conf configuration for creating {@link EditLogFileOutputStream}
   * @param sd the storage directory to perform the pre-upgrade procedure.
   * @throws IOException in the event of error
   */
static void doPreUpgrade(Configuration conf, StorageDirectory sd) throws IOException {
    LOG.info("Starting upgrade of storage directory " + sd.getRoot());
    // rename current to tmp
    renameCurToTmp(sd);
    final Path curDir = sd.getCurrentDir().toPath();
    final Path tmpDir = sd.getPreviousTmp().toPath();
    Files.walkFileTree(tmpDir, /* do not follow links */
    Collections.<FileVisitOption>emptySet(), 1, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            String name = file.getFileName().toString();
            if (Files.isRegularFile(file) && name.startsWith(NNStorage.NameNodeFile.EDITS.getName())) {
                Path newFile = curDir.resolve(name);
                Files.createLink(newFile, file);
            }
            return super.visitFile(file, attrs);
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 3 with FileVisitResult

use of java.nio.file.FileVisitResult in project cryptomator by cryptomator.

the class UpgradeVersion3to4 method upgrade.

@Override
protected void upgrade(Vault vault, Cryptor cryptor) throws UpgradeFailedException {
    Path dataDir = vault.getPath().resolve("d");
    Path metadataDir = vault.getPath().resolve("m");
    if (!Files.isDirectory(dataDir)) {
        // empty vault. no migration needed.
        return;
    }
    try {
        Files.walkFileTree(dataDir, new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String name = file.getFileName().toString();
                if (name.endsWith(LONG_FILENAME_SUFFIX)) {
                    migrateLong(metadataDir, file);
                } else {
                    migrate(file, attrs);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                throw exc;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        LOG.error("Migration failed.", e);
        throw new UpgradeFailedException(localization.getString("upgrade.version3to4.err.io"));
    }
    LOG.info("Migration finished.");
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 4 with FileVisitResult

use of java.nio.file.FileVisitResult in project buck by facebook.

the class FilePathHashLoader method get.

@Override
public HashCode get(Path root) throws IOException {
    // In case the root path is a directory, collect all files contained in it and sort them before
    // hashing to avoid non-deterministic directory traversal order from influencing the hash.
    final ImmutableSortedSet.Builder<Path> files = ImmutableSortedSet.naturalOrder();
    Files.walkFileTree(defaultCellRoot.resolve(root), ImmutableSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
            files.add(file);
            return FileVisitResult.CONTINUE;
        }
    });
    Hasher hasher = Hashing.sha1().newHasher();
    for (Path file : files.build()) {
        file = defaultCellRoot.resolve(file).toRealPath();
        boolean assumeModified = assumeModifiedFiles.contains(file);
        Path relativePath = MorePaths.relativize(defaultCellRoot, file);
        // For each file add its path to the hasher suffixed by whether we assume the file to be
        // modified or not. This way files with different paths always result in different hashes and
        // files that are assumed to be modified get different hashes than all unmodified files.
        StringHashing.hashStringAndLength(hasher, relativePath.toString());
        hasher.putBoolean(assumeModified);
    }
    return hasher.hash();
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) Hasher(com.google.common.hash.Hasher) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) FileVisitResult(java.nio.file.FileVisitResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 5 with FileVisitResult

use of java.nio.file.FileVisitResult in project elasticsearch by elastic.

the class ESLoggerUsageChecker method checkLoggerUsage.

private static void checkLoggerUsage(Consumer<WrongLoggerUsage> wrongUsageCallback, String... classDirectories) throws IOException {
    for (String classDirectory : classDirectories) {
        Path root = Paths.get(classDirectory);
        if (Files.isDirectory(root) == false) {
            throw new IllegalArgumentException(root + " should be an existing directory");
        }
        Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (Files.isRegularFile(file) && file.getFileName().toString().endsWith(".class")) {
                    try (InputStream in = Files.newInputStream(file)) {
                        ESLoggerUsageChecker.check(wrongUsageCallback, in);
                    }
                }
                return super.visitFile(file, attrs);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileVisitResult (java.nio.file.FileVisitResult)74 Path (java.nio.file.Path)67 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)66 IOException (java.io.IOException)65 File (java.io.File)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 JarEntry (java.util.jar.JarEntry)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 FileOutputStream (java.io.FileOutputStream)3 JarOutputStream (java.util.jar.JarOutputStream)3 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 FileSystemException (io.vertx.core.file.FileSystemException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2