Java Code Examples for java.nio.file.attribute.BasicFileAttributes#fileKey()

The following examples show how to use java.nio.file.attribute.BasicFileAttributes#fileKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultProjectFilesystem.java    From buck with Apache License 2.0 6 votes vote down vote up
private boolean willLoop(Path p, BasicFileAttributes attrs) {
  try {
    Object thisKey = attrs.fileKey();
    for (DirWalkState s : state) {
      if (s.isRootSentinel) {
        continue;
      }
      Object thatKey = s.attrs.fileKey();
      if (thisKey != null && thatKey != null) {
        if (thisKey.equals(thatKey)) {
          return true;
        }
      } else if (Files.isSameFile(p, s.dir)) {
        return true;
      }
    }
  } catch (IOException e) {
    return true;
  }
  return false;
}
 
Example 2
Source File: FileUtil.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
public static Object getFileKey(File file) {
  try {
    Path fileFullPath = Paths.get(file.getAbsolutePath());
    if (fileFullPath != null) {
      BasicFileAttributes basicAttr = Files.readAttributes(fileFullPath, BasicFileAttributes.class);
      return basicAttr.fileKey();
    }
  } catch (Throwable ex) {
    logger.error("Error getting file attributes for file=" + file, ex);
  }
  return file.toString();
}
 
Example 3
Source File: PollingWatchService.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             int sensitivityInSeconds)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensitivityInSeconds);
        return watchKey;
    }

}
 
Example 4
Source File: PollingWatchService.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private PollingWatchKey doPrivilegedRegister(Path path,
                                             Set<? extends WatchEvent.Kind<?>> events,
                                             int sensitivityInSeconds)
    throws IOException
{
    // check file is a directory and get its file key if possible
    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
    if (!attrs.isDirectory()) {
        throw new NotDirectoryException(path.toString());
    }
    Object fileKey = attrs.fileKey();
    if (fileKey == null)
        throw new AssertionError("File keys must be supported");

    // grab close lock to ensure that watch service cannot be closed
    synchronized (closeLock()) {
        if (!isOpen())
            throw new ClosedWatchServiceException();

        PollingWatchKey watchKey;
        synchronized (map) {
            watchKey = map.get(fileKey);
            if (watchKey == null) {
                // new registration
                watchKey = new PollingWatchKey(path, this, fileKey);
                map.put(fileKey, watchKey);
            } else {
                // update to existing registration
                watchKey.disable();
            }
        }
        watchKey.enable(events, sensitivityInSeconds);
        return watchKey;
    }

}
 
Example 5
Source File: DurablePositionTrack.java    From flume-plugin with Apache License 2.0 5 votes vote down vote up
private String getFileKey(File file) {
    BasicFileAttributes basicAttr;
    try {
        basicAttr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
    } catch (IOException e) {
        return new String();
    }
    Object obj = basicAttr.fileKey();
    if (obj == null) {
        return new String();
    }
    return obj.toString();
}
 
Example 6
Source File: TailFileEventReader.java    From flume-plugin with Apache License 2.0 5 votes vote down vote up
public static String getFileKey(File file) {
    BasicFileAttributes basicAttr;
    try {
        basicAttr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
    } catch (IOException e) {
        return new String();
    }
    Object obj = basicAttr.fileKey();
    if (obj == null) {
        return new String();
    }
    return obj.toString();
}
 
Example 7
Source File: MCRFileAttributes.java    From mycore with GNU General Public License v3.0 4 votes vote down vote up
public static <T> MCRFileAttributes<T> fromAttributes(BasicFileAttributes attrs, String md5) {
    return new MCRFileAttributes<>(FileType.fromAttribute(attrs), attrs.size(), (T) attrs.fileKey(), md5,
        attrs.creationTime(), attrs.lastModifiedTime(), attrs.lastAccessTime());
}
 
Example 8
Source File: WindowsFS.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** 
 * 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();
}
 
Example 9
Source File: SingerUtils.java    From singer with Apache License 2.0 3 votes vote down vote up
/**
 * Extracts a long representing an inode number from a Unix FileKey
 *
 * The following code shows how the string representation of a UnixFileKey is generated:
 *   StringBuilder sb = new StringBuilder();
 *   sb.append("(dev=")
 *     .append(Long.toHexString(st_dev))
 *     .append(",ino=")
 *     .append(st_ino)
 *     .append(')');
 *   return sb.toString();
 *
 * So, in order to get the inode number, we will parse the string.
 *
 * @param filePath the path to a file
 * @return The inode number of that file
 */
public static long getFileInode(Path filePath) throws IOException {
  BasicFileAttributes attrs = Files.readAttributes(filePath, BasicFileAttributes.class);
  Object fileKey = attrs.fileKey();
  String keyStr = fileKey.toString();
  String inodeStr = keyStr.substring(keyStr.indexOf("ino=") + 4, keyStr.indexOf(")"));
  return Long.parseLong(inodeStr);
}