Search in sources :

Example 66 with FileSystem

use of java.nio.file.FileSystem in project lucene-solr by apache.

the class TestIOUtils method testNVME.

public void testNVME() throws Exception {
    assumeFalse("windows is not supported", Constants.WINDOWS);
    Path dir = createTempDir();
    dir = FilterPath.unwrap(dir).toRealPath();
    // fake ssd
    FileStore root = new MockFileStore(dir.toString() + " (/dev/nvme0n1p1)", "btrfs", "/dev/nvme0n1p1");
    // make a fake /dev/nvme0n1p1 for it
    Path devdir = dir.resolve("dev");
    Files.createDirectories(devdir);
    Files.createFile(devdir.resolve("nvme0n1p1"));
    // make a fake /sys/block/nvme0n1/queue/rotational file for it
    Path sysdir = dir.resolve("sys").resolve("block").resolve("nvme0n1").resolve("queue");
    Files.createDirectories(sysdir);
    try (OutputStream o = Files.newOutputStream(sysdir.resolve("rotational"))) {
        o.write("0\n".getBytes(StandardCharsets.US_ASCII));
    }
    // As test for the longest path match, add some other devices (that have no queue/rotational), too:
    Files.createFile(dir.resolve("sys").resolve("block").resolve("nvme0"));
    Files.createFile(dir.resolve("sys").resolve("block").resolve("dummy"));
    Files.createFile(dir.resolve("sys").resolve("block").resolve("nvm"));
    Map<String, FileStore> mappings = Collections.singletonMap(dir.toString(), root);
    FileSystem mockLinux = new MockLinuxFileSystemProvider(dir.getFileSystem(), mappings, dir).getFileSystem(null);
    Path mockPath = mockLinux.getPath(dir.toString());
    assertFalse(IOUtils.spinsLinux(mockPath));
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) FileStore(java.nio.file.FileStore) OutputStream(java.io.OutputStream) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem)

Example 67 with FileSystem

use of java.nio.file.FileSystem in project lucene-solr by apache.

the class TestIOUtils method testNfsSpins.

public void testNfsSpins() throws Exception {
    Path dir = createTempDir();
    dir = FilterPath.unwrap(dir).toRealPath();
    // fake nfs
    FileStore root = new MockFileStore(dir.toString() + " (somenfsserver:/some/mount)", "nfs", "somenfsserver:/some/mount");
    Map<String, FileStore> mappings = Collections.singletonMap(dir.toString(), root);
    FileSystem mockLinux = new MockLinuxFileSystemProvider(dir.getFileSystem(), mappings, dir).getFileSystem(null);
    Path mockPath = mockLinux.getPath(dir.toString());
    assertTrue(IOUtils.spinsLinux(mockPath));
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) FileStore(java.nio.file.FileStore) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem)

Example 68 with FileSystem

use of java.nio.file.FileSystem in project lucene-solr by apache.

the class TestIOUtils method testRotatingPlatters.

public void testRotatingPlatters() throws Exception {
    assumeFalse("windows is not supported", Constants.WINDOWS);
    Path dir = createTempDir();
    dir = FilterPath.unwrap(dir).toRealPath();
    // fake ssd
    FileStore root = new MockFileStore(dir.toString() + " (/dev/zzz1)", "reiser4", "/dev/zzz1");
    // make a fake /dev/zzz1 for it
    Path devdir = dir.resolve("dev");
    Files.createDirectories(devdir);
    Files.createFile(devdir.resolve("zzz1"));
    // make a fake /sys/block/zzz/queue/rotational file for it
    Path sysdir = dir.resolve("sys").resolve("block").resolve("zzz").resolve("queue");
    Files.createDirectories(sysdir);
    try (OutputStream o = Files.newOutputStream(sysdir.resolve("rotational"))) {
        o.write("1\n".getBytes(StandardCharsets.US_ASCII));
    }
    Map<String, FileStore> mappings = Collections.singletonMap(dir.toString(), root);
    FileSystem mockLinux = new MockLinuxFileSystemProvider(dir.getFileSystem(), mappings, dir).getFileSystem(null);
    Path mockPath = mockLinux.getPath(dir.toString());
    assertTrue(IOUtils.spinsLinux(mockPath));
}
Also used : FilterPath(org.apache.lucene.mockfile.FilterPath) Path(java.nio.file.Path) FileStore(java.nio.file.FileStore) OutputStream(java.io.OutputStream) FileSystem(java.nio.file.FileSystem) FilterFileSystem(org.apache.lucene.mockfile.FilterFileSystem)

