Skip to content

Commit

Permalink
Support ; separated values for juicefs.users, juicefs.group (#4724)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyungwan-nam authored Apr 19, 2024
1 parent 6567244 commit dff2439
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
29 changes: 25 additions & 4 deletions sdk/java/src/main/java/io/juicefs/JuiceFileSystemImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;
import java.util.zip.ZipEntry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/****************************************************************
* Implement the FileSystem API for JuiceFS
Expand Down Expand Up @@ -107,6 +109,9 @@ public class JuiceFileSystemImpl extends FileSystem {
private String[] storageIds;
private Random random = new Random();

private static final String USERNAME_UID_PATTERN = "[a-zA-Z0-9_-]+:[0-9]+";
private static final String GROUPNAME_GID_USERNAMES_PATTERN = "[a-zA-Z0-9_-]+:[0-9]+:[,a-zA-Z0-9_-]+";

/*
go call back
*/
Expand Down Expand Up @@ -508,13 +513,29 @@ private String readFile(String file) throws IOException {
}
}

private String parseUidAndGrouping(String pattern, String input) {
String result = null;
if (input == null || "".equals(input.trim())) {
return result;
}
List<String> matched = new ArrayList<>();
Matcher matcher = Pattern.compile(pattern).matcher(input);
while (matcher.find()) {
matched.add(matcher.group());
}
if (matched.size() > 0) {
result = String.join("\n", matched);
}
return result;
}

private void updateUidAndGrouping(String uidFile, String groupFile) throws IOException {
String uidstr = null;
if (uidFile != null && !"".equals(uidFile.trim())) {
String uidstr = parseUidAndGrouping(USERNAME_UID_PATTERN, uidFile);
if (uidstr == null && uidFile != null && !"".equals(uidFile.trim())) {
uidstr = readFile(uidFile);
}
String grouping = null;
if (groupFile != null && !"".equals(groupFile.trim())) {
String grouping = parseUidAndGrouping(GROUPNAME_GID_USERNAMES_PATTERN, groupFile);
if (grouping == null && groupFile != null && !"".equals(groupFile.trim())) {
grouping = readFile(groupFile);
}

Expand Down
23 changes: 23 additions & 0 deletions sdk/java/src/test/java/io/juicefs/JuiceFileSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,29 @@ public void testGuidMapping() throws Exception {
fooFs.delete(f, false);
}

public void testGuidMappingFromString() throws Exception {
Configuration newConf = new Configuration(cfg);

newConf.set("juicefs.users", "bar:10000;foo:20000;baz:30000");
newConf.set("juicefs.groups", "user:1000:foo,bar;admin:2000:baz");
newConf.set("juicefs.superuser", UserGroupInformation.getCurrentUser().getShortUserName());

FileSystem fooFs = createNewFs(newConf, "foo", new String[]{"nogrp"});
Path f = new Path("/test_foo");
fooFs.create(f).close();
fooFs.setOwner(f, "foo", "user");
assertEquals("foo", fooFs.getFileStatus(f).getOwner());
assertEquals("user", fooFs.getFileStatus(f).getGroup());

newConf.set("juicefs.users", "foo:20001");
newConf.set("juicefs.groups", "user:1001:foo,bar;admin:2001:baz");
FileSystem newFS = FileSystem.newInstance(newConf);
assertEquals("20000", fooFs.getFileStatus(f).getOwner());
assertEquals("1000", fooFs.getFileStatus(f).getGroup());

fooFs.delete(f, false);
}

public void testTrash() throws Exception {
Trash trash = new Trash(fs, cfg);
Path trashFile = new Path("/tmp/trashfile");
Expand Down

0 comments on commit dff2439

Please sign in to comment.