LUCENE-5906: Use Files.delete everywhere instead of File.delete

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1622506 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2014-09-04 15:56:25 +00:00
parent 599eff6d44
commit 3604769c3a
75 changed files with 499 additions and 437 deletions

View File

@ -110,6 +110,10 @@ New Features
PushPostingsWriterBase for single-pass push of docs/positions to the PushPostingsWriterBase for single-pass push of docs/positions to the
postings format. (Mike McCandless) 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: API Changes:
* LUCENE-5900: Deprecated more constructors taking Version in *InfixSuggester and * LUCENE-5900: Deprecated more constructors taking Version in *InfixSuggester and

View File

@ -54,6 +54,7 @@ import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction; import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.ParseException; import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -203,6 +204,7 @@ public class Dictionary {
OutputStream out = new BufferedOutputStream(new FileOutputStream(aff)); OutputStream out = new BufferedOutputStream(new FileOutputStream(aff));
InputStream aff1 = null; InputStream aff1 = null;
InputStream aff2 = null; InputStream aff2 = null;
boolean success = false;
try { try {
// copy contents of affix stream to temp file // copy contents of affix stream to temp file
final byte [] buffer = new byte [1024 * 8]; final byte [] buffer = new byte [1024 * 8];
@ -228,9 +230,14 @@ public class Dictionary {
words = b.finish(); words = b.finish();
aliases = null; // no longer needed aliases = null; // no longer needed
morphAliases = null; // no longer needed morphAliases = null; // no longer needed
success = true;
} finally { } finally {
IOUtils.closeWhileHandlingException(out, aff1, aff2); 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); boolean success = false;
unsorted.delete(); try {
sorter.sort(unsorted, sorted);
ByteSequencesReader reader = new ByteSequencesReader(sorted); success = true;
BytesRefBuilder scratchLine = new BytesRefBuilder(); } finally {
if (success) {
// TODO: the flags themselves can be double-chars (long) or also numeric Files.delete(unsorted.toPath());
// 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 { } else {
end = line.indexOf(MORPH_SEPARATOR); IOUtils.deleteFilesIgnoringExceptions(unsorted);
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 boolean success2 = false;
Util.toUTF32(currentEntry, scratchInts); ByteSequencesReader reader = new ByteSequencesReader(sorted);
words.add(scratchInts.get(), currentOrds.get()); try {
BytesRefBuilder scratchLine = new BytesRefBuilder();
reader.close(); // TODO: the flags themselves can be double-chars (long) or also numeric
sorted.delete(); // 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) { static char[] decodeFlags(BytesRef b) {

View File

@ -87,7 +87,7 @@ public class TestFilesystemResourceLoader extends LuceneTestCase {
assertClasspathDelegation(rl); assertClasspathDelegation(rl);
assertNotFound(rl); assertNotFound(rl);
} finally { } finally {
TestUtil.rm(base); IOUtils.rm(base);
} }
} }

View File

@ -66,6 +66,7 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Locale; import java.util.Locale;
import java.util.StringTokenizer; import java.util.StringTokenizer;
@ -87,7 +88,7 @@ public class TestCompile extends LuceneTestCase {
Trie trie = loadTrie(compiled); Trie trie = loadTrie(compiled);
assertTrie(trie, path, true, true); assertTrie(trie, path, true, true);
assertTrie(trie, path, false, true); assertTrie(trie, path, false, true);
new File(compiled).delete(); Files.delete(new File(compiled).toPath());
} }
public void testCompileBackwards() throws Exception { public void testCompileBackwards() throws Exception {
@ -103,7 +104,7 @@ public class TestCompile extends LuceneTestCase {
Trie trie = loadTrie(compiled); Trie trie = loadTrie(compiled);
assertTrie(trie, path, true, true); assertTrie(trie, path, true, true);
assertTrie(trie, path, false, true); assertTrie(trie, path, false, true);
new File(compiled).delete(); Files.delete(new File(compiled).toPath());
} }
public void testCompileMulti() throws Exception { public void testCompileMulti() throws Exception {
@ -119,7 +120,7 @@ public class TestCompile extends LuceneTestCase {
Trie trie = loadTrie(compiled); Trie trie = loadTrie(compiled);
assertTrie(trie, path, true, true); assertTrie(trie, path, true, true);
assertTrie(trie, path, false, true); assertTrie(trie, path, false, true);
new File(compiled).delete(); Files.delete(new File(compiled).toPath());
} }
static Trie loadTrie(String path) throws IOException { static Trie loadTrie(String path) throws IOException {

View File

@ -355,7 +355,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
assertTrue(bos.toString(IOUtils.UTF_8).contains(IndexFormatTooOldException.class.getName())); assertTrue(bos.toString(IOUtils.UTF_8).contains(IndexFormatTooOldException.class.getName()));
dir.close(); 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 { 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: // 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); File indexDir = new File("/tmp/idx", dirName);
TestUtil.rm(indexDir); IOUtils.rm(indexDir);
Directory dir = newFSDirectory(indexDir); Directory dir = newFSDirectory(indexDir);
LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy(); LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy();
mp.setNoCFSRatio(doCFS ? 1.0 : 0.0); mp.setNoCFSRatio(doCFS ? 1.0 : 0.0);

View File

@ -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.tasks.SearchTask;
import org.apache.lucene.benchmark.byTask.utils.AnalyzerFactory; import org.apache.lucene.benchmark.byTask.utils.AnalyzerFactory;
import org.apache.lucene.benchmark.byTask.utils.Config; 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.TaxonomyReader;
import org.apache.lucene.facet.taxonomy.TaxonomyWriter; import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
import org.apache.lucene.index.DirectoryReader; 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 workDir = new File(config.get("work.dir","work"));
File indexDir = new File(workDir,dirName); File indexDir = new File(workDir,dirName);
if (eraseIndex && indexDir.exists()) { if (eraseIndex && indexDir.exists()) {
FileUtils.fullyDelete(indexDir); IOUtils.rm(indexDir);
} }
indexDir.mkdirs(); indexDir.mkdirs();
return FSDirectory.open(indexDir); return FSDirectory.open(indexDir);

View File

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

View File

@ -25,6 +25,7 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -37,12 +38,12 @@ public class ExtractReuters {
private File outputDir; private File outputDir;
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); 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.reutersDir = reutersDir;
this.outputDir = outputDir; this.outputDir = outputDir;
System.out.println("Deleting all files in " + outputDir); System.out.println("Deleting all files in " + outputDir);
for (File f : outputDir.listFiles()) { 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) { if (args.length != 2) {
usage("Wrong number of arguments ("+args.length+")"); usage("Wrong number of arguments ("+args.length+")");
return; return;

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Properties; import java.util.Properties;
import org.apache.lucene.benchmark.byTask.feeds.ContentSource; import org.apache.lucene.benchmark.byTask.feeds.ContentSource;
@ -44,13 +45,13 @@ public class ExtractWikipedia {
static final int BASE = 10; static final int BASE = 10;
protected DocMaker docMaker; protected DocMaker docMaker;
public ExtractWikipedia(DocMaker docMaker, File outputDir) { public ExtractWikipedia(DocMaker docMaker, File outputDir) throws IOException {
this.outputDir = outputDir; this.outputDir = outputDir;
this.docMaker = docMaker; this.docMaker = docMaker;
System.out.println("Deleting all files in " + outputDir); System.out.println("Deleting all files in " + outputDir);
File[] files = outputDir.listFiles(); File[] files = outputDir.listFiles();
for (int i = 0; i < files.length; i++) { for (int i = 0; i < files.length; i++) {
files[i].delete(); Files.delete(files[i].toPath());
} }
} }

View File

@ -38,8 +38,6 @@ public abstract class BenchmarkTestCase extends LuceneTestCase {
@BeforeClass @BeforeClass
public static void beforeClassBenchmarkTestCase() { public static void beforeClassBenchmarkTestCase() {
WORKDIR = createTempDir("benchmark"); WORKDIR = createTempDir("benchmark");
WORKDIR.delete();
WORKDIR.mkdirs();
} }
@AfterClass @AfterClass

View File

@ -22,6 +22,7 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.Collator; import java.text.Collator;
import java.util.List; import java.util.List;
import java.util.Locale; 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()); assertEquals(numLines + " lines were created but " + ir.numDocs() + " docs are in the index", numLines, ir.numDocs());
ir.close(); ir.close();
lineFile.delete(); Files.delete(lineFile.toPath());
} }
/** /**

View File

@ -30,6 +30,7 @@ import java.nio.charset.StandardCharsets;
import org.apache.commons.compress.compressors.CompressorStreamFactory; import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.lucene.benchmark.BenchmarkTestCase; import org.apache.lucene.benchmark.BenchmarkTestCase;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -136,14 +137,14 @@ public class StreamUtilsTest extends BenchmarkTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
testDir = new File(getWorkDir(),"ContentSourceTest"); testDir = new File(getWorkDir(),"ContentSourceTest");
TestUtil.rm(testDir); IOUtils.rm(testDir);
assertTrue(testDir.mkdirs()); assertTrue(testDir.mkdirs());
} }
@Override @Override
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
TestUtil.rm(testDir); IOUtils.rm(testDir);
super.tearDown(); super.tearDown();
} }

View File

@ -26,6 +26,7 @@ import java.io.FileOutputStream;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.FilterOutputStream; import java.io.FilterOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
@ -251,8 +252,7 @@ public abstract class FSDirectory extends BaseDirectory {
public void deleteFile(String name) throws IOException { public void deleteFile(String name) throws IOException {
ensureOpen(); ensureOpen();
File file = new File(directory, name); File file = new File(directory, name);
if (!file.delete()) Files.delete(file.toPath());
throw new IOException("Cannot delete " + file);
staleFiles.remove(name); staleFiles.remove(name);
} }
@ -271,8 +271,7 @@ public abstract class FSDirectory extends BaseDirectory {
throw new IOException("Cannot create directory: " + directory); throw new IOException("Cannot create directory: " + directory);
File file = new File(directory, name); File file = new File(directory, name);
if (file.exists() && !file.delete()) // delete existing, if any Files.deleteIfExists(file.toPath()); // delete existing, if any
throw new IOException("Cannot overwrite: " + file);
} }
/** /**

View File

@ -28,4 +28,8 @@ public class LockReleaseFailedException extends IOException {
public LockReleaseFailedException(String message) { public LockReleaseFailedException(String message) {
super(message); super(message);
} }
public LockReleaseFailedException(String message, Throwable cause) {
super(message, cause);
}
} }

View File

@ -19,6 +19,7 @@ package org.apache.lucene.store;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
/** /**
* <p>Implements {@link LockFactory} using {@link * <p>Implements {@link LockFactory} using {@link
@ -102,9 +103,7 @@ public class SimpleFSLockFactory extends FSLockFactory {
lockName = lockPrefix + "-" + lockName; lockName = lockPrefix + "-" + lockName;
} }
File lockFile = new File(lockDir, lockName); File lockFile = new File(lockDir, lockName);
if (lockFile.exists() && !lockFile.delete()) { Files.deleteIfExists(lockFile.toPath());
throw new IOException("Cannot delete " + lockFile);
}
} }
} }
} }
@ -147,8 +146,11 @@ class SimpleFSLock extends Lock {
@Override @Override
public void close() throws LockReleaseFailedException { public void close() throws LockReleaseFailedException {
if (lockFile.exists() && !lockFile.delete()) { // TODO: wierd that clearLock() throws the raw IOException...
throw new LockReleaseFailedException("failed to delete " + lockFile); try {
Files.deleteIfExists(lockFile.toPath());
} catch (Throwable cause) {
throw new LockReleaseFailedException("failed to delete " + lockFile, cause);
} }
} }

View File

@ -33,7 +33,11 @@ import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction; import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption; 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. /** This class emulates the new Java 7 "Try-With-Resources" statement.
* Remove once Lucene is on Java 7. * Remove once Lucene is on Java 7.
@ -69,22 +73,7 @@ public final class IOUtils {
* objects to call <tt>close()</tt> on * objects to call <tt>close()</tt> on
*/ */
public static void close(Closeable... objects) throws IOException { public static void close(Closeable... objects) throws IOException {
Throwable th = null; close(Arrays.asList(objects));
for (Closeable object : objects) {
try {
if (object != null) {
object.close();
}
} catch (Throwable t) {
addSuppressed(th, t);
if (th == null) {
th = t;
}
}
}
reThrow(th);
} }
/** /**
@ -118,14 +107,7 @@ public final class IOUtils {
* objects to call <tt>close()</tt> on * objects to call <tt>close()</tt> on
*/ */
public static void closeWhileHandlingException(Closeable... objects) { public static void closeWhileHandlingException(Closeable... objects) {
for (Closeable object : objects) { closeWhileHandlingException(Arrays.asList(objects));
try {
if (object != null) {
object.close();
}
} catch (Throwable t) {
}
}
} }
/** /**
@ -246,6 +228,113 @@ public final class IOUtils {
} }
} }
/**
* Deletes all given files, suppressing all thrown IOExceptions.
* <p>
* 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.
* <p>
* Some of the files may be null, if so they are ignored.
*/
public static void deleteFilesIgnoringExceptions(Iterable<? extends File> files) {
for (File name : files) {
if (name != null) {
try {
Files.delete(name.toPath());
} catch (Throwable ignored) {
// ignore
}
}
}
}
/**
* Deletes all given <tt>File</tt>s, if they exist. Some of the
* <tt>File</tt>s 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 <tt>File</tt>s, if they exist. Some of the
* <tt>File</tt>s 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<? extends File> 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<File,Throwable> unremoved = rm(new LinkedHashMap<File,Throwable>(), locations);
if (!unremoved.isEmpty()) {
StringBuilder b = new StringBuilder("Could not remove the following files (in the order of attempts):\n");
for (Map.Entry<File,Throwable> kv : unremoved.entrySet()) {
b.append(" ")
.append(kv.getKey().getAbsolutePath())
.append(": ")
.append(kv.getValue())
.append("\n");
}
throw new IOException(b.toString());
}
}
private static LinkedHashMap<File,Throwable> rm(LinkedHashMap<File,Throwable> 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 * Copy one file's contents to another file. The target will be overwritten
* if it exists. The source must exist. * if it exists. The source must exist.

View File

@ -31,6 +31,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@ -223,10 +224,10 @@ public final class OfflineSorter {
sortInfo = new SortInfo(); sortInfo = new SortInfo();
sortInfo.totalTime = System.currentTimeMillis(); sortInfo.totalTime = System.currentTimeMillis();
output.delete(); Files.deleteIfExists(output.toPath());
ArrayList<File> merges = new ArrayList<>(); ArrayList<File> merges = new ArrayList<>();
boolean success2 = false; boolean success3 = false;
try { try {
ByteSequencesReader is = new ByteSequencesReader(input); ByteSequencesReader is = new ByteSequencesReader(input);
boolean success = false; boolean success = false;
@ -240,11 +241,15 @@ public final class OfflineSorter {
// Handle intermediate merges. // Handle intermediate merges.
if (merges.size() == maxTempFiles) { if (merges.size() == maxTempFiles) {
File intermediate = File.createTempFile("sort", "intermediate", tempDirectory); File intermediate = File.createTempFile("sort", "intermediate", tempDirectory);
boolean success2 = false;
try { try {
mergePartitions(merges, intermediate); mergePartitions(merges, intermediate);
success2 = true;
} finally { } finally {
for (File file : merges) { if (success2) {
file.delete(); IOUtils.deleteFilesIfExist(merges);
} else {
IOUtils.deleteFilesIgnoringExceptions(merges);
} }
merges.clear(); merges.clear();
merges.add(intermediate); merges.add(intermediate);
@ -272,13 +277,13 @@ public final class OfflineSorter {
// otherwise merge the partitions with a priority queue. // otherwise merge the partitions with a priority queue.
mergePartitions(merges, output); mergePartitions(merges, output);
} }
success2 = true; success3 = true;
} finally { } finally {
for (File file : merges) { if (success3) {
file.delete(); IOUtils.deleteFilesIfExist(merges);
} } else {
if (!success2) { IOUtils.deleteFilesIgnoringExceptions(merges);
output.delete(); IOUtils.deleteFilesIgnoringExceptions(output);
} }
} }

View File

@ -178,6 +178,6 @@ public class TestAtomicUpdate extends LuceneTestCase {
directory = newFSDirectory(dirPath); directory = newFSDirectory(dirPath);
runTest(directory); runTest(directory);
directory.close(); directory.close();
TestUtil.rm(dirPath); IOUtils.rm(dirPath);
} }
} }

View File

@ -20,6 +20,7 @@ package org.apache.lucene.index;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
@ -41,6 +42,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NoSuchDirectoryException; import org.apache.lucene.store.NoSuchDirectoryException;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.junit.Assume; import org.junit.Assume;
@ -446,7 +448,7 @@ public void testFilesOpenClose() throws IOException {
dir.close(); dir.close();
// Try to erase the data - this ensures that the writer closed all files // Try to erase the data - this ensures that the writer closed all files
TestUtil.rm(dirFile); IOUtils.rm(dirFile);
dir = newFSDirectory(dirFile); dir = newFSDirectory(dirFile);
// Now create the data set again, just as before // 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 // The following will fail if reader did not close
// all files // all files
TestUtil.rm(dirFile); IOUtils.rm(dirFile);
} }
public void testOpenReaderAfterDelete() throws IOException { public void testOpenReaderAfterDelete() throws IOException {
@ -477,7 +479,7 @@ public void testFilesOpenClose() throws IOException {
// expected // expected
} }
dirFile.delete(); Files.delete(dirFile.toPath());
// Make sure we still get a CorruptIndexException (not NPE): // Make sure we still get a CorruptIndexException (not NPE):
try { try {
@ -716,7 +718,7 @@ public void testFilesOpenClose() throws IOException {
// good exception // good exception
public void testNoDir() throws Throwable { public void testNoDir() throws Throwable {
File tempDir = createTempDir("doesnotexist"); File tempDir = createTempDir("doesnotexist");
TestUtil.rm(tempDir); IOUtils.rm(tempDir);
Directory dir = newFSDirectory(tempDir); Directory dir = newFSDirectory(tempDir);
try { try {
DirectoryReader.open(dir); DirectoryReader.open(dir);
@ -1052,7 +1054,7 @@ public void testFilesOpenClose() throws IOException {
public void testIndexExistsOnNonExistentDirectory() throws Exception { public void testIndexExistsOnNonExistentDirectory() throws Exception {
File tempDir = createTempDir("testIndexExistsOnNonExistentDirectory"); File tempDir = createTempDir("testIndexExistsOnNonExistentDirectory");
tempDir.delete(); Files.delete(tempDir.toPath());
Directory dir = newFSDirectory(tempDir); Directory dir = newFSDirectory(tempDir);
assertFalse(DirectoryReader.indexExists(dir)); assertFalse(DirectoryReader.indexExists(dir));
dir.close(); dir.close();

View File

@ -26,6 +26,7 @@ import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
@ -87,7 +88,7 @@ public class TestDoc extends LuceneTestCase {
try { try {
File f = new File(workDir, name); File f = new File(workDir, name);
if (f.exists()) f.delete(); Files.deleteIfExists(f.toPath());
fw = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_8); fw = new OutputStreamWriter(new FileOutputStream(f), StandardCharsets.UTF_8);
pw = new PrintWriter(fw); pw = new PrintWriter(fw);

View File

@ -34,6 +34,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.junit.AfterClass; import org.junit.AfterClass;
@ -249,7 +250,7 @@ public class TestFieldsReader extends LuceneTestCase {
reader.close(); reader.close();
dir.close(); dir.close();
} finally { } finally {
TestUtil.rm(indexDir); IOUtils.rm(indexDir);
} }
} }

View File

@ -44,8 +44,6 @@ public class TestIndexWriterOnJRECrash extends TestNRTThreads {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
tempDir = createTempDir("jrecrash"); tempDir = createTempDir("jrecrash");
tempDir.delete();
tempDir.mkdir();
} }
@Override @Nightly @Override @Nightly

View File

@ -26,6 +26,7 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.store.BaseDirectoryWrapper; import org.apache.lucene.store.BaseDirectoryWrapper;
import org.apache.lucene.store.MockDirectoryWrapper; import org.apache.lucene.store.MockDirectoryWrapper;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
@ -107,6 +108,6 @@ public class TestNeverDelete extends LuceneTestCase {
w.close(); w.close();
d.close(); d.close();
TestUtil.rm(tmpDir); IOUtils.rm(tmpDir);
} }
} }

View File

@ -38,6 +38,7 @@ import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.ArrayUtil; import org.apache.lucene.util.ArrayUtil;
@ -279,7 +280,7 @@ public class TestBufferedIndexInput extends LuceneTestCase {
writer.close(); writer.close();
reader.close(); reader.close();
} finally { } finally {
TestUtil.rm(indexDir); IOUtils.rm(indexDir);
} }
} }

View File

@ -24,6 +24,7 @@ import java.nio.file.NoSuchFileException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
public class TestDirectory extends BaseDirectoryTestCase { public class TestDirectory extends BaseDirectoryTestCase {
@ -135,7 +136,7 @@ public class TestDirectory extends BaseDirectoryTestCase {
assertFalse(dir.isOpen); assertFalse(dir.isOpen);
} }
TestUtil.rm(path); IOUtils.rm(path);
} }
// LUCENE-1468 // LUCENE-1468
@ -147,7 +148,7 @@ public class TestDirectory extends BaseDirectoryTestCase {
Directory fsDir = new SimpleFSDirectory(path, null); Directory fsDir = new SimpleFSDirectory(path, null);
assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length); assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length);
} finally { } finally {
TestUtil.rm(path); IOUtils.rm(path);
} }
} }
@ -167,7 +168,7 @@ public class TestDirectory extends BaseDirectoryTestCase {
} }
} finally { } finally {
fsDir.close(); fsDir.close();
TestUtil.rm(path); IOUtils.rm(path);
} }
} }
} }

View File

@ -31,6 +31,7 @@ import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.TestIndexWriterReader; import org.apache.lucene.index.TestIndexWriterReader;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
public class TestFileSwitchDirectory extends BaseDirectoryTestCase { public class TestFileSwitchDirectory extends BaseDirectoryTestCase {
@ -99,8 +100,7 @@ public class TestFileSwitchDirectory extends BaseDirectoryTestCase {
public void testNoDir() throws Throwable { public void testNoDir() throws Throwable {
File primDir = createTempDir("foo"); File primDir = createTempDir("foo");
File secondDir = createTempDir("bar"); File secondDir = createTempDir("bar");
TestUtil.rm(primDir); IOUtils.rm(primDir, secondDir);
TestUtil.rm(secondDir);
Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.<String>emptySet()); Directory dir = newFSSwitchDirectory(primDir, secondDir, Collections.<String>emptySet());
try { try {
DirectoryReader.open(dir); DirectoryReader.open(dir);

View File

@ -19,6 +19,7 @@ package org.apache.lucene.store;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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.IndexSearcher;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
@ -170,7 +172,7 @@ public class TestLockFactory extends LuceneTestCase {
dir.close(); dir.close();
// Cleanup // Cleanup
TestUtil.rm(indexDir); IOUtils.rm(indexDir);
} }
// Verify: NativeFSLockFactory works correctly // Verify: NativeFSLockFactory works correctly
@ -208,9 +210,7 @@ public class TestLockFactory extends LuceneTestCase {
assertTrue("failed to obtain lock", l.obtain()); assertTrue("failed to obtain lock", l.obtain());
l.close(); l.close();
assertFalse("failed to release lock", l.isLocked()); assertFalse("failed to release lock", l.isLocked());
if (lockFile.exists()) { Files.deleteIfExists(lockFile.toPath());
lockFile.delete();
}
} }
// Verify: NativeFSLockFactory assigns null as lockPrefix if the lockDir is inside directory // Verify: NativeFSLockFactory assigns null as lockPrefix if the lockDir is inside directory
@ -230,8 +230,7 @@ public class TestLockFactory extends LuceneTestCase {
dir1.close(); dir1.close();
dir2.close(); dir2.close();
TestUtil.rm(fdir1); IOUtils.rm(fdir1, fdir2);
TestUtil.rm(fdir2);
} }
// Verify: default LockFactory has no prefix (ie // 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()); assertNull("Default lock prefix should be null", dir.getLockFactory().getLockPrefix());
dir.close(); dir.close();
TestUtil.rm(dirName); IOUtils.rm(dirName);
} }
private class WriterThread extends Thread { private class WriterThread extends Thread {

View File

@ -78,8 +78,8 @@ public class TestRAMDirectory extends BaseDirectoryTestCase {
fsDir = newFSDirectory(path); fsDir = newFSDirectory(path);
assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length); assertEquals(0, new RAMDirectory(fsDir, newIOContext(random())).listAll().length);
} finally { } finally {
TestUtil.rm(path);
IOUtils.close(fsDir); IOUtils.close(fsDir);
IOUtils.rm(path);
} }
} }

View File

@ -65,7 +65,6 @@ public class TestWindowsMMap extends LuceneTestCase {
// may take some time until the files are finally dereferenced. So clean the // may take some time until the files are finally dereferenced. So clean the
// directory up front, or otherwise new IndexWriter will fail. // directory up front, or otherwise new IndexWriter will fail.
File dirPath = createTempDir("testLuceneMmap"); File dirPath = createTempDir("testLuceneMmap");
rmDir(dirPath);
MMapDirectory dir = new MMapDirectory(dirPath, null); MMapDirectory dir = new MMapDirectory(dirPath, null);
// plan to add a set of useful stopwords, consider changing some of the // plan to add a set of useful stopwords, consider changing some of the
@ -88,16 +87,5 @@ public class TestWindowsMMap extends LuceneTestCase {
reader.close(); reader.close();
writer.close(); writer.close();
rmDir(dirPath);
}
private void rmDir(File dir) {
if (!dir.exists()) {
return;
}
for (File file : dir.listFiles()) {
file.delete();
}
dir.delete();
} }
} }

View File

@ -44,14 +44,12 @@ public class TestOfflineSorter extends LuceneTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
tempDir = createTempDir("mergesort"); tempDir = createTempDir("mergesort");
TestUtil.rm(tempDir);
tempDir.mkdirs();
} }
@Override @Override
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (tempDir != null) if (tempDir != null)
TestUtil.rm(tempDir); IOUtils.rm(tempDir);
super.tearDown(); super.tearDown();
} }

View File

@ -20,8 +20,10 @@ package org.apache.lucene.util.junitcompat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.file.Files;
import org.apache.lucene.util.Constants; import org.apache.lucene.util.Constants;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.junit.Assert; import org.junit.Assert;
@ -45,11 +47,11 @@ public class TestLeaveFilesIfTestFails extends WithNestedTests {
} }
@Test @Test
public void testLeaveFilesIfTestFails() { public void testLeaveFilesIfTestFails() throws IOException {
Result r = JUnitCore.runClasses(Nested1.class); Result r = JUnitCore.runClasses(Nested1.class);
Assert.assertEquals(1, r.getFailureCount()); Assert.assertEquals(1, r.getFailureCount());
Assert.assertTrue(Nested1.file != null && Nested1.file.exists()); Assert.assertTrue(Nested1.file != null && Nested1.file.exists());
Nested1.file.delete(); Files.delete(Nested1.file.toPath());
} }
public static class Nested2 extends WithNestedTests.AbstractNestedTest { public static class Nested2 extends WithNestedTests.AbstractNestedTest {
@ -75,6 +77,6 @@ public class TestLeaveFilesIfTestFails extends WithNestedTests {
Assert.assertEquals(1, r.getFailureCount()); Assert.assertEquals(1, r.getFailureCount());
Nested2.openFile.close(); Nested2.openFile.close();
TestUtil.rm(Nested2.parent); IOUtils.rm(Nested2.parent);
} }
} }

View File

@ -9,6 +9,7 @@ import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@ -941,9 +942,7 @@ public class DirectoryTaxonomyWriter implements TaxonomyWriter {
in.close(); in.close();
// Delete the temporary file, which is no longer needed. // Delete the temporary file, which is no longer needed.
if (!tmpfile.delete()) { Files.delete(tmpfile.toPath());
tmpfile.deleteOnExit();
}
return map; return map;
} }

View File

@ -96,7 +96,6 @@ public class TestCharBlockArray extends FacetTestCase {
array = CharBlockArray.open(in); array = CharBlockArray.open(in);
assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch after flush/load.", builder, array); assertEqualsInternal("GrowingCharArray<->StringBuilder mismatch after flush/load.", builder, array);
in.close(); in.close();
f.delete();
} }
private static void assertEqualsInternal(String msg, StringBuilder expected, CharBlockArray actual) { private static void assertEqualsInternal(String msg, StringBuilder expected, CharBlockArray actual) {

View File

@ -5,6 +5,7 @@ import java.nio.ByteBuffer;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction; import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@ -76,7 +77,7 @@ public class TestCompactLabelToOrdinal extends FacetTestCase {
if (i > 0 && i % flushInterval == 0) { if (i > 0 && i % flushInterval == 0) {
compact.flush(f); compact.flush(f);
compact = CompactLabelToOrdinal.open(f, 0.15f, 3); compact = CompactLabelToOrdinal.open(f, 0.15f, 3);
assertTrue(f.delete()); Files.delete(f.toPath());
if (flushInterval < (n / 10)) { if (flushInterval < (n / 10)) {
flushInterval *= 10; flushInterval *= 10;
} }

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import org.apache.lucene.replicator.ReplicationClient.SourceDirectoryFactory; import org.apache.lucene.replicator.ReplicationClient.SourceDirectoryFactory;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.IOUtils;
/** /**
* A {@link SourceDirectoryFactory} which returns {@link FSDirectory} under a * A {@link SourceDirectoryFactory} which returns {@link FSDirectory} under a
@ -40,19 +41,6 @@ public class PerSessionDirectoryFactory implements SourceDirectoryFactory {
this.workDir = workDir; 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 @Override
public Directory getDirectory(String sessionID, String source) throws IOException { public Directory getDirectory(String sessionID, String source) throws IOException {
File sessionDir = new File(workDir, sessionID); File sessionDir = new File(workDir, sessionID);
@ -71,7 +59,7 @@ public class PerSessionDirectoryFactory implements SourceDirectoryFactory {
if (sessionID.isEmpty()) { // protect against deleting workDir entirely! if (sessionID.isEmpty()) { // protect against deleting workDir entirely!
throw new IllegalArgumentException("sessionID cannot be empty"); throw new IllegalArgumentException("sessionID cannot be empty");
} }
rm(new File(workDir, sessionID)); IOUtils.rm(new File(workDir, sessionID));
} }
} }

View File

@ -202,12 +202,16 @@ public class SortedInputIterator implements InputIterator {
} }
private void close() throws IOException { private void close() throws IOException {
IOUtils.close(reader); boolean success = false;
if (tempInput != null) { try {
tempInput.delete(); IOUtils.close(reader);
} success = true;
if (tempSorted != null) { } finally {
tempSorted.delete(); if (success) {
IOUtils.deleteFilesIfExist(tempInput, tempSorted);
} else {
IOUtils.deleteFilesIgnoringExceptions(tempInput, tempSorted);
}
} }
} }

View File

@ -19,6 +19,7 @@ package org.apache.lucene.search.suggest.analyzing;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -500,7 +501,7 @@ public class AnalyzingSuggester extends Lookup {
new OfflineSorter(new AnalyzingComparator(hasPayloads)).sort(tempInput, tempSorted); new OfflineSorter(new AnalyzingComparator(hasPayloads)).sort(tempInput, tempSorted);
// Free disk space: // Free disk space:
tempInput.delete(); Files.delete(tempInput.toPath());
reader = new OfflineSorter.ByteSequencesReader(tempSorted); reader = new OfflineSorter.ByteSequencesReader(tempSorted);
@ -593,14 +594,13 @@ public class AnalyzingSuggester extends Lookup {
success = true; success = true;
} finally { } finally {
if (success) { IOUtils.closeWhileHandlingException(reader, writer);
IOUtils.close(reader, writer);
} else {
IOUtils.closeWhileHandlingException(reader, writer);
}
tempInput.delete(); if (success) {
tempSorted.delete(); IOUtils.deleteFilesIfExist(tempInput, tempSorted);
} else {
IOUtils.deleteFilesIgnoringExceptions(tempInput, tempSorted);
}
} }
} }

View File

@ -376,23 +376,12 @@ public class FreeTextSuggester extends Lookup {
} finally { } finally {
try { try {
if (success) { if (success) {
IOUtils.close(reader); IOUtils.close(reader, dir);
} else { } else {
IOUtils.closeWhileHandlingException(reader, writer); IOUtils.closeWhileHandlingException(reader, writer, dir);
} }
} finally { } finally {
for(String file : dir.listAll()) { IOUtils.rm(tempIndexPath);
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();
} }
} }
} }

View File

@ -20,6 +20,7 @@ package org.apache.lucene.search.suggest.fst;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.Comparator; import java.util.Comparator;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
@ -61,9 +62,18 @@ public class ExternalRefSorter implements BytesRefSorter, Closeable {
sorted = File.createTempFile("RefSorter-", ".sorted", sorted = File.createTempFile("RefSorter-", ".sorted",
OfflineSorter.defaultTempDir()); 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; input = null;
} }
@ -82,11 +92,16 @@ public class ExternalRefSorter implements BytesRefSorter, Closeable {
*/ */
@Override @Override
public void close() throws IOException { public void close() throws IOException {
boolean success = false;
try { try {
closeWriter(); closeWriter();
success = true;
} finally { } finally {
if (input != null) input.delete(); if (success) {
if (sorted != null) sorted.delete(); IOUtils.deleteFilesIfExist(input, sorted);
} else {
IOUtils.deleteFilesIgnoringExceptions(input, sorted);
}
} }
} }

View File

@ -19,6 +19,7 @@ package org.apache.lucene.search.suggest.fst;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; 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 // We don't know the distribution of scores and we need to bucket them, so we'll sort
// and divide into equal buckets. // and divide into equal buckets.
SortInfo info = new OfflineSorter().sort(tempInput, tempSorted); SortInfo info = new OfflineSorter().sort(tempInput, tempSorted);
tempInput.delete(); Files.delete(tempInput.toPath());
FSTCompletionBuilder builder = new FSTCompletionBuilder( FSTCompletionBuilder builder = new FSTCompletionBuilder(
buckets, sorter = new ExternalRefSorter(new OfflineSorter()), sharedTailLength); buckets, sorter = new ExternalRefSorter(new OfflineSorter()), sharedTailLength);
@ -231,13 +232,13 @@ public class FSTCompletionLookup extends Lookup implements Accountable {
success = true; success = true;
} finally { } finally {
if (success) IOUtils.closeWhileHandlingException(reader, writer, sorter);
IOUtils.close(reader, writer, sorter);
else
IOUtils.closeWhileHandlingException(reader, writer, sorter);
tempInput.delete(); if (success) {
tempSorted.delete(); Files.delete(tempSorted.toPath());
} else {
IOUtils.deleteFilesIgnoringExceptions(tempInput, tempSorted);
}
} }
} }

View File

@ -1356,7 +1356,7 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTest
fieldsProducer.close(); fieldsProducer.close();
dir.close(); dir.close();
TestUtil.rm(path); IOUtils.rm(path);
} }
public void testDocsOnly() throws Exception { public void testDocsOnly() throws Exception {
@ -1405,7 +1405,7 @@ public abstract class BasePostingsFormatTestCase extends BaseIndexFileFormatTest
fieldsProducer = null; fieldsProducer = null;
dir.close(); dir.close();
TestUtil.rm(path); IOUtils.rm(path);
} }
} }

View File

@ -42,6 +42,7 @@ import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits; import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FailOnNonBulkMergesInfoStream; import org.apache.lucene.util.FailOnNonBulkMergesInfoStream;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LineFileDocs; import org.apache.lucene.util.LineFileDocs;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.NamedThreadFactory; import org.apache.lucene.util.NamedThreadFactory;
@ -653,7 +654,7 @@ public abstract class ThreadedIndexingAndSearchingTestCase extends LuceneTestCas
TestUtil.checkIndex(dir); TestUtil.checkIndex(dir);
dir.close(); dir.close();
TestUtil.rm(tempDir); IOUtils.rm(tempDir);
if (VERBOSE) { if (VERBOSE) {
System.out.println("TEST: done [" + (System.currentTimeMillis()-t0) + " ms]"); System.out.println("TEST: done [" + (System.currentTimeMillis()-t0) + " ms]");

View File

@ -21,6 +21,7 @@ import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@ -453,7 +454,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {
* mkdir the underling directory in the filesystem. */ * mkdir the underling directory in the filesystem. */
public void testDontCreate() throws Throwable { public void testDontCreate() throws Throwable {
File path = createTempDir("doesnotexist"); File path = createTempDir("doesnotexist");
TestUtil.rm(path); IOUtils.rm(path);
assertTrue(!path.exists()); assertTrue(!path.exists());
Directory dir = getDirectory(path); Directory dir = getDirectory(path);
assertTrue(!path.exists()); 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. // LUCENE-3382 -- make sure we get exception if the directory really does not exist.
public void testNoDir() throws Throwable { public void testNoDir() throws Throwable {
File tempDir = createTempDir("doesnotexist"); File tempDir = createTempDir("doesnotexist");
TestUtil.rm(tempDir); IOUtils.rm(tempDir);
Directory dir = getDirectory(tempDir); Directory dir = getDirectory(tempDir);
try { try {
DirectoryReader.open(dir); DirectoryReader.open(dir);
@ -767,7 +768,7 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {
out.close(); out.close();
// delete it // delete it
assertTrue(new File(path, "afile").delete()); Files.delete(new File(path, "afile").toPath());
// directory is empty // directory is empty
assertEquals(0, fsdir.listAll().length); assertEquals(0, fsdir.listAll().length);

View File

@ -46,7 +46,7 @@ final class RemoveUponClose implements Closeable {
if (failureMarker.wasSuccessful()) { if (failureMarker.wasSuccessful()) {
if (file.exists()) { if (file.exists()) {
try { try {
TestUtil.rm(file); IOUtils.rm(file);
} catch (IOException e) { } catch (IOException e) {
throw new IOException( throw new IOException(
"Could not remove temporary location '" "Could not remove temporary location '"

View File

@ -122,7 +122,7 @@ final class TestRuleTemporaryFilesCleanup extends TestRuleAdapter {
// and leave them there. // and leave them there.
if (failureMarker.wasSuccessful()) { if (failureMarker.wasSuccessful()) {
try { try {
TestUtil.rm(everything); IOUtils.rm(everything);
} catch (IOException e) { } catch (IOException e) {
Class<?> suiteClass = RandomizedContext.current().getTargetClass(); Class<?> suiteClass = RandomizedContext.current().getTargetClass();
if (suiteClass.isAnnotationPresent(SuppressTempFileChecks.class)) { if (suiteClass.isAnnotationPresent(SuppressTempFileChecks.class)) {

View File

@ -28,10 +28,11 @@ import java.io.PrintStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; 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<File> unremoved = rm(new LinkedHashSet<File>(), 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<File> rm(LinkedHashSet<File> 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 * Convenience method unzipping zipName into destDir, cleaning up
* destDir first. * destDir first.
*/ */
public static void unzip(File zipName, File destDir) throws IOException { public static void unzip(File zipName, File destDir) throws IOException {
rm(destDir); IOUtils.rm(destDir);
destDir.mkdir(); destDir.mkdir();
ZipFile zipFile = new ZipFile(zipName); ZipFile zipFile = new ZipFile(zipName);

View File

@ -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#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.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

View File

@ -1,6 +1,7 @@
package org.apache.solr.handler.dataimport; package org.apache.solr.handler.dataimport;
import java.io.File; import java.io.File;
import java.nio.file.Files;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -60,7 +61,7 @@ public abstract class AbstractSqlEntityProcessorTestCase extends
} }
@After @After
public void afterSqlEntitiyProcessorTestCase() { public void afterSqlEntitiyProcessorTestCase() throws Exception {
useSimpleCaches = false; useSimpleCaches = false;
countryEntity = false; countryEntity = false;
countryCached = false; countryCached = false;
@ -74,8 +75,8 @@ public abstract class AbstractSqlEntityProcessorTestCase extends
//If an Assume was tripped while setting up the test, //If an Assume was tripped while setting up the test,
//the file might not ever have been created... //the file might not ever have been created...
if(fileLocation!=null) { if(fileLocation!=null) {
new File(fileLocation + File.separatorChar + fileName).delete(); Files.deleteIfExists(new File(fileLocation + File.separatorChar + fileName).toPath());
new File(fileLocation).delete(); Files.deleteIfExists(new File(fileLocation).toPath());
} }
} }

View File

@ -24,6 +24,7 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
@ -65,7 +66,7 @@ public class TestFileListEntityProcessor extends AbstractDataImportHandlerTestCa
@Test @Test
public void testBiggerSmallerFiles() throws IOException { public void testBiggerSmallerFiles() throws IOException {
File tmpdir = File.createTempFile("test", "tmp", createTempDir()); File tmpdir = File.createTempFile("test", "tmp", createTempDir());
tmpdir.delete(); Files.delete(tmpdir.toPath());
tmpdir.mkdir(); tmpdir.mkdir();
long minLength = Long.MAX_VALUE; long minLength = Long.MAX_VALUE;

View File

@ -92,7 +92,7 @@ public class TestNonWritablePersistFile extends AbstractDataImportHandlerTestCas
runFullImport(dataConfig_delta); runFullImport(dataConfig_delta);
assertQ(req("id:1"), "//*[@numFound='0']"); assertQ(req("id:1"), "//*[@numFound='0']");
} finally { } finally {
f.delete(); f.setWritable(true);
} }
} }
} }

View File

@ -26,6 +26,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.embedded.JettySolrRunner; import org.apache.solr.client.solrj.embedded.JettySolrRunner;
@ -335,7 +336,7 @@ public class TestSolrEntityProcessorEndToEnd extends AbstractDataImportHandlerTe
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {
TestUtil.rm(homeDir); IOUtils.rm(homeDir);
} }
} }

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashSet; import java.util.HashSet;
import java.util.Locale; import java.util.Locale;
@ -224,7 +225,7 @@ public class SolrOutputFormat<K, V> extends FileOutputFormat<K, V> {
// to store in the zip file // to store in the zip file
} }
out.delete(); Files.deleteIfExists(out.toPath());
int subst = dir.toString().length(); int subst = dir.toString().length();
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out)); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(out));
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];

View File

@ -18,6 +18,7 @@ package org.apache.solr.hadoop;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale; import java.util.Locale;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -44,7 +45,7 @@ public abstract class MRUnitBase extends SolrTestCaseJ4 {
@AfterClass @AfterClass
public static void teardownClass() throws Exception { 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 { protected void setupHadoopConfig(Configuration config) throws IOException {

View File

@ -19,6 +19,7 @@ package org.apache.solr.morphlines.solr;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
class FileUtils { class FileUtils {
@ -39,11 +40,7 @@ class FileUtils {
cleanDirectory(directory); cleanDirectory(directory);
} }
if (!directory.delete()) { Files.delete(directory.toPath());
String message =
"Unable to delete directory " + directory + ".";
throw new IOException(message);
}
} }
/** /**
@ -136,15 +133,7 @@ class FileUtils {
if (file.isDirectory()) { if (file.isDirectory()) {
deleteDirectory(file); deleteDirectory(file);
} else { } else {
boolean filePresent = file.exists(); Files.delete(file.toPath());
if (!file.delete()) {
if (!filePresent){
throw new FileNotFoundException("File does not exist: " + file);
}
String message =
"Unable to delete file: " + file;
throw new IOException(message);
}
} }
} }

View File

@ -19,6 +19,7 @@ package org.apache.solr.handler;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
@ -280,10 +281,12 @@ public class PingRequestHandler extends RequestHandlerBase implements SolrCoreAw
"Unable to write healthcheck flag file", e); "Unable to write healthcheck flag file", e);
} }
} else { } else {
if (healthcheck.exists() && !healthcheck.delete()){ try {
Files.deleteIfExists(healthcheck.toPath());
} catch (Throwable cause) {
throw new SolrException(SolrException.ErrorCode.NOT_FOUND, throw new SolrException(SolrException.ErrorCode.NOT_FOUND,
"Did not successfully delete healthcheck file: " "Did not successfully delete healthcheck file: "
+healthcheck.getAbsolutePath()); +healthcheck.getAbsolutePath(), cause);
} }
} }
} }

View File

@ -27,6 +27,7 @@ import java.io.Writer;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException; import java.nio.file.NoSuchFileException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -1021,28 +1022,30 @@ public class SnapPuller {
return nameVsFile.isEmpty() ? Collections.EMPTY_LIST : nameVsFile.values(); return nameVsFile.isEmpty() ? Collections.EMPTY_LIST : nameVsFile.values();
} }
static boolean delTree(File dir) { /**
boolean isSuccess = true; * This simulates File.delete exception-wise, since this class has some strange behavior with it.
File contents[] = dir.listFiles(); * The only difference is it returns null on success, throws SecurityException on SecurityException,
if (contents != null) { * otherwise returns Throwable preventing deletion (instead of false), for additional information.
for (File file : contents) { */
if (file.isDirectory()) { static Throwable delete(File file) {
boolean success = delTree(file); try {
if (!success) { Files.delete(file.toPath());
LOG.warn("Unable to delete directory : " + file); return null;
isSuccess = false; } catch (SecurityException e) {
} throw e;
} else { } catch (Throwable other) {
boolean success = file.delete(); return other;
if (!success) { }
LOG.warn("Unable to delete file : " + file); }
isSuccess = false;
return false; 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 //if the download is not complete then
//delete the file being downloaded //delete the file being downloaded
try { try {
file.delete(); Files.delete(file.toPath());
} catch (Exception e) { } catch (SecurityException e) {
LOG.error("Error deleting file in cleanup" + e.getMessage()); 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 the failure is due to a user abort it is returned nomally else an exception is thrown
if (!aborted) if (!aborted)

View File

@ -30,6 +30,7 @@ import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -183,7 +184,21 @@ public abstract class ManagedResourceStorage {
@Override @Override
public boolean delete(String storedResourceId) throws IOException { public boolean delete(String storedResourceId) throws IOException {
File storedFile = new File(storageDir, storedResourceId); 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 @Override

View File

@ -24,6 +24,7 @@ import java.io.RandomAccessFile;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -543,7 +544,12 @@ public class TransactionLog {
} }
if (deleteOnClose) { 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) { } catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);

View File

@ -24,6 +24,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -1444,10 +1445,8 @@ public class UpdateLog implements PluginInfoInitialized {
public static void deleteFile(File file) { public static void deleteFile(File file) {
boolean success = false; boolean success = false;
try { try {
success = file.delete(); Files.deleteIfExists(file.toPath());
if (!success) { success = true;
log.error("Error deleting file: " + file);
}
} catch (Exception e) { } catch (Exception e) {
log.error("Error deleting file: " + file, e); log.error("Error deleting file: " + file, e);
} }
@ -1489,9 +1488,11 @@ public class UpdateLog implements PluginInfoInitialized {
String[] files = getLogList(tlogDir); String[] files = getLogList(tlogDir);
for (String file : files) { for (String file : files) {
File f = new File(tlogDir, file); File f = new File(tlogDir, file);
boolean s = f.delete(); try {
if (!s) { Files.delete(f.toPath());
log.error("Could not remove tlog file:" + f); } catch (IOException cause) {
// NOTE: still throws SecurityException as before.
log.error("Could not remove tlog file:" + f, cause);
} }
} }
} }

View File

@ -21,7 +21,9 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -95,7 +97,12 @@ public class VersionedFile
List<File> deleted = new ArrayList<>(); List<File> deleted = new ArrayList<>();
for (File df : deleteList) { for (File df : deleteList) {
try { 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); // deleteList.remove(df);
deleted.add(df); deleted.add(df);
} catch (SecurityException e) { } catch (SecurityException e) {

View File

@ -26,6 +26,7 @@ import java.util.regex.Pattern;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4.SuppressSSL; import org.apache.solr.SolrTestCaseJ4.SuppressSSL;
import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrQuery;
@ -487,7 +488,7 @@ public class BasicDistributedZk2Test extends AbstractFullDistribZkTestBase {
assertEquals(Arrays.asList(files).toString(), 1, files.length); assertEquals(Arrays.asList(files).toString(), 1, files.length);
File snapDir = files[0]; File snapDir = files[0];
TestUtil.rm(snapDir); IOUtils.rm(snapDir);
} }
private void addNewReplica() throws Exception { private void addNewReplica() throws Exception {

View File

@ -22,6 +22,7 @@ import junit.framework.Assert;
import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.KeywordTokenizerFactory; import org.apache.lucene.analysis.core.KeywordTokenizerFactory;
import org.apache.lucene.analysis.ngram.NGramFilterFactory; import org.apache.lucene.analysis.ngram.NGramFilterFactory;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException;
@ -76,7 +77,7 @@ public class ResourceLoaderTest extends SolrTestCaseJ4
} }
loader.close(); loader.close();
} finally { } finally {
TestUtil.rm(temp); IOUtils.rm(temp);
} }
} }

View File

@ -28,6 +28,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
import java.nio.file.Files;
import java.util.Map; import java.util.Map;
public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 { 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: // 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) // 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. // 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 @Test

View File

@ -132,6 +132,5 @@ public class TestArbitraryIndexDir extends AbstractSolrTestCase{
"*[count(//doc)=1]" "*[count(//doc)=1]"
); );
dir.close(); dir.close();
newDir.delete();
} }
} }

View File

@ -30,6 +30,7 @@ import org.junit.Test;
import java.io.*; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
@ -62,7 +63,7 @@ public class TestCSVLoader extends SolrTestCaseJ4 {
// if you override setUp or tearDown, you better call // if you override setUp or tearDown, you better call
// the super classes version // the super classes version
super.tearDown(); super.tearDown();
deleteFile(); Files.delete(file.toPath());
} }
void makeFile(String contents) { void makeFile(String contents) {
@ -75,10 +76,6 @@ public class TestCSVLoader extends SolrTestCaseJ4 {
} }
} }
void deleteFile() {
file.delete();
}
void cleanup() { void cleanup() {
assertU(delQ("*:*")); assertU(delQ("*:*"));
assertU(commit()); assertU(commit());

View File

@ -34,6 +34,7 @@ import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.Set; import java.util.Set;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.lucene.util.LuceneTestCase.Slow;
import org.apache.lucene.util.TestUtil; import org.apache.lucene.util.TestUtil;
import org.apache.solr.BaseDistributedSearchTestCase; import org.apache.solr.BaseDistributedSearchTestCase;
@ -1437,7 +1438,7 @@ public class TestReplicationHandler extends SolrTestCaseJ4 {
} }
public void tearDown() throws Exception { public void tearDown() throws Exception {
TestUtil.rm(homeDir); IOUtils.rm(homeDir);
} }
public void copyConfigFile(String srcFile, String destFile) public void copyConfigFile(String srcFile, String destFile)

View File

@ -214,7 +214,7 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
} finally { } finally {
if(!namedBackup) { if(!namedBackup) {
TestUtil.rm(snapDir); org.apache.lucene.util.IOUtils.rm(snapDir);
} }
} }
} }

View File

@ -64,8 +64,6 @@ public class TestCollationField extends SolrTestCaseJ4 {
public static String setupSolrHome() throws Exception { public static String setupSolrHome() throws Exception {
// make a solr home underneath the test's TEMP_DIR // make a solr home underneath the test's TEMP_DIR
File tmpFile = createTempDir("collation1"); File tmpFile = createTempDir("collation1");
tmpFile.delete();
tmpFile.mkdir();
// make data and conf dirs // make data and conf dirs
new File(tmpFile, "data").mkdir(); new File(tmpFile, "data").mkdir();

View File

@ -62,8 +62,6 @@ public class TestCollationFieldDocValues extends SolrTestCaseJ4 {
public static String setupSolrHome() throws Exception { public static String setupSolrHome() throws Exception {
// make a solr home underneath the test's TEMP_DIR // make a solr home underneath the test's TEMP_DIR
File tmpFile = createTempDir("collation1"); File tmpFile = createTempDir("collation1");
tmpFile.delete();
tmpFile.mkdir();
// make data and conf dirs // make data and conf dirs
new File(tmpFile, "data").mkdir(); new File(tmpFile, "data").mkdir();

View File

@ -18,6 +18,7 @@ package org.apache.solr.schema;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.nio.file.Files;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -163,7 +164,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
assertSchemaResource(collection, "managed-schema"); assertSchemaResource(collection, "managed-schema");
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); 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"); assertSchemaResource(collection, "managed-schema");
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); 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{ public void testAddFieldWhenItAlreadyExists() throws Exception{
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); 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{ public void testAddSameFieldTwice() throws Exception{
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); 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{ public void testAddDynamicField() throws Exception{
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); 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 { public void testAddWithSchemaCodecFactory() throws Exception {
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema_codec.xml", tmpSolrHome.getPath()); initCore("solrconfig-managed-schema.xml", "schema_codec.xml", tmpSolrHome.getPath());
@ -368,7 +369,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
public void testAddWithSchemaSimilarityFactory() throws Exception { public void testAddWithSchemaSimilarityFactory() throws Exception {
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-bm25.xml", tmpSolrHome.getPath()); initCore("solrconfig-managed-schema.xml", "schema-bm25.xml", tmpSolrHome.getPath());
@ -397,7 +398,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
assertSchemaResource(collection, "managed-schema"); assertSchemaResource(collection, "managed-schema");
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field-unique-key.xml", tmpSolrHome.getPath()); 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 { public void testAddFieldThenReload() throws Exception {
deleteCore(); deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema"); 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"); System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath()); initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath());

View File

@ -34,6 +34,7 @@ import org.junit.Test;
import java.io.File; import java.io.File;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Arrays; import java.util.Arrays;
import java.util.Deque; import java.util.Deque;
@ -762,7 +763,7 @@ public class TestRecovery extends SolrTestCaseJ4 {
String[] files = ulog.getLogList(logDir); String[] files = ulog.getLogList(logDir);
for (String file : files) { for (String file : files) {
new File(logDir, file).delete(); Files.delete(new File(logDir, file).toPath());
} }
assertEquals(0, ulog.getLogList(logDir).length); assertEquals(0, ulog.getLogList(logDir).length);
@ -1094,7 +1095,7 @@ public class TestRecovery extends SolrTestCaseJ4 {
try { try {
String[] files = ulog.getLogList(logDir); String[] files = ulog.getLogList(logDir);
for (String file : files) { for (String file : files) {
new File(logDir, file).delete(); Files.delete(new File(logDir, file).toPath());
} }
assertEquals(0, ulog.getLogList(logDir).length); assertEquals(0, ulog.getLogList(logDir).length);

View File

@ -23,6 +23,7 @@ import java.io.FileOutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Writer; import java.io.Writer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date; import java.util.Date;
@ -64,7 +65,7 @@ public class CacheHeaderTest extends CacheHeaderTestBase {
HttpResponse response = getClient().execute(m); HttpResponse response = getClient().execute(m);
assertEquals(200, response.getStatusLine().getStatusCode()); assertEquals(200, response.getStatusLine().getStatusCode());
checkVetoHeaders(response, true); checkVetoHeaders(response, true);
f.delete(); Files.delete(f.toPath());
} }
@Test @Test

View File

@ -28,6 +28,7 @@ import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput; import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput; import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.MergeInfo; import org.apache.lucene.store.MergeInfo;
import org.apache.lucene.util.IOUtils;
import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.SolrTestCaseJ4;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@ -232,15 +233,12 @@ public class BlockDirectoryTest extends SolrTestCaseJ4 {
} }
public static void rm(File file) { public static void rm(File file) {
if (!file.exists()) { try {
return; 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();
} }
/** /**

View File

@ -28,6 +28,7 @@ import junit.framework.Assert;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.lucene.util.LuceneTestCase.Slow;
import org.apache.lucene.util.QuickPatchThreadsFilter; import org.apache.lucene.util.QuickPatchThreadsFilter;
@ -305,7 +306,7 @@ public class TestLBHttpSolrServer extends SolrTestCaseJ4 {
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (jetty != null) jetty.stop(); if (jetty != null) jetty.stop();
TestUtil.rm(homeDir); IOUtils.rm(homeDir);
} }
public void startJetty() throws Exception { public void startJetty() throws Exception {

View File

@ -1038,12 +1038,12 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase {
} }
/** /**
* @see TestUtil#rm(File...) * @see IOUtils#rm(File...)
*/ */
@Deprecated() @Deprecated()
public static boolean recurseDelete(File f) { public static boolean recurseDelete(File f) {
try { try {
TestUtil.rm(f); IOUtils.rm(f);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
System.err.println(e.toString()); System.err.println(e.toString());