David said:
Thank you,
now it works fine
. The problem was my waiting for diff before
reading the result. This is my running code:
Damn, I was wrong. The only reason why it worked was that I forgot to
replace the filenames in the diff invocation by the newly introduced
constants (which had different values). Thus the diff read from pipes
which were created in earlier tests and the pipes created by the given
code were not read from at all! I now corrected this but the
'original.fifo' cannot be found, even if I switch the order of the two
writer threads. I think I got something seriously wrong with thread
programming?
public PatchScript createPatch(final String orig, final String rev)
throws DiffFailedException {
PatchScript patch = null;
try {
logger.debug("Create named pipes to write input to.");
Runtime.getRuntime().exec("mkfifo " + ORIG_PIPE_NAME);
Runtime.getRuntime().exec("mkfifo " + REV_PIPE_NAME);
long millis = System.currentTimeMillis() + 5000;
while(System.currentTimeMillis() < millis) {}
logger.debug("Write input to named pipes.");
new Thread() {
public void run() {
try {
logger.debug("Open pipe:" + ORIG_PIPE_NAME);
File oPipe = new File(ORIG_PIPE_NAME);
if(oPipe.exists()) {
FileWriter oWriter = new FileWriter(oPipe);
oWriter.write(orig);
oWriter.flush();
oWriter.close();
logger.debug("Original written. Writer closed.");
} else logger.warn("Could not find named pipe: " +
ORIG_PIPE_NAME);
} catch(IOException e) {
logger.warn("Could not write 'orig' to named pipe.", e);
}
}
}.start();
new Thread() {
public void run() {
try {
logger.debug("Open pipe:" + REV_PIPE_NAME);
File rPipe = new File(REV_PIPE_NAME);
if(rPipe.exists()) {
FileWriter rWriter = new FileWriter(rPipe);
rWriter.write(rev);
rWriter.flush();
rWriter.close();
logger.debug("Revision written. Writer closed.");
} else logger.warn("Could not find named pipe: " +
REV_PIPE_NAME);
} catch(IOException e) {
logger.warn("Could not write 'rev' to named pipe.", e);
}
}
}.start();
logger.debug("Start 'diff' process.");
Process diffProc = Runtime.getRuntime().exec("diff -u " +
ORIG_PIPE_NAME + " " + REV_PIPE_NAME);
logger.debug("Read result.");
BufferedReader reader = new BufferedReader(new
InputStreamReader(diffProc.getInputStream()));
String line = reader.readLine();
String patchString = null;
if(line != null) patchString = line + "\n";
while((line = reader.readLine()) != null) patchString += line + "\n";
logger.debug("patch=\n" + patchString);
logger.debug("Wait for 'diff' to finish.");
diffProc.waitFor();
logger.debug("diff-status="+diffProc.exitValue()+". Read patch
script.");
patch = parser.parse(patchString);
} catch(Exception e) {
throw new DiffFailedException("Could not create patch script!", e);
}
return patch;
}