Search in sources :

Example 71 with BasicFileAttributes

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);
        }
    };
}
Also used : Path(java.nio.file.Path) FileStatus(org.apache.hadoop.fs.FileStatus) RawLocalFileSystem(org.apache.hadoop.fs.RawLocalFileSystem) NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) FileSystemProvider(java.nio.file.spi.FileSystemProvider) FsPermission(org.apache.hadoop.fs.permission.FsPermission) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 72 with BasicFileAttributes

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);
}
Also used : BlobPath(org.elasticsearch.common.blobstore.BlobPath) Path(java.nio.file.Path) PlainBlobMetaData(org.elasticsearch.common.blobstore.support.PlainBlobMetaData) HashMap(java.util.HashMap) PlainBlobMetaData(org.elasticsearch.common.blobstore.support.PlainBlobMetaData) BlobMetaData(org.elasticsearch.common.blobstore.BlobMetaData) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 73 with BasicFileAttributes

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;
    }
}
Also used : IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 74 with BasicFileAttributes

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();
}
Also used : Path(java.nio.file.Path) AtomicLong(java.util.concurrent.atomic.AtomicLong) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 75 with BasicFileAttributes

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);
}
Also used : Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)119 Path (java.nio.file.Path)93 IOException (java.io.IOException)87 FileVisitResult (java.nio.file.FileVisitResult)66 File (java.io.File)18 Test (org.junit.Test)13 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)11 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 FileNotFoundException (java.io.FileNotFoundException)7 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)6 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)5 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)5 SourcePath (com.facebook.buck.rules.SourcePath)4 ImmutableList (com.google.common.collect.ImmutableList)4 OutputStream (java.io.OutputStream)4 URI (java.net.URI)4 FileSystem (java.nio.file.FileSystem)4 FileVisitor (java.nio.file.FileVisitor)4