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

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@179609 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Doug Cutting 2005-06-02 16:57:10 +00:00
parent 4ade1d4606
commit eea1c63a62
2 changed files with 14 additions and 4 deletions

View File

@ -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)

View File

@ -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. */