1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (C) 2017 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 2, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
/*
fstail - Tool to dump the last few transactions from a FileStorage.
Format is the same as in fstail/py originally written by Jeremy Hylton:
https://github.com/zopefoundation/ZODB/blob/master/src/ZODB/scripts/fstail.py
https://github.com/zopefoundation/ZODB/commit/551122cc
*/
package main
import (
"crypto/sha1"
"flag"
"fmt"
"io"
"log"
"os"
"../../../../storage/fs1"
"../../../../xcommon/xbufio"
"lab.nexedi.com/kirr/go123/xbytes"
"lab.nexedi.com/kirr/go123/xfmt"
)
func fsTail(w io.Writer, path string, ntxn int) (err error) {
// path & fstail on error context
defer func() {
if err != nil {
err = fmt.Errorf("%s: fstail: %v", path, err)
}
}()
// we are not using fs1.Open here, since the file or index could be
// e.g. corrupt - we want to iterate directly by FileStorage records
// (same logic as in fstail/py)
f, err := os.Open(path)
if err != nil {
return err
}
defer func() {
err2 := f.Close()
if err == nil {
err = err2
}
}()
// get file size as topPos
fi, err := f.Stat()
if err != nil {
return err
}
topPos := fi.Size()
// txn header & data buffer for iterating
txnh := fs1.TxnHeader{}
data := []byte{}
// use sequential IO buffer
fSeq := xbufio.NewSeqReaderAt(f)
// start iterating at tail.
// this should get EOF but read txnh.LenPrev ok.
err = txnh.Load(fSeq, topPos, fs1.LoadAll)
if err != io.EOF {
if err == nil {
// XXX or allow this?
// though then, if we start not from a txn boundary - there are
// high chances further iteration will go wrong.
err = fmt.Errorf("@%v: reading past file end was unexpectedly successful: probably the file is being modified simultaneously", topPos)
}
// otherwise err already has the context
return err
}
if txnh.LenPrev <= 0 {
return fmt.Errorf("@%v: previous record could not be read", topPos)
}
// buffer for formatting
buf := xfmt.Buffer{}
// now loop loading transactions backwards until EOF / ntxn limit
for i := ntxn; i > 0; i-- {
err = txnh.LoadPrev(fSeq, fs1.LoadAll)
if err != nil {
if err == io.EOF {
err = nil // XXX -> okEOF(err)
}
break
}
// read raw data inside transaction record
dataLen := txnh.DataLen()
data = xbytes.Realloc64(data, dataLen)
_, err = fSeq.ReadAt(data, txnh.DataPos())
if err != nil {
// XXX -> txnh.Err(...) ?
// XXX err = noEOF(err)
err = &fs1.ErrTxnRecord{txnh.Pos, "read data payload", err}
break
}
// print information about read txn record
buf.Reset()
dataSha1 := sha1.Sum(data)
buf .V(txnh.Tid.Time()) .S(": hash=") .Xb(dataSha1[:])
// fstail.py uses repr to print user/description:
// https://github.com/zopefoundation/ZODB/blob/5.2.0-5-g6047e2fae/src/ZODB/scripts/fstail.py#L39
buf .S("\nuser=") .Qpyb(txnh.User) .S(" description=") .Qpyb(txnh.Description)
// NOTE in zodb/py .length is len - 8, in zodb/go - whole txn record length
buf .S(" length=") .D64(txnh.Len - 8)
buf .S(" offset=") .D64(txnh.Pos) .S(" (+") .D64(txnh.HeaderLen()) .S(")\n\n")
_, err = w.Write(buf.Bytes())
if err != nil {
break
}
}
return err
}
func main() {
ntxn := 10
flag.Usage = func() {
fmt.Fprintf(os.Stderr,
`fstail [options] <storage>
Dump transactions from a FileStorage in reverse order
<storage> is a path to FileStorage
options:
-h --help this help text.
-n <N> output the last <N> transactions (default %d).
`, ntxn)
}
flag.IntVar(&ntxn, "n", ntxn, "output the last <N> transactions")
flag.Parse()
argv := flag.Args()
if len(argv) < 1 {
flag.Usage()
os.Exit(2)
}
storPath := argv[0]
err := fsTail(os.Stdout, storPath, ntxn)
if err != nil {
log.Fatal(err)
}
}