diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index cc2ae8c0f32..1054a71f14f 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -110,6 +110,10 @@ New Features PushPostingsWriterBase for single-pass push of docs/positions to the postings format. (Mike McCandless) +* LUCENE-5906: Use Files.delete everywhere instead of File.delete, so that + when things go wrong, you get a real exception message why. + (Uwe Schindler, Robert Muir) + API Changes: * LUCENE-5900: Deprecated more constructors taking Version in *InfixSuggester and diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java index 06086baf20a..5aa28be7937 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java @@ -54,6 +54,7 @@ import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; @@ -203,6 +204,7 @@ public class Dictionary { OutputStream out = new BufferedOutputStream(new FileOutputStream(aff)); InputStream aff1 = null; InputStream aff2 = null; + boolean success = false; try { // copy contents of affix stream to temp file final byte [] buffer = new byte [1024 * 8]; @@ -228,9 +230,14 @@ public class Dictionary { words = b.finish(); aliases = null; // no longer needed morphAliases = null; // no longer needed + success = true; } finally { IOUtils.closeWhileHandlingException(out, aff1, aff2); - aff.delete(); + if (success) { + Files.delete(aff.toPath()); + } else { + IOUtils.deleteFilesIgnoringExceptions(aff); + } } } @@ -857,90 +864,107 @@ public class Dictionary { } } }); - sorter.sort(unsorted, sorted); - unsorted.delete(); - - ByteSequencesReader reader = new ByteSequencesReader(sorted); - BytesRefBuilder scratchLine = new BytesRefBuilder(); - - // TODO: the flags themselves can be double-chars (long) or also numeric - // either way the trick is to encode them as char... but they must be parsed differently - - String currentEntry = null; - IntsRefBuilder currentOrds = new IntsRefBuilder(); - - String line; - while (reader.read(scratchLine)) { - line = scratchLine.get().utf8ToString(); - String entry; - char wordForm[]; - int end; - - int flagSep = line.indexOf(FLAG_SEPARATOR); - if (flagSep == -1) { - wordForm = NOFLAGS; - end = line.indexOf(MORPH_SEPARATOR); - entry = line.substring(0, end); + boolean success = false; + try { + sorter.sort(unsorted, sorted); + success = true; + } finally { + if (success) { + Files.delete(unsorted.toPath()); } else { - end = line.indexOf(MORPH_SEPARATOR); - String flagPart = line.substring(flagSep + 1, end); - if (aliasCount > 0) { - flagPart = getAliasValue(Integer.parseInt(flagPart)); - } - - wordForm = flagParsingStrategy.parseFlags(flagPart); - Arrays.sort(wordForm); - entry = line.substring(0, flagSep); - } - // we possibly have morphological data - int stemExceptionID = 0; - if (hasStemExceptions && end+1 < line.length()) { - String stemException = parseStemException(line.substring(end+1)); - if (stemException != null) { - if (stemExceptionCount == stemExceptions.length) { - int newSize = ArrayUtil.oversize(stemExceptionCount+1, RamUsageEstimator.NUM_BYTES_OBJECT_REF); - stemExceptions = Arrays.copyOf(stemExceptions, newSize); - } - stemExceptionID = stemExceptionCount+1; // we use '0' to indicate no exception for the form - stemExceptions[stemExceptionCount++] = stemException; - } - } - - int cmp = currentEntry == null ? 1 : entry.compareTo(currentEntry); - if (cmp < 0) { - throw new IllegalArgumentException("out of order: " + entry + " < " + currentEntry); - } else { - encodeFlags(flagsScratch, wordForm); - int ord = flagLookup.add(flagsScratch.get()); - if (ord < 0) { - // already exists in our hash - ord = (-ord)-1; - } - // finalize current entry, and switch "current" if necessary - if (cmp > 0 && currentEntry != null) { - Util.toUTF32(currentEntry, scratchInts); - words.add(scratchInts.get(), currentOrds.get()); - } - // swap current - if (cmp > 0 || currentEntry == null) { - currentEntry = entry; - currentOrds = new IntsRefBuilder(); // must be this way - } - if (hasStemExceptions) { - currentOrds.append(ord); - currentOrds.append(stemExceptionID); - } else { - currentOrds.append(ord); - } + IOUtils.deleteFilesIgnoringExceptions(unsorted); } } - // finalize last entry - Util.toUTF32(currentEntry, scratchInts); - words.add(scratchInts.get(), currentOrds.get()); + boolean success2 = false; + ByteSequencesReader reader = new ByteSequencesReader(sorted); + try { + BytesRefBuilder scratchLine = new BytesRefBuilder(); - reader.close(); - sorted.delete(); + // TODO: the flags themselves can be double-chars (long) or also numeric + // either way the trick is to encode them as char... but they must be parsed differently + + String currentEntry = null; + IntsRefBuilder currentOrds = new IntsRefBuilder(); + + String line; + while (reader.read(scratchLine)) { + line = scratchLine.get().utf8ToString(); + String entry; + char wordForm[]; + int end; + + int flagSep = line.indexOf(FLAG_SEPARATOR); + if (flagSep == -1) { + wordForm = NOFLAGS; + end = line.indexOf(MORPH_SEPARATOR); + entry = line.substring(0, end); + } else { + end = line.indexOf(MORPH_SEPARATOR); + String flagPart = line.substring(flagSep + 1, end); + if (aliasCount > 0) { + flagPart = getAliasValue(Integer.parseInt(flagPart)); + } + + wordForm = flagParsingStrategy.parseFlags(flagPart); + Arrays.sort(wordForm); + entry = line.substring(0, flagSep); + } + // we possibly have morphological data + int stemExceptionID = 0; + if (hasStemExceptions && end+1 < line.length()) { + String stemException = parseStemException(line.substring(end+1)); + if (stemException != null) { + if (stemExceptionCount == stemExceptions.length) { + int newSize = ArrayUtil.oversize(stemExceptionCount+1, RamUsageEstimator.NUM_BYTES_OBJECT_REF); + stemExceptions = Arrays.copyOf(stemExceptions, newSize); + } + stemExceptionID = stemExceptionCount+1; // we use '0' to indicate no exception for the form + stemExceptions[stemExceptionCount++] = stemException; + } + } + + int cmp = currentEntry == null ? 1 : entry.compareTo(currentEntry); + if (cmp < 0) { + throw new IllegalArgumentException("out of order: " + entry + " < " + currentEntry); + } else { + encodeFlags(flagsScratch, wordForm); + int ord = flagLookup.add(flagsScratch.get()); + if (ord < 0) { + // already exists in our hash + ord = (-ord)-1; + } + // finalize current entry, and switch "current" if necessary + if (cmp > 0 && currentEntry != null) { + Util.toUTF32(currentEntry, scratchInts); + words.add(scratchInts.get(), currentOrds.get()); + } + // swap current + if (cmp > 0 || currentEntry == null) { + currentEntry = entry; + currentOrds = new IntsRefBuilder(); // must be this way + } + if (hasStemExceptions) { + currentOrds.append(ord); + currentOrds.append(stemExceptionID); + } else { + currentOrds.append(ord); + } + } + } + + // finalize last entry + Util.toUTF32(currentEntry, scratchInts); + words.add(scratchInts.get(), currentOrds.get()); + success2 = true; + } finally { + IOUtils.closeWhileHandlingException(reader); + if (success2) { + Files.delete(sorted.toPath()); + } else { + IOUtils.deleteFilesIgnoringExceptions(sorted); + } + } } static char[] decodeFlags(BytesRef b) { diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestFilesystemResourceLoader.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestFilesystemResourceLoader.java index 2c5f9084b07..b24c9372dd7 100644 --- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestFilesystemResourceLoader.java +++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/util/TestFilesystemResourceLoader.java @@ -87,7 +87,7 @@ public class TestFilesystemResourceLoader extends LuceneTestCase { assertClasspathDelegation(rl); assertNotFound(rl); } finally { - TestUtil.rm(base); + IOUtils.rm(base); } } diff --git a/lucene/analysis/stempel/src/test/org/egothor/stemmer/TestCompile.java b/lucene/analysis/stempel/src/test/org/egothor/stemmer/TestCompile.java index 4852bd8570f..6b5c99d5481 100644 --- a/lucene/analysis/stempel/src/test/org/egothor/stemmer/TestCompile.java +++ b/lucene/analysis/stempel/src/test/org/egothor/stemmer/TestCompile.java @@ -66,6 +66,7 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Locale; import java.util.StringTokenizer; @@ -87,7 +88,7 @@ public class TestCompile extends LuceneTestCase { Trie trie = loadTrie(compiled); assertTrie(trie, path, true, true); assertTrie(trie, path, false, true); - new File(compiled).delete(); + Files.delete(new File(compiled).toPath()); } public void testCompileBackwards() throws Exception { @@ -103,7 +104,7 @@ public class TestCompile extends LuceneTestCase { Trie trie = loadTrie(compiled); assertTrie(trie, path, true, true); assertTrie(trie, path, false, true); - new File(compiled).delete(); + Files.delete(new File(compiled).toPath()); } public void testCompileMulti() throws Exception { @@ -119,7 +120,7 @@ public class TestCompile extends LuceneTestCase { Trie trie = loadTrie(compiled); assertTrie(trie, path, true, true); assertTrie(trie, path, false, true); - new File(compiled).delete(); + Files.delete(new File(compiled).toPath()); } static Trie loadTrie(String path) throws IOException { diff --git a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java index b532bc587a2..cef25eb1372 100644 --- a/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java +++ b/lucene/backward-codecs/src/test/org/apache/lucene/index/TestBackwardsCompatibility.java @@ -355,7 +355,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase { assertTrue(bos.toString(IOUtils.UTF_8).contains(IndexFormatTooOldException.class.getName())); dir.close(); - TestUtil.rm(oldIndxeDir); + IOUtils.rm(oldIndxeDir); } } @@ -666,7 +666,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase { public File createIndex(String dirName, boolean doCFS, boolean fullyMerged) throws IOException { // we use a real directory name that is not cleaned up, because this method is only used to create backwards indexes: File indexDir = new File("/tmp/idx", dirName); - TestUtil.rm(indexDir); + IOUtils.rm(indexDir); Directory dir = newFSDirectory(indexDir); LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy(); mp.setNoCFSRatio(doCFS ? 1.0 : 0.0); diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java index 42584db46d9..a63181f95fd 100644 --- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java +++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/PerfRunData.java @@ -37,7 +37,6 @@ import org.apache.lucene.benchmark.byTask.tasks.ReadTask; import org.apache.lucene.benchmark.byTask.tasks.SearchTask; import org.apache.lucene.benchmark.byTask.utils.AnalyzerFactory; import org.apache.lucene.benchmark.byTask.utils.Config; -import org.apache.lucene.benchmark.byTask.utils.FileUtils; import org.apache.lucene.facet.taxonomy.TaxonomyReader; import org.apache.lucene.facet.taxonomy.TaxonomyWriter; import org.apache.lucene.index.DirectoryReader; @@ -195,7 +194,7 @@ public class PerfRunData implements Closeable { File workDir = new File(config.get("work.dir","work")); File indexDir = new File(workDir,dirName); if (eraseIndex && indexDir.exists()) { - FileUtils.fullyDelete(indexDir); + IOUtils.rm(indexDir); } indexDir.mkdirs(); return FSDirectory.open(indexDir); diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/FileUtils.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/FileUtils.java deleted file mode 100644 index 2ad6492be4f..00000000000 --- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/byTask/utils/FileUtils.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.apache.lucene.benchmark.byTask.utils; - -/* - * 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.File; -import java.io.IOException; - -/** - * File utilities. - */ -public class FileUtils { - - /** - * Delete files and directories, even if non-empty. - * - * @param dir file or directory - * @return true on success, false if no or part of files have been deleted - * @throws IOException If there is a low-level I/O error. - */ - public static boolean fullyDelete(File dir) throws IOException { - if (dir == null || !dir.exists()) return false; - File contents[] = dir.listFiles(); - if (contents != null) { - for (int i = 0; i < contents.length; i++) { - if (contents[i].isFile()) { - if (!contents[i].delete()) { - return false; - } - } else { - if (!fullyDelete(contents[i])) { - return false; - } - } - } - } - return dir.delete(); - } - -} diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractReuters.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractReuters.java index 8e7f7e639b7..7568374a3af 100644 --- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractReuters.java +++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractReuters.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -37,12 +38,12 @@ public class ExtractReuters { private File outputDir; private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - public ExtractReuters(File reutersDir, File outputDir) { + public ExtractReuters(File reutersDir, File outputDir) throws IOException { this.reutersDir = reutersDir; this.outputDir = outputDir; System.out.println("Deleting all files in " + outputDir); for (File f : outputDir.listFiles()) { - f.delete(); + Files.delete(f.toPath()); } } @@ -122,7 +123,7 @@ public class ExtractReuters { } } - public static void main(String[] args) { + public static void main(String[] args) throws Exception { if (args.length != 2) { usage("Wrong number of arguments ("+args.length+")"); return; diff --git a/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractWikipedia.java b/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractWikipedia.java index 6bc02196aa4..66ec9d0e1aa 100644 --- a/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractWikipedia.java +++ b/lucene/benchmark/src/java/org/apache/lucene/benchmark/utils/ExtractWikipedia.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Properties; import org.apache.lucene.benchmark.byTask.feeds.ContentSource; @@ -44,13 +45,13 @@ public class ExtractWikipedia { static final int BASE = 10; protected DocMaker docMaker; - public ExtractWikipedia(DocMaker docMaker, File outputDir) { + public ExtractWikipedia(DocMaker docMaker, File outputDir) throws IOException { this.outputDir = outputDir; this.docMaker = docMaker; System.out.println("Deleting all files in " + outputDir); File[] files = outputDir.listFiles(); for (int i = 0; i < files.length; i++) { - files[i].delete(); + Files.delete(files[i].toPath()); } } diff --git a/lucene/benchmark/src/test/org/apache/lucene/benchmark/BenchmarkTestCase.java b/lucene/benchmark/src/test/org/apache/lucene/benchmark/BenchmarkTestCase.java index f735c12719d..0b840e32bd7 100644 --- a/lucene/benchmark/src/test/org/apache/lucene/benchmark/BenchmarkTestCase.java +++ b/lucene/benchmark/src/test/org/apache/lucene/benchmark/BenchmarkTestCase.java @@ -38,8 +38,6 @@ public abstract class BenchmarkTestCase extends LuceneTestCase { @BeforeClass public static void beforeClassBenchmarkTestCase() { WORKDIR = createTempDir("benchmark"); - WORKDIR.delete(); - WORKDIR.mkdirs(); } @AfterClass diff --git a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java index f45cdfd4227..9166237cec5 100644 --- a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java +++ b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/TestPerfTasksLogic.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.text.Collator; import java.util.List; import java.util.Locale; @@ -444,7 +445,7 @@ public class TestPerfTasksLogic extends BenchmarkTestCase { assertEquals(numLines + " lines were created but " + ir.numDocs() + " docs are in the index", numLines, ir.numDocs()); ir.close(); - lineFile.delete(); + Files.delete(lineFile.toPath()); } /** diff --git a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/utils/StreamUtilsTest.java b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/utils/StreamUtilsTest.java index 8a88e2049a9..63bd31ae6fb 100644 --- a/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/utils/StreamUtilsTest.java +++ b/lucene/benchmark/src/test/org/apache/lucene/benchmark/byTask/utils/StreamUtilsTest.java @@ -30,6 +30,7 @@ import java.nio.charset.StandardCharsets; import org.apache.commons.compress.compressors.CompressorStreamFactory; import org.apache.lucene.benchmark.BenchmarkTestCase; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.TestUtil; import org.junit.After; import org.junit.Before; @@ -136,14 +137,14 @@ public class StreamUtilsTest extends BenchmarkTestCase { public void setUp() throws Exception { super.setUp(); testDir = new File(getWorkDir(),"ContentSourceTest"); - TestUtil.rm(testDir); + IOUtils.rm(testDir); assertTrue(testDir.mkdirs()); } @Override @After public void tearDown() throws Exception { - TestUtil.rm(testDir); + IOUtils.rm(testDir); super.tearDown(); } diff --git a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java index ed7968ab803..7aad29d1e76 100644 --- a/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java +++ b/lucene/core/src/java/org/apache/lucene/store/FSDirectory.java @@ -26,6 +26,7 @@ import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.FilterOutputStream; import java.io.IOException; +import java.nio.file.Files; import java.util.Collection; import java.util.HashSet; import java.util.Set; @@ -251,8 +252,7 @@ public abstract class FSDirectory extends BaseDirectory { public void deleteFile(String name) throws IOException { ensureOpen(); File file = new File(directory, name); - if (!file.delete()) - throw new IOException("Cannot delete " + file); + Files.delete(file.toPath()); staleFiles.remove(name); } @@ -271,8 +271,7 @@ public abstract class FSDirectory extends BaseDirectory { throw new IOException("Cannot create directory: " + directory); File file = new File(directory, name); - if (file.exists() && !file.delete()) // delete existing, if any - throw new IOException("Cannot overwrite: " + file); + Files.deleteIfExists(file.toPath()); // delete existing, if any } /** diff --git a/lucene/core/src/java/org/apache/lucene/store/LockReleaseFailedException.java b/lucene/core/src/java/org/apache/lucene/store/LockReleaseFailedException.java index fe43e7307db..68d296a3756 100644 --- a/lucene/core/src/java/org/apache/lucene/store/LockReleaseFailedException.java +++ b/lucene/core/src/java/org/apache/lucene/store/LockReleaseFailedException.java @@ -28,4 +28,8 @@ public class LockReleaseFailedException extends IOException { public LockReleaseFailedException(String message) { super(message); } + + public LockReleaseFailedException(String message, Throwable cause) { + super(message, cause); + } } diff --git a/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java b/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java index 6bae591a94b..1022b92c11a 100644 --- a/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java +++ b/lucene/core/src/java/org/apache/lucene/store/SimpleFSLockFactory.java @@ -19,6 +19,7 @@ package org.apache.lucene.store; import java.io.File; import java.io.IOException; +import java.nio.file.Files; /** *

Implements {@link LockFactory} using {@link @@ -102,9 +103,7 @@ public class SimpleFSLockFactory extends FSLockFactory { lockName = lockPrefix + "-" + lockName; } File lockFile = new File(lockDir, lockName); - if (lockFile.exists() && !lockFile.delete()) { - throw new IOException("Cannot delete " + lockFile); - } + Files.deleteIfExists(lockFile.toPath()); } } } @@ -147,8 +146,11 @@ class SimpleFSLock extends Lock { @Override public void close() throws LockReleaseFailedException { - if (lockFile.exists() && !lockFile.delete()) { - throw new LockReleaseFailedException("failed to delete " + lockFile); + // TODO: wierd that clearLock() throws the raw IOException... + try { + Files.deleteIfExists(lockFile.toPath()); + } catch (Throwable cause) { + throw new LockReleaseFailedException("failed to delete " + lockFile, cause); } } diff --git a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java index 88a2d7b03d3..42e923f1895 100644 --- a/lucene/core/src/java/org/apache/lucene/util/IOUtils.java +++ b/lucene/core/src/java/org/apache/lucene/util/IOUtils.java @@ -33,7 +33,11 @@ import java.nio.charset.Charset; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.StandardOpenOption; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; /** This class emulates the new Java 7 "Try-With-Resources" statement. * Remove once Lucene is on Java 7. @@ -69,22 +73,7 @@ public final class IOUtils { * objects to call close() on */ public static void close(Closeable... objects) throws IOException { - Throwable th = null; - - for (Closeable object : objects) { - try { - if (object != null) { - object.close(); - } - } catch (Throwable t) { - addSuppressed(th, t); - if (th == null) { - th = t; - } - } - } - - reThrow(th); + close(Arrays.asList(objects)); } /** @@ -118,14 +107,7 @@ public final class IOUtils { * objects to call close() on */ public static void closeWhileHandlingException(Closeable... objects) { - for (Closeable object : objects) { - try { - if (object != null) { - object.close(); - } - } catch (Throwable t) { - } - } + closeWhileHandlingException(Arrays.asList(objects)); } /** @@ -245,6 +227,113 @@ public final class IOUtils { } } } + + /** + * Deletes all given files, suppressing all thrown IOExceptions. + *

+ * Some of the files may be null, if so they are ignored. + */ + public static void deleteFilesIgnoringExceptions(File... files) { + deleteFilesIgnoringExceptions(Arrays.asList(files)); + } + + /** + * Deletes all given files, suppressing all thrown IOExceptions. + *

+ * Some of the files may be null, if so they are ignored. + */ + public static void deleteFilesIgnoringExceptions(Iterable files) { + for (File name : files) { + if (name != null) { + try { + Files.delete(name.toPath()); + } catch (Throwable ignored) { + // ignore + } + } + } + } + + /** + * Deletes all given Files, if they exist. Some of the + * Files may be null; they are + * ignored. After everything is deleted, the method either + * throws the first exception it hit while deleting, or + * completes normally if there were no exceptions. + * + * @param files files to delete + */ + public static void deleteFilesIfExist(File... files) throws IOException { + deleteFilesIfExist(Arrays.asList(files)); + } + + /** + * Deletes all given Files, if they exist. Some of the + * Files may be null; they are + * ignored. After everything is deleted, the method either + * throws the first exception it hit while deleting, or + * completes normally if there were no exceptions. + * + * @param files files to delete + */ + public static void deleteFilesIfExist(Iterable files) throws IOException { + Throwable th = null; + + for (File file : files) { + try { + if (file != null) { + Files.deleteIfExists(file.toPath()); + } + } catch (Throwable t) { + addSuppressed(th, t); + if (th == null) { + th = t; + } + } + } + + reThrow(th); + } + + /** + * Deletes one or more files or directories (and everything underneath it). + * + * @throws IOException if any of the given files (or their subhierarchy files in case + * of directories) cannot be removed. + */ + public static void rm(File... locations) throws IOException { + LinkedHashMap unremoved = rm(new LinkedHashMap(), locations); + if (!unremoved.isEmpty()) { + StringBuilder b = new StringBuilder("Could not remove the following files (in the order of attempts):\n"); + for (Map.Entry kv : unremoved.entrySet()) { + b.append(" ") + .append(kv.getKey().getAbsolutePath()) + .append(": ") + .append(kv.getValue()) + .append("\n"); + } + throw new IOException(b.toString()); + } + } + + private static LinkedHashMap rm(LinkedHashMap unremoved, File... locations) { + if (locations != null) { + for (File location : locations) { + if (location != null && location.exists()) { + if (location.isDirectory()) { + rm(unremoved, location.listFiles()); + } + + try { + Files.delete(location.toPath()); + } catch (Throwable cause) { + unremoved.put(location, cause); + } + } + } + } + return unremoved; + } /** * Copy one file's contents to another file. The target will be overwritten diff --git a/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java b/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java index 30a82b2b21b..441b6027566 100644 --- a/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java +++ b/lucene/core/src/java/org/apache/lucene/util/OfflineSorter.java @@ -31,6 +31,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -223,10 +224,10 @@ public final class OfflineSorter { sortInfo = new SortInfo(); sortInfo.totalTime = System.currentTimeMillis(); - output.delete(); + Files.deleteIfExists(output.toPath()); ArrayList merges = new ArrayList<>(); - boolean success2 = false; + boolean success3 = false; try { ByteSequencesReader is = new ByteSequencesReader(input); boolean success = false; @@ -240,11 +241,15 @@ public final class OfflineSorter { // Handle intermediate merges. if (merges.size() == maxTempFiles) { File intermediate = File.createTempFile("sort", "intermediate", tempDirectory); + boolean success2 = false; try { mergePartitions(merges, intermediate); + success2 = true; } finally { - for (File file : merges) { - file.delete(); + if (success2) { + IOUtils.deleteFilesIfExist(merges); + } else { + IOUtils.deleteFilesIgnoringExceptions(merges); } merges.clear(); merges.add(intermediate); @@ -272,13 +277,13 @@ public final class OfflineSorter { // otherwise merge the partitions with a priority queue. mergePartitions(merges, output); } - success2 = true; + success3 = true; } finally { - for (File file : merges) { - file.delete(); - } - if (!success2) { - output.delete(); + if (success3) { + IOUtils.deleteFilesIfExist(merges); + } else { + IOUtils.deleteFilesIgnoringExceptions(merges); + IOUtils.deleteFilesIgnoringExceptions(output); } } diff --git a/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.java b/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.java index e887e8516e4..94e55096cd4 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestAtomicUpdate.java @@ -178,6 +178,6 @@ public class TestAtomicUpdate extends LuceneTestCase { directory = newFSDirectory(dirPath); runTest(directory); directory.close(); - TestUtil.rm(dirPath); + IOUtils.rm(dirPath); } } diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java index 99231726ab1..1046363da64 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestDirectoryReader.java @@ -20,6 +20,7 @@ package org.apache.lucene.index; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.util.Collection; import java.util.HashSet; @@ -41,6 +42,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.store.NoSuchDirectoryException; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; import org.junit.Assume; @@ -446,7 +448,7 @@ public void testFilesOpenClose() throws IOException { dir.close(); // Try to erase the data - this ensures that the writer closed all files - TestUtil.rm(dirFile); + IOUtils.rm(dirFile); dir = newFSDirectory(dirFile); // Now create the data set again, just as before @@ -464,7 +466,7 @@ public void testFilesOpenClose() throws IOException { // The following will fail if reader did not close // all files - TestUtil.rm(dirFile); + IOUtils.rm(dirFile); } public void testOpenReaderAfterDelete() throws IOException { @@ -477,7 +479,7 @@ public void testFilesOpenClose() throws IOException { // expected } - dirFile.delete(); + Files.delete(dirFile.toPath()); // Make sure we still get a CorruptIndexException (not NPE): try { @@ -716,7 +718,7 @@ public void testFilesOpenClose() throws IOException { // good exception public void testNoDir() throws Throwable { File tempDir = createTempDir("doesnotexist"); - TestUtil.rm(tempDir); + IOUtils.rm(tempDir); Directory dir = newFSDirectory(tempDir); try { DirectoryReader.open(dir); @@ -1052,7 +1054,7 @@ public void testFilesOpenClose() throws IOException { public void testIndexExistsOnNonExistentDirectory() throws Exception { File tempDir = createTempDir("testIndexExistsOnNonExistentDirectory"); - tempDir.delete(); + Files.delete(tempDir.toPath()); Directory dir = newFSDirectory(tempDir); assertFalse(DirectoryReader.indexExists(dir)); dir.close(); diff --git a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java b/lucene/core/src/test/org/apache/lucene/index/TestDoc.java index 25da2996ff1..a2b06de5691 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestDoc.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestDoc.java @@ -26,6 +26,7 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -87,7 +88,7 @@ public class TestDoc extends LuceneTestCase { try { File f = new File(workDir, name); - if (f.exists()) f.delete(); + Files.deleteIfExists(f.toPath()); fw = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_8); pw = new PrintWriter(fw); diff --git a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java index 59c1835762b..fc582c0a4cc 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestFieldsReader.java @@ -34,6 +34,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; import org.junit.AfterClass; @@ -249,7 +250,7 @@ public class TestFieldsReader extends LuceneTestCase { reader.close(); dir.close(); } finally { - TestUtil.rm(indexDir); + IOUtils.rm(indexDir); } } diff --git a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java index 1ae4a5bce6d..c0db3dcd1eb 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestIndexWriterOnJRECrash.java @@ -44,8 +44,6 @@ public class TestIndexWriterOnJRECrash extends TestNRTThreads { public void setUp() throws Exception { super.setUp(); tempDir = createTempDir("jrecrash"); - tempDir.delete(); - tempDir.mkdir(); } @Override @Nightly diff --git a/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java b/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java index b88d8b26028..f5801209a53 100644 --- a/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java +++ b/lucene/core/src/test/org/apache/lucene/index/TestNeverDelete.java @@ -26,6 +26,7 @@ import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.store.BaseDirectoryWrapper; import org.apache.lucene.store.MockDirectoryWrapper; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; @@ -107,6 +108,6 @@ public class TestNeverDelete extends LuceneTestCase { w.close(); d.close(); - TestUtil.rm(tmpDir); + IOUtils.rm(tmpDir); } } diff --git a/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java b/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java index 52401e8a996..614e1923d3c 100644 --- a/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java +++ b/lucene/core/src/test/org/apache/lucene/store/TestBufferedIndexInput.java @@ -38,6 +38,7 @@ import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.ArrayUtil; @@ -279,7 +280,7 @@ public class TestBufferedIndexInput extends LuceneTestCase { writer.close(); reader.close(); } finally { - TestUtil.rm(indexDir); + IOUtils.rm(indexDir); } } diff --git a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java index d96b735abf0..66f1f81da7c 100644 --- a/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java +++ b/lucene/core/src/test/org/apache/lucene/store/TestDirectory.java @@ -24,6 +24,7 @@ import java.nio.file.NoSuchFileException; import java.util.Arrays; import java.util.Collections; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.TestUtil; public class TestDirectory extends BaseDirectoryTestCase { @@ -135,7 +136,7 @@ public class TestDirectory extends BaseDirectoryTestCase { assertFalse(dir.isOpen); } - TestUtil.rm(path); + IOUtils.rm(path); } // LUCENE-1468 @@ -147,7 +148,7 @@ public class TestDirectory extends BaseDirectoryTestCase { Directory fsDir = new SimpleFSDirectory(path, null); assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length); } finally { - TestUtil.rm(path); + IOUtils.rm(path); } } @@ -167,7 +168,7 @@ public class TestDirectory extends BaseDirectoryTestCase { } } finally { fsDir.close(); - TestUtil.rm(path); + IOUtils.rm(path); } } } diff --git a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java index 2946649e4e7..b3dd7caa793 100644 --- a/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java +++ b/lucene/core/src/test/org/apache/lucene/store/TestFileSwitchDirectory.java @@ -31,6 +31,7 @@ import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.TestIndexWriterReader; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.TestUtil; public class TestFileSwitchDirectory extends BaseDirectoryTestCase { @@ -99,8 +100,7 @@ public class TestFileSwitchDirectory extends BaseDirectoryTestCase { public void testNoDir() throws Throwable { File primDir = createTempDir("foo"); File secondDir = createTempDir("bar"); - TestUtil.rm(primDir); - TestUtil.rm(secondDir); + IOUtils.rm(primDir, secondDir); Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.emptySet()); try { DirectoryReader.open(dir); diff --git a/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java b/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java index d28228fb1b5..efd59a15ac4 100644 --- a/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java +++ b/lucene/core/src/test/org/apache/lucene/store/TestLockFactory.java @@ -19,6 +19,7 @@ package org.apache.lucene.store; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -35,6 +36,7 @@ import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; @@ -170,7 +172,7 @@ public class TestLockFactory extends LuceneTestCase { dir.close(); // Cleanup - TestUtil.rm(indexDir); + IOUtils.rm(indexDir); } // Verify: NativeFSLockFactory works correctly @@ -208,9 +210,7 @@ public class TestLockFactory extends LuceneTestCase { assertTrue("failed to obtain lock", l.obtain()); l.close(); assertFalse("failed to release lock", l.isLocked()); - if (lockFile.exists()) { - lockFile.delete(); - } + Files.deleteIfExists(lockFile.toPath()); } // Verify: NativeFSLockFactory assigns null as lockPrefix if the lockDir is inside directory @@ -230,8 +230,7 @@ public class TestLockFactory extends LuceneTestCase { dir1.close(); dir2.close(); - TestUtil.rm(fdir1); - TestUtil.rm(fdir2); + IOUtils.rm(fdir1, fdir2); } // Verify: default LockFactory has no prefix (ie @@ -253,7 +252,7 @@ public class TestLockFactory extends LuceneTestCase { assertNull("Default lock prefix should be null", dir.getLockFactory().getLockPrefix()); dir.close(); - TestUtil.rm(dirName); + IOUtils.rm(dirName); } private class WriterThread extends Thread { diff --git a/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java b/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java index ffb72605958..50612b571bb 100644 --- a/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java +++ b/lucene/core/src/test/org/apache/lucene/store/TestRAMDirectory.java @@ -78,8 +78,8 @@ public class TestRAMDirectory extends BaseDirectoryTestCase { fsDir = newFSDirectory(path); assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length); } finally { - TestUtil.rm(path); IOUtils.close(fsDir); + IOUtils.rm(path); } } diff --git a/lucene/core/src/test/org/apache/lucene/store/TestWindowsMMap.java b/lucene/core/src/test/org/apache/lucene/store/TestWindowsMMap.java index fa35a944f37..91eb8eb0d01 100644 --- a/lucene/core/src/test/org/apache/lucene/store/TestWindowsMMap.java +++ b/lucene/core/src/test/org/apache/lucene/store/TestWindowsMMap.java @@ -65,7 +65,6 @@ public class TestWindowsMMap extends LuceneTestCase { // may take some time until the files are finally dereferenced. So clean the // directory up front, or otherwise new IndexWriter will fail. File dirPath = createTempDir("testLuceneMmap"); - rmDir(dirPath); MMapDirectory dir = new MMapDirectory(dirPath, null); // plan to add a set of useful stopwords, consider changing some of the @@ -88,16 +87,5 @@ public class TestWindowsMMap extends LuceneTestCase { reader.close(); writer.close(); - rmDir(dirPath); - } - - private void rmDir(File dir) { - if (!dir.exists()) { - return; - } - for (File file : dir.listFiles()) { - file.delete(); - } - dir.delete(); } } diff --git a/lucene/core/src/test/org/apache/lucene/util/TestOfflineSorter.java b/lucene/core/src/test/org/apache/lucene/util/TestOfflineSorter.java index c13135caeb3..26a168dd2fe 100644 --- a/lucene/core/src/test/org/apache/lucene/util/TestOfflineSorter.java +++ b/lucene/core/src/test/org/apache/lucene/util/TestOfflineSorter.java @@ -44,14 +44,12 @@ public class TestOfflineSorter extends LuceneTestCase { public void setUp() throws Exception { super.setUp(); tempDir = createTempDir("mergesort"); - TestUtil.rm(tempDir); - tempDir.mkdirs(); } @Override public void tearDown() throws Exception { if (tempDir != null) - TestUtil.rm(tempDir); + IOUtils.rm(tempDir); super.tearDown(); } diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java index 6ffca05f1e7..8b479163302 100644 --- a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java +++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java @@ -20,8 +20,10 @@ package org.apache.lucene.util.junitcompat; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; +import java.nio.file.Files; import org.apache.lucene.util.Constants; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.TestUtil; import org.junit.Assert; @@ -45,11 +47,11 @@ public class TestLeaveFilesIfTestFails extends WithNestedTests { } @Test - public void testLeaveFilesIfTestFails() { + public void testLeaveFilesIfTestFails() throws IOException { Result r = JUnitCore.runClasses(Nested1.class); Assert.assertEquals(1, r.getFailureCount()); Assert.assertTrue(Nested1.file != null && Nested1.file.exists()); - Nested1.file.delete(); + Files.delete(Nested1.file.toPath()); } public static class Nested2 extends WithNestedTests.AbstractNestedTest { @@ -75,6 +77,6 @@ public class TestLeaveFilesIfTestFails extends WithNestedTests { Assert.assertEquals(1, r.getFailureCount()); Nested2.openFile.close(); - TestUtil.rm(Nested2.parent); + IOUtils.rm(Nested2.parent); } } diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java index d9bf2a02e73..ca334204542 100644 --- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java +++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java @@ -9,6 +9,7 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; @@ -941,9 +942,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter { in.close(); // Delete the temporary file, which is no longer needed. - if (!tmpfile.delete()) { - tmpfile.deleteOnExit(); - } + Files.delete(tmpfile.toPath()); return map; } diff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCharBlockArray.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCharBlockArray.java index 30729dae3e2..0c2045a7742 100644 --- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCharBlockArray.java +++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCharBlockArray.java @@ -96,7 +96,6 @@ public class TestCharBlockArray extends FacetTestCase { array = CharBlockArray.open(in); assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch after flush/load.", builder, array); in.close(); - f.delete(); } private static void assertEqualsInternal(String msg, StringBuilder expected, CharBlockArray actual) { diff --git a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCompactLabelToOrdinal.java b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCompactLabelToOrdinal.java index cfd71a32f28..6b1097ed02b 100644 --- a/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCompactLabelToOrdinal.java +++ b/lucene/facet/src/test/org/apache/lucene/facet/taxonomy/writercache/TestCompactLabelToOrdinal.java @@ -5,6 +5,7 @@ import java.nio.ByteBuffer; import java.nio.charset.CharsetDecoder; import java.nio.charset.CodingErrorAction; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashMap; import java.util.Map; import java.util.Random; @@ -76,7 +77,7 @@ public class TestCompactLabelToOrdinal extends FacetTestCase { if (i > 0 && i % flushInterval == 0) { compact.flush(f); compact = CompactLabelToOrdinal.open(f, 0.15f, 3); - assertTrue(f.delete()); + Files.delete(f.toPath()); if (flushInterval < (n / 10)) { flushInterval *= 10; } diff --git a/lucene/replicator/src/java/org/apache/lucene/replicator/PerSessionDirectoryFactory.java b/lucene/replicator/src/java/org/apache/lucene/replicator/PerSessionDirectoryFactory.java index 3dcd1b3fbc2..fe43752b674 100644 --- a/lucene/replicator/src/java/org/apache/lucene/replicator/PerSessionDirectoryFactory.java +++ b/lucene/replicator/src/java/org/apache/lucene/replicator/PerSessionDirectoryFactory.java @@ -23,6 +23,7 @@ import java.io.IOException; import org.apache.lucene.replicator.ReplicationClient.SourceDirectoryFactory; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.util.IOUtils; /** * A {@link SourceDirectoryFactory} which returns {@link FSDirectory} under a @@ -40,19 +41,6 @@ public class PerSessionDirectoryFactory implements SourceDirectoryFactory { this.workDir = workDir; } - private void rm(File file) throws IOException { - if (file.isDirectory()) { - for (File f : file.listFiles()) { - rm(f); - } - } - - // This should be either an empty directory, or a file - if (!file.delete() && file.exists()) { - throw new IOException("failed to delete " + file); - } - } - @Override public Directory getDirectory(String sessionID, String source) throws IOException { File sessionDir = new File(workDir, sessionID); @@ -71,7 +59,7 @@ public class PerSessionDirectoryFactory implements SourceDirectoryFactory { if (sessionID.isEmpty()) { // protect against deleting workDir entirely! throw new IllegalArgumentException("sessionID cannot be empty"); } - rm(new File(workDir, sessionID)); + IOUtils.rm(new File(workDir, sessionID)); } } diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedInputIterator.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedInputIterator.java index 8a4db6cc1d5..3a8ad160c88 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedInputIterator.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/SortedInputIterator.java @@ -202,12 +202,16 @@ public class SortedInputIterator implements InputIterator { } private void close() throws IOException { - IOUtils.close(reader); - if (tempInput != null) { - tempInput.delete(); - } - if (tempSorted != null) { - tempSorted.delete(); + boolean success = false; + try { + IOUtils.close(reader); + success = true; + } finally { + if (success) { + IOUtils.deleteFilesIfExist(tempInput, tempSorted); + } else { + IOUtils.deleteFilesIgnoringExceptions(tempInput, tempSorted); + } } } diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java index 48afe0bffb6..f42a70a1c59 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggester.java @@ -19,6 +19,7 @@ package org.apache.lucene.search.suggest.analyzing; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -500,7 +501,7 @@ public class AnalyzingSuggester extends Lookup { new OfflineSorter(new AnalyzingComparator(hasPayloads)).sort(tempInput, tempSorted); // Free disk space: - tempInput.delete(); + Files.delete(tempInput.toPath()); reader = new OfflineSorter.ByteSequencesReader(tempSorted); @@ -593,14 +594,13 @@ public class AnalyzingSuggester extends Lookup { success = true; } finally { - if (success) { - IOUtils.close(reader, writer); - } else { - IOUtils.closeWhileHandlingException(reader, writer); - } + IOUtils.closeWhileHandlingException(reader, writer); - tempInput.delete(); - tempSorted.delete(); + if (success) { + IOUtils.deleteFilesIfExist(tempInput, tempSorted); + } else { + IOUtils.deleteFilesIgnoringExceptions(tempInput, tempSorted); + } } } diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/FreeTextSuggester.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/FreeTextSuggester.java index c294dac8b31..cbcdc901434 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/FreeTextSuggester.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/analyzing/FreeTextSuggester.java @@ -376,23 +376,12 @@ public class FreeTextSuggester extends Lookup { } finally { try { if (success) { - IOUtils.close(reader); + IOUtils.close(reader, dir); } else { - IOUtils.closeWhileHandlingException(reader, writer); + IOUtils.closeWhileHandlingException(reader, writer, dir); } } finally { - for(String file : dir.listAll()) { - File path = new File(tempIndexPath, file); - if (path.delete() == false) { - throw new IllegalStateException("failed to remove " + path); - } - } - - if (tempIndexPath.delete() == false) { - throw new IllegalStateException("failed to remove " + tempIndexPath); - } - - dir.close(); + IOUtils.rm(tempIndexPath); } } } diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/ExternalRefSorter.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/ExternalRefSorter.java index 8ceb937e74d..78d69af551b 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/ExternalRefSorter.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/ExternalRefSorter.java @@ -20,6 +20,7 @@ package org.apache.lucene.search.suggest.fst; import java.io.Closeable; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.Comparator; import org.apache.lucene.util.BytesRef; @@ -61,9 +62,18 @@ public class ExternalRefSorter implements BytesRefSorter, Closeable { sorted = File.createTempFile("RefSorter-", ".sorted", OfflineSorter.defaultTempDir()); - sort.sort(input, sorted); + boolean success = false; + try { + sort.sort(input, sorted); + success = true; + } finally { + if (success) { + Files.delete(input.toPath()); + } else { + IOUtils.deleteFilesIgnoringExceptions(input); + } + } - input.delete(); input = null; } @@ -82,11 +92,16 @@ public class ExternalRefSorter implements BytesRefSorter, Closeable { */ @Override public void close() throws IOException { + boolean success = false; try { closeWriter(); + success = true; } finally { - if (input != null) input.delete(); - if (sorted != null) sorted.delete(); + if (success) { + IOUtils.deleteFilesIfExist(input, sorted); + } else { + IOUtils.deleteFilesIgnoringExceptions(input, sorted); + } } } diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java index e6b33de2d48..4af480db4f4 100644 --- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java +++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/fst/FSTCompletionLookup.java @@ -19,6 +19,7 @@ package org.apache.lucene.search.suggest.fst; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -189,7 +190,7 @@ public class FSTCompletionLookup extends Lookup implements Accountable { // We don't know the distribution of scores and we need to bucket them, so we'll sort // and divide into equal buckets. SortInfo info = new OfflineSorter().sort(tempInput, tempSorted); - tempInput.delete(); + Files.delete(tempInput.toPath()); FSTCompletionBuilder builder = new FSTCompletionBuilder( buckets, sorter = new ExternalRefSorter(new OfflineSorter()), sharedTailLength); @@ -231,13 +232,13 @@ public class FSTCompletionLookup extends Lookup implements Accountable { success = true; } finally { - if (success) - IOUtils.close(reader, writer, sorter); - else - IOUtils.closeWhileHandlingException(reader, writer, sorter); + IOUtils.closeWhileHandlingException(reader, writer, sorter); - tempInput.delete(); - tempSorted.delete(); + if (success) { + Files.delete(tempSorted.toPath()); + } else { + IOUtils.deleteFilesIgnoringExceptions(tempInput, tempSorted); + } } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java index d6038acd4bd..9b07768ffd7 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/BasePostingsFormatTestCase.java @@ -1356,7 +1356,7 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTest fieldsProducer.close(); dir.close(); - TestUtil.rm(path); + IOUtils.rm(path); } public void testDocsOnly() throws Exception { @@ -1405,7 +1405,7 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTest fieldsProducer = null; dir.close(); - TestUtil.rm(path); + IOUtils.rm(path); } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.java index 6383c1283d8..3c8c4edcdac 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/ThreadedIndexingAndSearchingTestCase.java @@ -42,6 +42,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.FailOnNonBulkMergesInfoStream; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LineFileDocs; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.NamedThreadFactory; @@ -653,7 +654,7 @@ public abstract class ThreadedIndexingAndSearchingTestCase extends LuceneTestCas TestUtil.checkIndex(dir); dir.close(); - TestUtil.rm(tempDir); + IOUtils.rm(tempDir); if (VERBOSE) { System.out.println("TEST: done [" + (System.currentTimeMillis()-t0) + " ms]"); 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.java index e21482a1af9..d52734d9e22 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 @@ -21,6 +21,7 @@ import java.io.EOFException; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.util.Arrays; import java.util.Collections; @@ -453,7 +454,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase { * mkdir the underling directory in the filesystem. */ public void testDontCreate() throws Throwable { File path = createTempDir("doesnotexist"); - TestUtil.rm(path); + IOUtils.rm(path); assertTrue(!path.exists()); Directory dir = getDirectory(path); assertTrue(!path.exists()); @@ -559,7 +560,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase { // LUCENE-3382 -- make sure we get exception if the directory really does not exist. public void testNoDir() throws Throwable { File tempDir = createTempDir("doesnotexist"); - TestUtil.rm(tempDir); + IOUtils.rm(tempDir); Directory dir = getDirectory(tempDir); try { DirectoryReader.open(dir); @@ -767,7 +768,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase { out.close(); // delete it - assertTrue(new File(path, "afile").delete()); + Files.delete(new File(path, "afile").toPath()); // directory is empty assertEquals(0, fsdir.listAll().length); diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/RemoveUponClose.java b/lucene/test-framework/src/java/org/apache/lucene/util/RemoveUponClose.java index d30c379ef30..4f012275a78 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/RemoveUponClose.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/RemoveUponClose.java @@ -46,7 +46,7 @@ final class RemoveUponClose implements Closeable { if (failureMarker.wasSuccessful()) { if (file.exists()) { try { - TestUtil.rm(file); + IOUtils.rm(file); } catch (IOException e) { throw new IOException( "Could not remove temporary location '" diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleTemporaryFilesCleanup.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleTemporaryFilesCleanup.java index 52433e28afe..d139d60b189 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleTemporaryFilesCleanup.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleTemporaryFilesCleanup.java @@ -122,7 +122,7 @@ final class TestRuleTemporaryFilesCleanup extends TestRuleAdapter { // and leave them there. if (failureMarker.wasSuccessful()) { try { - TestUtil.rm(everything); + IOUtils.rm(everything); } catch (IOException e) { Class suiteClass = RandomizedContext.current().getTargetClass(); if (suiteClass.isAnnotationPresent(SuppressTempFileChecks.class)) { diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java index deea11f438b..56f3f5f3c84 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java +++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestUtil.java @@ -28,10 +28,11 @@ import java.io.PrintStream; import java.math.BigDecimal; import java.math.BigInteger; import java.nio.CharBuffer; +import java.nio.file.Files; import java.util.Arrays; import java.util.Enumeration; import java.util.HashMap; -import java.util.LinkedHashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Random; @@ -99,48 +100,12 @@ public final class TestUtil { // } - /** - * Deletes one or more files or directories (and everything underneath it). - * - * @throws IOException if any of the given files (or their subhierarchy files in case - * of directories) cannot be removed. - */ - public static void rm(File... locations) throws IOException { - LinkedHashSet unremoved = rm(new LinkedHashSet(), locations); - if (!unremoved.isEmpty()) { - StringBuilder b = new StringBuilder("Could not remove the following files (in the order of attempts):\n"); - for (File f : unremoved) { - b.append(" ") - .append(f.getAbsolutePath()) - .append("\n"); - } - throw new IOException(b.toString()); - } - } - - private static LinkedHashSet rm(LinkedHashSet unremoved, File... locations) { - if (locations != null) { - for (File location : locations) { - if (location != null && location.exists()) { - if (location.isDirectory()) { - rm(unremoved, location.listFiles()); - } - - if (!location.delete()) { - unremoved.add(location); - } - } - } - } - return unremoved; - } - /** * Convenience method unzipping zipName into destDir, cleaning up * destDir first. */ public static void unzip(File zipName, File destDir) throws IOException { - rm(destDir); + IOUtils.rm(destDir); destDir.mkdir(); ZipFile zipFile = new ZipFile(zipName); diff --git a/lucene/tools/forbiddenApis/base.txt b/lucene/tools/forbiddenApis/base.txt index f634d84c1cf..755d087440b 100644 --- a/lucene/tools/forbiddenApis/base.txt +++ b/lucene/tools/forbiddenApis/base.txt @@ -29,3 +29,5 @@ java.util.Properties#store(java.io.OutputStream,java.lang.String) java.lang.Character#codePointBefore(char[],int) @ Implicit start offset is error-prone when the char[] is a buffer and the first chars are random chars java.lang.Character#codePointAt(char[],int) @ Implicit end offset is error-prone when the char[] is a buffer and the last chars are random chars + +java.io.File#delete() @ use Files.delete for real exception, IOUtils.deleteFilesIgnoringExceptions if you dont care diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/AbstractSqlEntityProcessorTestCase.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/AbstractSqlEntityProcessorTestCase.java index 7badb353356..78b3bfd027e 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/AbstractSqlEntityProcessorTestCase.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/AbstractSqlEntityProcessorTestCase.java @@ -1,6 +1,7 @@ package org.apache.solr.handler.dataimport; import java.io.File; +import java.nio.file.Files; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -60,7 +61,7 @@ public abstract class AbstractSqlEntityProcessorTestCase extends } @After - public void afterSqlEntitiyProcessorTestCase() { + public void afterSqlEntitiyProcessorTestCase() throws Exception { useSimpleCaches = false; countryEntity = false; countryCached = false; @@ -74,8 +75,8 @@ public abstract class AbstractSqlEntityProcessorTestCase extends //If an Assume was tripped while setting up the test, //the file might not ever have been created... if(fileLocation!=null) { - new File(fileLocation + File.separatorChar + fileName).delete(); - new File(fileLocation).delete(); + Files.deleteIfExists(new File(fileLocation + File.separatorChar + fileName).toPath()); + Files.deleteIfExists(new File(fileLocation).toPath()); } } diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestFileListEntityProcessor.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestFileListEntityProcessor.java index 1fac521c9d4..e87f7640091 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestFileListEntityProcessor.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestFileListEntityProcessor.java @@ -24,6 +24,7 @@ import org.junit.Test; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.text.SimpleDateFormat; import java.util.*; @@ -65,7 +66,7 @@ public class TestFileListEntityProcessor extends AbstractDataImportHandlerTestCa @Test public void testBiggerSmallerFiles() throws IOException { File tmpdir = File.createTempFile("test", "tmp", createTempDir()); - tmpdir.delete(); + Files.delete(tmpdir.toPath()); tmpdir.mkdir(); long minLength = Long.MAX_VALUE; diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestNonWritablePersistFile.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestNonWritablePersistFile.java index a592c455f4a..ab53346e304 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestNonWritablePersistFile.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestNonWritablePersistFile.java @@ -92,7 +92,7 @@ public class TestNonWritablePersistFile extends AbstractDataImportHandlerTestCas runFullImport(dataConfig_delta); assertQ(req("id:1"), "//*[@numFound='0']"); } finally { - f.delete(); + f.setWritable(true); } } } diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorEndToEnd.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorEndToEnd.java index c62b3648b9d..9f0fd9b7569 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorEndToEnd.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestSolrEntityProcessorEndToEnd.java @@ -26,6 +26,7 @@ import java.util.Map; import java.util.Map.Entry; import org.apache.commons.io.FileUtils; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.TestUtil; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.embedded.JettySolrRunner; @@ -335,7 +336,7 @@ public class TestSolrEntityProcessorEndToEnd extends AbstractDataImportHandlerTe } public void tearDown() throws Exception { - TestUtil.rm(homeDir); + IOUtils.rm(homeDir); } } diff --git a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrOutputFormat.java b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrOutputFormat.java index 9f0498dc455..bd4c1b0e9c6 100644 --- a/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrOutputFormat.java +++ b/solr/contrib/map-reduce/src/java/org/apache/solr/hadoop/SolrOutputFormat.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Arrays; import java.util.HashSet; import java.util.Locale; @@ -224,7 +225,7 @@ public class SolrOutputFormat extends FileOutputFormat { // to store in the zip file } - out.delete(); + Files.deleteIfExists(out.toPath()); int subst = dir.toString().length(); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out)); byte[] buf = new byte[1024]; diff --git a/solr/contrib/map-reduce/src/test/org/apache/solr/hadoop/MRUnitBase.java b/solr/contrib/map-reduce/src/test/org/apache/solr/hadoop/MRUnitBase.java index 42ff9d9a99b..f382f76db0e 100644 --- a/solr/contrib/map-reduce/src/test/org/apache/solr/hadoop/MRUnitBase.java +++ b/solr/contrib/map-reduce/src/test/org/apache/solr/hadoop/MRUnitBase.java @@ -18,6 +18,7 @@ package org.apache.solr.hadoop; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.Locale; import org.apache.commons.io.FileUtils; @@ -44,7 +45,7 @@ public abstract class MRUnitBase extends SolrTestCaseJ4 { @AfterClass public static void teardownClass() throws Exception { - if (solrHomeZip != null) solrHomeZip.delete(); + if (solrHomeZip != null) Files.delete(solrHomeZip.toPath()); } protected void setupHadoopConfig(Configuration config) throws IOException { diff --git a/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/FileUtils.java b/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/FileUtils.java index 691decbd962..e979d37bbc7 100644 --- a/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/FileUtils.java +++ b/solr/contrib/morphlines-core/src/java/org/apache/solr/morphlines/solr/FileUtils.java @@ -19,6 +19,7 @@ package org.apache.solr.morphlines.solr; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.nio.file.Files; class FileUtils { @@ -39,11 +40,7 @@ class FileUtils { cleanDirectory(directory); } - if (!directory.delete()) { - String message = - "Unable to delete directory " + directory + "."; - throw new IOException(message); - } + Files.delete(directory.toPath()); } /** @@ -136,15 +133,7 @@ class FileUtils { if (file.isDirectory()) { deleteDirectory(file); } else { - boolean filePresent = file.exists(); - if (!file.delete()) { - if (!filePresent){ - throw new FileNotFoundException("File does not exist: " + file); - } - String message = - "Unable to delete file: " + file; - throw new IOException(message); - } + Files.delete(file.toPath()); } } diff --git a/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.java b/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.java index a5359fd26db..1ca0d4fb66e 100644 --- a/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/PingRequestHandler.java @@ -19,6 +19,7 @@ package org.apache.solr.handler; import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.Date; import java.util.Locale; @@ -280,10 +281,12 @@ public class PingRequestHandler extends RequestHandlerBase implements SolrCoreAw "Unable to write healthcheck flag file", e); } } else { - if (healthcheck.exists() && !healthcheck.delete()){ + try { + Files.deleteIfExists(healthcheck.toPath()); + } catch (Throwable cause) { throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "Did not successfully delete healthcheck file: " - +healthcheck.getAbsolutePath()); + +healthcheck.getAbsolutePath(), cause); } } } diff --git a/solr/core/src/java/org/apache/solr/handler/SnapPuller.java b/solr/core/src/java/org/apache/solr/handler/SnapPuller.java index 65da7932737..fd92d38284c 100644 --- a/solr/core/src/java/org/apache/solr/handler/SnapPuller.java +++ b/solr/core/src/java/org/apache/solr/handler/SnapPuller.java @@ -27,6 +27,7 @@ import java.io.Writer; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.NoSuchFileException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -1021,28 +1022,30 @@ public class SnapPuller { return nameVsFile.isEmpty() ? Collections.EMPTY_LIST : nameVsFile.values(); } - static boolean delTree(File dir) { - boolean isSuccess = true; - File contents[] = dir.listFiles(); - if (contents != null) { - for (File file : contents) { - if (file.isDirectory()) { - boolean success = delTree(file); - if (!success) { - LOG.warn("Unable to delete directory : " + file); - isSuccess = false; - } - } else { - boolean success = file.delete(); - if (!success) { - LOG.warn("Unable to delete file : " + file); - isSuccess = false; - return false; - } - } - } + /** + * This simulates File.delete exception-wise, since this class has some strange behavior with it. + * The only difference is it returns null on success, throws SecurityException on SecurityException, + * otherwise returns Throwable preventing deletion (instead of false), for additional information. + */ + static Throwable delete(File file) { + try { + Files.delete(file.toPath()); + return null; + } catch (SecurityException e) { + throw e; + } catch (Throwable other) { + return other; + } + } + + static boolean delTree(File dir) { + try { + org.apache.lucene.util.IOUtils.rm(dir); + return true; + } catch (IOException e) { + LOG.warn("Unable to delete directory : " + dir, e); + return false; } - return isSuccess && dir.delete(); } /** @@ -1574,9 +1577,12 @@ public class SnapPuller { //if the download is not complete then //delete the file being downloaded try { - file.delete(); - } catch (Exception e) { + Files.delete(file.toPath()); + } catch (SecurityException e) { LOG.error("Error deleting file in cleanup" + e.getMessage()); + } catch (Throwable other) { + // TODO: should this class care if a file couldnt be deleted? + // this just emulates previous behavior, where only SecurityException would be handled. } //if the failure is due to a user abort it is returned nomally else an exception is thrown if (!aborted) diff --git a/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java b/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java index 3e830fb67d7..377ec9b8b25 100644 --- a/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java +++ b/solr/core/src/java/org/apache/solr/rest/ManagedResourceStorage.java @@ -30,6 +30,7 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashMap; import java.util.Locale; import java.util.Map; @@ -183,7 +184,21 @@ public abstract class ManagedResourceStorage { @Override public boolean delete(String storedResourceId) throws IOException { File storedFile = new File(storageDir, storedResourceId); - return storedFile.isFile() ? storedFile.delete() : false; + return deleteIfFile(storedFile); + } + + // TODO: this interface should probably be changed, this simulates the old behavior, + // only throw security exception, just return false otherwise + private boolean deleteIfFile(File f) { + if (!f.isFile()) { + return false; + } + try { + Files.delete(f.toPath()); + return true; + } catch (IOException cause) { + return false; + } } @Override diff --git a/solr/core/src/java/org/apache/solr/update/TransactionLog.java b/solr/core/src/java/org/apache/solr/update/TransactionLog.java index f5aef97651e..88b8133fd7c 100644 --- a/solr/core/src/java/org/apache/solr/update/TransactionLog.java +++ b/solr/core/src/java/org/apache/solr/update/TransactionLog.java @@ -24,6 +24,7 @@ import java.io.RandomAccessFile; import java.nio.ByteBuffer; import java.nio.channels.Channels; import java.nio.channels.FileChannel; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -543,7 +544,12 @@ public class TransactionLog { } if (deleteOnClose) { - tlogFile.delete(); + try { + Files.deleteIfExists(tlogFile.toPath()); + } catch (IOException e) { + // TODO: should this class care if a file couldnt be deleted? + // this just emulates previous behavior, where only SecurityException would be handled. + } } } catch (IOException e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); diff --git a/solr/core/src/java/org/apache/solr/update/UpdateLog.java b/solr/core/src/java/org/apache/solr/update/UpdateLog.java index f117469aa68..a19f15fdcd2 100644 --- a/solr/core/src/java/org/apache/solr/update/UpdateLog.java +++ b/solr/core/src/java/org/apache/solr/update/UpdateLog.java @@ -24,6 +24,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FilenameFilter; import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -1444,10 +1445,8 @@ public class UpdateLog implements PluginInfoInitialized { public static void deleteFile(File file) { boolean success = false; try { - success = file.delete(); - if (!success) { - log.error("Error deleting file: " + file); - } + Files.deleteIfExists(file.toPath()); + success = true; } catch (Exception e) { log.error("Error deleting file: " + file, e); } @@ -1489,9 +1488,11 @@ public class UpdateLog implements PluginInfoInitialized { String[] files = getLogList(tlogDir); for (String file : files) { File f = new File(tlogDir, file); - boolean s = f.delete(); - if (!s) { - log.error("Could not remove tlog file:" + f); + try { + Files.delete(f.toPath()); + } catch (IOException cause) { + // NOTE: still throws SecurityException as before. + log.error("Could not remove tlog file:" + f, cause); } } } diff --git a/solr/core/src/java/org/apache/solr/util/VersionedFile.java b/solr/core/src/java/org/apache/solr/util/VersionedFile.java index fc0a13fa384..d2342411aea 100644 --- a/solr/core/src/java/org/apache/solr/util/VersionedFile.java +++ b/solr/core/src/java/org/apache/solr/util/VersionedFile.java @@ -21,7 +21,9 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FilenameFilter; +import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -95,7 +97,12 @@ public class VersionedFile List deleted = new ArrayList<>(); for (File df : deleteList) { try { - df.delete(); + try { + Files.deleteIfExists(df.toPath()); + } catch (IOException cause) { + // TODO: should this class care if a file couldnt be deleted? + // this just emulates previous behavior, where only SecurityException would be handled. + } // deleteList.remove(df); deleted.add(df); } catch (SecurityException e) { diff --git a/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZk2Test.java b/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZk2Test.java index a1198e3f19a..651cbbed6b5 100644 --- a/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZk2Test.java +++ b/solr/core/src/test/org/apache/solr/cloud/BasicDistributedZk2Test.java @@ -26,6 +26,7 @@ import java.util.regex.Pattern; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.BasicResponseHandler; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.TestUtil; import org.apache.solr.SolrTestCaseJ4.SuppressSSL; import org.apache.solr.client.solrj.SolrQuery; @@ -487,7 +488,7 @@ public class BasicDistributedZk2Test extends AbstractFullDistribZkTestBase { assertEquals(Arrays.asList(files).toString(), 1, files.length); File snapDir = files[0]; - TestUtil.rm(snapDir); + IOUtils.rm(snapDir); } private void addNewReplica() throws Exception { diff --git a/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java b/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java index 7dda63ef76a..c0d12034884 100644 --- a/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java +++ b/solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.java @@ -22,6 +22,7 @@ import junit.framework.Assert; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.core.KeywordTokenizerFactory; import org.apache.lucene.analysis.ngram.NGramFilterFactory; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.TestUtil; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.SolrException; @@ -76,7 +77,7 @@ public class ResourceLoaderTest extends SolrTestCaseJ4 } loader.close(); } finally { - TestUtil.rm(temp); + IOUtils.rm(temp); } } diff --git a/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java b/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java index bfc2b3603a6..391690943b0 100644 --- a/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java +++ b/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java @@ -28,6 +28,7 @@ import org.junit.Before; import org.junit.Test; import java.io.File; +import java.nio.file.Files; import java.util.Map; public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 { @@ -41,7 +42,7 @@ public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 { // test tests native and simple in the same jvm in the same exact directory: // the file will remain after the native test (it cannot safely be deleted without the risk of deleting another guys lock) // its ok, these aren't "compatible" anyway: really this test should not re-use the same directory at all. - new File(new File(initCoreDataDir, "index"), IndexWriter.WRITE_LOCK_NAME).delete(); + Files.deleteIfExists(new File(new File(initCoreDataDir, "index"), IndexWriter.WRITE_LOCK_NAME).toPath()); } @Test diff --git a/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java b/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java index 642ccb37845..87dab276775 100644 --- a/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java +++ b/solr/core/src/test/org/apache/solr/core/TestArbitraryIndexDir.java @@ -132,6 +132,5 @@ public class TestArbitraryIndexDir extends AbstractSolrTestCase{ "*[count(//doc)=1]" ); dir.close(); - newDir.delete(); } } diff --git a/solr/core/src/test/org/apache/solr/handler/TestCSVLoader.java b/solr/core/src/test/org/apache/solr/handler/TestCSVLoader.java index 56824283401..031c0178d92 100644 --- a/solr/core/src/test/org/apache/solr/handler/TestCSVLoader.java +++ b/solr/core/src/test/org/apache/solr/handler/TestCSVLoader.java @@ -30,6 +30,7 @@ import org.junit.Test; import java.io.*; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.List; import java.util.ArrayList; @@ -62,7 +63,7 @@ public class TestCSVLoader extends SolrTestCaseJ4 { // if you override setUp or tearDown, you better call // the super classes version super.tearDown(); - deleteFile(); + Files.delete(file.toPath()); } void makeFile(String contents) { @@ -75,10 +76,6 @@ public class TestCSVLoader extends SolrTestCaseJ4 { } } - void deleteFile() { - file.delete(); - } - void cleanup() { assertU(delQ("*:*")); assertU(commit()); diff --git a/solr/core/src/test/org/apache/solr/handler/TestReplicationHandler.java b/solr/core/src/test/org/apache/solr/handler/TestReplicationHandler.java index f8491c718c4..270fcb015e1 100644 --- a/solr/core/src/test/org/apache/solr/handler/TestReplicationHandler.java +++ b/solr/core/src/test/org/apache/solr/handler/TestReplicationHandler.java @@ -34,6 +34,7 @@ import java.util.Collection; import java.util.Date; import java.util.Set; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.lucene.util.TestUtil; import org.apache.solr.BaseDistributedSearchTestCase; @@ -1437,7 +1438,7 @@ public class TestReplicationHandler extends SolrTestCaseJ4 { } public void tearDown() throws Exception { - TestUtil.rm(homeDir); + IOUtils.rm(homeDir); } public void copyConfigFile(String srcFile, String destFile) diff --git a/solr/core/src/test/org/apache/solr/handler/TestReplicationHandlerBackup.java b/solr/core/src/test/org/apache/solr/handler/TestReplicationHandlerBackup.java index 26f9090e758..fd34fb25475 100644 --- a/solr/core/src/test/org/apache/solr/handler/TestReplicationHandlerBackup.java +++ b/solr/core/src/test/org/apache/solr/handler/TestReplicationHandlerBackup.java @@ -214,7 +214,7 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase { } finally { if(!namedBackup) { - TestUtil.rm(snapDir); + org.apache.lucene.util.IOUtils.rm(snapDir); } } } diff --git a/solr/core/src/test/org/apache/solr/schema/TestCollationField.java b/solr/core/src/test/org/apache/solr/schema/TestCollationField.java index d4168c8e285..b92013d751b 100644 --- a/solr/core/src/test/org/apache/solr/schema/TestCollationField.java +++ b/solr/core/src/test/org/apache/solr/schema/TestCollationField.java @@ -64,8 +64,6 @@ public class TestCollationField extends SolrTestCaseJ4 { public static String setupSolrHome() throws Exception { // make a solr home underneath the test's TEMP_DIR File tmpFile = createTempDir("collation1"); - tmpFile.delete(); - tmpFile.mkdir(); // make data and conf dirs new File(tmpFile, "data").mkdir(); diff --git a/solr/core/src/test/org/apache/solr/schema/TestCollationFieldDocValues.java b/solr/core/src/test/org/apache/solr/schema/TestCollationFieldDocValues.java index 6e2927ac59b..70bfb79c2a5 100644 --- a/solr/core/src/test/org/apache/solr/schema/TestCollationFieldDocValues.java +++ b/solr/core/src/test/org/apache/solr/schema/TestCollationFieldDocValues.java @@ -62,8 +62,6 @@ public class TestCollationFieldDocValues extends SolrTestCaseJ4 { public static String setupSolrHome() throws Exception { // make a solr home underneath the test's TEMP_DIR File tmpFile = createTempDir("collation1"); - tmpFile.delete(); - tmpFile.mkdir(); // make data and conf dirs new File(tmpFile, "data").mkdir(); diff --git a/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java b/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java index 6854af18a6b..0636606af41 100644 --- a/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java +++ b/solr/core/src/test/org/apache/solr/schema/TestManagedSchema.java @@ -18,6 +18,7 @@ package org.apache.solr.schema; import java.io.File; import java.io.FileInputStream; +import java.nio.file.Files; import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -163,7 +164,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { assertSchemaResource(collection, "managed-schema"); deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); @@ -192,7 +193,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { assertSchemaResource(collection, "managed-schema"); deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); @@ -239,7 +240,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { public void testAddFieldWhenItAlreadyExists() throws Exception{ deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); @@ -272,7 +273,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { public void testAddSameFieldTwice() throws Exception{ deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); @@ -307,7 +308,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { public void testAddDynamicField() throws Exception{ deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); @@ -340,7 +341,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { public void testAddWithSchemaCodecFactory() throws Exception { deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema_codec.xml", tmpSolrHome.getPath()); @@ -368,7 +369,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { public void testAddWithSchemaSimilarityFactory() throws Exception { deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-bm25.xml", tmpSolrHome.getPath()); @@ -397,7 +398,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { assertSchemaResource(collection, "managed-schema"); deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field-unique-key.xml", tmpSolrHome.getPath()); @@ -433,7 +434,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase { public void testAddFieldThenReload() throws Exception { deleteCore(); File managedSchemaFile = new File(tmpConfDir, "managed-schema"); - assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema + Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema System.setProperty("managed.schema.mutable", "true"); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); diff --git a/solr/core/src/test/org/apache/solr/search/TestRecovery.java b/solr/core/src/test/org/apache/solr/search/TestRecovery.java index a5823bc5934..870eb3bfd60 100644 --- a/solr/core/src/test/org/apache/solr/search/TestRecovery.java +++ b/solr/core/src/test/org/apache/solr/search/TestRecovery.java @@ -34,6 +34,7 @@ import org.junit.Test; import java.io.File; import java.io.RandomAccessFile; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayDeque; import java.util.Arrays; import java.util.Deque; @@ -762,7 +763,7 @@ public class TestRecovery extends SolrTestCaseJ4 { String[] files = ulog.getLogList(logDir); for (String file : files) { - new File(logDir, file).delete(); + Files.delete(new File(logDir, file).toPath()); } assertEquals(0, ulog.getLogList(logDir).length); @@ -1094,7 +1095,7 @@ public class TestRecovery extends SolrTestCaseJ4 { try { String[] files = ulog.getLogList(logDir); for (String file : files) { - new File(logDir, file).delete(); + Files.delete(new File(logDir, file).toPath()); } assertEquals(0, ulog.getLogList(logDir).length); diff --git a/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java b/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java index aacc945981a..c00b37219be 100644 --- a/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java +++ b/solr/core/src/test/org/apache/solr/servlet/CacheHeaderTest.java @@ -23,6 +23,7 @@ import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Arrays; import java.util.Date; @@ -64,7 +65,7 @@ public class CacheHeaderTest extends CacheHeaderTestBase { HttpResponse response = getClient().execute(m); assertEquals(200, response.getStatusLine().getStatusCode()); checkVetoHeaders(response, true); - f.delete(); + Files.delete(f.toPath()); } @Test diff --git a/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java b/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java index cfa17d9bb1e..7deaf51f04e 100644 --- a/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java +++ b/solr/core/src/test/org/apache/solr/store/blockcache/BlockDirectoryTest.java @@ -28,6 +28,7 @@ import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.MergeInfo; +import org.apache.lucene.util.IOUtils; import org.apache.solr.SolrTestCaseJ4; import org.junit.After; import org.junit.Before; @@ -232,15 +233,12 @@ public class BlockDirectoryTest extends SolrTestCaseJ4 { } public static void rm(File file) { - if (!file.exists()) { - return; + try { + IOUtils.rm(file); + } catch (Throwable ignored) { + // TODO: should this class care if a file couldnt be deleted? + // this just emulates previous behavior, where only SecurityException would be handled. } - if (file.isDirectory()) { - for (File f : file.listFiles()) { - rm(f); - } - } - file.delete(); } /** diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/TestLBHttpSolrServer.java b/solr/solrj/src/test/org/apache/solr/client/solrj/TestLBHttpSolrServer.java index 06e8209008b..2667d193121 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/TestLBHttpSolrServer.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/TestLBHttpSolrServer.java @@ -28,6 +28,7 @@ import junit.framework.Assert; import org.apache.commons.io.FileUtils; import org.apache.http.client.HttpClient; +import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.lucene.util.QuickPatchThreadsFilter; @@ -305,7 +306,7 @@ public class TestLBHttpSolrServer extends SolrTestCaseJ4 { public void tearDown() throws Exception { if (jetty != null) jetty.stop(); - TestUtil.rm(homeDir); + IOUtils.rm(homeDir); } public void startJetty() throws Exception { diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java index feaa88e7ff5..33d4644fbf4 100644 --- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java +++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java @@ -1038,12 +1038,12 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { } /** - * @see TestUtil#rm(File...) + * @see IOUtils#rm(File...) */ @Deprecated() public static boolean recurseDelete(File f) { try { - TestUtil.rm(f); + IOUtils.rm(f); return true; } catch (IOException e) { System.err.println(e.toString());