Beispiel #1
0
func newJournalWriter(file storage.File) (p *journalWriter, err error) {
	w := new(journalWriter)
	w.file = file
	w.writer, err = file.Create()
	if err != nil {
		return
	}
	w.journal = journal.NewWriter(w.writer)
	return w, nil
}
Beispiel #2
0
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()
}