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
postings format. (Mike McCandless)
* LUCENE-5906: Use Files.delete everywhere instead of File.delete, so that
when things go wrong, you get a real exception message why.
(Uwe Schindler, Robert Muir)
API Changes:
* LUCENE-5900: Deprecated more constructors taking Version in *InfixSuggester and

View File

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

View File

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

View File

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

View File

@ -355,7 +355,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
assertTrue(bos.toString(IOUtils.UTF_8).contains(IndexFormatTooOldException.class.getName()));
dir.close();
TestUtil.rm(oldIndxeDir);
IOUtils.rm(oldIndxeDir);
}
}
@ -666,7 +666,7 @@ public class TestBackwardsCompatibility extends LuceneTestCase {
public File createIndex(String dirName, boolean doCFS, boolean fullyMerged) throws IOException {
// we use a real directory name that is not cleaned up, because this method is only used to create backwards indexes:
File indexDir = new File("/tmp/idx", dirName);
TestUtil.rm(indexDir);
IOUtils.rm(indexDir);
Directory dir = newFSDirectory(indexDir);
LogByteSizeMergePolicy mp = new LogByteSizeMergePolicy();
mp.setNoCFSRatio(doCFS ? 1.0 : 0.0);

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

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

View File

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

View File

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

View File

@ -22,6 +22,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.Collator;
import java.util.List;
import java.util.Locale;
@ -444,7 +445,7 @@ public class TestPerfTasksLogic extends BenchmarkTestCase {
assertEquals(numLines + " lines were created but " + ir.numDocs() + " docs are in the index", numLines, ir.numDocs());
ir.close();
lineFile.delete();
Files.delete(lineFile.toPath());
}
/**

View File

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

View File

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

View File

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

View File

@ -33,7 +33,11 @@ import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
/** This class emulates the new Java 7 "Try-With-Resources" statement.
* Remove once Lucene is on Java 7.
@ -69,22 +73,7 @@ public final class IOUtils {
* objects to call <tt>close()</tt> on
*/
public static void close(Closeable... objects) throws IOException {
Throwable th = null;
for (Closeable object : objects) {
try {
if (object != null) {
object.close();
}
} catch (Throwable t) {
addSuppressed(th, t);
if (th == null) {
th = t;
}
}
}
reThrow(th);
close(Arrays.asList(objects));
}
/**
@ -118,14 +107,7 @@ public final class IOUtils {
* objects to call <tt>close()</tt> on
*/
public static void closeWhileHandlingException(Closeable... objects) {
for (Closeable object : objects) {
try {
if (object != null) {
object.close();
}
} catch (Throwable t) {
}
}
closeWhileHandlingException(Arrays.asList(objects));
}
/**
@ -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
* if it exists. The source must exist.

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import org.apache.lucene.replicator.ReplicationClient.SourceDirectoryFactory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.IOUtils;
/**
* A {@link SourceDirectoryFactory} which returns {@link FSDirectory} under a
@ -40,19 +41,6 @@ public class PerSessionDirectoryFactory implements SourceDirectoryFactory {
this.workDir = workDir;
}
private void rm(File file) throws IOException {
if (file.isDirectory()) {
for (File f : file.listFiles()) {
rm(f);
}
}
// This should be either an empty directory, or a file
if (!file.delete() && file.exists()) {
throw new IOException("failed to delete " + file);
}
}
@Override
public Directory getDirectory(String sessionID, String source) throws IOException {
File sessionDir = new File(workDir, sessionID);
@ -71,7 +59,7 @@ public class PerSessionDirectoryFactory implements SourceDirectoryFactory {
if (sessionID.isEmpty()) { // protect against deleting workDir entirely!
throw new IllegalArgumentException("sessionID cannot be empty");
}
rm(new File(workDir, sessionID));
IOUtils.rm(new File(workDir, sessionID));
}
}

View File

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

View File

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

View File

@ -376,23 +376,12 @@ public class FreeTextSuggester extends Lookup {
} finally {
try {
if (success) {
IOUtils.close(reader);
IOUtils.close(reader, dir);
} else {
IOUtils.closeWhileHandlingException(reader, writer);
IOUtils.closeWhileHandlingException(reader, writer, dir);
}
} finally {
for(String file : dir.listAll()) {
File path = new File(tempIndexPath, file);
if (path.delete() == false) {
throw new IllegalStateException("failed to remove " + path);
}
}
if (tempIndexPath.delete() == false) {
throw new IllegalStateException("failed to remove " + tempIndexPath);
}
dir.close();
IOUtils.rm(tempIndexPath);
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -28,10 +28,11 @@ import java.io.PrintStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.CharBuffer;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
@ -99,48 +100,12 @@ public final class TestUtil {
//
}
/**
* Deletes one or more files or directories (and everything underneath it).
*
* @throws IOException if any of the given files (or their subhierarchy files in case
* of directories) cannot be removed.
*/
public static void rm(File... locations) throws IOException {
LinkedHashSet<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
* destDir first.
*/
public static void unzip(File zipName, File destDir) throws IOException {
rm(destDir);
IOUtils.rm(destDir);
destDir.mkdir();
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#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;
import java.io.File;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -60,7 +61,7 @@ public abstract class AbstractSqlEntityProcessorTestCase extends
}
@After
public void afterSqlEntitiyProcessorTestCase() {
public void afterSqlEntitiyProcessorTestCase() throws Exception {
useSimpleCaches = false;
countryEntity = false;
countryCached = false;
@ -74,8 +75,8 @@ public abstract class AbstractSqlEntityProcessorTestCase extends
//If an Assume was tripped while setting up the test,
//the file might not ever have been created...
if(fileLocation!=null) {
new File(fileLocation + File.separatorChar + fileName).delete();
new File(fileLocation).delete();
Files.deleteIfExists(new File(fileLocation + File.separatorChar + fileName).toPath());
Files.deleteIfExists(new File(fileLocation).toPath());
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -18,6 +18,7 @@ package org.apache.solr.hadoop;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Locale;
import org.apache.commons.io.FileUtils;
@ -44,7 +45,7 @@ public abstract class MRUnitBase extends SolrTestCaseJ4 {
@AfterClass
public static void teardownClass() throws Exception {
if (solrHomeZip != null) solrHomeZip.delete();
if (solrHomeZip != null) Files.delete(solrHomeZip.toPath());
}
protected void setupHadoopConfig(Configuration config) throws IOException {

View File

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

View File

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

View File

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

View File

@ -30,6 +30,7 @@ import java.io.OutputStreamWriter;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@ -183,7 +184,21 @@ public abstract class ManagedResourceStorage {
@Override
public boolean delete(String storedResourceId) throws IOException {
File storedFile = new File(storageDir, storedResourceId);
return storedFile.isFile() ? storedFile.delete() : false;
return deleteIfFile(storedFile);
}
// TODO: this interface should probably be changed, this simulates the old behavior,
// only throw security exception, just return false otherwise
private boolean deleteIfFile(File f) {
if (!f.isFile()) {
return false;
}
try {
Files.delete(f.toPath());
return true;
} catch (IOException cause) {
return false;
}
}
@Override

View File

@ -24,6 +24,7 @@ import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -543,7 +544,12 @@ public class TransactionLog {
}
if (deleteOnClose) {
tlogFile.delete();
try {
Files.deleteIfExists(tlogFile.toPath());
} catch (IOException e) {
// TODO: should this class care if a file couldnt be deleted?
// this just emulates previous behavior, where only SecurityException would be handled.
}
}
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);

View File

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

View File

@ -21,7 +21,9 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -95,7 +97,12 @@ public class VersionedFile
List<File> deleted = new ArrayList<>();
for (File df : deleteList) {
try {
df.delete();
try {
Files.deleteIfExists(df.toPath());
} catch (IOException cause) {
// TODO: should this class care if a file couldnt be deleted?
// this just emulates previous behavior, where only SecurityException would be handled.
}
// deleteList.remove(df);
deleted.add(df);
} catch (SecurityException e) {

View File

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

View File

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

View File

@ -28,6 +28,7 @@ import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.nio.file.Files;
import java.util.Map;
public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 {
@ -41,7 +42,7 @@ public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 {
// test tests native and simple in the same jvm in the same exact directory:
// the file will remain after the native test (it cannot safely be deleted without the risk of deleting another guys lock)
// its ok, these aren't "compatible" anyway: really this test should not re-use the same directory at all.
new File(new File(initCoreDataDir, "index"), IndexWriter.WRITE_LOCK_NAME).delete();
Files.deleteIfExists(new File(new File(initCoreDataDir, "index"), IndexWriter.WRITE_LOCK_NAME).toPath());
}
@Test

View File

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

View File

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

View File

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

View File

@ -214,7 +214,7 @@ public class TestReplicationHandlerBackup extends SolrJettyTestBase {
} finally {
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 {
// make a solr home underneath the test's TEMP_DIR
File tmpFile = createTempDir("collation1");
tmpFile.delete();
tmpFile.mkdir();
// make data and conf dirs
new File(tmpFile, "data").mkdir();

View File

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

View File

@ -18,6 +18,7 @@ package org.apache.solr.schema;
import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -163,7 +164,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
assertSchemaResource(collection, "managed-schema");
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath());
@ -192,7 +193,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
assertSchemaResource(collection, "managed-schema");
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath());
@ -239,7 +240,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
public void testAddFieldWhenItAlreadyExists() throws Exception{
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath());
@ -272,7 +273,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
public void testAddSameFieldTwice() throws Exception{
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath());
@ -307,7 +308,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
public void testAddDynamicField() throws Exception{
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath());
@ -340,7 +341,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
public void testAddWithSchemaCodecFactory() throws Exception {
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema_codec.xml", tmpSolrHome.getPath());
@ -368,7 +369,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
public void testAddWithSchemaSimilarityFactory() throws Exception {
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-bm25.xml", tmpSolrHome.getPath());
@ -397,7 +398,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
assertSchemaResource(collection, "managed-schema");
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field-unique-key.xml", tmpSolrHome.getPath());
@ -433,7 +434,7 @@ public class TestManagedSchema extends AbstractBadConfigTestBase {
public void testAddFieldThenReload() throws Exception {
deleteCore();
File managedSchemaFile = new File(tmpConfDir, "managed-schema");
assertTrue(managedSchemaFile.delete()); // Delete managed-schema so it won't block parsing a new schema
Files.delete(managedSchemaFile.toPath()); // Delete managed-schema so it won't block parsing a new schema
System.setProperty("managed.schema.mutable", "true");
initCore("solrconfig-managed-schema.xml", "schema-one-field-no-dynamic-field.xml", tmpSolrHome.getPath());

View File

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

View File

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

View File

@ -28,6 +28,7 @@ import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.MergeInfo;
import org.apache.lucene.util.IOUtils;
import org.apache.solr.SolrTestCaseJ4;
import org.junit.After;
import org.junit.Before;
@ -232,15 +233,12 @@ public class BlockDirectoryTest extends SolrTestCaseJ4 {
}
public static void rm(File file) {
if (!file.exists()) {
return;
try {
IOUtils.rm(file);
} catch (Throwable ignored) {
// TODO: should this class care if a file couldnt be deleted?
// this just emulates previous behavior, where only SecurityException would be handled.
}
if (file.isDirectory()) {
for (File f : file.listFiles()) {
rm(f);
}
}
file.delete();
}
/**

View File

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

View File

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