-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbranch.go
766 lines (559 loc) · 15.8 KB
/
branch.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
* Rufs - Remote Union File System
*
* Copyright 2017 Matthias Ladkau. All rights reserved.
*
* This Source Code Form is subject to the terms of the MIT
* License, If a copy of the MIT License was not distributed with this
* file, You can obtain one at https://opensource.org/licenses/MIT.
*/
/*
Package rufs contains the main API to Rufs.
Rufs is organized as a collection of branches. Each branch represents a physical
file system structure which can be queried and updated by an authorized client.
On the client side one or several branches are organized into a tree. The
single branches can overlay each other. For example:
Branch A
/foo/A
/foo/B
/bar/C
Branch B
/foo/C
/test/D
Tree 1
/myspace => Branch A, Branch B
Accessing tree with:
/myspace/foo/A gets file /foo/A from Branch A while
/myspace/foo/C gets file /foo/C from Branch B
Write operations go only to branches which are mapped as writing branches
and who accept them (i.e. are not set to readonly on the side of the branch).
*/
package rufs
import (
"bytes"
"crypto/tls"
"encoding/gob"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"devt.de/krotik/common/errorutil"
"devt.de/krotik/common/fileutil"
"devt.de/krotik/common/pools"
"devt.de/krotik/common/stringutil"
"devt.de/krotik/rufs/config"
"devt.de/krotik/rufs/node"
)
func init() {
// Make sure we can use the relevant types in a gob operation
gob.Register([][]os.FileInfo{})
gob.Register(&FileInfo{})
}
/*
Branch models a single exported branch in Rufs.
*/
type Branch struct {
rootPath string // Local directory (absolute path) modeling the branch root
node *node.RufsNode // Local RPC node
readonly bool // Flag if this branch is readonly
}
/*
NewBranch returns a new exported branch.
*/
func NewBranch(cfg map[string]interface{}, cert *tls.Certificate) (*Branch, error) {
var err error
var b *Branch
// Make sure the given config is ok
if err = config.CheckBranchExportConfig(cfg); err == nil {
// Create RPC server
addr := fmt.Sprintf("%v:%v", fileutil.ConfStr(cfg, config.RPCHost),
fileutil.ConfStr(cfg, config.RPCPort))
rn := node.NewNode(addr, fileutil.ConfStr(cfg, config.BranchName),
fileutil.ConfStr(cfg, config.BranchSecret), cert, nil)
// Start the rpc server
if err = rn.Start(cert); err == nil {
var rootPath string
// Construct root path
if rootPath, err = filepath.Abs(fileutil.ConfStr(cfg, config.LocalFolder)); err == nil {
b = &Branch{rootPath, rn, fileutil.ConfBool(cfg, config.EnableReadOnly)}
rn.DataHandler = b.requestHandler
}
}
}
return b, err
}
/*
Name returns the name of the branch.
*/
func (b *Branch) Name() string {
return b.node.Name()
}
/*
SSLFingerprint returns the SSL fingerprint of the branch.
*/
func (b *Branch) SSLFingerprint() string {
return b.node.SSLFingerprint()
}
/*
Shutdown shuts the branch down.
*/
func (b *Branch) Shutdown() error {
return b.node.Shutdown()
}
/*
IsReadOnly returns if this branch is read-only.
*/
func (b *Branch) IsReadOnly() bool {
return b.readonly
}
/*
checkReadOnly returns an error if this branch is read-only.
*/
func (b *Branch) checkReadOnly() error {
var err error
if b.IsReadOnly() {
err = fmt.Errorf("Branch %v is read-only", b.Name())
}
return err
}
// Branch API
// ==========
/*
Dir returns file listings matching a given pattern of one or more directories.
The contents of the given path is returned along with checksums if the checksum
flag is specified. Optionally, also the contents of all subdirectories can be
returned if the recursive flag is set. The return values is a list of traversed
directories (platform-agnostic) and their corresponding contents.
*/
func (b *Branch) Dir(spath string, pattern string, recursive bool, checksums bool) ([]string, [][]os.FileInfo, error) {
var fis []os.FileInfo
// Compile pattern
re, err := regexp.Compile(pattern)
if err != nil {
return nil, nil, err
}
createRufsFileInfos := func(dirname string, afis []os.FileInfo) []os.FileInfo {
var fis []os.FileInfo
fis = make([]os.FileInfo, 0, len(afis))
for _, fi := range afis {
// Append if it matches the pattern
if re.MatchString(fi.Name()) {
fis = append(fis, fi)
}
}
// Wrap normal file infos and calculate checksum if necessary
ret := WrapFileInfos(dirname, fis)
if checksums {
for _, fi := range fis {
if !fi.IsDir() {
// The sum is either there or not ... - access errors should
// be caught when trying to read the file
sum, _ := fileutil.CheckSumFileFast(filepath.Join(dirname, fi.Name()))
fi.(*FileInfo).FiChecksum = sum
}
}
}
return ret
}
subPath, err := b.constructSubPath(spath)
if err == nil {
if !recursive {
if fis, err = ioutil.ReadDir(subPath); err == nil {
return []string{spath},
[][]os.FileInfo{createRufsFileInfos(subPath, fis)}, nil
}
} else {
var rpaths []string
var rfis [][]os.FileInfo
var addSubDir func(string, string) error
// Recursive function to walk directories and symlinks
// in a platform-agnostic way
addSubDir = func(p string, rp string) error {
fis, err = ioutil.ReadDir(p)
if err == nil {
rpaths = append(rpaths, rp)
rfis = append(rfis, createRufsFileInfos(p, fis))
for _, fi := range fis {
if err == nil && fi.IsDir() {
err = addSubDir(filepath.Join(p, fi.Name()),
path.Join(rp, fi.Name()))
}
}
}
return err
}
if err = addSubDir(subPath, spath); err == nil {
return rpaths, rfis, nil
}
}
}
// Ignore any not exists errors
if os.IsNotExist(err) {
err = nil
}
return nil, nil, err
}
/*
ReadFileToBuffer reads a complete file into a given buffer which implements
io.Writer.
*/
func (b *Branch) ReadFileToBuffer(spath string, buf io.Writer) error {
var n int
var err error
var offset int64
readBuf := make([]byte, DefaultReadBufferSize)
for err == nil {
n, err = b.ReadFile(spath, readBuf, offset)
if err == nil {
_, err = buf.Write(readBuf[:n])
offset += int64(n)
} else if IsEOF(err) {
// We reached the end of the file
err = nil
break
}
}
return err
}
/*
ReadFile reads up to len(p) bytes into p from the given offset. It
returns the number of bytes read (0 <= n <= len(p)) and any error
encountered.
*/
func (b *Branch) ReadFile(spath string, p []byte, offset int64) (int, error) {
var n int
subPath, err := b.constructSubPath(spath)
if err == nil {
var fi os.FileInfo
if fi, err = os.Stat(subPath); err == nil {
if fi.IsDir() {
err = fmt.Errorf("read /%v: is a directory", spath)
} else if err == nil {
var f *os.File
if f, err = os.Open(subPath); err == nil {
defer f.Close()
sr := io.NewSectionReader(f, 0, fi.Size())
if _, err = sr.Seek(offset, io.SeekStart); err == nil {
n, err = sr.Read(p)
}
}
}
}
}
return n, err
}
/*
WriteFileFromBuffer writes a complete file from a given buffer which implements
io.Reader.
*/
func (b *Branch) WriteFileFromBuffer(spath string, buf io.Reader) error {
var err error
var offset int64
if err = b.checkReadOnly(); err == nil {
writeBuf := make([]byte, DefaultReadBufferSize)
for err == nil {
var n int
if n, err = buf.Read(writeBuf); err == nil {
_, err = b.WriteFile(spath, writeBuf[:n], offset)
offset += int64(n)
} else if IsEOF(err) {
// We reached the end of the file
b.WriteFile(spath, []byte{}, offset)
err = nil
break
}
}
}
return err
}
/*
WriteFile writes p into the given file from the given offset. It
returns the number of written bytes and any error encountered.
*/
func (b *Branch) WriteFile(spath string, p []byte, offset int64) (int, error) {
var n int
var m int64
if err := b.checkReadOnly(); err != nil {
return 0, err
}
buf := byteSlicePool.Get().([]byte)
defer func() {
byteSlicePool.Put(buf)
}()
growFile := func(f *os.File, n int64) {
var err error
toWrite := n
for err == nil && toWrite > 0 {
if toWrite > int64(DefaultReadBufferSize) {
_, err = f.Write(buf[:DefaultReadBufferSize])
toWrite -= int64(DefaultReadBufferSize)
} else {
_, err = f.Write(buf[:toWrite])
toWrite = 0
}
}
}
subPath, err := b.constructSubPath(spath)
if err == nil {
var fi os.FileInfo
var f *os.File
if fi, err = os.Stat(subPath); os.IsNotExist(err) {
// Ensure path exists
dir, _ := filepath.Split(subPath)
if err = os.MkdirAll(dir, 0755); err == nil {
// Create the file newly
if f, err = os.OpenFile(subPath, os.O_RDWR|os.O_CREATE, 0644); err == nil {
defer f.Close()
if offset > 0 {
growFile(f, offset)
}
m, err = io.Copy(f, bytes.NewBuffer(p))
n += int(m)
}
}
} else if err == nil {
// File does exist
if f, err := os.OpenFile(subPath, os.O_RDWR, 0644); err == nil {
defer f.Close()
if fi.Size() < offset {
f.Seek(fi.Size(), io.SeekStart)
growFile(f, offset-fi.Size())
} else {
f.Seek(offset, io.SeekStart)
}
m, err = io.Copy(f, bytes.NewBuffer(p))
errorutil.AssertOk(err)
n += int(m)
}
}
}
return n, err
}
/*
ItemOp parameter
*/
const (
ItemOpAction = "itemop_action" // ItemOp action
ItemOpName = "itemop_name" // Item name
ItemOpNewName = "itemop_newname" // New item name
)
/*
ItemOp actions
*/
const (
ItemOpActRename = "rename" // Rename a file or directory
ItemOpActDelete = "delete" // Delete a file or directory
ItemOpActMkDir = "mkdir" // Create a directory
)
/*
ItemOp executes a file or directory specific operation which can either
succeed or fail (e.g. rename or delete). Actions and parameters should
be given in the opdata map.
*/
func (b *Branch) ItemOp(spath string, opdata map[string]string) (bool, error) {
res := false
if err := b.checkReadOnly(); err != nil {
return false, err
}
subPath, err := b.constructSubPath(spath)
if err == nil {
action := opdata[ItemOpAction]
fileFromOpData := func(key string) (string, error) {
// Make sure we are only dealing with files
_, name := filepath.Split(opdata[key])
if name == "" {
return "", fmt.Errorf("This operation requires a specific file or directory")
}
// Build the relative paths
return filepath.Join(filepath.FromSlash(subPath), name), nil
}
if action == ItemOpActMkDir {
var name string
// Make directory action
if name, err = fileFromOpData(ItemOpName); err == nil {
err = os.MkdirAll(name, 0755)
}
} else if action == ItemOpActRename {
var name, newname string
// Rename action
if name, err = fileFromOpData(ItemOpName); err == nil {
if newname, err = fileFromOpData(ItemOpNewName); err == nil {
err = os.Rename(name, newname)
}
}
} else if action == ItemOpActDelete {
var name string
// Delete action
if name, err = fileFromOpData(ItemOpName); err == nil {
del := func(name string) error {
var err error
if ok, _ := fileutil.PathExists(name); ok {
err = os.RemoveAll(name)
} else {
err = os.ErrNotExist
}
return err
}
if strings.Contains(name, "*") {
var rex string
// We have a wildcard
rootdir, glob := filepath.Split(name)
// Create a regex from the given glob expression
if rex, err = stringutil.GlobToRegex(glob); err == nil {
var dirs []string
var fis [][]os.FileInfo
if dirs, fis, err = b.Dir(spath, rex, true, false); err == nil {
for i, dir := range dirs {
// Remove all files and dirs according to the wildcard
for _, fi := range fis[i] {
os.RemoveAll(filepath.Join(rootdir,
filepath.FromSlash(dir), fi.Name()))
}
}
}
}
} else {
err = del(name)
}
}
}
// Determine if we succeeded
res = err == nil || os.IsNotExist(err)
}
return res, err
}
// Request handling functions
// ==========================
/*
DefaultReadBufferSize is the default size for file reading.
*/
var DefaultReadBufferSize = 1024 * 16
/*
bufferPool holds buffers which are used to marshal objects.
*/
var bufferPool = pools.NewByteBufferPool()
/*
byteSlicePool holds buffers which are used to read files
*/
var byteSlicePool = pools.NewByteSlicePool(DefaultReadBufferSize)
/*
Meta parameter
*/
const (
ParamAction = "a" // Requested action
ParamPath = "p" // Path string
ParamPattern = "x" // Pattern string
ParamRecursive = "r" // Recursive flag
ParamChecksums = "c" // Checksum flag
ParamOffset = "o" // Offset parameter
ParamSize = "s" // Size parameter
)
/*
Possible actions
*/
const (
OpDir = "dir" // Read the contents of a path
OpRead = "read" // Read the contents of a file
OpWrite = "write" // Read the contents of a file
OpItemOp = "itemop" // File or directory operation
)
/*
requestHandler handles incoming requests from other branches or trees.
*/
func (b *Branch) requestHandler(ctrl map[string]string, data []byte) ([]byte, error) {
var err error
var res interface{}
var ret []byte
action := ctrl[ParamAction]
// Handle operation requests
if action == OpDir {
var dirs []string
var fis [][]os.FileInfo
dir := ctrl[ParamPath]
pattern := ctrl[ParamPattern]
rec := strings.ToLower(ctrl[ParamRecursive]) == "true"
sum := strings.ToLower(ctrl[ParamChecksums]) == "true"
if dirs, fis, err = b.Dir(dir, pattern, rec, sum); err == nil {
res = []interface{}{dirs, fis}
}
} else if action == OpItemOp {
res, err = b.ItemOp(ctrl[ParamPath], ctrl)
} else if action == OpRead {
var size, n int
var offset int64
spath := ctrl[ParamPath]
if size, err = strconv.Atoi(ctrl[ParamSize]); err == nil {
if offset, err = strconv.ParseInt(ctrl[ParamOffset], 10, 64); err == nil {
buf := byteSlicePool.Get().([]byte)
defer func() {
byteSlicePool.Put(buf)
}()
if len(buf) < size {
// Constantly requesting bigger buffers will
// eventually replace all default sized buffers
buf = make([]byte, size)
}
if n, err = b.ReadFile(spath, buf[:size], offset); err == nil {
res = []interface{}{n, buf[:size]}
}
}
}
} else if action == OpWrite {
var offset int64
spath := ctrl[ParamPath]
if offset, err = strconv.ParseInt(ctrl[ParamOffset], 10, 64); err == nil {
res, err = b.WriteFile(spath, data, offset)
}
}
// Send the response
if err == nil {
// Allocate a new encoding buffer - no need to lock as
// it is based on sync.Pool
// Pooled encoding buffers are used to keep expensive buffer
// reallocations to a minimum. It is better to allocate the
// actual response buffer once the response size is known.
bb := bufferPool.Get().(*bytes.Buffer)
if err = gob.NewEncoder(bb).Encode(res); err == nil {
toSend := bb.Bytes()
// Allocate the response array
ret = make([]byte, len(toSend))
// Copy encoded result into the response array
copy(ret, toSend)
}
// Return the encoding buffer back to the pool
go func() {
bb.Reset()
bufferPool.Put(bb)
}()
}
if err != nil {
// Ensure we don't leak local paths - this might not work in
// all situations and depends on the underlying os. In this
// error messages might include information on the full local
// path in error messages.
absRoot, _ := filepath.Abs(b.rootPath)
err = fmt.Errorf("%v", strings.Replace(err.Error(), absRoot, "", -1))
}
return ret, err
}
// Util functions
// ==============
func (b *Branch) constructSubPath(rpath string) (string, error) {
// Produce the actual subpath - this should also produce windows
// paths correctly (i.e. foo/bar -> C:\root\foo\bar)
subPath := filepath.Join(b.rootPath, filepath.FromSlash(rpath))
// Check that the new sub path is under the root path
absSubPath, err := filepath.Abs(subPath)
if err == nil {
if strings.HasPrefix(absSubPath, b.rootPath) {
return subPath, nil
}
err = fmt.Errorf("Requested path %v is outside of the branch", rpath)
}
return "", err
}