Apache Lucene open-source search software
LUCENE-6146: Replaced Directory.copy() with Directory.copyFrom()
Robert Muir9fadf77120f9
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 6c1e3ba53a9..f070359096b 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -329,6 +329,9 @@ API Changes* LUCENE-6150: Remove staleFiles set and onIndexOutputClosed() from FSDirectory.(Uwe Schindler, Robert Muir, Mike McCandless)+* LUCENE-6146: Replaced Directory.copy() with Directory.copyFrom().+ (Robert Muir)+Bug Fixes* LUCENE-5650: Enforce read-only access to any path outside the temporarydiff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.javaindex c001f4da53e..a8bda05f733 100644--- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java@@ -2634,7 +2634,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable {assert !slowFileExists(directory, newFileName): "file \"" + newFileName + "\" already exists; newInfo.files=" + newInfo.files();- info.info.dir.copy(directory, file, newFileName, context);+ directory.copyFrom(info.info.dir, file, newFileName, context);}success = true;} finally {diff --git a/lucene/core/src/java/org/apache/lucene/store/Directory.java b/lucene/core/src/java/org/apache/lucene/store/Directory.javaindex 342145918e6..69323ab7b17 100644--- a/lucene/core/src/java/org/apache/lucene/store/Directory.java+++ b/lucene/core/src/java/org/apache/lucene/store/Directory.java@@ -126,7 +126,7 @@ public abstract class Directory implements Closeable {}/**- * Copies the file <i>src</i> to {@link Directory} <i>to</i> under the new+ * Copies the file <i>src</i> in <i>from</i> to this directory under the new* file name <i>dest</i>.* <p>* If you want to copy the entire source directory to the destination one, you@@ -135,31 +135,22 @@ public abstract class Directory implements Closeable {* <pre class="prettyprint">* Directory to; // the directory to copy to* for (String file : dir.listAll()) {- * dir.copy(to, file, newFile, IOContext.DEFAULT); // newFile can be either file, or a new name+ * to.copyFrom(dir, file, newFile, IOContext.DEFAULT); // newFile can be either file, or a new name* }* </pre>* <p>* <b>NOTE:</b> this method does not check whether <i>dest</i> exist and will* overwrite it if it does.*/- public void copy(Directory to, String src, String dest, IOContext context) throws IOException {- IndexOutput os = null;- IndexInput is = null;+ public void copyFrom(Directory from, String src, String dest, IOContext context) throws IOException {boolean success = false;- try {- os = to.createOutput(dest, context);- is = openInput(src, context);+ try (IndexInput is = from.openInput(src, context);+ IndexOutput os = createOutput(dest, context)) {os.copyBytes(is, is.length());success = true;} finally {- if (success) {- IOUtils.close(os, is);- } else {- IOUtils.closeWhileHandlingException(os, is);- try {- to.deleteFile(dest);- } catch (Throwable t) {- }+ if (!success) {+ IOUtils.deleteFilesIgnoringExceptions(this, dest);}}}diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.javaindex 6f7bed1023f..a1c20ad44ed 100644--- a/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java+++ b/lucene/core/src/java/org/apache/lucene/store/RAMDirectory.java@@ -94,7 +94,7 @@ public class RAMDirectory extends BaseDirectory implements Accountable {private RAMDirectory(Directory dir, boolean closeDir, IOContext context) throws IOException {this();for (String file : dir.listAll()) {- dir.copy(this, file, file, context);+ copyFrom(dir, file, file, context);}if (closeDir) {dir.close();diff --git a/lucene/core/src/java/org/apache/lucene/store/RateLimitedDirectoryWrapper.java b/lucene/core/src/java/org/apache/lucene/store/RateLimitedDirectoryWrapper.javaindex 4a514857532..7f53ad61196 100644--- a/lucene/core/src/java/org/apache/lucene/store/RateLimitedDirectoryWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/store/RateLimitedDirectoryWrapper.java@@ -51,9 +51,9 @@ public final class RateLimitedDirectoryWrapper extends FilterDirectory {}@Override- public void copy(Directory to, String src, String dest, IOContext context) throws IOException {+ public void copyFrom(Directory from, String src, String dest, IOContext context) throws IOException {ensureOpen();- in.copy(to, src, dest, context);+ in.copyFrom(from, src, dest, context);}private RateLimiter getRateLimiter(IOContext.Context context) {diff --git a/lucene/core/src/java/org/apache/lucene/store/TrackingDirectoryWrapper.java b/lucene/core/src/java/org/apache/lucene/store/TrackingDirectoryWrapper.javaindex 231fa595001..179fd9811ba 100644--- a/lucene/core/src/java/org/apache/lucene/store/TrackingDirectoryWrapper.java+++ b/lucene/core/src/java/org/apache/lucene/store/TrackingDirectoryWrapper.java@@ -34,20 +34,30 @@ public final class TrackingDirectoryWrapper extends FilterDirectory {@Overridepublic void deleteFile(String name) throws IOException {- createdFileNames.remove(name);in.deleteFile(name);+ createdFileNames.remove(name);}@Overridepublic IndexOutput createOutput(String name, IOContext context) throws IOException {+ IndexOutput output = in.createOutput(name, context);createdFileNames.add(name);- return in.createOutput(name, context);+ return output;}@Override- public void copy(Directory to, String src, String dest, IOContext context) throws IOException {+ public void copyFrom(Directory from, String src, String dest, IOContext context) throws IOException {+ in.copyFrom(from, src, dest, context);createdFileNames.add(dest);- in.copy(to, src, dest, context);+ }++ @Override+ public void renameFile(String source, String dest) throws IOException {+ in.renameFile(source, dest);+ synchronized (createdFileNames) {+ createdFileNames.add(dest);+ createdFileNames.remove(source);+ }}// maybe clone before returning.... all callers arediff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.javaindex 2880c1f3e0b..51c6f49939e 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOutOfFileDescriptors.java@@ -135,7 +135,7 @@ public class TestIndexWriterOutOfFileDescriptors extends LuceneTestCase {dirCopy = newMockFSDirectory(createTempDir("TestIndexWriterOutOfFileDescriptors.copy"));Set<String> files = new HashSet<>();for (String file : dir.listAll()) {- dir.copy(dirCopy, file, file, IOContext.DEFAULT);+ dirCopy.copyFrom(dir, file, file, IOContext.DEFAULT);files.add(file);}dirCopy.sync(files);diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.javaindex b9fedcef7f6..1b6021b6368 100644--- a/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.java+++ b/lucene/core/src/test/org/apache/lucene/store/TestFilterDirectory.java@@ -19,20 +19,25 @@ package org.apache.lucene.store;import java.io.IOException;import java.lang.reflect.Method;+import java.nio.file.Path;import java.util.HashSet;import java.util.Set;-import org.apache.lucene.util.LuceneTestCase;import org.junit.Test;-public class TestFilterDirectory extends LuceneTestCase {+public class TestFilterDirectory extends BaseDirectoryTestCase {+ @Override+ protected Directory getDirectory(Path path) throws IOException {+ return new FilterDirectory(new RAMDirectory());+ }+@Testpublic void testOverrides() throws Exception {// verify that all methods of Directory are overridden by FilterDirectory,// except those under the 'exclude' listSet<Method> exclude = new HashSet<>();- exclude.add(Directory.class.getMethod("copy", Directory.class, String.class, String.class, IOContext.class));+ exclude.add(Directory.class.getMethod("copyFrom", Directory.class, String.class, String.class, IOContext.class));exclude.add(Directory.class.getMethod("openChecksumInput", String.class, IOContext.class));for (Method m : FilterDirectory.class.getMethods()) {if (m.getDeclaringClass() == Directory.class) {diff --git a/lucene/core/src/test/org/apache/lucene/store/TestTrackingDirectoryWrapper.java b/lucene/core/src/test/org/apache/lucene/store/TestTrackingDirectoryWrapper.javanew file mode 100644index 00000000000..0985587eb84--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/store/TestTrackingDirectoryWrapper.java@@ -0,0 +1,67 @@+package org.apache.lucene.store;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import java.io.IOException;+import java.nio.file.Path;+import java.util.Collections;++public class TestTrackingDirectoryWrapper extends BaseDirectoryTestCase {++ @Override+ protected Directory getDirectory(Path path) throws IOException {+ return new TrackingDirectoryWrapper(new RAMDirectory());+ }++ public void testTrackEmpty() throws IOException {+ TrackingDirectoryWrapper dir = new TrackingDirectoryWrapper(new RAMDirectory());+ assertEquals(Collections.emptySet(), dir.getCreatedFiles());+ }++ public void testTrackCreate() throws IOException {+ TrackingDirectoryWrapper dir = new TrackingDirectoryWrapper(new RAMDirectory());+ dir.createOutput("foo", newIOContext(random())).close();+ assertEquals(asSet("foo"), dir.getCreatedFiles());+ }++ public void testTrackDelete() throws IOException {+ TrackingDirectoryWrapper dir = new TrackingDirectoryWrapper(new RAMDirectory());+ dir.createOutput("foo", newIOContext(random())).close();+ assertEquals(asSet("foo"), dir.getCreatedFiles());+ dir.deleteFile("foo");+ assertEquals(Collections.emptySet(), dir.getCreatedFiles());+ }++ public void testTrackRename() throws IOException {+ TrackingDirectoryWrapper dir = new TrackingDirectoryWrapper(new RAMDirectory());+ dir.createOutput("foo", newIOContext(random())).close();+ assertEquals(asSet("foo"), dir.getCreatedFiles());+ dir.renameFile("foo", "bar");+ assertEquals(asSet("bar"), dir.getCreatedFiles());+ }++ public void testTrackCopyFrom() throws IOException {+ TrackingDirectoryWrapper source = new TrackingDirectoryWrapper(new RAMDirectory());+ TrackingDirectoryWrapper dest = new TrackingDirectoryWrapper(new RAMDirectory());+ source.createOutput("foo", newIOContext(random())).close();+ assertEquals(asSet("foo"), source.getCreatedFiles());+ dest.copyFrom(source, "foo", "bar", newIOContext(random()));+ assertEquals(asSet("bar"), dest.getCreatedFiles());+ assertEquals(asSet("foo"), source.getCreatedFiles());+ }+}diff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/IndexAndTaxonomyReplicationHandler.java b/lucene/replicator/src/java/org/apache/lucene/replicator/IndexAndTaxonomyReplicationHandler.javaindex 3ae695b8c64..a7868a91f60 100644--- a/lucene/replicator/src/java/org/apache/lucene/replicator/IndexAndTaxonomyReplicationHandler.java+++ b/lucene/replicator/src/java/org/apache/lucene/replicator/IndexAndTaxonomyReplicationHandler.java@@ -132,9 +132,9 @@ public class IndexAndTaxonomyReplicationHandler implements ReplicationHandler {// reader sees a more advanced taxonomy than the index.if (taxoSegmentsFile != null) {- taxoClientDir.copy(taxoDir, taxoSegmentsFile, taxoPendingFile, IOContext.READONCE);+ taxoDir.copyFrom(taxoClientDir, taxoSegmentsFile, taxoPendingFile, IOContext.READONCE);}- indexClientDir.copy(indexDir, indexSegmentsFile, indexPendingFile, IOContext.READONCE);+ indexDir.copyFrom(indexClientDir, indexSegmentsFile, indexPendingFile, IOContext.READONCE);if (taxoSegmentsFile != null) {taxoDir.sync(Collections.singletonList(taxoPendingFile));diff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.java b/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.javaindex 192e2f75e1b..e4fed8ccf98 100644--- a/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.java+++ b/lucene/replicator/src/java/org/apache/lucene/replicator/IndexReplicationHandler.java@@ -173,7 +173,7 @@ public class IndexReplicationHandler implements ReplicationHandler {public static void copyFiles(Directory source, Directory target, List<String> files) throws IOException {if (!source.equals(target)) {for (String file : files) {- source.copy(target, file, file, IOContext.READONCE);+ target.copyFrom(source, file, file, IOContext.READONCE);}}}@@ -232,7 +232,7 @@ public class IndexReplicationHandler implements ReplicationHandler {indexDir.sync(files);// now copy and fsync segmentsFile as pending, then rename (simulating lucene commit)- clientDir.copy(indexDir, segmentsFile, pendingSegmentsFile, IOContext.READONCE);+ indexDir.copyFrom(clientDir, segmentsFile, pendingSegmentsFile, IOContext.READONCE);indexDir.sync(Collections.singletonList(pendingSegmentsFile));indexDir.renameFile(pendingSegmentsFile, segmentsFile);diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.javaindex 943bf45e61d..faa26ff7f90 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java@@ -46,7 +46,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {// first some basic tests for the directory api- public void testCopy() throws Exception {+ public void testCopyFrom() throws Exception {Directory source = getDirectory(createTempDir("testCopy"));Directory dest = newDirectory();@@ -57,7 +57,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {output.writeBytes(bytes, bytes.length);output.close();- source.copy(dest, "foobar", "foobaz", newIOContext(random()));+ dest.copyFrom(source, "foobar", "foobaz", newIOContext(random()));assertTrue(slowFileExists(dest, "foobaz"));IndexInput input = dest.openInput("foobaz", newIOContext(random()));@@ -71,7 +71,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {IOUtils.close(source, dest);}- public void testCopyDestination() throws Exception {+ public void testCopyFromDestination() throws Exception {Directory source = newDirectory();Directory dest = getDirectory(createTempDir("testCopyDestination"));@@ -82,7 +82,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {output.writeBytes(bytes, bytes.length);output.close();- source.copy(dest, "foobar", "foobaz", newIOContext(random()));+ dest.copyFrom(source, "foobar", "foobaz", newIOContext(random()));assertTrue(slowFileExists(dest, "foobaz"));IndexInput input = dest.openInput("foobaz", newIOContext(random()));@@ -141,7 +141,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {output2.writeString("bogus!");output2.close();- source.copy(dest, "foobar", "foobaz", newIOContext(random()));+ dest.copyFrom(source, "foobar", "foobaz", newIOContext(random()));assertTrue(slowFileExists(dest, "foobaz"));IndexInput input = dest.openInput("foobaz", newIOContext(random()));diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryWrapper.javaindex 86a0caea684..cf0350cc48d 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryWrapper.java@@ -73,8 +73,9 @@ public class BaseDirectoryWrapper extends FilterDirectory {return crossCheckTermVectorsOnClose;}+ // why does this class override this method?@Override- public void copy(Directory to, String src, String dest, IOContext context) throws IOException {- in.copy(to, src, dest, context);+ public void copyFrom(Directory from, String src, String dest, IOContext context) throws IOException {+ in.copyFrom(from, src, dest, context);}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.javaindex fec20e22e66..385ccf63568 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/MockDirectoryWrapper.java@@ -1048,11 +1048,13 @@ public class MockDirectoryWrapper extends BaseDirectoryWrapper {}}+ // TODO: why does this class override this method?+ // we should use the default implementation so all of our checks work?@Override- public synchronized void copy(Directory to, String src, String dest, IOContext context) throws IOException {+ public synchronized void copyFrom(Directory from, String src, String dest, IOContext context) throws IOException {maybeYield();// randomize the IOContext here?- in.copy(to, src, dest, context);+ in.copyFrom(from, src, dest, context);}/** Use this when throwing fake {@code IOException},diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.javaindex c83c7e6f41f..e8519a1578d 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java@@ -1337,7 +1337,7 @@ public abstract class LuceneTestCase extends Assert {public static BaseDirectoryWrapper newDirectory(Random r, Directory d) throws IOException {Directory impl = newDirectoryImpl(r, TEST_DIRECTORY);for (String file : d.listAll()) {- d.copy(impl, file, file, newIOContext(r));+ impl.copyFrom(d, file, file, newIOContext(r));}return wrapDirectory(r, impl, rarely(r));}diff --git a/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java b/solr/core/src/java/org/apache/solr/core/DirectoryFactory.javaindex a1e39eb347b..42374002bed 100644--- a/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java+++ b/solr/core/src/java/org/apache/solr/core/DirectoryFactory.java@@ -142,7 +142,7 @@ public abstract class DirectoryFactory implements NamedListInitializedPlugin,* @throws IOException If there is a low-level I/O error.*/public void move(Directory fromDir, Directory toDir, String fileName, IOContext ioContext) throws IOException {- fromDir.copy(toDir, fileName, fileName, ioContext);+ toDir.copyFrom(fromDir, fileName, fileName, ioContext);fromDir.deleteFile(fileName);}diff --git a/solr/core/src/java/org/apache/solr/handler/SnapShooter.java b/solr/core/src/java/org/apache/solr/handler/SnapShooter.javaindex 14aa0ee0232..b322dfcb67b 100644--- a/solr/core/src/java/org/apache/solr/handler/SnapShooter.java+++ b/solr/core/src/java/org/apache/solr/handler/SnapShooter.java@@ -226,7 +226,7 @@ public class SnapShooter {private static void copyFiles(Directory sourceDir, Collection<String> files, File destDir) throws IOException {try (FSDirectory dir = new SimpleFSDirectory(destDir.toPath(), NoLockFactory.INSTANCE)) {for (String indexFile : files) {- sourceDir.copy(dir, indexFile, indexFile, DirectoryFactory.IOCONTEXT_NO_CACHE);+ dir.copyFrom(sourceDir, indexFile, indexFile, DirectoryFactory.IOCONTEXT_NO_CACHE);}}}