Search in sources :

Example 41 with BasicFileAttributes

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

the class DeletingVisitorTest method testVisitFileRelativizesAgainstBase.

@Test
public void testVisitFileRelativizesAgainstBase() throws IOException {
    final PathCondition filter = new PathCondition() {

        @Override
        public boolean accept(final Path baseDir, final Path relativePath, final BasicFileAttributes attrs) {
            final Path expected = Paths.get("relative");
            assertEquals(expected, relativePath);
            return true;
        }

        @Override
        public void beforeFileTreeWalk() {
        }
    };
    final Path base = Paths.get("/a/b/c");
    final DeletingVisitorHelper visitor = new DeletingVisitorHelper(base, Arrays.asList(filter), false);
    final Path child = Paths.get("/a/b/c/relative");
    visitor.visitFile(child, null);
}
Also used : Path(java.nio.file.Path) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

Example 42 with BasicFileAttributes

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

the class CorePropertiesLocator method discover.

@Override
public List<CoreDescriptor> discover(final CoreContainer cc) {
    logger.debug("Looking for core definitions underneath {}", rootDirectory);
    final List<CoreDescriptor> cds = Lists.newArrayList();
    try {
        Set<FileVisitOption> options = new HashSet<>();
        options.add(FileVisitOption.FOLLOW_LINKS);
        final int maxDepth = 256;
        Files.walkFileTree(this.rootDirectory, options, maxDepth, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.getFileName().toString().equals(PROPERTIES_FILENAME)) {
                    CoreDescriptor cd = buildCoreDescriptor(file, cc);
                    if (cd != null) {
                        logger.debug("Found core {} in {}", cd.getName(), cd.getInstanceDir());
                        cds.add(cd);
                    }
                    return FileVisitResult.SKIP_SIBLINGS;
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                // otherwise, log a warning and continue to try and load other cores
                if (file.equals(rootDirectory)) {
                    logger.error("Error reading core root directory {}: {}", file, exc);
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error reading core root directory");
                }
                logger.warn("Error visiting {}: {}", file, exc);
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Couldn't walk file tree under " + this.rootDirectory, e);
    }
    logger.info("Found {} core definitions underneath {}", cds.size(), rootDirectory);
    if (cds.size() > 0) {
        logger.info("Cores are: {}", cds.stream().map(CoreDescriptor::getName).collect(Collectors.toList()));
    }
    return cds;
}
Also used : Path(java.nio.file.Path) FileVisitOption(java.nio.file.FileVisitOption) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Example 43 with BasicFileAttributes

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

the class WindowsFS method getKey.

/** 
   * Returns file "key" (e.g. inode) for the specified path 
   */
private Object getKey(Path existing) throws IOException {
    BasicFileAttributeView view = Files.getFileAttributeView(existing, BasicFileAttributeView.class);
    BasicFileAttributes attributes = view.readAttributes();
    return attributes.fileKey();
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 44 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project intellij-community by JetBrains.

the class JrtHandler method createEntriesMap.

@NotNull
@Override
protected Map<String, EntryInfo> createEntriesMap() throws IOException {
    Map<String, EntryInfo> map = ContainerUtil.newHashMap();
    map.put("", createRootEntry());
    Path root = getFileSystem().getPath("/modules");
    if (!Files.exists(root))
        throw new FileNotFoundException("JRT root missing");
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            process(dir, attrs);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            process(file, attrs);
            return FileVisitResult.CONTINUE;
        }

        private void process(Path entry, BasicFileAttributes attrs) throws IOException {
            int pathLength = entry.getNameCount();
            if (pathLength > 1) {
                Path relativePath = entry.subpath(1, pathLength);
                String path = relativePath.toString();
                if (!map.containsKey(path)) {
                    EntryInfo parent = map.get(pathLength > 2 ? relativePath.getParent().toString() : "");
                    if (parent == null)
                        throw new IOException("Out of order: " + entry);
                    String shortName = entry.getFileName().toString();
                    long modified = attrs.lastModifiedTime().toMillis();
                    map.put(path, new EntryInfo(shortName, attrs.isDirectory(), attrs.size(), modified, parent));
                }
            }
        }
    });
    return map;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) NotNull(org.jetbrains.annotations.NotNull)

Example 45 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project wildfly by wildfly.

the class JBeretSubsystemParsingTestCase method testLegacySubsystems.

@Test
public void testLegacySubsystems() throws Exception {
    // Get a list of all the logging_x_x.xml files
    final Pattern pattern = Pattern.compile("(.*-subsystem)_\\d+_\\d+\\.xml");
    // Using the CP as that's the standardSubsystemTest will use to find the config file
    final String cp = WildFlySecurityManager.getPropertyPrivileged("java.class.path", ".");
    final String[] entries = cp.split(Pattern.quote(File.pathSeparator));
    final List<String> configs = new ArrayList<>();
    for (String entry : entries) {
        final Path path = Paths.get(entry);
        if (Files.isDirectory(path)) {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                    final String name = file.getFileName().toString();
                    if (pattern.matcher(name).matches()) {
                        configs.add("/" + name);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        }
    }
    // The paths shouldn't be empty
    Assert.assertFalse("No configs were found", configs.isEmpty());
    for (String configId : configs) {
        // Run the standard subsystem test, but don't compare the XML as it should never match
        standardSubsystemTest(configId, false);
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) ArrayList(java.util.ArrayList) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test)

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