-
Notifications
You must be signed in to change notification settings - Fork 1
/
charm.go
458 lines (395 loc) · 10.4 KB
/
charm.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package charm
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"path/filepath"
"time"
iofs "io/fs"
"github.com/charmbracelet/charm/client"
"github.com/charmbracelet/charm/crypt"
charmfs "github.com/charmbracelet/charm/fs"
"github.com/rclone/rclone/fs"
"github.com/rclone/rclone/fs/config/configmap"
"github.com/rclone/rclone/fs/config/configstruct"
"github.com/rclone/rclone/fs/hash"
"golang.org/x/sync/errgroup"
)
var (
timeUnset = time.Unix(0, 0)
// Check the interfaces are satisfied
_ fs.Fs = &Fs{}
_ fs.PutStreamer = &Fs{}
_ fs.Object = &Object{}
_ fs.MimeTyper = &Object{}
)
var (
errNotSupported = errors.New("not supported")
)
func init() {
fsi := &fs.RegInfo{
Name: "charm",
Description: "Charm Server FS",
NewFs: NewFs,
Options: []fs.Option{{
Name: "url",
Help: "URL of your Charm server.",
Required: false,
}},
}
fs.Register(fsi)
}
// Options defines the configuration for this backend
type Options struct {
Endpoint string `config:"url"`
}
// Fs stores the interface to the remote CharmFS files
type Fs struct {
name string
root string
features *fs.Features // optional features
opt Options // options for this backend
ci *fs.ConfigInfo // global config
cfs *charmfs.FS
charmClient *client.Client
crypt *crypt.Crypt
endpoint *url.URL
ctx context.Context
}
// Object is a remote object that has been stat'd
type Object struct {
fs *Fs
remote string
modTime time.Time
contentType string
info iofs.FileInfo
}
// NewFs creates a new Fs object from the name and root. It connects to
// the host specified in the config file.
func NewFs(ctx context.Context, name, root string, m configmap.Mapper) (fs.Fs, error) {
// Parse config into Options struct
opt := new(Options)
err := configstruct.Set(m, opt)
if err != nil {
return nil, err
}
// Parse the endpoint and stick the root onto it
base, err := url.Parse(opt.Endpoint)
if err != nil {
return nil, err
}
charmClient, err := client.NewClientWithDefaults()
if err != nil {
return nil, err
}
cfs, err := charmfs.NewFSWithClient(charmClient)
if err != nil {
return nil, err
}
crypt, err := crypt.NewCrypt()
if err != nil {
return nil, err
}
ci := fs.GetConfig(ctx)
ci.IgnoreSize = true
f := &Fs{
name: name,
root: root,
opt: *opt,
ctx: ctx,
ci: ci,
cfs: cfs,
crypt: crypt,
endpoint: base,
charmClient: charmClient,
}
f.features = (&fs.Features{
CanHaveEmptyDirectories: true,
}).Fill(ctx, f)
o, err := f.NewObject(ctx, root)
if err != nil {
return f, err
}
if o.(*Object).info == nil {
nr := filepath.Dir(root)
if nr == "." {
nr = ""
}
f.root = nr
return f, nil
}
if !o.(*Object).info.IsDir() {
nr := filepath.Dir(root)
if nr == "." {
nr = ""
}
f.root = nr
fs.Infof(f, "root is file, new root: %s", f.root)
return f, fs.ErrorIsFile
}
return f, nil
}
func (f *Fs) Name() string {
return f.name
}
func (f *Fs) Root() string {
return f.root
}
func (f *Fs) String() string {
return f.endpoint.String()
}
func (f *Fs) Features() *fs.Features {
return f.features
}
func (f *Fs) Precision() time.Duration {
return time.Second
}
func (f *Fs) NewObject(ctx context.Context, remote string) (fs.Object, error) {
o := &Object{
fs: f,
remote: remote,
}
err := o.stat(ctx)
if err != nil {
fs.Debugf(f, "failed to stat object %s", remote)
}
return o, nil
}
func (f *Fs) List(ctx context.Context, dir string) (entries fs.DirEntries, err error) {
centries, err := f.cfs.ReadDir(filepath.Join(f.root, dir))
if err != nil {
return nil, err
}
// Charm returns an empty list without error if the directory does
// not exist
// https://github.com/charmbracelet/charm/blob/9d0f28b6e656e8b22170a3ab12f5121d7c72b8ea/fs/fs.go#L289
if len(centries) == 0 {
return nil, fs.ErrorDirNotFound
}
for _, ce := range centries {
var entry fs.DirEntry
path := ce.Name()
df := filepath.Join(dir, path)
if ce.IsDir() {
entry = fs.NewDir(df, timeUnset)
} else {
entry = &Object{
fs: f,
remote: df,
}
}
entries = append(entries, entry)
}
return entries, nil
}
// Put in to the remote path with the modTime given of the given size
//
// May create the object even if it returns an error - if so
// will return the object and the error, otherwise will return
// nil and the error
func (f *Fs) Put(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
// Temporary object under construction
o := &Object{
fs: f,
remote: src.Remote(),
}
err := o.Update(ctx, in, src, options...)
if err != nil {
return nil, err
}
return o, nil
}
// PutStream uploads to the remote path with the modTime given of indeterminate size
func (f *Fs) PutStream(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) (fs.Object, error) {
return nil, errNotSupported
}
// Fs is the filesystem this remote file object is located within
func (o *Object) Fs() fs.Info {
return o.fs
}
// String returns the URL to the remote HTTP file
func (o *Object) String() string {
if o == nil {
return "<nil>"
}
return o.remote
}
// Remote the name of the remote HTTP file, relative to the fs root
func (o *Object) Remote() string {
return o.remote
}
// Hash returns "" since HTTP (in Go or OpenSSH) doesn't support remote calculation of hashes
func (o *Object) Hash(ctx context.Context, r hash.Type) (string, error) {
return "", hash.ErrUnsupported
}
// Size returns the remote object size
//
// Files are encrypted in CharmFS so it's not possible to compare
// source object size (plaintext) with destination object file (ciphertext)
// without reading the remote object and decrypting it first.
func (o *Object) Size() int64 {
if o.info == nil {
return -1
}
return o.info.Size()
}
// ModTime returns the modification time of the remote file
func (o *Object) ModTime(ctx context.Context) time.Time {
return o.modTime
}
// stat updates the info field in the Object
func (o *Object) stat(ctx context.Context) error {
fs.Infof(o, "stat root: %s, remote: %s", o.fs.root, o.remote)
ro, err := o.fs.cfs.Open(o.fs.root)
if err != nil {
return err
}
o.info, err = ro.Stat()
if err != nil {
return err
}
return nil
}
// SetModTime sets the modification and access time to the specified time
//
// it also updates the info field
func (o *Object) SetModTime(ctx context.Context, modTime time.Time) error {
return errNotSupported
}
// Storable returns whether the remote file is a regular file (not a directory, symbolic link, block device, character device, named pipe, etc.)
func (o *Object) Storable() bool {
return true
}
// Open a remote file object for reading. Seek is supported
func (o *Object) Open(ctx context.Context, options ...fs.OpenOption) (in io.ReadCloser, err error) {
in, err = o.fs.cfs.Open(o.remote)
if err != nil {
return nil, err
}
return in, err
}
// Hashes returns hash.HashNone to indicate remote hashing is unavailable
func (f *Fs) Hashes() hash.Set {
return hash.Set(hash.None)
}
// Mkdir makes the root directory of the Fs object
func (f *Fs) Mkdir(ctx context.Context, dir string) error {
return errNotSupported
}
// Remove a remote file object
func (o *Object) Remove(ctx context.Context) error {
opath := filepath.Join(o.fs.root, o.remote)
fs.Infof(o, "removing object %s", opath)
return o.fs.cfs.Remove(opath)
}
// Rmdir removes the root directory of the Fs object
func (f *Fs) Rmdir(ctx context.Context, dir string) error {
centries, err := f.cfs.ReadDir(dir)
if err != nil {
return err
}
if len(centries) > 0 {
return fs.ErrorDirectoryNotEmpty
}
return nil
}
// Update in to the object with the modTime given of the given size
func (o *Object) Update(ctx context.Context, in io.Reader, src fs.ObjectInfo, options ...fs.OpenOption) error {
fs.Infof(o, "update remote: %s, root: %s ", o.remote, o.fs.root)
err := o.fs.writeFile(ctx, filepath.Join(o.fs.root, o.remote), in)
if err != nil {
return err
}
return nil
}
// MimeType of an Object if known, "" otherwise
func (o *Object) MimeType(ctx context.Context) string {
return o.contentType
}
// From https://github.com/charmbracelet/charm/blob/9d0f28b6e656e8b22170a3ab12f5121d7c72b8ea/fs/fs.go#L184
//
// Takes a io.Reader instead of an fs.File, as we can't always stat stat
// remote objects.
func (f *Fs) writeFile(ctx context.Context, name string, in io.Reader) error {
fs.Infof(f, "writing remote file %s", name)
ebuf := bytes.NewBuffer(nil)
eb, err := f.crypt.NewEncryptedWriter(ebuf)
if err != nil {
return err
}
if _, err = io.Copy(eb, in); err != nil {
return err
}
if err := eb.Close(); err != nil {
return err
}
eb.Close() //nolint:errcheck
databuf := bytes.NewBuffer(nil)
w := multipart.NewWriter(databuf)
if _, err := w.CreateFormFile("data", name); err != nil {
return err
}
headlen := databuf.Len()
header := make([]byte, headlen)
if _, err := databuf.Read(header); err != nil {
return err
}
if err := w.Close(); err != nil {
return err
}
w.Close() //nolint:errcheck
bounlen := databuf.Len()
boun := make([]byte, bounlen)
if _, err := databuf.Read(boun); err != nil {
return err
}
// headlen is the length of the multipart part header, bounlen is the length of the multipart boundary footer.
contentLength := int64(headlen) + int64(ebuf.Len()) + int64(bounlen)
// pipe the multipart request to the server
rr, rw := io.Pipe()
defer rr.Close() // nolint:errcheck
errs, _ := errgroup.WithContext(ctx)
errs.Go(func() error {
defer rw.Close() // nolint:errcheck
// write multipart header
if _, err := rw.Write(header); err != nil {
return err
}
// chunk the read data into 64MB chunks
buf := make([]byte, 1024*1024*64)
for {
n, err := ebuf.Read(buf)
if err != nil {
break
}
if _, err := rw.Write(buf[:n]); err != nil {
return err
}
}
// write multipart boundary
_, err := rw.Write(boun)
return err
})
ep, err := f.cfs.EncryptPath(name)
if err != nil {
return err
}
// FIXME: mode is hardcoded here, not sure if there's a way to get mode
// from every src object
path := fmt.Sprintf("/v1/fs/%s?mode=436", ep)
headers := http.Header{
"Content-Type": {w.FormDataContentType()},
"Content-Length": {fmt.Sprintf("%d", contentLength)},
}
resp, err := f.charmClient.AuthedRequest("POST", path, headers, rr)
if err != nil {
return err
}
defer resp.Body.Close()
return errs.Wait()
}