Search in sources :

Example 6 with FilePath

use of org.h2.store.fs.FilePath in project h2database by h2database.

the class FileZip2 method newDirectoryStream.

@Override
public ArrayList<FilePath> newDirectoryStream() {
    String path = name;
    try {
        if (path.indexOf('!') < 0) {
            path += "!";
        }
        if (!path.endsWith("/")) {
            path += "/";
        }
        ZipInputStream file = openZip();
        String dirName = getEntryName();
        String prefix = path.substring(0, path.length() - dirName.length());
        ArrayList<FilePath> list = New.arrayList();
        while (true) {
            ZipEntry entry = file.getNextEntry();
            if (entry == null) {
                break;
            }
            String entryName = entry.getName();
            if (entryName.startsWith(dirName) && entryName.length() > dirName.length()) {
                int idx = entryName.indexOf('/', dirName.length());
                if (idx < 0 || idx >= entryName.length() - 1) {
                    list.add(getPath(prefix + entryName));
                }
            }
            file.closeEntry();
        }
        file.close();
        return list;
    } catch (IOException e) {
        throw DbException.convertIOException(e, "listFiles " + path);
    }
}
Also used : FilePath(org.h2.store.fs.FilePath) ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException)

Example 7 with FilePath

use of org.h2.store.fs.FilePath in project h2database by h2database.

the class TestMVStore method testFileHeaderCorruption.

private void testFileHeaderCorruption() throws Exception {
    String fileName = getBaseDir() + "/" + getTestName();
    FileUtils.delete(fileName);
    MVStore s = new MVStore.Builder().fileName(fileName).pageSplitSize(1000).autoCommitDisabled().open();
    s.setRetentionTime(0);
    MVMap<Integer, byte[]> map;
    map = s.openMap("test");
    map.put(0, new byte[100]);
    for (int i = 0; i < 10; i++) {
        map = s.openMap("test" + i);
        map.put(0, new byte[1000]);
        s.commit();
    }
    FileStore fs = s.getFileStore();
    long size = fs.getFile().size();
    for (int i = 0; i < 100; i++) {
        map = s.openMap("test" + i);
        s.removeMap(map);
        s.commit();
        s.compact(100, 1);
        if (fs.getFile().size() <= size) {
            break;
        }
    }
    // the last chunk is at the end
    s.setReuseSpace(false);
    map = s.openMap("test2");
    map.put(1, new byte[1000]);
    s.close();
    FilePath f = FilePath.get(fileName);
    int blockSize = 4 * 1024;
    // test corrupt file headers
    for (int i = 0; i <= blockSize; i += blockSize) {
        FileChannel fc = f.open("rw");
        if (i == 0) {
            // corrupt the last block (the end header)
            fc.write(ByteBuffer.allocate(256), fc.size() - 256);
        }
        ByteBuffer buff = ByteBuffer.allocate(4 * 1024);
        fc.read(buff, i);
        String h = new String(buff.array(), StandardCharsets.UTF_8).trim();
        int idx = h.indexOf("fletcher:");
        int old = Character.digit(h.charAt(idx + "fletcher:".length()), 16);
        int bad = (old + 1) & 15;
        buff.put(idx + "fletcher:".length(), (byte) Character.forDigit(bad, 16));
        buff.rewind();
        fc.write(buff, i);
        fc.close();
        if (i == 0) {
            // if the first header is corrupt, the second
            // header should be used
            s = openStore(fileName);
            map = s.openMap("test");
            assertEquals(100, map.get(0).length);
            map = s.openMap("test2");
            assertFalse(map.containsKey(1));
            s.close();
        } else {
            // both headers are corrupt
            try {
                s = openStore(fileName);
                fail();
            } catch (Exception e) {
            // expected
            }
        }
    }
}
Also used : MVStore(org.h2.mvstore.MVStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FilePath(org.h2.store.fs.FilePath) FileStore(org.h2.mvstore.FileStore) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer)

Example 8 with FilePath

use of org.h2.store.fs.FilePath in project h2database by h2database.

the class FileReorderWrites method open.

@Override
public FileChannel open(String mode) throws IOException {
    InputStream in = newInputStream();
    FilePath copy = FilePath.get(getBase().toString() + ".copy");
    OutputStream out = copy.newOutputStream(false);
    IOUtils.copy(in, out);
    in.close();
    out.close();
    FileChannel base = getBase().open(mode);
    FileChannel readBase = copy.open(mode);
    return new FileReorderWrites(this, base, readBase);
}
Also used : FilePath(org.h2.store.fs.FilePath) InputStream(java.io.InputStream) FileChannel(java.nio.channels.FileChannel) OutputStream(java.io.OutputStream)

Aggregations

FilePath (org.h2.store.fs.FilePath)8 FileChannel (java.nio.channels.FileChannel)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ByteBuffer (java.nio.ByteBuffer)2 ZipEntry (java.util.zip.ZipEntry)2 FileLock (java.nio.channels.FileLock)1 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 FileStore (org.h2.mvstore.FileStore)1 MVStore (org.h2.mvstore.MVStore)1 FilePathDisk (org.h2.store.fs.FilePathDisk)1 FilePathEncrypt (org.h2.store.fs.FilePathEncrypt)1 AssertThrows (org.h2.test.utils.AssertThrows)1