41 import java.nio.file.*;
42 import java.nio.file.attribute.*;
43 import java.io.IOException;
45 import java.util.regex.Pattern;
54 static Set<AclEntryPermission> parsePermissions(String permsString) {
55 Set<AclEntryPermission> perms =
new HashSet<AclEntryPermission>();
56 String[] result = permsString.split(
"/");
57 for (String s : result) {
61 perms.add(AclEntryPermission.valueOf(s.toUpperCase()));
62 }
catch (IllegalArgumentException x) {
63 System.err.format(
"Invalid permission '%s'\n", s);
71 static Set<AclEntryFlag> parseFlags(String flagsString) {
72 Set<AclEntryFlag> flags =
new HashSet<AclEntryFlag>();
73 String[] result = flagsString.split(
"/");
74 for (String s : result) {
78 flags.add(AclEntryFlag.valueOf(s.toUpperCase()));
79 }
catch (IllegalArgumentException x) {
80 System.err.format(
"Invalid flag '%s'\n", s);
88 static AclEntryType parseType(String typeString) {
90 if (typeString.equalsIgnoreCase(
"allow"))
91 return AclEntryType.ALLOW;
92 if (typeString.equalsIgnoreCase(
"deny"))
93 return AclEntryType.DENY;
94 System.err.format(
"Invalid type '%s'\n", typeString);
103 static AclEntry parseAceString(String s,
104 UserPrincipalLookupService lookupService)
106 String[] result = s.split(
":");
109 if (result.length < 3)
113 int remaining = result.length;
116 boolean isGroup =
false;
117 if (result[index].equalsIgnoreCase(
"user") ||
118 result[index].equalsIgnoreCase(
"group"))
122 isGroup = result[index++].equalsIgnoreCase(
"group");
126 String userString = result[index++]; remaining--;
127 String permsString = result[index++]; remaining--;
130 String flagsString =
"";
131 String typeString =
null;
132 if (remaining == 1) {
133 typeString = result[index++];
135 if (remaining == 2) {
136 flagsString = result[index++];
137 typeString = result[index++];
144 UserPrincipal user =
null;
147 lookupService.lookupPrincipalByGroupName(userString) :
148 lookupService.lookupPrincipalByName(userString);
149 }
catch (UserPrincipalNotFoundException x) {
150 System.err.format(
"Invalid %s '%s'\n",
151 ((isGroup) ?
"group" :
"user"),
154 }
catch (IOException x) {
155 System.err.format(
"Lookup of '%s' failed: %s\n", userString, x);
160 Set<AclEntryPermission> perms = parsePermissions(permsString);
161 Set<AclEntryFlag> flags = parseFlags(flagsString);
162 AclEntryType type = parseType(typeString);
165 return AclEntry.newBuilder()
168 .setPermissions(perms).setFlags(flags).build();
171 static void usage() {
172 System.err.println(
"usage: java AclEdit [ACL-operation] file");
173 System.err.println(
"");
174 System.err.println(
"Example 1: Prepends access control entry to the begining of the myfile's ACL");
175 System.err.println(
" java AclEdit A+alice:read_data/read_attributes:allow myfile");
176 System.err.println(
"");
177 System.err.println(
"Example 2: Remove the entry at index 6 of myfile's ACL");
178 System.err.println(
" java AclEdit A6- myfile");
179 System.err.println(
"");
180 System.err.println(
"Example 3: Replace the entry at index 2 of myfile's ACL");
181 System.err.println(
" java AclEdit A2=bob:write_data/append_data:deny myfile");
195 public static void main(String[] args)
throws IOException {
196 Action action =
null;
198 String entryString =
null;
201 if (args.length < 1 || args[0].equals(
"-help") || args[0].equals(
"-?"))
204 if (args.length == 1) {
205 action = Action.PRINT;
210 if (Pattern.matches(
"^A[0-9]*\\+.*", s)) {
211 String[] result = s.split(
"\\+", 2);
212 if (result.length == 2) {
213 if (result[0].length() < 2) {
216 index = Integer.parseInt(result[0].substring(1));
218 entryString = result[1];
224 if (Pattern.matches(
"^A[0-9]+\\-", s)) {
225 String[] result = s.split(
"\\-", 2);
226 if (result.length == 2) {
227 index = Integer.parseInt(result[0].substring(1));
228 entryString = result[1];
229 action = Action.REMOVE;
234 if (Pattern.matches(
"^A[0-9]+=.*", s)) {
235 String[] result = s.split(
"=", 2);
236 if (result.length == 2) {
237 index = Integer.parseInt(result[0].substring(1));
238 entryString = result[1];
239 action = Action.REPLACE;
246 int fileArg = (action == Action.PRINT) ? 0 : 1;
247 Path file = Paths.get(args[fileArg]);
250 AclFileAttributeView view =
251 Files.getFileAttributeView(file, AclFileAttributeView.class);
253 System.err.println(
"ACLs not supported on this platform");
256 List<AclEntry> acl = view.getAcl();
261 for (
int i=0; i<acl.size(); i++) {
262 System.out.format(
"%5d: %s\n", i, acl.get(i));
269 AclEntry entry = parseAceString(entryString, file
270 .getFileSystem().getUserPrincipalLookupService());
271 if (index >= acl.size()) {
274 acl.add(index, entry);
282 if (index >= acl.size()) {
283 System.err.format(
"Index '%d' is invalid", index);
293 if (index >= acl.size()) {
294 System.err.format(
"Index '%d' is invalid", index);
297 AclEntry entry = parseAceString(entryString, file
298 .getFileSystem().getUserPrincipalLookupService());
299 acl.set(index, entry);