use of java.nio.file.attribute.BasicFileAttributeView 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();
}
use of java.nio.file.attribute.BasicFileAttributeView in project hadoop by apache.
the class RawLocalFileSystem method setTimes.
/**
* Sets the {@link Path}'s last modified time and last access time to
* the given valid times.
*
* @param mtime the modification time to set (only if no less than zero).
* @param atime the access time to set (only if no less than zero).
* @throws IOException if setting the times fails.
*/
@Override
public void setTimes(Path p, long mtime, long atime) throws IOException {
try {
BasicFileAttributeView view = Files.getFileAttributeView(pathToFile(p).toPath(), BasicFileAttributeView.class);
FileTime fmtime = (mtime >= 0) ? FileTime.fromMillis(mtime) : null;
FileTime fatime = (atime >= 0) ? FileTime.fromMillis(atime) : null;
view.setTimes(fmtime, fatime, null);
} catch (NoSuchFileException e) {
throw new FileNotFoundException("File " + p + " does not exist");
}
}
use of java.nio.file.attribute.BasicFileAttributeView in project nifi by apache.
the class ListFile method createAttributes.
@Override
protected Map<String, String> createAttributes(final FileInfo fileInfo, final ProcessContext context) {
final Map<String, String> attributes = new HashMap<>();
final String fullPath = fileInfo.getFullPathFileName();
final File file = new File(fullPath);
final Path filePath = file.toPath();
final Path directoryPath = new File(getPath(context)).toPath();
final Path relativePath = directoryPath.toAbsolutePath().relativize(filePath.getParent());
String relativePathString = relativePath.toString();
relativePathString = relativePathString.isEmpty() ? "." + File.separator : relativePathString + File.separator;
final Path absPath = filePath.toAbsolutePath();
final String absPathString = absPath.getParent().toString() + File.separator;
attributes.put(CoreAttributes.PATH.key(), relativePathString);
attributes.put(CoreAttributes.FILENAME.key(), fileInfo.getFileName());
attributes.put(CoreAttributes.ABSOLUTE_PATH.key(), absPathString);
try {
FileStore store = Files.getFileStore(filePath);
if (store.supportsFileAttributeView("basic")) {
try {
final DateFormat formatter = new SimpleDateFormat(FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US);
BasicFileAttributeView view = Files.getFileAttributeView(filePath, BasicFileAttributeView.class);
BasicFileAttributes attrs = view.readAttributes();
attributes.put(FILE_SIZE_ATTRIBUTE, Long.toString(attrs.size()));
attributes.put(FILE_LAST_MODIFY_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastModifiedTime().toMillis())));
attributes.put(FILE_CREATION_TIME_ATTRIBUTE, formatter.format(new Date(attrs.creationTime().toMillis())));
attributes.put(FILE_LAST_ACCESS_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastAccessTime().toMillis())));
} catch (Exception ignore) {
}
// allow other attributes if these fail
}
if (store.supportsFileAttributeView("owner")) {
try {
FileOwnerAttributeView view = Files.getFileAttributeView(filePath, FileOwnerAttributeView.class);
attributes.put(FILE_OWNER_ATTRIBUTE, view.getOwner().getName());
} catch (Exception ignore) {
}
// allow other attributes if these fail
}
if (store.supportsFileAttributeView("posix")) {
try {
PosixFileAttributeView view = Files.getFileAttributeView(filePath, PosixFileAttributeView.class);
attributes.put(FILE_PERMISSIONS_ATTRIBUTE, PosixFilePermissions.toString(view.readAttributes().permissions()));
attributes.put(FILE_GROUP_ATTRIBUTE, view.readAttributes().group().getName());
} catch (Exception ignore) {
}
// allow other attributes if these fail
}
} catch (IOException ioe) {
// well then this FlowFile gets none of these attributes
getLogger().warn("Error collecting attributes for file {}, message is {}", new Object[] { absPathString, ioe.getMessage() });
}
return attributes;
}
use of java.nio.file.attribute.BasicFileAttributeView in project nifi by apache.
the class GetFile method getAttributesFromFile.
protected Map<String, String> getAttributesFromFile(final Path file) {
Map<String, String> attributes = new HashMap<>();
try {
FileStore store = Files.getFileStore(file);
if (store.supportsFileAttributeView("basic")) {
try {
final DateFormat formatter = new SimpleDateFormat(FILE_MODIFY_DATE_ATTR_FORMAT, Locale.US);
BasicFileAttributeView view = Files.getFileAttributeView(file, BasicFileAttributeView.class);
BasicFileAttributes attrs = view.readAttributes();
attributes.put(FILE_LAST_MODIFY_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastModifiedTime().toMillis())));
attributes.put(FILE_CREATION_TIME_ATTRIBUTE, formatter.format(new Date(attrs.creationTime().toMillis())));
attributes.put(FILE_LAST_ACCESS_TIME_ATTRIBUTE, formatter.format(new Date(attrs.lastAccessTime().toMillis())));
} catch (Exception ignore) {
}
// allow other attributes if these fail
}
if (store.supportsFileAttributeView("owner")) {
try {
FileOwnerAttributeView view = Files.getFileAttributeView(file, FileOwnerAttributeView.class);
attributes.put(FILE_OWNER_ATTRIBUTE, view.getOwner().getName());
} catch (Exception ignore) {
}
// allow other attributes if these fail
}
if (store.supportsFileAttributeView("posix")) {
try {
PosixFileAttributeView view = Files.getFileAttributeView(file, PosixFileAttributeView.class);
attributes.put(FILE_PERMISSIONS_ATTRIBUTE, PosixFilePermissions.toString(view.readAttributes().permissions()));
attributes.put(FILE_GROUP_ATTRIBUTE, view.readAttributes().group().getName());
} catch (Exception ignore) {
}
// allow other attributes if these fail
}
} catch (IOException ioe) {
// well then this FlowFile gets none of these attributes
}
return attributes;
}
use of java.nio.file.attribute.BasicFileAttributeView in project sakuli by ConSol.
the class LogCleanUpResultServiceImplTest method createFileWithDate.
private Path createFileWithDate(Path path, FileTime todayMinus15) throws IOException {
Path file = Files.createFile(path);
BasicFileAttributeView fileAttributeView = Files.getFileAttributeView(file, BasicFileAttributeView.class);
fileAttributeView.setTimes(todayMinus15, todayMinus15, todayMinus15);
return file;
}
Aggregations