-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdput.go
259 lines (236 loc) · 6.07 KB
/
cmdput.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
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
// Copyright 2021 Michael J. Fromberger. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmdput
import (
"context"
"flag"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"strings"
"time"
"github.com/creachadair/command"
"github.com/creachadair/ffs/blob"
"github.com/creachadair/ffs/cmd/ffs/config"
"github.com/creachadair/ffs/file"
"github.com/creachadair/taskgroup"
"github.com/pkg/xattr"
)
var putFlags struct {
Stat bool
XAttr bool
Verbose bool
}
var Command = &command.C{
Name: "put",
Usage: "<path> ...",
Help: `Write file and directory contents to the store.
Add each specified path to the store and print its storage key.
`,
SetFlags: func(_ *command.Env, fs *flag.FlagSet) {
fs.BoolVar(&putFlags.Stat, "stat", false, "Capture file and directory stat")
fs.BoolVar(&putFlags.XAttr, "xattr", false, "Capture extended attributes")
fs.BoolVar(&putFlags.Verbose, "v", false, "Enable verbose logging")
},
Run: runPut,
}
func runPut(env *command.Env, args []string) error {
if len(args) == 0 {
return env.Usagef("missing required path")
}
cfg := env.Config.(*config.Settings)
return cfg.WithStore(cfg.Context, func(s blob.CAS) error {
keys := make([]string, len(args))
for i, path := range args {
if putFlags.Verbose {
log.Printf("put %q", path)
}
f, err := putDir(cfg.Context, s, path)
if err != nil {
return err
}
key, err := f.Flush(cfg.Context)
if err != nil {
return err
}
keys[i] = key
if putFlags.Verbose {
log.Printf("finished %q (%x)", path, key)
}
}
for _, key := range keys {
fmt.Printf("%x\n", key)
}
return nil
})
}
// putFile puts a single file or symlink into the store.
// The caller is responsible for closing in after putFile returns.
func putFile(ctx context.Context, s blob.CAS, path string, fi fs.FileInfo) (*file.File, error) {
f := file.New(s, &file.NewOptions{
Name: fi.Name(),
Stat: fileInfoToStat(fi),
})
// Extended attributes (if -xattr is set)
if err := addExtAttrs(path, f); err != nil {
return nil, err
}
if fi.Mode().IsRegular() {
// Copy file contents.
in, err := os.Open(path)
if err != nil {
return nil, err
}
defer in.Close()
if err := f.SetData(ctx, in); err != nil {
return nil, fmt.Errorf("copying data: %w", err)
}
} else if fi.Mode()&fs.ModeSymlink != 0 {
// Write symbolic link target as file content.
tgt, err := os.Readlink(path)
if err != nil {
return nil, err
} else if err := f.SetData(ctx, strings.NewReader(tgt)); err != nil {
return nil, err
}
}
return f, nil
}
// putDir puts a single file, directory, or symlink into the store.
// If path names a plain file or symlin, it calls putFile.
func putDir(ctx context.Context, s blob.CAS, path string) (*file.File, error) {
fi, err := os.Lstat(path)
if err != nil {
return nil, err
}
if !fi.IsDir() {
// Non-directory files, symlinks, etc.
return putFile(ctx, s, path, fi)
}
if putFlags.Verbose {
log.Printf("enter %q", path)
}
// Directory
d := file.New(s, &file.NewOptions{
Name: fi.Name(),
Stat: fileInfoToStat(fi),
})
// Extended attributes (if -xattr is set)
if err := addExtAttrs(path, d); err != nil {
return nil, err
}
// Children
elts, err := os.ReadDir(path)
if err != nil {
return nil, err
}
type entry struct {
sub string
name string
fi fs.FileInfo
kid *file.File
}
// Partition the contents of the directory into plain files and directories.
var files, dirs []*entry
for _, elt := range elts {
sub := filepath.Join(path, elt.Name())
if elt.IsDir() {
dirs = append(dirs, &entry{sub: sub, name: elt.Name()})
} else if t := elt.Type(); t != 0 && (t&fs.ModeSymlink == 0) {
continue // e.g., socket, pipe, device, fifo, etc.
} else if fi, err = elt.Info(); err != nil {
return nil, err
} else {
files = append(files, &entry{sub: sub, name: elt.Name(), fi: fi})
}
}
// Process subdirectories serially. We do this so that the recurrence does
// not explode concurrency.
for _, e := range dirs {
kid, err := putDir(ctx, s, e.sub)
if err != nil {
return nil, err
}
d.Child().Set(e.name, kid)
}
// Process plain files in parallel.
if len(files) != 0 {
if putFlags.Verbose {
log.Printf("in %q: storing %d files", path, len(files))
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
g, start := taskgroup.New(taskgroup.Trigger(cancel)).Limit(64)
for _, e := range files {
e := e
start(func() error {
if putFlags.Verbose {
log.Printf("copying %d bytes from %q", e.fi.Size(), e.name)
if e.fi.Size() > 1<<20 {
st := time.Now()
defer func() {
log.Printf("finished %q [%v elapsed]",
e.name, time.Since(st).Truncate(time.Millisecond))
}()
}
}
kid, err := putFile(ctx, s, e.sub, e.fi)
if err != nil {
return err
}
e.kid = kid
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
for _, e := range files {
d.Child().Set(e.name, e.kid)
}
}
return d, nil
}
func addExtAttrs(path string, f *file.File) error {
if !putFlags.XAttr {
return nil
}
names, err := xattr.LList(path)
if err != nil {
return fmt.Errorf("listing xattr: %w", err)
}
xa := f.XAttr()
for _, name := range names {
data, err := xattr.LGet(path, name)
if err != nil {
return fmt.Errorf("get xattr %q: %w", name, err)
}
xa.Set(name, string(data))
}
return nil
}
func fileInfoToStat(fi fs.FileInfo) *file.Stat {
if !putFlags.Stat {
return nil
}
owner, group := ownerAndGroup(fi)
return &file.Stat{
Mode: fi.Mode(),
ModTime: fi.ModTime(),
OwnerID: owner,
GroupID: group,
}
}