Don't use Files.copy() when decrypting to stdout

Files.copy() attempts to delete the file if it already exists, which obviously
won't work very well for /dev/stdout.  Instead copy directly from the decoder
to standard output.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=228384575
This commit is contained in:
mmuller 2019-01-08 13:06:24 -08:00 committed by Shicong Huang
parent 04a495bc99
commit 9e0b675275
5 changed files with 47 additions and 3 deletions

View file

@ -31,6 +31,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import javax.inject.Inject;
import javax.inject.Provider;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
@ -101,9 +102,15 @@ final class GhostrydeCommand implements CommandWithRemoteApi {
private void runDecrypt() throws IOException, PGPException {
try (InputStream in = Files.newInputStream(input);
InputStream ghostDecoder = Ghostryde.decoder(in, rdeStagingDecryptionKey.get())) {
Path outFile =
Files.isDirectory(output) ? output.resolve(input.getFileName() + ".decrypt") : output;
Files.copy(ghostDecoder, outFile, REPLACE_EXISTING);
System.err.println("output = " + output);
if (output.toString().equals("/dev/stdout")) {
System.err.println("doing copy");
IOUtils.copy(ghostDecoder, System.out);
} else {
Path outFile =
Files.isDirectory(output) ? output.resolve(input.getFileName() + ".decrypt") : output;
Files.copy(ghostDecoder, outFile, REPLACE_EXISTING);
}
}
}
}