-
Notifications
You must be signed in to change notification settings - Fork 1
/
dir.go
220 lines (196 loc) · 6.12 KB
/
dir.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
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
// Package fs implements blob store structures for representing files and directories.
package fs
import (
"context"
"io"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
"github.com/bobg/bs"
"github.com/bobg/bs/schema"
"github.com/bobg/bs/split"
)
// Dir is a directory of files, symlinks, and subdirs.
// It is implemented as a schema.Map,
// with each key the name of the entry
// and each payload a serialized Dirent.
type Dir schema.Map
// NewDir produces a new, blank Dir,
// not yet written to a blob store.
func NewDir() *Dir {
return (*Dir)(schema.NewMap())
}
// Load loads the directory at ref into d.
func (d *Dir) Load(ctx context.Context, g bs.Getter, ref bs.Ref) error {
return bs.GetProto(ctx, g, ref, (*schema.Map)(d))
}
// Each calls a callback on each name/Dirent pair in d
// (in an indeterminate order).
// If the callback returns an error,
// Each exits with that error.
func (d *Dir) Each(ctx context.Context, g bs.Getter, f func(string, *Dirent) error) error {
return (*schema.Map)(d).Each(ctx, g, func(pair *schema.MapPair) error {
var dirent Dirent
err := proto.Unmarshal(pair.Payload, &dirent)
if err != nil {
return err
}
return f(string(pair.Key), &dirent)
})
}
// Set sets the given name in d to the given Dirent.
// It returns the possibly updated Ref of d
// and the schema.Outcome resulting from the underlying "bs/schema".Map.Set call.
func (d *Dir) Set(ctx context.Context, store bs.Store, name string, dirent *Dirent) (bs.Ref, schema.Outcome, error) {
direntBytes, err := proto.Marshal(dirent)
if err != nil {
return bs.Zero, schema.ONone, errors.Wrapf(err, "marshaling dirent for %s", name)
}
return (*schema.Map)(d).Set(ctx, store, []byte(name), direntBytes)
}
// Dirent finds the entry in d with the given name.
// It returns nil if no such entry exists.
// It does not traverse subdirs,
// and name must not be a multi-segment path.
// For that,
// use Find.
// Note: Dirent does not understand "." and "..".
func (d *Dir) Dirent(ctx context.Context, g bs.Getter, name string) (*Dirent, error) {
dbytes, ok, err := (*schema.Map)(d).Lookup(ctx, g, []byte(name))
if err != nil {
return nil, errors.Wrapf(err, "looking up %s", name)
}
if !ok {
return nil, nil
}
var dirent Dirent
err = proto.Unmarshal(dbytes, &dirent)
return &dirent, errors.Wrapf(err, "unmarshaling dirent at %s", name)
}
// Find resolves a path starting at d,
// traversing into subdirs as appropriate,
// and returning the entry found at the end of path.
// It does not resolve symlinks in its traversal.
// Leading path separators in `path` are ignored;
// traversal is always relative to d.
// Note: Find does not understand "." and "..".
func (d *Dir) Find(ctx context.Context, g bs.Getter, path string) (*Dirent, error) {
path = strings.TrimLeft(path, "/")
var name string
path, name = filepath.Split(path)
if path != "" {
dirent, err := d.Find(ctx, g, path)
if err != nil {
return nil, errors.Wrapf(err, "finding %s", path)
}
d, err = dirent.Dir(ctx, g)
if err != nil {
return nil, errors.Wrapf(err, "resolving dir %s", path)
}
}
return d.Dirent(ctx, g, name)
}
// Add adds the file, symlink, or dir at path to d.
// If path is a dir,
// this is recursive.
// It returns the possibly-updated Ref for d.
func (d *Dir) Add(ctx context.Context, store bs.Store, path string) (bs.Ref, error) {
dirent, err := add(ctx, store, path)
if err != nil {
return bs.Zero, err
}
ref, _, err := d.Set(ctx, store, filepath.Base(path), dirent)
return ref, err
}
func add(ctx context.Context, store bs.Store, path string) (*Dirent, error) {
info, err := os.Lstat(path)
if err != nil {
return nil, errors.Wrapf(err, "statting %s", path)
}
mode := uint32(info.Mode())
dirent := &Dirent{Mode: mode}
if dirent.IsLink() {
target, err := os.Readlink(path)
if err != nil {
return nil, errors.Wrapf(err, "reading symlink %s", path)
}
dirent.Item = target
return dirent, nil
}
if dirent.IsDir() {
m := make(map[string][]byte)
err = addDirToMap(ctx, store, path, m)
if err != nil {
return nil, errors.Wrap(err, "populating dir map")
}
_, ref, err := schema.MapFromGo(ctx, store, m)
if err != nil {
return nil, errors.Wrap(err, "storing dir map")
}
dirent.Item = ref.String()
return dirent, nil
}
if (mode & uint32(os.ModeType)) != 0 {
return nil, errors.Wrapf(err, "unsupported file type 0%o for %s", fs.FileMode(mode).Type(), path)
}
f, err := os.Open(path)
if err != nil {
return nil, errors.Wrapf(err, "opening file %s", path)
}
defer f.Close()
w := split.NewWriter(ctx, store)
_, err = io.Copy(w, f)
if err != nil {
return nil, errors.Wrapf(err, "split-writing to store from %s", path)
}
err = w.Close()
if err != nil {
return nil, errors.Wrapf(err, "finishing split-writing to store from %s", path)
}
dirent.Item = w.Root.String()
return dirent, nil
}
// AddDir adds the members of the directory at path to d, recursively.
func (d *Dir) AddDir(ctx context.Context, store bs.Store, path string) (bs.Ref, error) {
m := make(map[string][]byte)
err := (*schema.Map)(d).Each(ctx, store, func(pair *schema.MapPair) error {
m[string(pair.Key)] = pair.Payload
return nil
})
if err != nil {
return bs.Zero, errors.Wrap(err, "iterating over existing dir")
}
err = addDirToMap(ctx, store, path, m)
if err != nil {
return bs.Zero, errors.Wrap(err, "populating dir map")
}
_, ref, err := schema.MapFromGo(ctx, store, m)
return ref, errors.Wrap(err, "storing dir map")
}
func addDirToMap(ctx context.Context, store bs.Store, path string, m map[string][]byte) error {
infos, err := ioutil.ReadDir(path)
if err != nil {
return errors.Wrapf(err, "reading dir %s", path)
}
for _, info := range infos {
name := info.Name()
dirent, err := add(ctx, store, filepath.Join(path, name))
if err != nil {
return errors.Wrapf(err, "adding %s/%s", path, name)
}
direntBytes, err := proto.Marshal(dirent)
if err != nil {
return errors.Wrapf(err, "marshaling dirent for %s/%s", path, name)
}
m[name] = direntBytes
}
return nil
}
// Ref returns d's Ref.
func (d *Dir) Ref() (bs.Ref, error) {
return bs.ProtoRef((*schema.Map)(d))
}