Thursday, 13 October 2016

Set File permissions in Java

File permissions are operating system specific, For example, operating systems like Linux, Mac (or) any other UNIX flavors provides owner, group and others permissions. In this post, I am going to explain generic permissions followed by setting POSIX (Owner, Group, Others) related permissions.

Java File class provides following methods to update the file permissions.

Method
Description
public boolean setExecutable(boolean executable, boolean ownerOnly)
If the argument 'executable' is true then it sets the access permission to allow execute operations; if false to disallow execute operations.

If the argument ownerOnly is set to true, the execute permission applies only to the owner's execute permission; otherwise, it applies to everybody.
public boolean setExecutable(boolean executable)
This method is used to set owner executable permissions. Equivalent to the statement ‘file.setExecutable(executable, true)’.
public boolean setReadable(boolean readable, boolean ownerOnly)
If the argument readable is true then it sets the access permission to allow read operations; if false to disallow read operations.

If the argument ownerOnly is set to true, the read permission applies only to the owner's read permission; otherwise, it applies to everybody.
public boolean setReadable(boolean readable)
This method is used to set owner read permissions. Equivalent to the statement ‘file.setReadable (readable, true)’.
public boolean setWritable(boolean writable,boolean ownerOnly)
If the argument writable is true then it sets the access permission to allow write operations; if false to disallow write operations.

If the argument ownerOnly is set to true, the write permission applies only to the owner's write permission; otherwise, it applies to everybody.
public boolean setWritable(boolean writable)
This method is used to set owner write permissions. Equivalent to the statement ‘file.setWritable (writable, true)’.

In addition to above methods, following methods are used to test, whether a file has read, write, execute permissions.

public boolean canRead()
public boolean canWrite()
public boolean canExecute()
import java.io.File;

public class FilePermissionDemo {
 public static void main(String args[]) {

  File file = new File("C:\\Users\\Downloads\\softwares\\ChromeSetup.exe");

  System.out.println("Is Execute allow : " + file.canExecute());
  System.out.println("Is Write allow : " + file.canWrite());
  System.out.println("Is Read allow : " + file.canRead());

  file.setExecutable(false);
  file.setReadable(false);
  file.setWritable(false);

  System.out.println("Is Execute allow : " + file.canExecute());
  System.out.println("Is Write allow : " + file.canWrite());
  System.out.println("Is Read allow : " + file.canRead());

 }
}

Working with POSIX Permissions
If your operating system is POSIX compliant, then java provides the flexibility to set the Read-Write-Execute permissions to owner, group, others separately.

PosixFilePermission class provides following enum constants to specify the permissions.

Constant
Description
GROUP_EXECUTE
Execute/search permission, group.
GROUP_READ
Read permission, group.
GROUP_WRITE
Write permission, group.
OTHERS_EXECUTE
Execute/search permission, others.
OTHERS_READ
Read permission, others.
OTHERS_WRITE
Write permission, others.
OWNER_EXECUTE
Execute/search permission, owner.
OWNER_READ
Read permission, owner.
OWNER_WRITE
Write permission, owner.

Files class provides ‘setPosixFilePermissions’ method, to set the POSIX permissions.

public static Path setPosixFilePermissions(Path path, Set<PosixFilePermission> perms)

Following statements add Read-Write-Execute permissions to the owner.

Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
                
/* add owners permission */
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_EXECUTE);

Files.setPosixFilePermissions(path, perms);


Following is the complete working application.
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
import java.util.Set;

public class FilePermissionDemo {
 public static void main(String args[]) throws IOException {

  File file = new File("C:\\Users\\Downloads\\softwares\\ChromeSetup.exe");

  System.out.println("Is Execute allow : " + file.canExecute());
  System.out.println("Is Write allow : " + file.canWrite());
  System.out.println("Is Read allow : " + file.canRead());

  boolean isPosix = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");

  if (!isPosix) {
   System.out.println("Operating system don't support posix");
   return;
  }

  Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
  /* add owners permission */
  perms.add(PosixFilePermission.OWNER_READ);
  perms.add(PosixFilePermission.OWNER_WRITE);
  perms.add(PosixFilePermission.OWNER_EXECUTE);

  Files.setPosixFilePermissions(file.toPath(), perms);

  System.out.println("Is Execute allow : " + file.canExecute());
  System.out.println("Is Write allow : " + file.canWrite());
  System.out.println("Is Read allow : " + file.canRead());

 }
}



No comments:

Post a Comment