Commits

James Dumay committed 94f850d

BAM-8352 - ExternalProcess should quote environment variable value. Doing this for non-windows platforms to begin with.

  • Participants
  • Parent commits bf0e26f

Comments (0)

Files changed (1)

File src/main/java/com/atlassian/utils/process/ExternalProcess.java

 import java.io.File;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 
         ProcessBuilder pb = new ProcessBuilder(cmdArray);
         pb.directory(workingDir);
-        pb.environment().putAll(environment);
+        pb.environment().putAll(prepareEnvironment(environment));
         return pb.start();
     }
 
                     .command(cmdArray)
                     .directory(workingDir);
 
-            processBuilder.environment().putAll(environment);
+            processBuilder.environment().putAll(prepareEnvironment(environment));
 
     		return processBuilder.start();
     	}
     }
+
+    /**
+     * Adds quotes around the value of the environment variable to correctly escape spaces for non-windows platforms.
+     * @param environment
+     * @return environment
+     */
+    private Map<String, String> prepareEnvironment(Map<String, String> environment)
+    {
+        if (!isWindows())
+        {
+            final Map<String, String> newEnvironment = new HashMap<String, String>();
+            for (Map.Entry<String, String> entry : environment.entrySet())
+            {
+                final String newValue = "\"" + entry.getValue() + "\"";
+                newEnvironment.put(entry.getKey(), newValue);
+            }
+            return newEnvironment;
+        }
+        return environment;
+    }
     
     /**
      * Start the external process and setup the IO pump threads needed to