Search in sources :

Example 6 with FileSystemProvider

use of java.nio.file.spi.FileSystemProvider 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 7 with FileSystemProvider

use of java.nio.file.spi.FileSystemProvider in project dex2jar by pxb1988.

the class Dex2jar method createZip.

private static FileSystem createZip(Path output) throws IOException {
    Map<String, Object> env = new HashMap<>();
    env.put("create", "true");
    Files.deleteIfExists(output);
    Path parent = output.getParent();
    if (parent != null && !Files.exists(parent)) {
        Files.createDirectories(parent);
    }
    for (FileSystemProvider p : FileSystemProvider.installedProviders()) {
        String s = p.getScheme();
        if ("jar".equals(s) || "zip".equalsIgnoreCase(s)) {
            return p.newFileSystem(output, env);
        }
    }
    throw new IOException("cant find zipfs support");
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileSystemProvider(java.nio.file.spi.FileSystemProvider) IOException(java.io.IOException)

Aggregations

FileSystemProvider (java.nio.file.spi.FileSystemProvider)7 IOException (java.io.IOException)2 URI (java.net.URI)2 Path (java.nio.file.Path)2 FileNotFoundException (java.io.FileNotFoundException)1 FileSystem (java.nio.file.FileSystem)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 HashMap (java.util.HashMap)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 RawLocalFileSystem (org.apache.hadoop.fs.RawLocalFileSystem)1 FsPermission (org.apache.hadoop.fs.permission.FsPermission)1 FilterFileSystemProvider (org.apache.lucene.mockfile.FilterFileSystemProvider)1 BeforeClass (org.junit.BeforeClass)1