-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy patheosbinary.go
1148 lines (998 loc) · 33.7 KB
/
eosbinary.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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018-2021 CERN
//
// 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.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
package eosbinary
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/eosclient"
"github.com/cs3org/reva/pkg/errtypes"
"github.com/cs3org/reva/pkg/storage/utils/acl"
"github.com/google/uuid"
"github.com/pkg/errors"
"go.opencensus.io/trace"
)
const (
versionPrefix = ".sys.v#."
lwShareAttrKey = "reva.lwshare"
userACLEvalKey = "eval.useracl"
)
const (
// SystemAttr is the system extended attribute.
SystemAttr eosclient.AttrType = iota
// UserAttr is the user extended attribute.
UserAttr
)
func serializeAttribute(a *eosclient.Attribute) string {
return fmt.Sprintf("%s.%s=%s", attrTypeToString(a.Type), a.Key, a.Val)
}
func attrTypeToString(at eosclient.AttrType) string {
switch at {
case SystemAttr:
return "sys"
case UserAttr:
return "user"
default:
return "invalid"
}
}
func attrStringToType(t string) (eosclient.AttrType, error) {
switch t {
case "sys":
return SystemAttr, nil
case "user":
return UserAttr, nil
default:
return 0, errtypes.InternalError("attr type not existing")
}
}
func isValidAttribute(a *eosclient.Attribute) bool {
// validate that an attribute is correct.
if (a.Type != SystemAttr && a.Type != UserAttr) || a.Key == "" {
return false
}
return true
}
// Options to configure the Client.
type Options struct {
// ForceSingleUserMode forces all connections to use only one user.
// This is the case when access to EOS is done from FUSE under apache or www-data.
ForceSingleUserMode bool
// UseKeyTabAuth changes will authenticate requests by using an EOS keytab.
UseKeytab bool
// Whether to maintain the same inode across various versions of a file.
// Requires extra metadata operations if set to true
VersionInvariant bool
// SingleUsername is the username to use when connecting to EOS.
// Defaults to apache
SingleUsername string
// Location of the eos binary.
// Default is /usr/bin/eos.
EosBinary string
// Location of the xrdcopy binary.
// Default is /opt/eos/xrootd/bin/xrdcopy.
XrdcopyBinary string
// URL of the EOS MGM.
// Default is root://eos-example.org
URL string
// Location on the local fs where to store reads.
// Defaults to os.TempDir()
CacheDirectory string
// Keytab is the location of the EOS keytab file.
Keytab string
// SecProtocol is the comma separated list of security protocols used by xrootd.
// For example: "sss, unix"
SecProtocol string
// TokenExpiry stores in seconds the time after which generated tokens will expire
// Default is 3600
TokenExpiry int
}
func (opt *Options) init() {
if opt.ForceSingleUserMode && opt.SingleUsername != "" {
opt.SingleUsername = "apache"
}
if opt.EosBinary == "" {
opt.EosBinary = "/usr/bin/eos"
}
if opt.XrdcopyBinary == "" {
opt.XrdcopyBinary = "/opt/eos/xrootd/bin/xrdcopy"
}
if opt.URL == "" {
opt.URL = "root://eos-example.org"
}
if opt.CacheDirectory == "" {
opt.CacheDirectory = os.TempDir()
}
}
// Client performs actions against a EOS management node (MGM).
// It requires the eos-client and xrootd-client packages installed to work.
type Client struct {
opt *Options
}
// New creates a new client with the given options.
func New(opt *Options) (*Client, error) {
opt.init()
c := new(Client)
c.opt = opt
return c, nil
}
// executeXRDCopy executes xrdcpy commands and returns the stdout, stderr and return code
func (c *Client) executeXRDCopy(ctx context.Context, cmdArgs []string) (string, string, error) {
log := appctx.GetLogger(ctx)
outBuf := &bytes.Buffer{}
errBuf := &bytes.Buffer{}
cmd := exec.CommandContext(ctx, c.opt.XrdcopyBinary, cmdArgs...)
cmd.Stdout = outBuf
cmd.Stderr = errBuf
cmd.Env = []string{
"EOS_MGM_URL=" + c.opt.URL,
}
if c.opt.UseKeytab {
cmd.Env = append(cmd.Env, "XrdSecPROTOCOL="+c.opt.SecProtocol)
cmd.Env = append(cmd.Env, "XrdSecSSSKT="+c.opt.Keytab)
}
err := cmd.Run()
var exitStatus int
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
exitStatus = status.ExitStatus()
switch exitStatus {
case 0:
err = nil
case int(syscall.ENOENT):
err = errtypes.NotFound(errBuf.String())
}
}
}
args := fmt.Sprintf("%s", cmd.Args)
env := fmt.Sprintf("%s", cmd.Env)
log.Info().Str("args", args).Str("env", env).Int("exit", exitStatus).Msg("eos cmd")
if err != nil && exitStatus != int(syscall.ENOENT) { // don't wrap the errtypes.NotFoundError
err = errors.Wrap(err, "eosclient: error while executing command")
}
return outBuf.String(), errBuf.String(), err
}
// exec executes only EOS commands the command and returns the stdout, stderr and return code.
func (c *Client) executeEOS(ctx context.Context, cmdArgs []string, auth eosclient.Authorization) (string, string, error) {
log := appctx.GetLogger(ctx)
outBuf := &bytes.Buffer{}
errBuf := &bytes.Buffer{}
cmd := exec.CommandContext(ctx, c.opt.EosBinary)
cmd.Stdout = outBuf
cmd.Stderr = errBuf
cmd.Env = []string{
"EOS_MGM_URL=" + c.opt.URL,
}
if auth.Token != "" {
cmd.Env = append(cmd.Env, "EOSAUTHZ="+auth.Token)
} else if auth.Role.UID != "" && auth.Role.GID != "" {
cmd.Args = append(cmd.Args, []string{"-r", auth.Role.UID, auth.Role.GID}...)
}
if c.opt.UseKeytab {
cmd.Env = append(cmd.Env, "XrdSecPROTOCOL="+c.opt.SecProtocol)
cmd.Env = append(cmd.Env, "XrdSecSSSKT="+c.opt.Keytab)
}
cmd.Args = append(cmd.Args, cmdArgs...)
trace := trace.FromContext(ctx).SpanContext().TraceID.String()
cmd.Args = append(cmd.Args, "--comment", trace)
err := cmd.Run()
var exitStatus int
if exiterr, ok := err.(*exec.ExitError); ok {
// The program has exited with an exit code != 0
// This works on both Unix and Windows. Although package
// syscall is generally platform dependent, WaitStatus is
// defined for both Unix and Windows and in both cases has
// an ExitStatus() method with the same signature.
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
exitStatus = status.ExitStatus()
switch exitStatus {
case 0:
err = nil
case int(syscall.ENOENT):
err = errtypes.NotFound(errBuf.String())
case int(syscall.EPERM), int(syscall.E2BIG), int(syscall.EINVAL):
// eos reports back error code 1 (EPERM) when ?
// eos reports back error code 7 (E2BIG) when the user is not allowed to read the directory
// eos reports back error code 22 (EINVAL) when the user is not allowed to enter the instance
err = errtypes.PermissionDenied(errBuf.String())
}
}
}
args := fmt.Sprintf("%s", cmd.Args)
env := fmt.Sprintf("%s", cmd.Env)
log.Info().Str("args", args).Str("env", env).Int("exit", exitStatus).Str("err", errBuf.String()).Msg("eos cmd")
if err != nil && exitStatus != int(syscall.ENOENT) { // don't wrap the errtypes.NotFoundError
err = errors.Wrap(err, "eosclient: error while executing command")
}
return outBuf.String(), errBuf.String(), err
}
// AddACL adds an new acl to EOS with the given aclType.
func (c *Client) AddACL(ctx context.Context, auth, rootAuth eosclient.Authorization, path string, pos uint, a *acl.Entry) error {
finfo, err := c.GetFileInfoByPath(ctx, auth, path)
if err != nil {
return err
}
if a.Type == acl.TypeLightweight {
sysACL := ""
aclStr, ok := finfo.Attrs["sys."+lwShareAttrKey]
if ok {
acls, err := acl.Parse(aclStr, acl.ShortTextForm)
if err != nil {
return err
}
err = acls.SetEntry(a.Type, a.Qualifier, a.Permissions)
if err != nil {
return err
}
sysACL = acls.Serialize()
} else {
sysACL = a.CitrineSerialize()
}
sysACLAttr := &eosclient.Attribute{
Type: SystemAttr,
Key: lwShareAttrKey,
Val: sysACL,
}
if err = c.SetAttr(ctx, auth, sysACLAttr, finfo.IsDir, path); err != nil {
return err
}
return nil
}
sysACL := a.CitrineSerialize()
args := []string{"acl"}
if finfo.IsDir {
args = append(args, "--sys", "--recursive")
} else {
args = append(args, "--user")
userACLAttr := &eosclient.Attribute{
Type: SystemAttr,
Key: userACLEvalKey,
Val: "1",
}
if err = c.SetAttr(ctx, auth, userACLAttr, false, path); err != nil {
return err
}
}
// set position of ACLs to add. The default is to append to the end, so no arguments will be added in this case
// the first position starts at 1 = eosclient.StartPosition
if pos != eosclient.EndPosition {
args = append(args, "--position", fmt.Sprint(pos))
}
args = append(args, sysACL, path)
_, _, err = c.executeEOS(ctx, args, rootAuth)
return err
}
// RemoveACL removes the acl from EOS.
func (c *Client) RemoveACL(ctx context.Context, auth, rootAuth eosclient.Authorization, path string, a *acl.Entry) error {
finfo, err := c.GetFileInfoByPath(ctx, auth, path)
if err != nil {
return err
}
if a.Type == acl.TypeLightweight {
sysACL := ""
aclStr, ok := finfo.Attrs["sys."+lwShareAttrKey]
if ok {
acls, err := acl.Parse(aclStr, acl.ShortTextForm)
if err != nil {
return err
}
acls.DeleteEntry(a.Type, a.Qualifier)
if err != nil {
return err
}
sysACL = acls.Serialize()
} else {
sysACL = a.CitrineSerialize()
}
sysACLAttr := &eosclient.Attribute{
Type: SystemAttr,
Key: lwShareAttrKey,
Val: sysACL,
}
if err = c.SetAttr(ctx, auth, sysACLAttr, finfo.IsDir, path); err != nil {
return err
}
return nil
}
sysACL := a.CitrineSerialize()
args := []string{"acl"}
if finfo.IsDir {
args = append(args, "--sys", "--recursive")
} else {
args = append(args, "--user")
}
args = append(args, sysACL, path)
_, _, err = c.executeEOS(ctx, args, rootAuth)
return err
}
// UpdateACL updates the EOS acl.
func (c *Client) UpdateACL(ctx context.Context, auth, rootAuth eosclient.Authorization, path string, position uint, a *acl.Entry) error {
return c.AddACL(ctx, auth, rootAuth, path, position, a)
}
// GetACL for a file
func (c *Client) GetACL(ctx context.Context, auth eosclient.Authorization, path, aclType, target string) (*acl.Entry, error) {
acls, err := c.ListACLs(ctx, auth, path)
if err != nil {
return nil, err
}
for _, a := range acls {
if a.Type == aclType && a.Qualifier == target {
return a, nil
}
}
return nil, errtypes.NotFound(fmt.Sprintf("%s:%s", aclType, target))
}
// ListACLs returns the list of ACLs present under the given path.
// EOS returns uids/gid for Citrine version and usernames for older versions.
// For Citire we need to convert back the uid back to username.
func (c *Client) ListACLs(ctx context.Context, auth eosclient.Authorization, path string) ([]*acl.Entry, error) {
parsedACLs, err := c.getACLForPath(ctx, auth, path)
if err != nil {
return nil, err
}
// EOS Citrine ACLs are stored with uid. The UID will be resolved to the
// user opaque ID at the eosfs level.
return parsedACLs.Entries, nil
}
func (c *Client) getACLForPath(ctx context.Context, auth eosclient.Authorization, path string) (*acl.ACLs, error) {
finfo, err := c.GetFileInfoByPath(ctx, auth, path)
if err != nil {
return nil, err
}
return finfo.SysACL, nil
}
// GetFileInfoByInode returns the FileInfo by the given inode
func (c *Client) GetFileInfoByInode(ctx context.Context, auth eosclient.Authorization, inode uint64) (*eosclient.FileInfo, error) {
args := []string{"file", "info", fmt.Sprintf("inode:%d", inode), "-m"}
stdout, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return nil, err
}
info, err := c.parseFileInfo(stdout)
if err != nil {
return nil, err
}
if c.opt.VersionInvariant && isVersionFolder(info.File) {
info, err = c.getFileInfoFromVersion(ctx, auth, info.File)
if err != nil {
return nil, err
}
info.Inode = inode
}
return c.mergeParentACLsForFiles(ctx, auth, info), nil
}
// GetFileInfoByFXID returns the FileInfo by the given file id in hexadecimal
func (c *Client) GetFileInfoByFXID(ctx context.Context, auth eosclient.Authorization, fxid string) (*eosclient.FileInfo, error) {
args := []string{"file", "info", fmt.Sprintf("fxid:%s", fxid), "-m"}
stdout, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return nil, err
}
info, err := c.parseFileInfo(stdout)
if err != nil {
return nil, err
}
return c.mergeParentACLsForFiles(ctx, auth, info), nil
}
// GetFileInfoByPath returns the FilInfo at the given path
func (c *Client) GetFileInfoByPath(ctx context.Context, auth eosclient.Authorization, path string) (*eosclient.FileInfo, error) {
args := []string{"file", "info", path, "-m"}
stdout, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return nil, err
}
info, err := c.parseFileInfo(stdout)
if err != nil {
return nil, err
}
if c.opt.VersionInvariant && !isVersionFolder(path) && !info.IsDir {
if inode, err := c.getVersionFolderInode(ctx, auth, path); err == nil {
info.Inode = inode
}
}
return c.mergeParentACLsForFiles(ctx, auth, info), nil
}
func (c *Client) mergeParentACLsForFiles(ctx context.Context, auth eosclient.Authorization, info *eosclient.FileInfo) *eosclient.FileInfo {
// We need to inherit the ACLs for the parent directory as these are not available for files
if !info.IsDir {
parentInfo, err := c.GetFileInfoByPath(ctx, auth, path.Dir(info.File))
// Even if this call fails, at least return the current file object
if err == nil {
info.SysACL.Entries = append(info.SysACL.Entries, parentInfo.SysACL.Entries...)
}
}
return info
}
// SetAttr sets an extended attributes on a path.
func (c *Client) SetAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, recursive bool, path string) error {
if !isValidAttribute(attr) {
return errors.New("eos: attr is invalid: " + serializeAttribute(attr))
}
var args []string
if recursive {
args = []string{"attr", "-r", "set", serializeAttribute(attr), path}
} else {
args = []string{"attr", "set", serializeAttribute(attr), path}
}
_, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return err
}
return nil
}
// UnsetAttr unsets an extended attribute on a path.
func (c *Client) UnsetAttr(ctx context.Context, auth eosclient.Authorization, attr *eosclient.Attribute, path string) error {
if !isValidAttribute(attr) {
return errors.New("eos: attr is invalid: " + serializeAttribute(attr))
}
args := []string{"attr", "-r", "rm", fmt.Sprintf("%d.%s", attr.Type, attr.Key), path}
_, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return err
}
return nil
}
// GetAttr returns the attribute specified by key
func (c *Client) GetAttr(ctx context.Context, auth eosclient.Authorization, key, path string) (*eosclient.Attribute, error) {
args := []string{"attr", "get", key, path}
attrOut, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return nil, err
}
attr, err := deserializeAttribute(attrOut)
if err != nil {
return nil, err
}
return attr, nil
}
func deserializeAttribute(attrStr string) (*eosclient.Attribute, error) {
// the string is in the form sys.forced.checksum="adler"
keyValue := strings.Split(strings.TrimSpace(attrStr), "=") // keyValue = ["sys.forced.checksum", "\"adler\""]
if len(keyValue) != 2 {
return nil, errtypes.InternalError("wrong attr format to deserialize")
}
type2key := strings.SplitN(keyValue[0], ".", 2) // type2key = ["sys", "forced.checksum"]
if len(type2key) != 2 {
return nil, errtypes.InternalError("wrong attr format to deserialize")
}
t, err := attrStringToType(type2key[0])
if err != nil {
return nil, err
}
// trim \" from value
value := strings.Trim(keyValue[1], "\"")
return &eosclient.Attribute{Type: t, Key: type2key[1], Val: value}, nil
}
// GetQuota gets the quota of a user on the quota node defined by path
func (c *Client) GetQuota(ctx context.Context, username string, rootAuth eosclient.Authorization, path string) (*eosclient.QuotaInfo, error) {
args := []string{"quota", "ls", "-u", username, "-m"}
stdout, _, err := c.executeEOS(ctx, args, rootAuth)
if err != nil {
return nil, err
}
return c.parseQuota(path, stdout)
}
// SetQuota sets the quota of a user on the quota node defined by path
func (c *Client) SetQuota(ctx context.Context, rootAuth eosclient.Authorization, info *eosclient.SetQuotaInfo) error {
maxBytes := fmt.Sprintf("%d", info.MaxBytes)
maxFiles := fmt.Sprintf("%d", info.MaxFiles)
args := []string{"quota", "set", "-u", info.Username, "-p", info.QuotaNode, "-v", maxBytes, "-i", maxFiles}
_, _, err := c.executeEOS(ctx, args, rootAuth)
if err != nil {
return err
}
return nil
}
// Touch creates a 0-size,0-replica file in the EOS namespace.
func (c *Client) Touch(ctx context.Context, auth eosclient.Authorization, path string) error {
args := []string{"file", "touch", path}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// Chown given path
func (c *Client) Chown(ctx context.Context, auth, chownauth eosclient.Authorization, path string) error {
args := []string{"chown", chownauth.Role.UID + ":" + chownauth.Role.GID, path}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// Chmod given path
func (c *Client) Chmod(ctx context.Context, auth eosclient.Authorization, mode, path string) error {
args := []string{"chmod", mode, path}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// CreateDir creates a directory at the given path
func (c *Client) CreateDir(ctx context.Context, auth eosclient.Authorization, path string) error {
args := []string{"mkdir", "-p", path}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// Remove removes the resource at the given path
func (c *Client) Remove(ctx context.Context, auth eosclient.Authorization, path string) error {
args := []string{"rm", "-r", path}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// Rename renames the resource referenced by oldPath to newPath
func (c *Client) Rename(ctx context.Context, auth eosclient.Authorization, oldPath, newPath string) error {
args := []string{"file", "rename", oldPath, newPath}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// List the contents of the directory given by path
func (c *Client) List(ctx context.Context, auth eosclient.Authorization, path string) ([]*eosclient.FileInfo, error) {
args := []string{"find", "--fileinfo", "--maxdepth", "1", path}
stdout, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return nil, errors.Wrapf(err, "eosclient: error listing fn=%s", path)
}
return c.parseFind(path, stdout)
}
// Read reads a file from the mgm
func (c *Client) Read(ctx context.Context, auth eosclient.Authorization, path string) (io.ReadCloser, error) {
rand := "eosread-" + uuid.New().String()
localTarget := fmt.Sprintf("%s/%s", c.opt.CacheDirectory, rand)
defer os.RemoveAll(localTarget)
xrdPath := fmt.Sprintf("%s//%s", c.opt.URL, path)
args := []string{"--nopbar", "--silent", "-f", xrdPath, localTarget}
if auth.Token != "" {
args[3] += "?authz=" + auth.Token
} else if auth.Role.UID != "" && auth.Role.GID != "" {
args = append(args, fmt.Sprintf("-OSeos.ruid=%s&eos.rgid=%s", auth.Role.UID, auth.Role.GID))
}
_, _, err := c.executeXRDCopy(ctx, args)
if err != nil {
return nil, err
}
return os.Open(localTarget)
}
// Write writes a stream to the mgm
func (c *Client) Write(ctx context.Context, auth eosclient.Authorization, path string, stream io.ReadCloser) error {
fd, err := ioutil.TempFile(c.opt.CacheDirectory, "eoswrite-")
if err != nil {
return err
}
defer fd.Close()
defer os.RemoveAll(fd.Name())
// copy stream to local temp file
_, err = io.Copy(fd, stream)
if err != nil {
return err
}
return c.WriteFile(ctx, auth, path, fd.Name())
}
// WriteFile writes an existing file to the mgm
func (c *Client) WriteFile(ctx context.Context, auth eosclient.Authorization, path, source string) error {
xrdPath := fmt.Sprintf("%s//%s", c.opt.URL, path)
args := []string{"--nopbar", "--silent", "-f", source, xrdPath}
if auth.Token != "" {
args[4] += "?authz=" + auth.Token
} else if auth.Role.UID != "" && auth.Role.GID != "" {
args = append(args, fmt.Sprintf("-ODeos.ruid=%s&eos.rgid=%s", auth.Role.UID, auth.Role.GID))
}
_, _, err := c.executeXRDCopy(ctx, args)
return err
}
// ListDeletedEntries returns a list of the deleted entries.
func (c *Client) ListDeletedEntries(ctx context.Context, auth eosclient.Authorization) ([]*eosclient.DeletedEntry, error) {
// TODO(labkode): add protection if slave is configured and alive to count how many files are in the trashbin before
// triggering the recycle ls call that could break the instance because of unavailable memory.
args := []string{"recycle", "ls", "-m"}
stdout, _, err := c.executeEOS(ctx, args, auth)
if err != nil {
return nil, err
}
return parseRecycleList(stdout)
}
// RestoreDeletedEntry restores a deleted entry.
func (c *Client) RestoreDeletedEntry(ctx context.Context, auth eosclient.Authorization, key string) error {
args := []string{"recycle", "restore", key}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// PurgeDeletedEntries purges all entries from the recycle bin.
func (c *Client) PurgeDeletedEntries(ctx context.Context, auth eosclient.Authorization) error {
args := []string{"recycle", "purge"}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// ListVersions list all the versions for a given file.
func (c *Client) ListVersions(ctx context.Context, auth eosclient.Authorization, p string) ([]*eosclient.FileInfo, error) {
versionFolder := getVersionFolder(p)
finfos, err := c.List(ctx, auth, versionFolder)
if err != nil {
// we send back an empty list
return []*eosclient.FileInfo{}, nil
}
return finfos, nil
}
// RollbackToVersion rollbacks a file to a previous version.
func (c *Client) RollbackToVersion(ctx context.Context, auth eosclient.Authorization, path, version string) error {
args := []string{"file", "versions", path, version}
_, _, err := c.executeEOS(ctx, args, auth)
return err
}
// ReadVersion reads the version for the given file.
func (c *Client) ReadVersion(ctx context.Context, auth eosclient.Authorization, p, version string) (io.ReadCloser, error) {
versionFile := path.Join(getVersionFolder(p), version)
return c.Read(ctx, auth, versionFile)
}
// GenerateToken returns a token on behalf of the resource owner to be used by lightweight accounts
func (c *Client) GenerateToken(ctx context.Context, auth eosclient.Authorization, p string, a *acl.Entry) (string, error) {
expiration := strconv.FormatInt(time.Now().Add(time.Duration(c.opt.TokenExpiry)*time.Second).Unix(), 10)
args := []string{"token", "--permission", a.Permissions, "--tree", "--path", p, "--expires", expiration}
stdout, _, err := c.executeEOS(ctx, args, auth)
return stdout, err
}
func (c *Client) getVersionFolderInode(ctx context.Context, auth eosclient.Authorization, p string) (uint64, error) {
versionFolder := getVersionFolder(p)
md, err := c.GetFileInfoByPath(ctx, auth, versionFolder)
if err != nil {
if err = c.CreateDir(ctx, auth, versionFolder); err != nil {
return 0, err
}
md, err = c.GetFileInfoByPath(ctx, auth, versionFolder)
if err != nil {
return 0, err
}
}
return md.Inode, nil
}
func (c *Client) getFileInfoFromVersion(ctx context.Context, auth eosclient.Authorization, p string) (*eosclient.FileInfo, error) {
file := getFileFromVersionFolder(p)
md, err := c.GetFileInfoByPath(ctx, auth, file)
if err != nil {
return nil, err
}
return md, nil
}
func isVersionFolder(p string) bool {
return strings.HasPrefix(path.Base(p), versionPrefix)
}
func getVersionFolder(p string) string {
return path.Join(path.Dir(p), versionPrefix+path.Base(p))
}
func getFileFromVersionFolder(p string) string {
return path.Join(path.Dir(p), strings.TrimPrefix(path.Base(p), versionPrefix))
}
func parseRecycleList(raw string) ([]*eosclient.DeletedEntry, error) {
entries := []*eosclient.DeletedEntry{}
rawLines := strings.FieldsFunc(raw, func(c rune) bool {
return c == '\n'
})
for _, rl := range rawLines {
if rl == "" {
continue
}
entry, err := parseRecycleEntry(rl)
if err != nil {
return nil, err
}
entries = append(entries, entry)
}
return entries, nil
}
// parse entries like these:
// recycle=ls recycle-bin=/eos/backup/proc/recycle/ uid=gonzalhu gid=it size=0 deletion-time=1510823151 type=recursive-dir keylength.restore-path=45 restore-path=/eos/scratch/user/g/gonzalhu/.sys.v#.app.ico/ restore-key=0000000000a35100
// recycle=ls recycle-bin=/eos/backup/proc/recycle/ uid=gonzalhu gid=it size=381038 deletion-time=1510823151 type=file keylength.restore-path=36 restore-path=/eos/scratch/user/g/gonzalhu/app.ico restore-key=000000002544fdb3
func parseRecycleEntry(raw string) (*eosclient.DeletedEntry, error) {
partsBySpace := strings.FieldsFunc(raw, func(c rune) bool {
return c == ' '
})
restoreKeyPair, partsBySpace := partsBySpace[len(partsBySpace)-1], partsBySpace[:len(partsBySpace)-1]
restorePathPair := strings.Join(partsBySpace[8:], " ")
partsBySpace = partsBySpace[:8]
partsBySpace = append(partsBySpace, restorePathPair)
partsBySpace = append(partsBySpace, restoreKeyPair)
kv := getMap(partsBySpace)
size, err := strconv.ParseUint(kv["size"], 10, 64)
if err != nil {
return nil, err
}
isDir := false
if kv["type"] == "recursive-dir" {
isDir = true
}
deletionMTime, err := strconv.ParseUint(strings.Split(kv["deletion-time"], ".")[0], 10, 64)
if err != nil {
return nil, err
}
entry := &eosclient.DeletedEntry{
RestorePath: kv["restore-path"],
RestoreKey: kv["restore-key"],
Size: size,
DeletionMTime: deletionMTime,
IsDir: isDir,
}
return entry, nil
}
func getMap(partsBySpace []string) map[string]string {
kv := map[string]string{}
for _, pair := range partsBySpace {
parts := strings.Split(pair, "=")
if len(parts) > 1 {
kv[parts[0]] = parts[1]
}
}
return kv
}
func (c *Client) parseFind(dirPath, raw string) ([]*eosclient.FileInfo, error) {
finfos := []*eosclient.FileInfo{}
rawLines := strings.FieldsFunc(raw, func(c rune) bool {
return c == '\n'
})
var parent *eosclient.FileInfo
for _, rl := range rawLines {
if rl == "" {
continue
}
fi, err := c.parseFileInfo(rl)
if err != nil {
return nil, err
}
// dirs in eos end with a slash, like /eos/user/g/gonzalhu/
// we skip the current directory as eos find will return the directory we
// ask to find
if fi.File == path.Clean(dirPath) {
parent = fi
continue
}
finfos = append(finfos, fi)
}
for _, fi := range finfos {
// For files, inherit ACLs from the parent
if !fi.IsDir && parent != nil {
fi.SysACL.Entries = append(fi.SysACL.Entries, parent.SysACL.Entries...)
}
}
return finfos, nil
}
func (c Client) parseQuotaLine(line string) map[string]string {
partsBySpace := strings.FieldsFunc(line, func(c rune) bool {
return c == ' '
})
m := getMap(partsBySpace)
return m
}
func (c *Client) parseQuota(path, raw string) (*eosclient.QuotaInfo, error) {
rawLines := strings.FieldsFunc(raw, func(c rune) bool {
return c == '\n'
})
for _, rl := range rawLines {
if rl == "" {
continue
}
m := c.parseQuotaLine(rl)
// map[maxbytes:2000000000000 maxlogicalbytes:1000000000000 percentageusedbytes:0.49 quota:node uid:gonzalhu space:/eos/scratch/user/ usedbytes:9829986500 usedlogicalbytes:4914993250 statusfiles:ok usedfiles:334 maxfiles:1000000 statusbytes:ok]
space := m["space"]
if strings.HasPrefix(path, filepath.Clean(space)) {
maxBytesString := m["maxlogicalbytes"]
usedBytesString := m["usedlogicalbytes"]
maxBytes, _ := strconv.ParseUint(maxBytesString, 10, 64)
usedBytes, _ := strconv.ParseUint(usedBytesString, 10, 64)
maxInodesString := m["maxfiles"]
usedInodesString := m["usedfiles"]
maxInodes, _ := strconv.ParseUint(maxInodesString, 10, 64)
usedInodes, _ := strconv.ParseUint(usedInodesString, 10, 64)
qi := &eosclient.QuotaInfo{
AvailableBytes: maxBytes,
UsedBytes: usedBytes,
AvailableInodes: maxInodes,
UsedInodes: usedInodes,
}
return qi, nil
}
}
return &eosclient.QuotaInfo{}, nil
}
// TODO(labkode): better API to access extended attributes.
func (c *Client) parseFileInfo(raw string) (*eosclient.FileInfo, error) {
line := raw[15:]
index := strings.Index(line, " file=/")
lengthString := line[0:index]
length, err := strconv.ParseUint(lengthString, 10, 64)
if err != nil {
return nil, err
}
line = line[index+6:] // skip ' file='
name := line[0:length]
kv := make(map[string]string)
attrs := make(map[string]string)
// strip trailing slash
kv["file"] = strings.TrimSuffix(name, "/")
line = line[length+1:]
partsBySpace := strings.FieldsFunc(line, func(c rune) bool { // we have [size=45 container=3 ...}
return c == ' '
})
var previousXAttr = ""
for _, p := range partsBySpace {
partsByEqual := strings.Split(p, "=") // we have kv pairs like [size 14]
if len(partsByEqual) == 2 {
// handle xattrn and xattrv special cases
switch {
case partsByEqual[0] == "xattrn":
previousXAttr = partsByEqual[1]
if previousXAttr != "user.acl" {
previousXAttr = strings.Replace(previousXAttr, "user.", "", 1)
}
case partsByEqual[0] == "xattrv":
attrs[previousXAttr] = partsByEqual[1]
previousXAttr = ""
default:
kv[partsByEqual[0]] = partsByEqual[1]
}
}
}
fi, err := c.mapToFileInfo(kv, attrs)
if err != nil {
return nil, err
}
return fi, nil