8286706: JFR: 'jfr scrub' should overwrite output

Reviewed-by: mgronlun
This commit is contained in:
Erik Gahlin 2022-05-18 04:44:03 +00:00
parent b5526e5e59
commit ab144190c9
2 changed files with 35 additions and 1 deletions

View File

@ -27,6 +27,7 @@ package jdk.jfr.internal.tool;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Deque;
@ -132,7 +133,7 @@ final class Scrub extends Command {
input = last;
output = dir == null ? Path.of(t) : dir.resolve(t);
}
ensureFileDoesNotExist(output);
ensureUsableOutput(input, output);
List<Predicate<RecordedEvent>> filters = new ArrayList<>();
int optionCount = options.size();
@ -191,4 +192,25 @@ final class Scrub extends Command {
println("Scrubbed recording file written to:");
println(output.toAbsolutePath().toString());
}
private void ensureUsableOutput(Path input, Path output) throws UserSyntaxException, UserDataException {
if (!Files.exists(output)) {
return;
}
if (!Files.exists(input)) {
return; // Will fail later when reading file
}
try {
if (Files.isSameFile(input, output)) {
throw new UserSyntaxException("output file can't be same as input file " + input.toAbsolutePath());
}
} catch (IOException e) {
throw new UserDataException("could not access " + input.toAbsolutePath() + " or " + output.toAbsolutePath() + ". " + e.getMessage());
}
try {
Files.delete(output);
} catch (IOException e) {
throw new UserDataException("could not delete existing output file " + output.toAbsolutePath() + ". " + e.getMessage());
}
}
}

View File

@ -79,6 +79,7 @@ public class TestScrub {
r.stop();
r.dump(file);
}
testInputOutput(file);
testAutogeneratedFilename(file, autogenerated);
testEventInclude(file);
@ -92,6 +93,17 @@ public class TestScrub {
testThreadInclude(file);
}
private static void testInputOutput(Path file) throws Throwable {
List<String> arguments = new ArrayList<>();
arguments.add("scrub");
arguments.add(file.toAbsolutePath().toString());
arguments.add(file.toAbsolutePath().toString());
OutputAnalyzer oa = ExecuteHelper.jfr(arguments.toArray(String[]::new));
oa.shouldContain("output file can't be same as input file");
oa.shouldNotHaveExitValue(0);
}
private static void testAutogeneratedFilename(Path file, Path autogenerated) throws Throwable {
List<String> arguments = new ArrayList<>();
arguments.add("scrub");