Mercurial > jhg
comparison src/org/tmatesoft/hg/internal/ProcessExecHelper.java @ 413:7f27122011c3
Support and respect for symbolic links and executable flag, with /bin/ls backed implementation to discover these
| author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
|---|---|
| date | Wed, 21 Mar 2012 20:40:28 +0100 |
| parents | |
| children | 9c9c442b5f2e |
comparison
equal
deleted
inserted
replaced
| 406:d56ea1a2537a | 413:7f27122011c3 |
|---|---|
| 1 /* | |
| 2 * Copyright (c) 2012 TMate Software Ltd | |
| 3 * | |
| 4 * This program is free software; you can redistribute it and/or modify | |
| 5 * it under the terms of the GNU General Public License as published by | |
| 6 * the Free Software Foundation; version 2 of the License. | |
| 7 * | |
| 8 * This program is distributed in the hope that it will be useful, | |
| 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 11 * GNU General Public License for more details. | |
| 12 * | |
| 13 * For information on how to redistribute this software under | |
| 14 * the terms of a license other than GNU General Public License | |
| 15 * contact TMate Software at support@hg4j.com | |
| 16 */ | |
| 17 package org.tmatesoft.hg.internal; | |
| 18 | |
| 19 import java.io.File; | |
| 20 import java.io.IOException; | |
| 21 import java.io.InputStreamReader; | |
| 22 import java.nio.CharBuffer; | |
| 23 import java.util.Arrays; | |
| 24 import java.util.LinkedList; | |
| 25 import java.util.List; | |
| 26 | |
| 27 /** | |
| 28 * Utility to run shell commands. Not thread-safe. | |
| 29 * Beware of memory overcommitment issue on Linux - suprocess get allocated virtual memory of parent process size | |
| 30 * @see http://developers.sun.com/solaris/articles/subprocess/subprocess.html | |
| 31 * | |
| 32 * @author Artem Tikhomirov | |
| 33 * @author Tmate Software Ltd. | |
| 34 */ | |
| 35 public class ProcessExecHelper { | |
| 36 private File dir; | |
| 37 private int exitValue; | |
| 38 private ProcessBuilder pb; | |
| 39 | |
| 40 public ProcessExecHelper() { | |
| 41 } | |
| 42 | |
| 43 protected List<String> prepareCommand(List<String> cmd) { | |
| 44 return cmd; | |
| 45 } | |
| 46 | |
| 47 public CharSequence exec(String... command) throws IOException, InterruptedException { | |
| 48 return exec(Arrays.asList(command)); | |
| 49 } | |
| 50 | |
| 51 public CharSequence exec(List<String> command) throws IOException, InterruptedException { | |
| 52 List<String> cmd = prepareCommand(command); | |
| 53 if (pb == null) { | |
| 54 pb = new ProcessBuilder(cmd).directory(dir).redirectErrorStream(true); | |
| 55 } else { | |
| 56 pb.command(cmd); // dir and redirect are set | |
| 57 } | |
| 58 Process p = pb.start(); | |
| 59 InputStreamReader stdOut = new InputStreamReader(p.getInputStream()); | |
| 60 LinkedList<CharBuffer> l = new LinkedList<CharBuffer>(); | |
| 61 int r = -1; | |
| 62 CharBuffer b = null; | |
| 63 do { | |
| 64 if (b == null || b.remaining() < b.capacity() / 3) { | |
| 65 b = CharBuffer.allocate(512); | |
| 66 l.add(b); | |
| 67 } | |
| 68 r = stdOut.read(b); | |
| 69 } while (r != -1); | |
| 70 int total = 0; | |
| 71 for (CharBuffer cb : l) { | |
| 72 total += cb.position(); | |
| 73 cb.flip(); | |
| 74 } | |
| 75 CharBuffer res = CharBuffer.allocate(total); | |
| 76 for (CharBuffer cb : l) { | |
| 77 res.put(cb); | |
| 78 } | |
| 79 res.flip(); | |
| 80 p.waitFor(); | |
| 81 exitValue = p.exitValue(); | |
| 82 return res; | |
| 83 } | |
| 84 | |
| 85 public int exitValue() { | |
| 86 return exitValue; | |
| 87 } | |
| 88 | |
| 89 public ProcessExecHelper cwd(File wd) { | |
| 90 dir = wd; | |
| 91 if (pb != null) { | |
| 92 pb.directory(dir); | |
| 93 } | |
| 94 return this; | |
| 95 } | |
| 96 } |
