diff --git a/commons-lib/src/main/java/org/opencb/commons/utils/FileUtils.java b/commons-lib/src/main/java/org/opencb/commons/utils/FileUtils.java index 4321af78..0c125f6e 100644 --- a/commons-lib/src/main/java/org/opencb/commons/utils/FileUtils.java +++ b/commons-lib/src/main/java/org/opencb/commons/utils/FileUtils.java @@ -16,6 +16,9 @@ package org.opencb.commons.utils; +import org.apache.commons.lang3.StringUtils; +import org.opencb.commons.exec.Command; + import java.io.*; import java.nio.charset.Charset; import java.nio.file.Files; @@ -157,4 +160,55 @@ public static BufferedWriter newBufferedWriter(Path path, Charset charset) throw return bufferedWriter; } + public static String getUser(Path path) throws IOException { + return getUser(path, false); + } + + public static String getUser(Path path, boolean numericId) throws IOException { + return getLsOutput(path, numericId).split(" ")[2]; + } + + public static String getGroup(Path path) throws IOException { + return getGroup(path, false); + } + + public static String getGroup(Path path, boolean numericId) throws IOException { + return getLsOutput(path, numericId).split(" ")[3]; + } + + public static String[] getUserAndGroup(Path path) throws IOException { + return getUserAndGroup(path, false); + } + + public static String[] getUserAndGroup(Path path, boolean numericId) throws IOException { + String[] split = getLsOutput(path, numericId).split(" "); + + return new String[]{split[2], split[3]}; + } + + private static String getLsOutput(Path path, boolean numericId) throws IOException { + FileUtils.checkPath(path); + + String dirOption = path.toFile().isDirectory() ? "d" : ""; + String numericOption = numericId ? "n" : ""; + String cmdline = "ls -l" + dirOption + numericOption + " " + path.toAbsolutePath().toString(); + + // Execute command line + Command cmd = new Command(cmdline); + cmd.run(); + + // Get output and error + String output = cmd.getOutput(); + String error = cmd.getError(); + + if (org.apache.commons.lang3.StringUtils.isNotEmpty(error)) { + throw new IOException(error); + } + + if (StringUtils.isEmpty(output)) { + throw new IOException("Error accessing to path: " + path); + } + + return output; + } } diff --git a/commons-lib/src/test/java/org/opencb/commons/utils/FileUtilsTest.java b/commons-lib/src/test/java/org/opencb/commons/utils/FileUtilsTest.java new file mode 100644 index 00000000..8cae2f90 --- /dev/null +++ b/commons-lib/src/test/java/org/opencb/commons/utils/FileUtilsTest.java @@ -0,0 +1,53 @@ +package org.opencb.commons.utils; + +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.junit.Assert.*; + +public class FileUtilsTest { + + Path path = Paths.get("/tmp"); + + @Test + public void getUser() throws IOException { + String user = FileUtils.getUser(path); + Assert.assertEquals("root", user); + } + + @Test + public void getUserNumeric() throws IOException { + String user = FileUtils.getUser(path, true); + Assert.assertEquals("0", user); + } + + @Test + public void getGroup() throws IOException { + String group = FileUtils.getGroup(path); + Assert.assertEquals("root", group); + } + + @Test + public void getGroupNumeric() throws IOException { + String group = FileUtils.getGroup(path, true); + Assert.assertEquals("0", group); + } + + @Test + public void getUserAndGroup() throws IOException { + String[] user = FileUtils.getUserAndGroup(path); + Assert.assertEquals("root", user[0]); + Assert.assertEquals("root", user[1]); + } + + @Test + public void getUserAndGroupNumeric() throws IOException { + String[] user = FileUtils.getUserAndGroup(path, true); + Assert.assertEquals("0", user[0]); + Assert.assertEquals("0", user[1]); + } +} \ No newline at end of file