Skip to content

Commit

Permalink
commons: add new functionality to FileUtils: get owner and group for …
Browse files Browse the repository at this point in the history
…a given path, #46
  • Loading branch information
jtarraga committed Feb 17, 2020
1 parent 5753fbe commit e7ad9d5
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
54 changes: 54 additions & 0 deletions commons-lib/src/main/java/org/opencb/commons/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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]);
}
}

0 comments on commit e7ad9d5

Please sign in to comment.