From eea1c63a62289d694fe0c4081b78cf6a811ed6bb Mon Sep 17 00:00:00 2001 From: Doug Cutting Date: Thu, 2 Jun 2005 16:57:10 +0000 Subject: [PATCH] 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 --- CHANGES.txt | 6 ++++++ src/java/org/apache/lucene/store/FSDirectory.java | 12 ++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) 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. */