Apache Lucene open-source search software
LUCENE-2637: FSDirectory.copyBytes isn't safe for SimpleFSDirectory
Robert Muir5fd2316799a1
diff --git a/lucene/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/src/java/org/apache/lucene/store/FSDirectory.javaindex b7ffb54db86..62c399028ba 100644--- a/lucene/src/java/org/apache/lucene/store/FSDirectory.java+++ b/lucene/src/java/org/apache/lucene/store/FSDirectory.java@@ -22,7 +22,6 @@ import java.io.FileNotFoundException;import java.io.FilenameFilter;import java.io.IOException;import java.io.RandomAccessFile;-import java.nio.channels.FileChannel;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;@@ -33,7 +32,6 @@ import java.util.HashSet;import java.util.Set;import java.util.concurrent.Future;-import org.apache.lucene.store.SimpleFSDirectory.SimpleFSIndexInput;import org.apache.lucene.util.ThreadInterruptedException;import org.apache.lucene.util.Constants;@@ -468,46 +466,6 @@ public abstract class FSDirectory extends Directory {public void flushBuffer(byte[] b, int offset, int size) throws IOException {file.write(b, offset, size);}-- @Override- public void copyBytes(DataInput input, long numBytes) throws IOException {- // Optimized copy only if the number of bytes to copy is larger than the- // buffer size, and the given IndexInput supports FileChannel copying.- // NOTE: the below check relies on NIOIndexInput extending Simple. If that- // changes in the future, we should change the check as well.- if (numBytes <= BUFFER_SIZE || !(input instanceof SimpleFSIndexInput)) {- super.copyBytes(input, numBytes);- return;- }-- SimpleFSIndexInput fsInput = (SimpleFSIndexInput) input;-- // flush any bytes in the input's buffer.- numBytes -= fsInput.flushBuffer(this, numBytes);-- // flush any bytes in the buffer- flush();-- // do the optimized copy- FileChannel in = fsInput.file.getChannel();-- // Necessary because BufferedIndexInput does lazy seeking:- in.position(fsInput.getFilePointer());-- FileChannel out = file.getChannel();- long pos = out.position();- long writeTo = numBytes + pos;- while (pos < writeTo) {- pos += out.transferFrom(in, pos, Math.min(CHANNEL_CHUNK_SIZE, writeTo - pos));- }- // transferFrom does not change the position of the channel. Need to change it manually- out.position(pos);-- // corrects the position in super (BufferedIndexOutput), so that calls- // to getFilePointer will return the correct pointer.- // Perhaps a specific method is better?- super.seek(out.position());- }@Overridepublic void close() throws IOException {diff --git a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.javaindex e9e998aa1dc..bbbf7cf8b8a 100644--- a/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java+++ b/lucene/src/test/org/apache/lucene/index/TestIndexWriter.java@@ -5134,8 +5134,7 @@ public class TestIndexWriter extends LuceneTestCase {}public void testRandomStoredFields() throws IOException {- File index = _TestUtil.getTempDir("lucenerandfields");- Directory dir = FSDirectory.open(index);+ Directory dir = newDirectory(random);Random rand = random;RandomIndexWriter w = new RandomIndexWriter(rand, dir, newIndexWriterConfig(rand, TEST_VERSION_CURRENT, new MockAnalyzer()).setMaxBufferedDocs(_TestUtil.nextInt(rand, 5, 20)));//w.w.setInfoStream(System.out);@@ -5221,7 +5220,6 @@ public class TestIndexWriter extends LuceneTestCase {}w.close();dir.close();- _TestUtil.rmDir(index);}private static class FailTwiceDuringMerge extends MockDirectoryWrapper.Failure {