Apache Lucene open-source search software
LUCENE-1464: in FSDir, don't mkdir until first createOutput is called
Michael McCandless371a33eda8f6
diff --git a/src/java/org/apache/lucene/index/SegmentInfos.java b/src/java/org/apache/lucene/index/SegmentInfos.javaindex 588e9269e61..a6ae54ee6ed 100644--- a/src/java/org/apache/lucene/index/SegmentInfos.java+++ b/src/java/org/apache/lucene/index/SegmentInfos.java@@ -130,8 +130,6 @@ final class SegmentInfos extends Vector {*/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);}diff --git a/src/java/org/apache/lucene/store/FSDirectory.java b/src/java/org/apache/lucene/store/FSDirectory.javaindex 3d8c4ddfae2..d4a6b99bcb4 100644--- a/src/java/org/apache/lucene/store/FSDirectory.java+++ b/src/java/org/apache/lucene/store/FSDirectory.java@@ -178,7 +178,7 @@ public class FSDirectory extends Directory {public static FSDirectory getDirectory(File file, LockFactory lockFactory)throws IOException{- file = createCanonicalDir(file);+ file = getCanonicalPath(file);FSDirectory dir;synchronized (DIRECTORIES) {@@ -197,6 +197,7 @@ public class FSDirectory extends Directory {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 @@ public class FSDirectory extends Directory {}// 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());+ }++ private boolean checked;- if (file.exists() && !file.isDirectory())- throw new IOException(file + " not a directory");+ final void createDir() throws IOException {+ if (!checked) {+ if (directory.exists() && !directory.isDirectory())+ throw new IOException(directory + " not a directory");- if (!file.exists())- if (!file.mkdirs())- throw new IOException("Cannot create directory: " + file);+ if (!directory.exists())+ if (!directory.mkdirs())+ throw new IOException("Cannot create directory: " + directory);- return file;+ checked = true;+ }}private File directory = null;@@ -283,7 +290,7 @@ public class FSDirectory extends Directory {* 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 @@ public class FSDirectory extends Directory {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 anythrow new IOException("Cannot overwrite: " + file);diff --git a/src/test/org/apache/lucene/store/TestDirectory.java b/src/test/org/apache/lucene/store/TestDirectory.javaindex 71a180e68bc..f57b51d13ba 100644--- a/src/test/org/apache/lucene/store/TestDirectory.java+++ b/src/test/org/apache/lucene/store/TestDirectory.java@@ -18,6 +18,7 @@ package org.apache.lucene.store;*/import org.apache.lucene.util.LuceneTestCase;+import org.apache.lucene.util._TestUtil;import java.io.File;@@ -115,5 +116,17 @@ public class TestDirectory extends LuceneTestCase {}}+ // 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);+ }+ }}diff --git a/src/test/org/apache/lucene/store/TestLockFactory.java b/src/test/org/apache/lucene/store/TestLockFactory.javaindex a661f49cd25..faae9b74655 100755--- a/src/test/org/apache/lucene/store/TestLockFactory.java+++ b/src/test/org/apache/lucene/store/TestLockFactory.java@@ -300,8 +300,8 @@ public class TestLockFactory extends LuceneTestCase {// Different lock factory instance should hit IOException:try {- FSDirectory fs2 = FSDirectory.getDirectory(indexDirName, new SingleInstanceLockFactory());- fail("Should have hit an IOException because LockFactory instances differ");+ FSDirectory.getDirectory(indexDirName, new SingleInstanceLockFactory());+ fail("Should have hit an IOException because LockFactory instances differ");} catch (IOException e) {}@@ -371,8 +371,6 @@ public class TestLockFactory extends LuceneTestCase {NativeFSLockFactory f = new NativeFSLockFactory(System.getProperty("tempDir"));- NativeFSLockFactory f2 = new NativeFSLockFactory(System.getProperty("tempDir"));-f.setLockPrefix("test");Lock l = f.makeLock("commit");Lock l2 = f.makeLock("commit");@@ -485,7 +483,6 @@ public class TestLockFactory extends LuceneTestCase {}public void run() {IndexSearcher searcher = null;- WhitespaceAnalyzer analyzer = new WhitespaceAnalyzer();Query query = new TermQuery(new Term("content", "aaa"));for(int i=0;i<this.numIteration;i++) {try{@@ -566,10 +563,12 @@ public class TestLockFactory extends LuceneTestCase {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();}}