Skip to content

Apache Lucene open-source search software

Fix FSDirectory.createOutput() to always create new files.

Doug Cuttingeea1c63a6228
diff --git a/CHANGES.txt b/CHANGES.txt
index ed45e921422..f0df9cdc8a7 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -164,6 +164,12 @@ Bug fixes
a given amount of milliseconds, but this didn't work.
(John Wang via Daniel Naber, Bug #33799)
+ 8. Fix FSDirectory.createOutput() to always create new files.
+ Previously, existing files were overwritten, and an index could be
+ corrupted when the old version of a file was longer than the new.
+ Now any existing file is first removed. (Doug Cutting)
+
+
Optimizations
1. Disk usage (peak requirements during indexing and optimization)
diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.java
index b8535c35c36..bf6020d8625 100644
--- a/src/java/org/apache/lucene/store/FSDirectory.java
+++ b/src/java/org/apache/lucene/store/FSDirectory.java
@@ -241,7 +241,7 @@ public class FSDirectory extends Directory {
public void deleteFile(String name) throws IOException {
File file = new File(directory, name);
if (!file.delete())
- throw new IOException("Cannot delete " + name);
+ throw new IOException("Cannot delete " + file);
}
/** Renames an existing file in the directory. */
@@ -256,7 +256,7 @@ public class FSDirectory extends Directory {
if (nu.exists())
if (!nu.delete())
- throw new IOException("Cannot delete " + to);
+ throw new IOException("Cannot delete " + nu);
// Rename the old file to the new one. Unfortunately, the renameTo()
// method does not work reliably under some JVMs. Therefore, if the
@@ -282,7 +282,7 @@ public class FSDirectory extends Directory {
old.delete();
}
catch (IOException ioe) {
- throw new IOException("Cannot rename " + from + " to " + to);
+ throw new IOException("Cannot rename " + old + " to " + nu);
}
finally {
if (in != null) {
@@ -306,7 +306,11 @@ public class FSDirectory extends Directory {
/** Creates a new, empty file in the directory with the given name.
Returns a stream writing this file. */
public IndexOutput createOutput(String name) throws IOException {
- return new FSIndexOutput(new File(directory, name));
+ File file = new File(directory, name);
+ if (file.exists() && !file.delete()) // delete existing, if any
+ throw new IOException("Cannot overwrite: " + file);
+
+ return new FSIndexOutput(file);
}
/** Returns a stream reading an existing file. */