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);
}
});
}
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);
}
});
}
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.");
}
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();
}
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);
}
});
}
}
Aggregations