Index: src/test/org/apache/lucene/store/TestLockFactory.java =================================================================== --- src/test/org/apache/lucene/store/TestLockFactory.java (revision 718246) +++ src/test/org/apache/lucene/store/TestLockFactory.java (working copy) @@ -566,10 +566,12 @@ private void rmDir(String dirName) { File dir = new java.io.File(dirName); String[] files = dir.list(); // clear old files - for (int i = 0; i < files.length; i++) { + if (files != null) { + for (int i = 0; i < files.length; i++) { File file = new File(dir, files[i]); file.delete(); + } + dir.delete(); } - dir.delete(); } } Index: src/test/org/apache/lucene/store/TestDirectory.java =================================================================== --- src/test/org/apache/lucene/store/TestDirectory.java (revision 718246) +++ src/test/org/apache/lucene/store/TestDirectory.java (working copy) @@ -18,6 +18,7 @@ */ import org.apache.lucene.util.LuceneTestCase; +import org.apache.lucene.util._TestUtil; import java.io.File; @@ -115,5 +116,17 @@ } } + // LUCENE-1464 + public void testDontCreate() throws Throwable { + File path = new File(System.getProperty("tempDir"), "doesnotexist"); + try { + assertTrue(!path.exists()); + Directory dir = new FSDirectory(path, null); + assertTrue(!path.exists()); + dir.close(); + } finally { + _TestUtil.rmDir(path); + } + } } Index: src/java/org/apache/lucene/index/SegmentInfos.java =================================================================== --- src/java/org/apache/lucene/index/SegmentInfos.java (revision 718246) +++ src/java/org/apache/lucene/index/SegmentInfos.java (working copy) @@ -130,8 +130,6 @@ */ public static long getCurrentSegmentGeneration(Directory directory) throws IOException { String[] files = directory.list(); - if (files == null) - throw new IOException("cannot read directory " + directory + ": list() returned null"); return getCurrentSegmentGeneration(files); } Index: src/java/org/apache/lucene/store/FSDirectory.java =================================================================== --- src/java/org/apache/lucene/store/FSDirectory.java (revision 719831) +++ src/java/org/apache/lucene/store/FSDirectory.java (working copy) @@ -178,7 +178,7 @@ public static FSDirectory getDirectory(File file, LockFactory lockFactory) throws IOException { - file = createCanonicalDir(file); + file = getCanonicalPath(file); FSDirectory dir; synchronized (DIRECTORIES) { @@ -197,6 +197,7 @@ if (lockFactory != null && lockFactory != dir.getLockFactory()) { throw new IOException("Directory was previously created with a different LockFactory instance; please pass null as the lockFactory instance and use setLockFactory to change it"); } + dir.checked = false; } } synchronized (dir) { @@ -256,17 +257,23 @@ } // returns the canonical version of the directory, creating it if it doesn't exist. - private static File createCanonicalDir(File file) throws IOException { - file = new File(file.getCanonicalPath()); + private static File getCanonicalPath(File file) throws IOException { + return new File(file.getCanonicalPath()); + } - if (file.exists() && !file.isDirectory()) - throw new IOException(file + " not a directory"); + private boolean checked; - if (!file.exists()) - if (!file.mkdirs()) - throw new IOException("Cannot create directory: " + file); + final void createDir() throws IOException { + if (!checked) { + if (directory.exists() && !directory.isDirectory()) + throw new IOException(directory + " not a directory"); - return file; + if (!directory.exists()) + if (!directory.mkdirs()) + throw new IOException("Cannot create directory: " + directory); + + checked = true; + } } private File directory = null; @@ -283,7 +290,7 @@ * Use {@link #getDirectory(String)} if singletons per path are needed. */ public FSDirectory(File path, LockFactory lockFactory) throws IOException { - path = createCanonicalDir(path); + path = getCanonicalPath(path); init(path, lockFactory); refCount = 1; } @@ -469,6 +476,7 @@ Returns a stream writing this file. */ public IndexOutput createOutput(String name) throws IOException { ensureOpen(); + createDir(); File file = new File(directory, name); if (file.exists() && !file.delete()) // delete existing, if any throw new IOException("Cannot overwrite: " + file);