通过list修改ACL权限

    1. package org.mmedu.example.chap08.acl;
    2. import org.apache.hadoop.conf.Configuration;
    3. import org.apache.hadoop.fs.FileSystem;
    4. import org.apache.hadoop.fs.Path;
    5. import org.apache.hadoop.fs.permission.AclEntry;
    6. import java.io.IOException;
    7. import java.util.ArrayList;
    8. import java.util.List;
    9. /**
    10. * @author Administrator
    11. */
    12. public class SetAclList {
    13. public static void main(String[] args) {
    14. //1.设定配置信息
    15. System.setProperty("HADOOP_USER_NAME", "cxy");
    16. //2.设定配置信息
    17. Configuration conf = new Configuration();
    18. conf.addResource("config.xml");
    19. //2.1 初始化文件系统
    20. try {
    21. FileSystem fs = FileSystem.get(conf);
    22. Path aPath = new Path("/mamaedu.txt");
    23. //3.组织acl
    24. List<AclEntry> aclEntryList = new ArrayList<>();
    25. AclEntry ae1 = AclEntry.parseAclEntry("user:root:rwx", true);
    26. AclEntry ae2 = AclEntry.parseAclEntry("group:root:r--", true);
    27. AclEntry ae3 = AclEntry.parseAclEntry("other::---", true);
    28. aclEntryList.add(ae1);
    29. aclEntryList.add(ae2);
    30. aclEntryList.add(ae3);
    31. //修改acl
    32. fs.modifyAclEntries(aPath, aclEntryList);
    33. System.out.println("完成");
    34. } catch (IOException e) {
    35. e.printStackTrace();
    36. }
    37. }
    38. }

    通过FsPermissions修改权限

    1. package org.mmedu.example.chap08.acl;
    2. import org.apache.hadoop.conf.Configuration;
    3. import org.apache.hadoop.fs.FileSystem;
    4. import org.apache.hadoop.fs.Path;
    5. import org.apache.hadoop.fs.permission.FsAction;
    6. import org.apache.hadoop.fs.permission.FsPermission;
    7. import java.io.IOException;
    8. /**
    9. * @author Administrator
    10. */
    11. public class SetAclList {
    12. public static void main(String[] args) {
    13. //1.设定配置信息
    14. System.setProperty("HADOOP_USER_NAME", "cxy");
    15. //2.设定配置信息
    16. Configuration conf = new Configuration();
    17. conf.addResource("config.xml");
    18. //2.1 初始化文件系统
    19. try {
    20. FileSystem fs = FileSystem.get(conf);
    21. Path aPath = new Path("/mamaedu.txt");
    22. //3.组织acl
    23. FsPermission permission = new FsPermission(FsAction.ALL, FsAction.READ, FsAction.NONE);
    24. //修改acl
    25. fs.setPermission(aPath, permission);
    26. System.out.println("完成");
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. }
    31. }

    微信截图_20210504163514.png微信截图_20210504165953.png微信截图_20210504185442.png

    获取文件ACL权限
    问题一:我这个版本的jdk跟老师上课用的源码不一样,我这个AclStatus没办法获取到当前用户的permission
    image.png
    可以看见没有getPermission()这个API
    image.png
    问题二:最后输出的entry条目只有三个,mask和other条目都没有输出
    image.png
    正常应该有的条目如下
    image.png