Search in sources :

Example 26 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class MockDirectoryWrapper method maybeThrowIOExceptionOnOpen.

void maybeThrowIOExceptionOnOpen(String name) throws IOException {
    if (randomState.nextDouble() < randomIOExceptionRateOnOpen) {
        if (LuceneTestCase.VERBOSE) {
            System.out.println(Thread.currentThread().getName() + ": MockDirectoryWrapper: now throw random exception during open file=" + name);
            new Throwable().printStackTrace(System.out);
        }
        if (allowRandomFileNotFoundException == false || randomState.nextBoolean()) {
            throw new IOException("a random IOException (" + name + ")");
        } else {
            throw randomState.nextBoolean() ? new FileNotFoundException("a random IOException (" + name + ")") : new NoSuchFileException("a random IOException (" + name + ")");
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException)

Example 27 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class TestMockDirectoryWrapper method testCorruptOnCloseIsWorking.

private void testCorruptOnCloseIsWorking(Directory dir) throws Exception {
    dir = new PreventCloseDirectoryWrapper(dir);
    try (MockDirectoryWrapper wrapped = new MockDirectoryWrapper(random(), dir)) {
        // otherwise MDW sometimes randomly leaves the file intact and we'll see false test failures:
        wrapped.alwaysCorrupt = true;
        // MDW will only try to corrupt things if it sees an index:
        RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
        iw.addDocument(new Document());
        iw.close();
        // not sync'd!
        try (IndexOutput out = wrapped.createOutput("foo", IOContext.DEFAULT)) {
            for (int i = 0; i < 100; i++) {
                out.writeInt(i);
            }
        }
    // MDW.close now corrupts our unsync'd file (foo):
    }
    boolean changed = false;
    IndexInput in = null;
    try {
        in = dir.openInput("foo", IOContext.DEFAULT);
    } catch (NoSuchFileException | FileNotFoundException fnfe) {
        // ok
        changed = true;
    }
    if (in != null) {
        for (int i = 0; i < 100; i++) {
            int x;
            try {
                x = in.readInt();
            } catch (EOFException eofe) {
                changed = true;
                break;
            }
            if (x != i) {
                changed = true;
                break;
            }
        }
        in.close();
    }
    assertTrue("MockDirectoryWrapper on dir=" + dir + " failed to corrupt an unsync'd file", changed);
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) FileNotFoundException(java.io.FileNotFoundException) EOFException(java.io.EOFException) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 28 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class Node method readLocalFileMetaData.

/** Opens the specified file, reads its identifying information, including file length, full index header (includes the unique segment
   *  ID) and the full footer (includes checksum), and returns the resulting {@link FileMetaData}.
   *
   *  <p>This returns null, logging a message, if there are any problems (the file does not exist, is corrupt, truncated, etc.).</p> */
public FileMetaData readLocalFileMetaData(String fileName) throws IOException {
    Map<String, FileMetaData> cache = lastFileMetaData;
    FileMetaData result;
    if (cache != null) {
        // We may already have this file cached from the last NRT point:
        result = cache.get(fileName);
    } else {
        result = null;
    }
    if (result == null) {
        // Pull from the filesystem
        long checksum;
        long length;
        byte[] header;
        byte[] footer;
        try (IndexInput in = dir.openInput(fileName, IOContext.DEFAULT)) {
            try {
                length = in.length();
                header = CodecUtil.readIndexHeader(in);
                footer = CodecUtil.readFooter(in);
                checksum = CodecUtil.retrieveChecksum(in);
            } catch (EOFException | CorruptIndexException cie) {
                // to delete such unreferenced files, but virus checker can block that, leaving this bad file.
                if (VERBOSE_FILES) {
                    message("file " + fileName + ": will copy [existing file is corrupt]");
                }
                return null;
            }
            if (VERBOSE_FILES) {
                message("file " + fileName + " has length=" + bytesToString(length));
            }
        } catch (FileNotFoundException | NoSuchFileException e) {
            if (VERBOSE_FILES) {
                message("file " + fileName + ": will copy [file does not exist]");
            }
            return null;
        }
        // NOTE: checksum is redundant w/ footer, but we break it out separately because when the bits cross the wire we need direct access to
        // checksum when copying to catch bit flips:
        result = new FileMetaData(header, footer, length, checksum);
    }
    return result;
}
Also used : EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexInput(org.apache.lucene.store.IndexInput) CorruptIndexException(org.apache.lucene.index.CorruptIndexException)

Example 29 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class FileSwitchDirectory method listAll.

@Override
public String[] listAll() throws IOException {
    Set<String> files = new HashSet<>();
    // LUCENE-3380: either or both of our dirs could be FSDirs,
    // but if one underlying delegate is an FSDir and mkdirs() has not
    // yet been called, because so far everything is written to the other,
    // in this case, we don't want to throw a NoSuchFileException
    NoSuchFileException exc = null;
    try {
        for (String f : primaryDir.listAll()) {
            files.add(f);
        }
    } catch (NoSuchFileException e) {
        exc = e;
    }
    try {
        for (String f : secondaryDir.listAll()) {
            files.add(f);
        }
    } catch (NoSuchFileException e) {
        // rethrow the first.
        if (exc != null) {
            throw exc;
        }
        // and the primary is empty.
        if (files.isEmpty()) {
            throw e;
        }
    }
    // and the secondary is empty.
    if (exc != null && files.isEmpty()) {
        throw exc;
    }
    String[] result = files.toArray(new String[files.size()]);
    Arrays.sort(result);
    return result;
}
Also used : NoSuchFileException(java.nio.file.NoSuchFileException) HashSet(java.util.HashSet)

Example 30 with NoSuchFileException

use of java.nio.file.NoSuchFileException in project lucene-solr by apache.

the class TestIndexWriter method testLeftoverTempFiles.

public void testLeftoverTempFiles() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    IndexWriter w = new IndexWriter(dir, iwc);
    w.close();
    IndexOutput out = dir.createTempOutput("_0", "bkd", IOContext.DEFAULT);
    String tempName = out.getName();
    out.close();
    iwc = new IndexWriterConfig(new MockAnalyzer(random()));
    w = new IndexWriter(dir, iwc);
    // Make sure IW deleted the unref'd file:
    try {
        dir.openInput(tempName, IOContext.DEFAULT);
        fail("did not hit exception");
    } catch (FileNotFoundException | NoSuchFileException e) {
    // expected
    }
    w.close();
    dir.close();
}
Also used : MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) IndexOutput(org.apache.lucene.store.IndexOutput) MMapDirectory(org.apache.lucene.store.MMapDirectory) Directory(org.apache.lucene.store.Directory) RAMDirectory(org.apache.lucene.store.RAMDirectory) FSDirectory(org.apache.lucene.store.FSDirectory) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) NIOFSDirectory(org.apache.lucene.store.NIOFSDirectory)

Aggregations

NoSuchFileException (java.nio.file.NoSuchFileException)255 IOException (java.io.IOException)103 Path (java.nio.file.Path)103 FileNotFoundException (java.io.FileNotFoundException)41 Test (org.junit.Test)35 InputStream (java.io.InputStream)30 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)25 File (java.io.File)20 NotDirectoryException (java.nio.file.NotDirectoryException)19 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)18 HashSet (java.util.HashSet)16 OutputStream (java.io.OutputStream)15 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)15 ArrayList (java.util.ArrayList)15 FileChannel (java.nio.channels.FileChannel)14 AccessDeniedException (java.nio.file.AccessDeniedException)14 HashMap (java.util.HashMap)13 Map (java.util.Map)12 ByteBuffer (java.nio.ByteBuffer)11 SeekableByteChannel (java.nio.channels.SeekableByteChannel)11