Skip to content

Commit

Permalink
Fix broken support for arguments with spaces on Linux (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
vorburger committed Dec 3, 2021
1 parent a3e6355 commit f95e4d2
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/main/java/ch/vorburger/exec/ManagedProcessBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ protected ManagedProcessBuilder addArgument(String argPart1, String separator, S
final StringBuilder sb = new StringBuilder();
final String arg;

// @see MariaDB4j Issue #501 Fix for spaces in data path doesn't work on windows
// https://github.com/vorburger/MariaDB4j/issues/501
// Internally Runtime.exec is being used, which says that an argument such
// @see https://github.com/vorburger/MariaDB4j/issues/501 - Fix for spaces in data path doesn't work on windows:
// Internally Runtime.exec() is being used, which says that an argument such
// as --name="escaped value" isn't escaped, since there's no leading quote
// and it contains a space, so it reescapes it, causing applications to split
// and it contains a space, so it re-escapes it, causing applications such as mysqld.exe to split
// it into multiple pieces, which is why we quote the whole arg (key and value) instead.
if ("=".equals(separator)) {
// We do this trick only on Windows, because on Linux it breaks the behavior.
if (isWindows()) {
sb.append(argPart1);
sb.append(separator);
sb.append(argPart2);
Expand Down Expand Up @@ -256,7 +256,7 @@ public ManagedProcessBuilder addStdErr(OutputStream stdError) {
/* package-local... let's keep ch.vorburger.exec's API separate from Apache Commons Exec, so it
* COULD be replaced */
CommandLine getCommandLine() {
if ((getWorkingDirectory() == null) && commonsExecCommandLine.isFile()) {
if (getWorkingDirectory() == null && commonsExecCommandLine.isFile()) {
File exec = new File(commonsExecCommandLine.getExecutable());
File dir = exec.getParentFile();
if (dir == null) {
Expand All @@ -278,4 +278,9 @@ CommandLine getCommandLine() {
return commonsExecCommandLine.toString();
}

// inspired by org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS
// without security manager support; assumes System.getProperty() works
private boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}
}

0 comments on commit f95e4d2

Please sign in to comment.