use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class NdkLibraryDescription method findSources.
@VisibleForTesting
protected ImmutableSortedSet<SourcePath> findSources(final ProjectFilesystem filesystem, final Path buildRulePath) {
final ImmutableSortedSet.Builder<SourcePath> srcs = ImmutableSortedSet.naturalOrder();
try {
final Path rootDirectory = filesystem.resolve(buildRulePath);
Files.walkFileTree(rootDirectory, EnumSet.of(FileVisitOption.FOLLOW_LINKS), /* maxDepth */
Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (EXTENSIONS_REGEX.matcher(file.toString()).matches()) {
srcs.add(new PathSourcePath(filesystem, buildRulePath.resolve(rootDirectory.relativize(file))));
}
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
return srcs.build();
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class IjProjectTemplateDataPreparer method createExcludes.
public ImmutableSet<IjFolder> createExcludes(final IjModule module) throws IOException {
final ImmutableSet.Builder<IjFolder> excludesBuilder = ImmutableSet.builder();
final Path moduleBasePath = module.getModuleBasePath();
projectFilesystem.walkRelativeFileTree(moduleBasePath, new FileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
// When we create excludes for that module.
if (filesystemTraversalBoundaryPaths.contains(dir) && !moduleBasePath.equals(dir)) {
return FileVisitResult.SKIP_SUBTREE;
}
if (isRootAndroidResourceDirectory(module, dir)) {
return FileVisitResult.SKIP_SUBTREE;
}
if (!referencedFolderPaths.contains(dir)) {
excludesBuilder.add(new ExcludeFolder(dir));
return FileVisitResult.SKIP_SUBTREE;
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
return excludesBuilder.build();
}
use of java.nio.file.attribute.BasicFileAttributes in project elasticsearch by elastic.
the class StoreRecoveryTests method hardLinksSupported.
public boolean hardLinksSupported(Path path) throws IOException {
try {
Files.createFile(path.resolve("foo.bar"));
Files.createLink(path.resolve("test"), path.resolve("foo.bar"));
BasicFileAttributes destAttr = Files.readAttributes(path.resolve("test"), BasicFileAttributes.class);
BasicFileAttributes sourceAttr = Files.readAttributes(path.resolve("foo.bar"), BasicFileAttributes.class);
// we won't get here - no permission ;)
return destAttr.fileKey() != null && destAttr.fileKey().equals(sourceAttr.fileKey());
} catch (AccessControlException ex) {
// if we run into that situation we know it's supported.
return true;
} catch (UnsupportedOperationException ex) {
return false;
}
}
use of java.nio.file.attribute.BasicFileAttributes in project elasticsearch by elastic.
the class ExceptionSerializationTests method testExceptionRegistration.
public void testExceptionRegistration() throws ClassNotFoundException, IOException, URISyntaxException {
final Set<Class<?>> notRegistered = new HashSet<>();
final Set<Class<?>> hasDedicatedWrite = new HashSet<>();
final Set<Class<?>> registered = new HashSet<>();
final String path = "/org/elasticsearch";
final Path startPath = PathUtils.get(ElasticsearchException.class.getProtectionDomain().getCodeSource().getLocation().toURI()).resolve("org").resolve("elasticsearch");
final Set<? extends Class<?>> ignore = Sets.newHashSet(CancellableThreadsTests.CustomException.class, org.elasticsearch.rest.BytesRestResponseTests.WithHeadersException.class, AbstractClientHeadersTestCase.InternalException.class);
FileVisitor<Path> visitor = new FileVisitor<Path>() {
private Path pkgPrefix = PathUtils.get(path).getParent();
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
Path next = pkgPrefix.resolve(dir.getFileName());
if (ignore.contains(next)) {
return FileVisitResult.SKIP_SUBTREE;
}
pkgPrefix = next;
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
checkFile(file.getFileName().toString());
return FileVisitResult.CONTINUE;
}
private void checkFile(String filename) {
if (filename.endsWith(".class") == false) {
return;
}
try {
checkClass(loadClass(filename));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
private void checkClass(Class<?> clazz) {
if (ignore.contains(clazz) || isAbstract(clazz.getModifiers()) || isInterface(clazz.getModifiers())) {
return;
}
if (isEsException(clazz) == false) {
return;
}
if (ElasticsearchException.isRegistered(clazz.asSubclass(Throwable.class), Version.CURRENT) == false && ElasticsearchException.class.equals(clazz.getEnclosingClass()) == false) {
notRegistered.add(clazz);
} else if (ElasticsearchException.isRegistered(clazz.asSubclass(Throwable.class), Version.CURRENT)) {
registered.add(clazz);
try {
if (clazz.getMethod("writeTo", StreamOutput.class) != null) {
hasDedicatedWrite.add(clazz);
}
} catch (Exception e) {
// fair enough
}
}
}
private boolean isEsException(Class<?> clazz) {
return ElasticsearchException.class.isAssignableFrom(clazz);
}
private Class<?> loadClass(String filename) throws ClassNotFoundException {
StringBuilder pkg = new StringBuilder();
for (Path p : pkgPrefix) {
pkg.append(p.getFileName().toString()).append(".");
}
pkg.append(filename.substring(0, filename.length() - 6));
return getClass().getClassLoader().loadClass(pkg.toString());
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
throw exc;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
pkgPrefix = pkgPrefix.getParent();
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(startPath, visitor);
final Path testStartPath = PathUtils.get(ExceptionSerializationTests.class.getResource(path).toURI());
Files.walkFileTree(testStartPath, visitor);
assertTrue(notRegistered.remove(TestException.class));
assertTrue(notRegistered.remove(UnknownHeaderException.class));
assertTrue("Classes subclassing ElasticsearchException must be registered \n" + notRegistered.toString(), notRegistered.isEmpty());
// check
assertTrue(registered.removeAll(ElasticsearchException.getRegisteredKeys()));
assertEquals(registered.toString(), 0, registered.size());
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class ZipStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) {
if (filesystem.exists(pathToZipFile)) {
context.postEvent(ConsoleEvent.severe("Attempting to overwrite an existing zip: %s", pathToZipFile));
return StepExecutionResult.ERROR;
}
// Since filesystem traversals can be non-deterministic, sort the entries we find into
// a tree map before writing them out.
final Map<String, Pair<CustomZipEntry, Optional<Path>>> entries = Maps.newTreeMap();
FileVisitor<Path> pathFileVisitor = new SimpleFileVisitor<Path>() {
private boolean isSkipFile(Path file) {
return !paths.isEmpty() && !paths.contains(file);
}
private String getEntryName(Path path) {
Path relativePath = junkPaths ? path.getFileName() : baseDir.relativize(path);
return MorePaths.pathWithUnixSeparators(relativePath);
}
private CustomZipEntry getZipEntry(String entryName, final Path path, BasicFileAttributes attr) throws IOException {
boolean isDirectory = filesystem.isDirectory(path);
if (isDirectory) {
entryName += "/";
}
CustomZipEntry entry = new CustomZipEntry(entryName);
// We want deterministic ZIPs, so avoid mtimes.
entry.setFakeTime();
entry.setCompressionLevel(isDirectory ? ZipCompressionLevel.MIN_COMPRESSION_LEVEL.getValue() : compressionLevel.getValue());
// If we're using STORED files, we must manually set the CRC, size, and compressed size.
if (entry.getMethod() == ZipEntry.STORED && !isDirectory) {
entry.setSize(attr.size());
entry.setCompressedSize(attr.size());
entry.setCrc(new ByteSource() {
@Override
public InputStream openStream() throws IOException {
return filesystem.newFileInputStream(path);
}
}.hash(Hashing.crc32()).padToLong());
}
long externalAttributes = filesystem.getFileAttributesForZipEntry(path);
LOG.verbose("Setting mode for entry %s path %s to 0x%08X", entryName, path, externalAttributes);
entry.setExternalAttributes(externalAttributes);
return entry;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!isSkipFile(file)) {
CustomZipEntry entry = getZipEntry(getEntryName(file), file, attrs);
entries.put(entry.getName(), new Pair<>(entry, Optional.of(file)));
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if (!dir.equals(baseDir) && !isSkipFile(dir)) {
CustomZipEntry entry = getZipEntry(getEntryName(dir), dir, attrs);
entries.put(entry.getName(), new Pair<>(entry, Optional.empty()));
}
return FileVisitResult.CONTINUE;
}
};
try (BufferedOutputStream baseOut = new BufferedOutputStream(filesystem.newFileOutputStream(pathToZipFile));
CustomZipOutputStream out = ZipOutputStreams.newOutputStream(baseOut, THROW_EXCEPTION)) {
filesystem.walkRelativeFileTree(baseDir, pathFileVisitor);
// Write the entries out using the iteration order of the tree map above.
for (Pair<CustomZipEntry, Optional<Path>> entry : entries.values()) {
out.putNextEntry(entry.getFirst());
if (entry.getSecond().isPresent()) {
try (InputStream input = filesystem.newFileInputStream(entry.getSecond().get())) {
ByteStreams.copy(input, out);
}
}
out.closeEntry();
}
} catch (IOException e) {
context.logError(e, "Error creating zip file %s", pathToZipFile);
return StepExecutionResult.ERROR;
}
return StepExecutionResult.SUCCESS;
}
Aggregations