Table of Contents

ACL (Access Control List)

Access control list (ACL) Access control lists (ACLs) provide an additional layer of access control to files and directories stored in extended attributes on the filesystem. These ACLs are set and verified with the setfacl and getfacl Linux commands.

Examples

 def set_acl(path, user, permissions):
     subprocess.run(['setfacl', '-m', f'u:{user}:{permissions}', path], check=True)
 def get_acl(path):
     result = subprocess.run(['getfacl', path], capture_output=True, text=True)
     return result.stdout
 # Example usage
 file_path = '/path/to/file'
 set_acl(file_path, 'username', 'rwx')
 acl_info = get_acl(file_path)
 print(acl_info)
 ```

 public class AclExample {
     public static void setAcl(String filePath, String userName, Set permissions) throws IOException {
         Path path = Paths.get(filePath);
         UserPrincipal user = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(userName);
         AclEntry entry = AclEntry.newBuilder()
                 .setType(AclEntryType.ALLOW)
                 .setPrincipal(user)
                 .setPermissions(permissions)
                 .build();
         List acl = Files.getFileAttributeView(path, AclFileAttributeView.class).getAcl();
         acl.add(entry);
         Files.getFileAttributeView(path, AclFileAttributeView.class).setAcl(acl);
     }
     public static List getAcl(String filePath) throws IOException {
         Path path = Paths.get(filePath);
         return Files.getFileAttributeView(path, AclFileAttributeView.class).getAcl();
     }
     public static void main(String[] args) throws IOException {
         String filePath = "/path/to/file";
         String userName = "username";
         Set permissions = EnumSet.of(AclEntryPermission.READ_DATA, AclEntryPermission.WRITE_DATA, AclEntryPermission.EXECUTE);
         setAcl(filePath, userName, permissions);
         List acl = getAcl(filePath);
         for (AclEntry entry : acl) {
             System.out.println(entry);
         }
     }
 }
 ```

Summary