Skip to content

Apache Lucene open-source search software

LUCENE-5588: Workaround for fsyncing non-existing directory

Uwe Schindlerd825efca44ad
diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
index 1cbfac1da3a..886752bb2ea 100644
--- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java
@@ -289,10 +289,15 @@ public abstract class FSDirectory extends BaseDirectory {
Set<String> toSync = new HashSet<>(names);
toSync.retainAll(staleFiles);
- for (String name : toSync)
+ for (String name : toSync) {
fsync(name);
+ }
- IOUtils.fsync(directory, true);
+ // fsync the directory itsself, but only if there was any file fsynced before
+ // (otherwise it can happen that the directory does not yet exist)!
+ if (!toSync.isEmpty()) {
+ IOUtils.fsync(directory, true);
+ }
staleFiles.removeAll(toSync);
}