xdocs: Fix a race condition in generating the temporary file name
TimeUtil.nowTs() has only millisecond resolution, so calling getNanos() on
it does not add any precision. With millisecond resolution it can happen
for a side-by-side diff that the same temporary file name is created for
the two files to compare, resulting in an exception like
com.google.common.util.concurrent.UncheckedExecutionException:
java.io.FileNotFoundException: [...]/xdocs/tmp/asciidoctor-891000000.tmp
(No such file or directory)
at com.google.common.cache.AbstractLoadingCache.getUnchecked(AbstractLoadingCache.java:55)
at com.googlesource.gerrit.plugins.xdocs.XDocCache.get(XDocCache.java:52)
at com.googlesource.gerrit.plugins.xdocs.XDocWebLink.getFileUrl(XDocWebLink.java:123)
at com.googlesource.gerrit.plugins.xdocs.XDocWebLink.getFileWebLink(XDocWebLink.java:92)
Fix this by letting Java determine a unique file name via
File.createTempFile() and let AsciiDoctor simply overwrite it.
Change-Id: Icf3843f8cd8c4c6f4ae10b3a8b264f2e52b72f89
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
index 8bbe7a0..caa31c4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
@@ -75,8 +75,9 @@
formatters.getFormatterConfig(NAME, projectName);
// asciidoctor ignores all attributes if no output file is specified,
// this is why we must specify an output file and then read its content
- File tmpFile =
- new File(baseDir, "tmp/asciidoctor-" + TimeUtil.nowTs().getNanos() + ".tmp");
+ File tmpDir = new File(baseDir, "tmp");
+ tmpDir.mkdirs();
+ File tmpFile = File.createTempFile("asciidoctor-", null, tmpDir);
try {
Asciidoctor.Factory.create(AsciidoctorFormatter.class.getClassLoader())
.render(raw, createOptions(projectCfg, abbrRev, tmpFile));