Search in sources :

Example 41 with FileVisitResult

use of java.nio.file.FileVisitResult in project vert.x by eclipse.

the class FileSystemImpl method chmodInternal.

protected BlockingAction<Void> chmodInternal(String path, String perms, String dirPerms, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(path);
    Set<PosixFilePermission> permissions = PosixFilePermissions.fromString(perms);
    Set<PosixFilePermission> dirPermissions = dirPerms == null ? null : PosixFilePermissions.fromString(dirPerms);
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path target = vertx.resolveFile(path).toPath();
                if (dirPermissions != null) {
                    Files.walkFileTree(target, new SimpleFileVisitor<Path>() {

                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                            //The directory entries typically have different permissions to the files, e.g. execute permission
                            //or can't cd into it
                            Files.setPosixFilePermissions(dir, dirPermissions);
                            return FileVisitResult.CONTINUE;
                        }

                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            Files.setPosixFilePermissions(file, permissions);
                            return FileVisitResult.CONTINUE;
                        }
                    });
                } else {
                    Files.setPosixFilePermissions(target, permissions);
                }
            } catch (SecurityException e) {
                throw new FileSystemException("Accessed denied for chmod on " + path);
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileSystemException(io.vertx.core.file.FileSystemException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 42 with FileVisitResult

use of java.nio.file.FileVisitResult in project vert.x by eclipse.

the class FileSystemImpl method copyInternal.

private BlockingAction<Void> copyInternal(String from, String to, boolean recursive, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(from);
    Objects.requireNonNull(to);
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path source = vertx.resolveFile(from).toPath();
                Path target = vertx.resolveFile(to).toPath();
                if (recursive) {
                    Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                            Path targetDir = target.resolve(source.relativize(dir));
                            try {
                                Files.copy(dir, targetDir);
                            } catch (FileAlreadyExistsException e) {
                                if (!Files.isDirectory(targetDir)) {
                                    throw e;
                                }
                            }
                            return FileVisitResult.CONTINUE;
                        }

                        @Override
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            Files.copy(file, target.resolve(source.relativize(file)));
                            return FileVisitResult.CONTINUE;
                        }
                    });
                } else {
                    Files.copy(source, target);
                }
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystemException(io.vertx.core.file.FileSystemException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 43 with FileVisitResult

use of java.nio.file.FileVisitResult in project elasticsearch by elastic.

the class RelocationIT method testCancellationCleansTempFiles.

public void testCancellationCleansTempFiles() throws Exception {
    final String indexName = "test";
    final String p_node = internalCluster().startNode();
    prepareCreate(indexName, Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1, IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).get();
    internalCluster().startNode();
    internalCluster().startNode();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = scaledRandomIntBetween(25, 250);
    for (int i = 0; i < numDocs; i++) {
        requests.add(client().prepareIndex(indexName, "type").setSource("{}", XContentType.JSON));
    }
    indexRandom(true, requests);
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("3").setWaitForGreenStatus().get().isTimedOut());
    flush();
    int allowedFailures = randomIntBetween(3, 10);
    logger.info("--> blocking recoveries from primary (allowed failures: [{}])", allowedFailures);
    CountDownLatch corruptionCount = new CountDownLatch(allowedFailures);
    ClusterService clusterService = internalCluster().getInstance(ClusterService.class, p_node);
    MockTransportService mockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, p_node);
    for (DiscoveryNode node : clusterService.state().nodes()) {
        if (!node.equals(clusterService.localNode())) {
            mockTransportService.addDelegate(internalCluster().getInstance(TransportService.class, node.getName()), new RecoveryCorruption(mockTransportService.original(), corruptionCount));
        }
    }
    client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)).get();
    corruptionCount.await();
    logger.info("--> stopping replica assignment");
    assertAcked(client().admin().cluster().prepareUpdateSettings().setTransientSettings(Settings.builder().put(EnableAllocationDecider.CLUSTER_ROUTING_ALLOCATION_ENABLE_SETTING.getKey(), "none")));
    logger.info("--> wait for all replica shards to be removed, on all nodes");
    assertBusy(() -> {
        for (String node : internalCluster().getNodeNames()) {
            if (node.equals(p_node)) {
                continue;
            }
            ClusterState state = client(node).admin().cluster().prepareState().setLocal(true).get().getState();
            assertThat(node + " indicates assigned replicas", state.getRoutingTable().index(indexName).shardsWithState(ShardRoutingState.UNASSIGNED).size(), equalTo(1));
        }
    });
    logger.info("--> verifying no temporary recoveries are left");
    for (String node : internalCluster().getNodeNames()) {
        NodeEnvironment nodeEnvironment = internalCluster().getInstance(NodeEnvironment.class, node);
        for (final Path shardLoc : nodeEnvironment.availableShardPaths(new ShardId(indexName, "_na_", 0))) {
            if (Files.exists(shardLoc)) {
                assertBusy(() -> {
                    try {
                        Files.walkFileTree(shardLoc, new SimpleFileVisitor<Path>() {

                            @Override
                            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                                assertThat("found a temporary recovery file: " + file, file.getFileName().toString(), not(startsWith("recovery.")));
                                return FileVisitResult.CONTINUE;
                            }
                        });
                    } catch (IOException e) {
                        throw new AssertionError("failed to walk file tree starting at [" + shardLoc + "]", e);
                    }
                });
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ClusterState(org.elasticsearch.cluster.ClusterState) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) MockTransportService(org.elasticsearch.test.transport.MockTransportService) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ShardId(org.elasticsearch.index.shard.ShardId) ClusterService(org.elasticsearch.cluster.service.ClusterService) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 44 with FileVisitResult

use of java.nio.file.FileVisitResult in project elasticsearch by elastic.

the class OldIndexUtils method copyIndex.

// randomly distribute the files from src over dests paths
public static void copyIndex(final Logger logger, final Path src, final String folderName, final Path... dests) throws IOException {
    Path destinationDataPath = dests[randomInt(dests.length - 1)];
    for (Path dest : dests) {
        Path indexDir = dest.resolve(folderName);
        assertFalse(Files.exists(indexDir));
        Files.createDirectories(indexDir);
    }
    Files.walkFileTree(src, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Path relativeDir = src.relativize(dir);
            for (Path dest : dests) {
                Path destDir = dest.resolve(folderName).resolve(relativeDir);
                Files.createDirectories(destDir);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (file.getFileName().toString().equals(IndexWriter.WRITE_LOCK_NAME)) {
                // skip lock file, we don't need it
                logger.trace("Skipping lock file: {}", file);
                return FileVisitResult.CONTINUE;
            }
            Path relativeFile = src.relativize(file);
            Path destFile = destinationDataPath.resolve(folderName).resolve(relativeFile);
            logger.trace("--> Moving {} to {}", relativeFile, destFile);
            Files.move(file, destFile);
            assertFalse(Files.exists(file));
            assertTrue(Files.exists(destFile));
            return FileVisitResult.CONTINUE;
        }
    });
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 45 with FileVisitResult

use of java.nio.file.FileVisitResult in project elasticsearch by elastic.

the class AbstractSnapshotIntegTestCase method numberOfFiles.

public static int numberOfFiles(Path dir) throws IOException {
    final AtomicInteger count = new AtomicInteger();
    Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            count.incrementAndGet();
            return FileVisitResult.CONTINUE;
        }
    });
    return count.get();
}
Also used : Path(java.nio.file.Path) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Aggregations

FileVisitResult (java.nio.file.FileVisitResult)74 Path (java.nio.file.Path)67 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)66 IOException (java.io.IOException)65 File (java.io.File)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 JarEntry (java.util.jar.JarEntry)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableSet (com.google.common.collect.ImmutableSet)3 FileOutputStream (java.io.FileOutputStream)3 JarOutputStream (java.util.jar.JarOutputStream)3 PathSourcePath (com.facebook.buck.rules.PathSourcePath)2 SourcePath (com.facebook.buck.rules.SourcePath)2 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)2 FileSystemException (io.vertx.core.file.FileSystemException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2