Search in sources :

Example 1 with BasicFileAttributeView

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();
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with BasicFileAttributeView

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");
    }
}
Also used : BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) FileTime(java.nio.file.attribute.FileTime)

Example 3 with BasicFileAttributeView

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;
}
Also used : Path(java.nio.file.Path) FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) HashMap(java.util.HashMap) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileStore(java.nio.file.FileStore) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 4 with BasicFileAttributeView

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;
}
Also used : FileOwnerAttributeView(java.nio.file.attribute.FileOwnerAttributeView) HashMap(java.util.HashMap) IOException(java.io.IOException) Date(java.util.Date) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView) FileStore(java.nio.file.FileStore) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDateFormat(java.text.SimpleDateFormat) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 5 with BasicFileAttributeView

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;
}
Also used : Path(java.nio.file.Path) BasicFileAttributeView(java.nio.file.attribute.BasicFileAttributeView)

Aggregations

BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)28 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)13 Path (java.nio.file.Path)11 FileTime (java.nio.file.attribute.FileTime)11 IOException (java.io.IOException)7 Test (org.junit.Test)7 File (java.io.File)3 NoSuchFileException (java.nio.file.NoSuchFileException)3 FileOwnerAttributeView (java.nio.file.attribute.FileOwnerAttributeView)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 InputStream (java.io.InputStream)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 FileStore (java.nio.file.FileStore)2 SecureDirectoryStream (java.nio.file.SecureDirectoryStream)2 StandardCopyOption (java.nio.file.StandardCopyOption)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2