Example 69 with FileSystem

use of java.nio.file.FileSystem in project logging-log4j2 by apache.

the class ResolverUtilTest method createJar.

static void createJar(final URI jarURI, final File workDir, final File f) throws Exception {
    final Map<String, String> env = new HashMap<>();
    env.put("create", "true");
    final URI uri = URI.create("jar:file://" + jarURI.getRawPath());
    try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        final Path path = zipfs.getPath(workDir.toPath().relativize(f.toPath()).toString());
        if (path.getParent() != null) {
            Files.createDirectories(path.getParent());
        }
        Files.copy(f.toPath(), path, StandardCopyOption.REPLACE_EXISTING);
    }
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileSystem(java.nio.file.FileSystem) URI(java.net.URI)

Example 70 with FileSystem

use of java.nio.file.FileSystem in project beam by apache.

the class ApexYarnLauncher method createJar.

/**
   * Create a jar file from the given directory.
   * @param dir source directory
   * @param jarFile jar file name
   * @throws IOException when file cannot be created
   */
public static void createJar(File dir, File jarFile) throws IOException {
    final Map<String, ?> env = Collections.singletonMap("create", "true");
    if (jarFile.exists() && !jarFile.delete()) {
        throw new RuntimeException("Failed to remove " + jarFile);
    }
    URI uri = URI.create("jar:" + jarFile.toURI());
    try (final FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
        Files.createDirectory(zipfs.getPath("META-INF"));
        try (final OutputStream out = Files.newOutputStream(zipfs.getPath(JarFile.MANIFEST_NAME))) {
            if (!manifestFile.exists()) {
                new Manifest().write(out);
            } else {
                FileUtils.copyFile(manifestFile, out);
            }
        }
        final java.nio.file.Path root = dir.toPath();
        Files.walkFileTree(root, new java.nio.file.SimpleFileVisitor<Path>() {

            String relativePath;

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                relativePath = root.relativize(dir).toString();
                if (!relativePath.isEmpty()) {
                    if (!relativePath.endsWith("/")) {
                        relativePath += "/";
                    }
                    if (!relativePath.equals("META-INF/")) {
                        final Path dstDir = zipfs.getPath(relativePath);
                        Files.createDirectory(dstDir);
                    }
                }
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String name = relativePath + file.getFileName();
                if (!JarFile.MANIFEST_NAME.equals(name)) {
                    try (final OutputStream out = Files.newOutputStream(zipfs.getPath(name))) {
                        FileUtils.copyFile(file.toFile(), out);
                    }
                }
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                relativePath = root.relativize(dir.getParent()).toString();
                if (!relativePath.isEmpty() && !relativePath.endsWith("/")) {
                    relativePath += "/";
                }
                return super.postVisitDirectory(dir, exc);
            }
        });
    }
}
Also used : Path(java.nio.file.Path) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) Manifest(java.util.jar.Manifest) URI(java.net.URI) FileSystem(java.nio.file.FileSystem) JarFile(java.util.jar.JarFile) File(java.io.File) Path(java.nio.file.Path) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileSystem (java.nio.file.FileSystem)159 Path (java.nio.file.Path)112 Test (org.junit.Test)66 IOException (java.io.IOException)25 File (java.io.File)17 ArrayList (java.util.ArrayList)14 FilterFileSystem (org.apache.lucene.mockfile.FilterFileSystem)13 URI (java.net.URI)11 FilterPath (org.apache.lucene.mockfile.FilterPath)11 InputStream (java.io.InputStream)10 OutputStream (java.io.OutputStream)10 FileStore (java.nio.file.FileStore)8 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)6 HashMap (java.util.HashMap)6 DefaultProjectFilesystemDelegate (com.facebook.buck.io.DefaultProjectFilesystemDelegate)5 ProjectFilesystemDelegate (com.facebook.buck.io.ProjectFilesystemDelegate)5 WindowsFS (org.apache.lucene.mockfile.WindowsFS)5 FSDirectory (org.apache.lucene.store.FSDirectory)5 BuckConfig (com.facebook.buck.cli.BuckConfig)4 FakeBuckConfig (com.facebook.buck.cli.FakeBuckConfig)4