func newJournalReader(file storage.File, checksum bool, dropf journal.DropFunc) (p *journalReader, err error) { r := new(journalReader) r.file = file r.reader, err = file.Open() if err != nil { return } r.journal = journal.NewReader(r.reader, checksum, dropf) return r, nil }
func newJournalReader(file storage.File, checksum bool, dropf journal.DropFunc) (p *journalReader, err error) { r, err := file.Open() if err != nil { return nil, err } jr, err := journal.NewReader(r, 0, checksum, dropf) if err != nil { return nil, err } return &journalReader{ file: file, reader: r, journal: jr, }, nil }
func (h *dbCorruptHarness) corrupt(ft storage.FileType, offset, n int) { p := &h.dbHarness t := p.t var file storage.File ff, _ := p.stor.GetFiles(ft) for _, f := range ff { if file == nil || f.Num() > file.Num() { file = f } } if file == nil { t.Fatalf("no such file with type %q", ft) } r, err := file.Open() if err != nil { t.Fatal("cannot open file: ", err) } x, err := r.Seek(0, 2) if err != nil { t.Fatal("cannot query file size: ", err) } m := int(x) if _, err := r.Seek(0, 0); err != nil { t.Fatal(err) } if offset < 0 { if -offset > m { offset = 0 } else { offset = m + offset } } if offset > m { offset = m } if offset+n > m { n = m - offset } buf := make([]byte, m) _, err = io.ReadFull(r, buf) if err != nil { t.Fatal("cannot read file: ", err) } r.Close() for i := 0; i < n; i++ { buf[offset+i] ^= 0x80 } err = file.Remove() if err != nil { t.Fatal("cannot remove old file: ", err) } w, err := file.Create() if err != nil { t.Fatal("cannot create new file: ", err) } _, err = w.Write(buf) if err != nil { t.Fatal("cannot write new file: ", err) } w.Close() }
func scanTable(f storage.File, checksum bool) (corrupted bool) { fi := storage.NewFileInfo(f) r, err := f.Open() if err != nil { log.Fatal(err) } defer r.Close() size, err := r.Seek(0, os.SEEK_END) if err != nil { log.Fatal(err) } o := &opt.Options{Strict: opt.NoStrict} if checksum { o.Strict = opt.StrictBlockChecksum | opt.StrictReader } tr, err := table.NewReader(r, size, fi, nil, bpool, o) if err != nil { log.Fatal(err) } defer tr.Release() checkData := func(i int, t string, data []byte) bool { if len(data) == 0 { panic(fmt.Sprintf("[%v] nil data: i=%d t=%s", fi, i, t)) } checksum0, checksum1 := dataChecksum(data) if checksum0 != checksum1 { atomic.StoreUint32(&fail, 1) atomic.StoreUint32(&done, 1) corrupted = true data0, data1 := dataSplit(data) data0c0, data0c1 := dataChecksum(data0) data1c0, data1c1 := dataChecksum(data1) log.Printf("FATAL: [%v] Corrupted data i=%d t=%s (%#x != %#x): %x(%v) vs %x(%v)", fi, i, t, checksum0, checksum1, data0, data0c0 == data0c1, data1, data1c0 == data1c1) return true } return false } iter := tr.NewIterator(nil, nil) defer iter.Release() for i := 0; iter.Next(); i++ { ukey, _, kt, kerr := parseIkey(iter.Key()) if kerr != nil { atomic.StoreUint32(&fail, 1) atomic.StoreUint32(&done, 1) corrupted = true log.Printf("FATAL: [%v] Corrupted ikey i=%d: %v", fi, i, kerr) return } if checkData(i, "key", ukey) { return } if kt == ktVal && checkData(i, "value", iter.Value()) { return } } if err := iter.Error(); err != nil { if errors.IsCorrupted(err) { atomic.StoreUint32(&fail, 1) atomic.StoreUint32(&done, 1) corrupted = true log.Printf("FATAL: [%v] Corruption detected: %v", fi, err) } else { log.Fatal(err) } } return }