forked from keybase/kbfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmd_dump.go
189 lines (153 loc) · 4.65 KB
/
md_dump.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"flag"
"fmt"
"github.com/keybase/kbfs/kbfsmd"
"github.com/keybase/kbfs/libkbfs"
"github.com/keybase/kbfs/tlf"
"golang.org/x/net/context"
)
func mdDumpImmutableRMD(ctx context.Context, config libkbfs.Config,
replacements replacementMap,
irmd libkbfs.ImmutableRootMetadata) error {
err := mdDumpFillReplacements(
ctx, config.Codec(), config.KeybaseService(), "md dump",
irmd.GetBareRootMetadata(), irmd.Extra(), replacements)
if err != nil {
printError("md dump", err)
}
fmt.Print("Immutable metadata\n")
fmt.Print("------------------\n")
fmt.Printf("MD ID: %s\n", irmd.MdID())
fmt.Printf("Local timestamp: %s\n", irmd.LocalTimestamp())
fmt.Printf("Last modifying device (verifying key): %s\n",
mdDumpReplaceAll(irmd.LastModifyingWriterVerifyingKey().String(), replacements))
fmt.Print("\n")
return mdDumpReadOnlyRMDWithReplacements(
ctx, config.Codec(), replacements, irmd.ReadOnly())
}
func mdDumpChunk(ctx context.Context, config libkbfs.Config,
replacements replacementMap, tlfStr, branchStr string,
tlfID tlf.ID, branchID kbfsmd.BranchID,
start, stop kbfsmd.Revision) error {
min := start
max := stop
reversed := false
if start > stop {
min = stop
max = start
reversed = true
}
irmds, err := mdGet(ctx, config, tlfID, branchID, min, max)
if err != nil {
return err
}
if reversed {
irmds = reverseIRMDList(irmds)
}
fmt.Printf("%d results for %q:\n\n", len(irmds),
mdJoinInput(tlfStr, branchStr, start.String(), stop.String()))
for _, irmd := range irmds {
err = mdDumpImmutableRMD(ctx, config, replacements, irmd)
if err != nil {
return err
}
fmt.Print("\n")
}
return nil
}
func mdDumpInput(ctx context.Context, config libkbfs.Config,
replacements replacementMap, input string) error {
tlfStr, branchStr, startStr, stopStr, err := mdSplitInput(input)
if err != nil {
return err
}
tlfID, branchID, start, stop, err :=
mdParseInput(ctx, config, tlfStr, branchStr, startStr, stopStr)
if err != nil {
return err
}
// TODO: Ideally we'd fetch MDs concurrently with dumping
// them, but this works well enough for now.
//
// TODO: Make maxChunkSize configurable.
const maxChunkSize = 100
if start <= stop {
for chunkStart := start; chunkStart <= stop; chunkStart += maxChunkSize {
chunkStop := chunkStart + maxChunkSize - 1
if chunkStop > stop {
chunkStop = stop
}
err = mdDumpChunk(ctx, config, replacements, tlfStr, branchStr, tlfID, branchID, chunkStart, chunkStop)
if err != nil {
return err
}
}
} else {
for chunkStart := start; chunkStart >= stop; chunkStart -= maxChunkSize {
chunkStop := chunkStart - maxChunkSize + 1
if chunkStop < stop {
chunkStop = stop
}
err = mdDumpChunk(ctx, config, replacements, tlfStr, branchStr, tlfID, branchID, chunkStart, chunkStop)
if err != nil {
return err
}
}
}
return nil
}
const mdDumpUsageStr = `Usage:
kbfstool md dump input [inputs...]
Each input must be in the following format:
TLF
TLF:Branch
TLF^RevisionRange
TLF:Branch^RevisionRange
where TLF can be:
- a TLF ID string (32 hex digits), or
- a keybase TLF path (e.g., "/keybase/public/user1,user2", or
"/keybase/private/user1,assertion2");
Branch can be:
- a Branch ID string (32 hex digits),
- the string "device", which indicates the unmerged branch for the
current device, or the master branch if there is no unmerged branch,
- the string "master", which is a shorthand for
the ID of the master branch "00000000000000000000000000000000", or
- omitted, in which case it is treated as if it were the string "device";
and RevisionRange can be in the following format:
Revision
Revision-Revision
where Revision can be:
- a hex number prefixed with "0x",
- a decimal number with no prefix,
- the string "latest", which indicates the latest revision for the
branch, or
- omitted, in which case it is treated as if it were the string "latest".
If a single revision "rev" is specified, it's treated as if the range
"rev-rev" was specified. If a range "rev1-rev2" is specified, then the
revisions are dumped in ascending order if rev1 <= rev2, and descending
order if rev1 > rev2.
`
func mdDump(ctx context.Context, config libkbfs.Config, args []string) (exitStatus int) {
flags := flag.NewFlagSet("kbfs md dump", flag.ContinueOnError)
err := flags.Parse(args)
if err != nil {
printError("md dump", err)
return 1
}
inputs := flags.Args()
if len(inputs) < 1 {
fmt.Print(mdDumpUsageStr)
return 1
}
replacements := make(replacementMap)
for _, input := range inputs {
err := mdDumpInput(ctx, config, replacements, input)
if err != nil {
printError("md dump", err)
return 1
}
}
return 0
}