Search in sources :

Example 36 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project geode by apache.

the class LogFilterTest method testAcceptFileWithCreateTimeAvailable.

@Test
public void testAcceptFileWithCreateTimeAvailable() throws Exception {
    long now = System.currentTimeMillis();
    Path path = mock(Path.class);
    when(path.toFile()).thenReturn(mock(File.class));
    when(path.toFile().lastModified()).thenReturn(System.currentTimeMillis());
    BasicFileAttributes attributes = mock(BasicFileAttributes.class);
    when(path.getFileSystem()).thenReturn(mock(FileSystem.class));
    when(path.getFileSystem().provider()).thenReturn(mock(FileSystemProvider.class));
    when(path.getFileSystem().provider().readAttributes(path, BasicFileAttributes.class)).thenReturn(attributes);
    // a filter with no start/end date should accept this file
    LogFilter filter = new LogFilter(Level.INFO, null, null);
    assertThat(filter.acceptsFile(path)).isTrue();
    // a filter with a start date of now should not accept the file
    filter = new LogFilter(Level.INFO, LocalDateTime.now(), null);
    assertThat(filter.acceptsFile(path)).isFalse();
    // a filter with a start date of now minus an hour should not accept the file
    filter = new LogFilter(Level.INFO, LocalDateTime.now().minusHours(1), null);
    assertThat(filter.acceptsFile(path)).isTrue();
    // a filter with an end date of now should accept the file
    filter = new LogFilter(Level.INFO, null, LocalDateTime.now());
    assertThat(filter.acceptsFile(path)).isTrue();
    // a filter with an end date of an hour ago should accept the file
    filter = new LogFilter(Level.INFO, null, LocalDateTime.now().minusHours(1));
    assertThat(filter.acceptsFile(path)).isTrue();
}
Also used : Path(java.nio.file.Path) FileSystemProvider(java.nio.file.spi.FileSystemProvider) FileSystem(java.nio.file.FileSystem) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 37 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project ignite by apache.

the class LocalIgfsSecondaryFileSystem method info.

/** {@inheritDoc} */
@Override
public IgfsFile info(final IgfsPath path) {
    File file = fileForPath(path);
    if (!file.exists())
        return null;
    boolean isDir = file.isDirectory();
    PosixFileAttributes attrs = LocalFileSystemUtils.posixAttributes(file);
    Map<String, String> props = LocalFileSystemUtils.posixAttributesToMap(attrs);
    BasicFileAttributes basicAttrs = LocalFileSystemUtils.basicAttributes(file);
    if (isDir) {
        return new LocalFileSystemIgfsFile(path, false, true, 0, basicAttrs.lastAccessTime().toMillis(), basicAttrs.lastModifiedTime().toMillis(), 0, props);
    } else {
        return new LocalFileSystemIgfsFile(path, file.isFile(), false, 0, basicAttrs.lastAccessTime().toMillis(), basicAttrs.lastModifiedTime().toMillis(), file.length(), props);
    }
}
Also used : LocalFileSystemIgfsFile(org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemIgfsFile) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) IgfsFile(org.apache.ignite.igfs.IgfsFile) LocalFileSystemIgfsFile(org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemIgfsFile) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 38 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project ignite by apache.

the class LocalIgfsSecondaryFileSystem method deleteRecursive.

/**
     * Delete directory recursively.
     *
     * @param f Directory.
     * @param deleteIfExists Ignore delete errors if the file doesn't exist.
     * @return {@code true} if successful.
     */
private boolean deleteRecursive(File f, boolean deleteIfExists) {
    BasicFileAttributes attrs;
    try {
        attrs = Files.readAttributes(f.toPath(), BasicFileAttributes.class, LinkOption.NOFOLLOW_LINKS);
    } catch (IOException ignore) {
        return deleteIfExists && !f.exists();
    }
    if (!attrs.isDirectory() || attrs.isSymbolicLink())
        return f.delete() || (deleteIfExists && !f.exists());
    File[] entries = f.listFiles();
    if (entries != null) {
        for (File entry : entries) {
            boolean res = deleteRecursive(entry, true);
            if (!res)
                return false;
        }
    }
    return f.delete() || (deleteIfExists && !f.exists());
}
Also used : IOException(java.io.IOException) IgfsFile(org.apache.ignite.igfs.IgfsFile) LocalFileSystemIgfsFile(org.apache.ignite.internal.processors.igfs.secondary.local.LocalFileSystemIgfsFile) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 39 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project karaf by apache.

the class Profiles method loadProfiles.

public static Map<String, Profile> loadProfiles(final Path root) throws IOException {
    final Map<String, Profile> profiles = new HashMap<>();
    Files.walkFileTree(root, new SimpleFileVisitor<Path>() {

        ProfileBuilder builder;

        @Override
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            Path fileName = dir.getFileName();
            if (fileName != null && (fileName.toString().endsWith(PROFILE_FOLDER_SUFFIX) || fileName.toString().endsWith(PROFILE_FOLDER_SUFFIX + "/"))) {
                String profileId = root.relativize(dir).toString();
                if (profileId.endsWith("/")) {
                    profileId = profileId.substring(0, profileId.length() - 1);
                }
                profileId = profileId.replaceAll(root.getFileSystem().getSeparator(), "-");
                profileId = profileId.substring(0, profileId.length() - PROFILE_FOLDER_SUFFIX.length());
                builder = ProfileBuilder.Factory.create(profileId);
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
            if (exc != null) {
                throw exc;
            }
            if (builder != null) {
                Profile profile = builder.getProfile();
                profiles.put(profile.getId(), profile);
                builder = null;
            }
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            if (builder != null) {
                String pid = file.getFileName().toString();
                byte[] data = Files.readAllBytes(file);
                builder.addFileConfiguration(pid, data);
            }
            return FileVisitResult.CONTINUE;
        }
    });
    return profiles;
}
Also used : Path(java.nio.file.Path) HashMap(java.util.HashMap) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) ProfileBuilder(org.apache.karaf.profile.ProfileBuilder) Profile(org.apache.karaf.profile.Profile) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 40 with BasicFileAttributes

use of java.nio.file.attribute.BasicFileAttributes in project karaf by apache.

the class FilesStream method files.

private static Stream<Path> files(Path cur, String glob) {
    String prefix;
    String rem;
    int idx = glob.lastIndexOf('/');
    if (idx >= 0) {
        prefix = glob.substring(0, idx + 1);
        rem = glob.substring(idx + 1);
    } else {
        prefix = "";
        rem = glob;
    }
    Path dir = cur.resolve(prefix);
    final PathMatcher matcher = dir.getFileSystem().getPathMatcher("glob:" + rem);
    Stream.Builder<Path> stream = Stream.builder();
    try {
        Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.equals(dir)) {
                    return FileVisitResult.CONTINUE;
                }
                if (Files.isHidden(file)) {
                    return FileVisitResult.SKIP_SUBTREE;
                }
                Path r = dir.relativize(file);
                if (matcher.matches(r)) {
                    stream.add(file);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (!Files.isHidden(file)) {
                    Path r = dir.relativize(file);
                    if (matcher.matches(r)) {
                        stream.add(file);
                    }
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        LOGGER.warn("Error generating filenames", e);
    }
    return stream.build();
}
Also used : Path(java.nio.file.Path) PathMatcher(java.nio.file.PathMatcher) Stream(java.util.stream.Stream) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) 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