LUCENE-1464: in FSDir, don't mkdir until first createOutput is called

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@721670 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael McCandless 2008-11-29 12:16:54 +00:00
parent 93f7ed2d58
commit 371a33eda8
4 changed files with 37 additions and 19 deletions

View File

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

View File

@ -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());
}
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 @@ 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 any
throw new IOException("Cannot overwrite: " + file);

View File

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

View File

@ -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();
}
}