use of java.nio.file.attribute.BasicFileAttributes in project elasticsearch by elastic.
the class TestingFs method wrap.
// wrap hadoop rawlocalfilesystem to behave less crazy
static RawLocalFileSystem wrap(final Path base) {
final FileSystemProvider baseProvider = base.getFileSystem().provider();
return new RawLocalFileSystem() {
private org.apache.hadoop.fs.Path box(Path path) {
return new org.apache.hadoop.fs.Path(path.toUri());
}
private Path unbox(org.apache.hadoop.fs.Path path) {
return baseProvider.getPath(path.toUri());
}
@Override
protected org.apache.hadoop.fs.Path getInitialWorkingDirectory() {
return box(base);
}
@Override
public void setPermission(org.apache.hadoop.fs.Path path, FsPermission permission) {
// no execution, thank you very much!
}
// pretend we don't support symlinks (which causes hadoop to want to do crazy things),
// returning the boolean does not seem to really help, link-related operations are still called.
@Override
public boolean supportsSymlinks() {
return false;
}
@Override
public FileStatus getFileLinkStatus(org.apache.hadoop.fs.Path path) throws IOException {
return getFileStatus(path);
}
@Override
public org.apache.hadoop.fs.Path getLinkTarget(org.apache.hadoop.fs.Path path) throws IOException {
return path;
}
@Override
public FileStatus getFileStatus(org.apache.hadoop.fs.Path path) throws IOException {
BasicFileAttributes attributes;
try {
attributes = Files.readAttributes(unbox(path), BasicFileAttributes.class);
} catch (NoSuchFileException e) {
// unfortunately, specific exceptions are not guaranteed. don't wrap hadoop over a zip filesystem or something.
FileNotFoundException fnfe = new FileNotFoundException("File " + path + " does not exist");
fnfe.initCause(e);
throw fnfe;
}
// we set similar values to raw local filesystem, except we are never a symlink
long length = attributes.size();
boolean isDir = attributes.isDirectory();
int blockReplication = 1;
long blockSize = getDefaultBlockSize(path);
long modificationTime = attributes.creationTime().toMillis();
return new FileStatus(length, isDir, blockReplication, blockSize, modificationTime, path);
}
};
}
use of java.nio.file.attribute.BasicFileAttributes in project elasticsearch by elastic.
the class FsBlobContainer method listBlobsByPrefix.
@Override
public Map<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException {
// If we get duplicate files we should just take the last entry
Map<String, BlobMetaData> builder = new HashMap<>();
blobNamePrefix = blobNamePrefix == null ? "" : blobNamePrefix;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, blobNamePrefix + "*")) {
for (Path file : stream) {
final BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
if (attrs.isRegularFile()) {
builder.put(file.getFileName().toString(), new PlainBlobMetaData(file.getFileName().toString(), attrs.size()));
}
}
}
return unmodifiableMap(builder);
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class SocketLossKiller method arm.
/**
* Start the service, recording the current socket file info.
*
* If the socket does not exist or can't be accessed, the kill task will be called.
*
* This is meant to be called after the server is started and socket created, such as in
* {@code NailMain}. Repeated calls are no-ops.
*/
public synchronized void arm() {
if (isArmed.getAndSet(true)) {
// Already armed.
return;
}
try {
BasicFileAttributes socketFileAttributes = readFileAttributes(absolutePathToSocket);
checkSocketTask = scheduledExecutorService.scheduleAtFixedRate(() -> checkSocket(socketFileAttributes), 0, 5000, TimeUnit.MILLISECONDS);
} catch (IOException e) {
LOG.error(e, "Failed to read socket when arming SocketLossKiller.");
cancelAndKill();
return;
}
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class DirectoryCleaner method computeDirSizeBytesRecursively.
private static long computeDirSizeBytesRecursively(Path directoryPath) throws IOException {
final AtomicLong totalSizeBytes = new AtomicLong(0);
Files.walkFileTree(directoryPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
totalSizeBytes.addAndGet(attrs.size());
return FileVisitResult.CONTINUE;
}
});
return totalSizeBytes.get();
}
use of java.nio.file.attribute.BasicFileAttributes in project buck by facebook.
the class ProjectWorkspace method assertPathsEqual.
/**
* For every file in the template directory whose name ends in {@code .expected}, checks that an
* equivalent file has been written in the same place under the destination directory.
*
* @param templateSubdirectory An optional subdirectory to check. Only files in this directory
* will be compared.
*/
private void assertPathsEqual(final Path templateSubdirectory, final Path destinationSubdirectory) throws IOException {
SimpleFileVisitor<Path> copyDirVisitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String fileName = file.getFileName().toString();
if (fileName.endsWith(EXPECTED_SUFFIX)) {
// Get File for the file that should be written, but without the ".expected" suffix.
Path generatedFileWithSuffix = destinationSubdirectory.resolve(templateSubdirectory.relativize(file));
Path directory = generatedFileWithSuffix.getParent();
Path observedFile = directory.resolve(MorePaths.getNameWithoutExtension(file));
assertFilesEqual(file, observedFile);
}
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(templateSubdirectory, copyDirVisitor);
}
Aggregations