From 4087330039dd557ac515065199710a794f67d141 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 31 May 2018 10:34:24 +0200 Subject: [PATCH] Improved code. --- src/fr/devinsy/util/unix/Unix.java | 55 ++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 10 deletions(-) diff --git a/src/fr/devinsy/util/unix/Unix.java b/src/fr/devinsy/util/unix/Unix.java index 5a77f71..6429858 100644 --- a/src/fr/devinsy/util/unix/Unix.java +++ b/src/fr/devinsy/util/unix/Unix.java @@ -64,7 +64,7 @@ public class Unix { try { - sudo("bash", "-c", "echo \"" + text + "\" >> " + path); + bash(String.format("echo \"%s\" >> %s", text, path)); } catch (CmdExecException exception) { @@ -86,7 +86,7 @@ public class Unix { String result; - if (SystemUtils.IS_OS_LINUX) + if (SystemUtils.IS_OS_UNIX) { result = CmdExecUtils.run(BASH + " -c " + command); } @@ -103,7 +103,7 @@ public class Unix { String result; - if (SystemUtils.IS_OS_LINUX) + if (SystemUtils.IS_OS_UNIX) { result = CmdExecUtils.run(BASH + " -c " + command); } @@ -140,8 +140,8 @@ public class Unix { try { - // CmdExec.run(SUDO, "chmod", changes, path); - sudo("chmod", changes, path); + // CmdExec.run(BASH, "chmod", changes, path); + bash("chmod", changes, path); } catch (CmdExecException exception) { @@ -383,9 +383,9 @@ public class Unix { String result; - if (SystemUtils.IS_OS_LINUX) + if (SystemUtils.IS_OS_UNIX) { - result = CmdExecUtils.run(SUDO + " " + command); + result = CmdExecUtils.run(command); } else { @@ -483,6 +483,7 @@ public class Unix public static void link(final String sourcePath, final String targetPath) throws UnixException { logger.info("[{}][{}]", sourcePath, targetPath); + if ((sourcePath == null) || (sourcePath.length() == 0) || (targetPath == null) || (targetPath.length() == 0)) { throw new IllegalArgumentException("Parameter undefined: [" + sourcePath + "][" + targetPath + "]."); @@ -825,7 +826,7 @@ public class Unix { String result; - if (SystemUtils.IS_OS_LINUX) + if (SystemUtils.IS_OS_UNIX) { result = CmdExecUtils.run(SUDO + " " + command); } @@ -851,7 +852,7 @@ public class Unix { String result; - if (SystemUtils.IS_OS_LINUX) + if (SystemUtils.IS_OS_UNIX) { String[] newCommand = ArrayUtils.add(command, 0, SUDO); result = CmdExecUtils.run(newCommand); @@ -878,7 +879,7 @@ public class Unix { String result; - if (SystemUtils.IS_OS_LINUX) + if (SystemUtils.IS_OS_UNIX) { result = CmdExecUtils.run(BASH + " -c " + command); } @@ -891,6 +892,40 @@ public class Unix return result; } + /** + * Sudo chmod. + * + * @param changes + * the changes + * @param path + * the path + * @throws UnixException + * the unix exception + */ + public static void sudoChmod(final String changes, final String path) throws UnixException + { + if ((changes == null) || (changes.length() == 0) || (path == null) || (path.length() == 0)) + { + throw new IllegalArgumentException("Parameter undefined: [" + changes + "][" + path + "]."); + } + else if (!new File(path).exists()) + { + throw new IllegalArgumentException("Path not found: [" + path + "]."); + } + else + { + try + { + // CmdExec.run(SUDO, "chmod", changes, path); + sudo("chmod", changes, path); + } + catch (CmdExecException exception) + { + throw new UnixException("Error running chmod command for [" + changes + "][" + path + "].", exception); + } + } + } + /** * Unlink. *