forked from kopia/kopia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_blob_shards_modify.go
More file actions
275 lines (212 loc) · 6.68 KB
/
Copy pathcommand_blob_shards_modify.go
File metadata and controls
275 lines (212 loc) · 6.68 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package cli
import (
"context"
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/blob/sharded"
)
type commandBlobShardsModify struct {
rootPath string
defaultShardSpec string
overrideSpecs []string
removeOverrides []string
dryRun bool
unshardedLength int
out textOutput
}
func (c *commandBlobShardsModify) setup(svc appServices, parent commandParent) {
c.unshardedLength = -1
cmd := parent.Command("modify", "Perform low-level resharding of blob storage").Hidden().Alias("reshard")
cmd.Flag("i-am-sure-kopia-is-not-running", "Confirm that no other instance of kopia is running").Required().Bool()
cmd.Flag("path", "Sharded directory path").Required().ExistingDirVar(&c.rootPath)
cmd.Flag("default-shards", "Default specification 'n1,..nN' or 'flat')").StringVar(&c.defaultShardSpec)
cmd.Flag("override", "Override specification 'prefix=n1,..nN')").StringsVar(&c.overrideSpecs)
cmd.Flag("remove-override", "Override specification 'prefix=n1,..nN')").StringsVar(&c.removeOverrides)
cmd.Flag("unsharded-length", "Minimum sharded length").IntVar(&c.unshardedLength)
cmd.Flag("dry-run", "Dry run").BoolVar(&c.dryRun)
cmd.Action(svc.noRepositoryAction(c.run))
c.out.setup(svc)
}
func (c *commandBlobShardsModify) getParameters(dotShardsFile string) (*sharded.Parameters, error) {
//nolint:gosec
f, err := os.Open(dotShardsFile)
if err != nil {
return nil, errors.Wrap(err, "unable to open shards file")
}
p := &sharded.Parameters{}
if err := p.Load(f); err != nil {
return nil, errors.Wrap(err, "error loading parameters")
}
return p, nil
}
func parseShardSpec(shards string) ([]int, error) {
result := []int{}
if shards == "flat" {
return result, nil
}
parts := strings.SplitSeq(shards, ",")
for p := range parts {
if p == "" {
continue
}
v, err := strconv.Atoi(p)
if err != nil || v < 0 {
return nil, errors.New("invalid shard specification")
}
result = append(result, v)
}
return result, nil
}
func prefixAndShardsWithout(pas []sharded.PrefixAndShards, without blob.ID) []sharded.PrefixAndShards {
result := []sharded.PrefixAndShards{}
for _, it := range pas {
if it.Prefix != without {
result = append(result, it)
}
}
return result
}
func (c *commandBlobShardsModify) applyParameterChangesFromFlags(p *sharded.Parameters) error {
if c.defaultShardSpec != "" {
v, err := parseShardSpec(c.defaultShardSpec)
if err != nil {
return errors.New("invalid --default-shards")
}
p.DefaultShards = v
}
for _, ov := range c.removeOverrides {
p.Overrides = prefixAndShardsWithout(p.Overrides, blob.ID(ov))
}
for _, ov := range c.overrideSpecs {
parts := strings.Split(ov, "=")
if len(parts) <= 1 {
return errors.Errorf("invalid override %q, must be prefix=n1,..,nM", ov)
}
v, err := parseShardSpec(parts[1])
if err != nil {
return errors.Errorf("invalid override %q, must be prefix=n1,..,nM", ov)
}
p.Overrides = append(
prefixAndShardsWithout(p.Overrides, blob.ID(parts[0])),
sharded.PrefixAndShards{
Prefix: blob.ID(parts[0]),
Shards: v,
})
}
if c.unshardedLength != -1 {
p.UnshardedLength = c.unshardedLength
}
return nil
}
func (c *commandBlobShardsModify) run(ctx context.Context) error {
var numMoved, numUnchanged, numRemoved int
dotShardsFile := filepath.Join(c.rootPath, sharded.ParametersFile)
log(ctx).Info("Reading .shards file.")
srcPar, err := c.getParameters(dotShardsFile)
if err != nil {
return err
}
dstPar := srcPar.Clone()
if err2 := c.applyParameterChangesFromFlags(dstPar); err2 != nil {
return err2
}
log(ctx).Info("Moving files...")
if err2 := c.renameBlobs(ctx, c.rootPath, "", dstPar, &numMoved, &numUnchanged); err2 != nil {
return errors.Wrap(err2, "error processing directory")
}
if c.dryRun {
log(ctx).Infof("Would move %v file, %v unchanged.", numMoved, numUnchanged)
return nil
}
log(ctx).Infof("Moved %v files, %v unchanged.", numMoved, numUnchanged)
log(ctx).Info("Removing empty directories...")
if _, err2 := c.removeEmptyDirs(ctx, c.rootPath, &numRemoved); err2 != nil {
return errors.Wrap(err2, "error removing empty directories")
}
log(ctx).Infof("Removed %v empty directories...", numRemoved)
log(ctx).Info("Writing new .shards file.")
of, err := os.Create(dotShardsFile) //nolint:gosec
if err != nil {
return errors.Wrap(err, "error creating .shards file")
}
defer of.Close() //nolint:errcheck
return errors.Wrap(dstPar.Save(of), "error saving .shards file")
}
func (c *commandBlobShardsModify) removeEmptyDirs(ctx context.Context, dir string, numRemoved *int) (bool, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return false, errors.Wrap(err, "error reading directory")
}
isEmpty := true
for _, ent := range entries {
//nolint:nestif
if ent.IsDir() {
childPath := path.Join(dir, ent.Name())
subDirEmpty, err := c.removeEmptyDirs(ctx, childPath, numRemoved)
if err != nil {
return false, err
}
if !subDirEmpty {
isEmpty = false
} else {
c.out.printStdout("rmdir %v\n", childPath)
*numRemoved++
if !c.dryRun {
if err := os.Remove(childPath); err != nil {
log(ctx).Errorf("Unable to remove directory %v", childPath)
isEmpty = false
}
}
}
} else {
isEmpty = false
}
}
return isEmpty, nil
}
func (c *commandBlobShardsModify) renameBlobs(ctx context.Context, dir, prefix string, params *sharded.Parameters, numMoved, numUnchanged *int) error {
entries, err := os.ReadDir(dir)
if err != nil {
return errors.Wrap(err, "error reading directory")
}
for _, ent := range entries {
//nolint:nestif
if ent.IsDir() {
if err := c.renameBlobs(ctx, path.Join(dir, ent.Name()), prefix+ent.Name(), params, numMoved, numUnchanged); err != nil {
return err
}
} else if name, ok := strings.CutSuffix(ent.Name(), sharded.CompleteBlobSuffix); ok {
blobID := prefix + name
destDir, destBlobID := params.GetShardDirectoryAndBlob(c.rootPath, blob.ID(blobID))
srcFile := path.Join(dir, ent.Name())
destFile := fmt.Sprintf("%v/%v%v", destDir, destBlobID, sharded.CompleteBlobSuffix)
if srcFile == destFile {
log(ctx).Debugf("Unchanged: %v", srcFile)
*numUnchanged++
} else {
c.out.printStdout("mv %v %v\n", srcFile, destFile)
if !c.dryRun {
err := os.Rename(srcFile, destFile)
if os.IsNotExist(err) {
//nolint:mnd
if err2 := os.MkdirAll(destDir, 0o700); err2 != nil {
return errors.Wrap(err2, "error creating directory")
}
err = os.Rename(srcFile, destFile)
}
if err != nil {
return errors.Wrap(err, "error moving")
}
}
*numMoved++
}
}
}
return nil
}