Exemple #1
0
  public void setAttrs(FileAttrs attrs) throws IOException {
    UserPrincipal posixOwner;
    GroupPrincipal posixGroup;
    Set<PosixFilePermission> posixPerms;
    UserPrincipalLookupService lookup;

    if (System.getProperty("os.name").startsWith("Windows")) {
      Files.setAttribute(this.path.toPath(), "dos:archive", attrs.getDosArchive());
      Files.setAttribute(this.path.toPath(), "dos:hidden", attrs.getDosHidden());
      Files.setAttribute(this.path.toPath(), "dos:readonly", attrs.getDosReadonly());
      Files.setAttribute(this.path.toPath(), "dos:system", attrs.getDosSystem());
    } else {
      lookup = this.path.toPath().getFileSystem().getUserPrincipalLookupService();

      posixOwner = lookup.lookupPrincipalByName(attrs.getPosixOwner());
      posixGroup = lookup.lookupPrincipalByGroupName(attrs.getPosixGroup());
      posixPerms = PosixFilePermissions.fromString(attrs.getPosixPermission());

      Files.setOwner(this.path.toPath(), posixOwner);
      Files.getFileAttributeView(this.path.toPath(), PosixFileAttributeView.class)
          .setGroup(posixGroup);
      Files.setPosixFilePermissions(this.path.toPath(), posixPerms);
    }
  }
Exemple #2
0
  public FileAttrs getAttrs() throws IOException {
    FileAttrs result;
    BasicFileAttributes attr;
    DosFileAttributes dosAttr;
    PosixFileAttributes posixAttr;

    result = new FileAttrs();
    attr = Files.readAttributes(this.path.toPath(), BasicFileAttributes.class);

    result.setCtime(attr.creationTime().toMillis());
    result.setMtime(attr.lastModifiedTime().toMillis());
    // result.append("symlink", attr.isSymbolicLink()); //Redundant
    result.setSize(attr.size());

    if (System.getProperty("os.name").startsWith("Windows")) {
      dosAttr = Files.readAttributes(this.path.toPath(), DosFileAttributes.class);

      result.setDosArchive(dosAttr.isArchive());
      result.setDosHidden(dosAttr.isHidden());
      result.setDosReadonly(dosAttr.isReadOnly());
      result.setDosSystem(dosAttr.isSystem());
    } else {
      posixAttr = Files.readAttributes(this.path.toPath(), PosixFileAttributes.class);

      result.setPosixSymlink(this.isSymlink());

      if (result.getPosixSymlink()) {
        result.setLinkTo(Files.readSymbolicLink(this.path.toPath()).toString());
      }

      result.setPosixOwner(posixAttr.owner().getName());
      result.setPosixGroup(posixAttr.group().getName());
      result.setPosixPermission(PosixFilePermissions.toString(posixAttr.permissions()));
    }

    return result;
  }