示例#1
0
  /**
   * Given a file object, this creates a PLUSFile representation filled with appropriate metadata,
   * file owner, and so on.
   *
   * @param file
   * @return
   */
  public static PLUSFile extractMetadata(File file, AbstractProvenanceClient client) {
    if (!file.exists() || !file.canRead()) return null;

    PLUSFile f = new PLUSFile(file);

    // Try to figure out who owns this file, if it exists.
    if (f.getOwner() == null) {
      // This is a java-7 ism that permits us to access the file owner.
      try {
        Path path = Paths.get(file.getAbsolutePath());
        UserPrincipal owner = Files.getOwner(path);
        String username = owner.getName();

        try {
          PLUSActor a = client.actorExistsByName(username);
          if (a == null) a = new PLUSActor(username);

          f.setOwner(a);
        } catch (PLUSException e) {
          log.warning(e.getMessage());
        }
      } catch (IOException exc) {
        log.warning(exc.getMessage());
      }
    }

    try {
      f.getMetadata().put("isLink", "" + file.getCanonicalFile().equals(file.getAbsoluteFile()));
    } catch (Exception exc) {;
    }
    try {
      f.getMetadata().put("exists", "" + file.exists());
    } catch (Exception exc) {;
    }
    try {
      f.getMetadata().put("path", file.getAbsolutePath());
    } catch (Exception exc) {;
    }
    try {
      f.getMetadata().put("canonical", file.getCanonicalPath());
    } catch (Exception exc) {;
    }
    try {
      f.getMetadata().put("directory", "" + file.isDirectory());
    } catch (Exception exc) {;
    }
    try {
      f.getMetadata().put("lastmodified", "" + file.lastModified());
    } catch (Exception exc) {;
    }
    try {
      f.getMetadata().put("size", "" + file.length());
    } catch (Exception exc) {;
    }

    return f;
  } // End extractMetadata
 public static void main(String[] args) throws IOException {
   Path path = Paths.get(FileService.FILE_WITH_LINES);
   System.out.println("Files.isDirectory = " + Files.isDirectory(path));
   System.out.println("Files.isExecutable = " + Files.isExecutable(path));
   System.out.println("Files.isHidden = " + Files.isHidden(path));
   System.out.println("Files.isReadable = " + Files.isReadable(path));
   System.out.println("Files.isWritable = " + Files.isWritable(path));
   System.out.println("Files.getLastModifiedTime = " + Files.getLastModifiedTime(path));
   System.out.println("Files.getOwner = " + Files.getOwner(path));
   System.out.println("Files.size = " + Files.size(path));
   // change attribute: Files.setAttribute(path, "dos:hidden", false);
 }
  /*
   * Make file non-readable by modifying its permissions.
   * If file supports "posix" attributes, then modify it.
   * Otherwise check for "acl" attributes.
   */
  private static void makeFileNonReadable(String file) throws IOException {
    Path filePath = Paths.get(file);
    Set<String> supportedAttr = filePath.getFileSystem().supportedFileAttributeViews();

    if (supportedAttr.contains("posix")) {
      Files.setPosixFilePermissions(filePath, PosixFilePermissions.fromString("-w--w----"));
    } else if (supportedAttr.contains("acl")) {
      UserPrincipal fileOwner = Files.getOwner(filePath);

      AclFileAttributeView view = Files.getFileAttributeView(filePath, AclFileAttributeView.class);

      AclEntry entry =
          AclEntry.newBuilder()
              .setType(AclEntryType.DENY)
              .setPrincipal(fileOwner)
              .setPermissions(AclEntryPermission.READ_DATA)
              .build();

      List<AclEntry> acl = view.getAcl();
      acl.add(0, entry);
      view.setAcl(acl);
    }
  }