Example #1
0
 private void setOwnership(File dir, String group, String owner) {
   try {
     Path path = dir.toPath();
     UserPrincipalLookupService lookupService =
         FileSystems.getDefault().getUserPrincipalLookupService();
     GroupPrincipal groupPrincipal = lookupService.lookupPrincipalByGroupName(group);
     UserPrincipal userPrincipal = lookupService.lookupPrincipalByName(owner);
     PosixFileAttributeView pfav =
         Files.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
     pfav.setGroup(groupPrincipal);
     pfav.setOwner(userPrincipal);
   } catch (Exception ex) {
     cp.appendln("Unable to set the file group and owner for\n   " + dir);
   }
 }
Example #2
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);
    }
  }