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();
}
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);
}
}
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());
}
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;
}
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();
}
Aggregations