This repository has been archived by the owner on Mar 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
collection.go
1482 lines (1148 loc) · 34.4 KB
/
collection.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 (c) 2016, University of Florida Research Foundation, Inc. and The BioTeam, Inc. ***
*** For more information please refer to the LICENSE.md file ***/
package gorods
// #include "wrapper.h"
import "C"
import (
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"
"unsafe"
)
// Collection structs contain information about single collections in an iRODS zone.
type Collection struct {
options *CollectionOptions
readOpts *CollectionReadOpts
readInfo *CollectionReadInfo
trimRepls bool
path string
name string
dataObjects IRodsObjs
metaCol *MetaCollection
con *Connection
col *Collection
recursive bool
hasInit bool
typ int
parent *Collection
ownerName string
owner *User
createTime time.Time
modifyTime time.Time
opened bool
cColHandle C.collHandle_t
}
// CollectionOptions stores options relating to collection initialization.
// Path is the full path of the collection you're requesting.
// Recursive if set to true will load sub collections into memory, until the end of the collection "tree" is found.
type CollectionOptions struct {
Path string
Recursive bool
GetRepls bool
SkipCache bool
}
// String shows the contents of the collection.
//
// d = DataObj
//
// C = Collection
//
// Sample output:
//
// Collection: /tempZone/home/admin/gorods
// d: build.sh
// C: bin
// C: pkg
// C: src
func (obj *Collection) String() string {
str := fmt.Sprintf("Collection: %v\n", obj.path)
objs, _ := obj.All()
for _, o := range objs {
str += fmt.Sprintf("\t%v: %v\n", getTypeString(o.Type()), o.Name())
}
return str
}
// initCollection initializes collection from *C.collEnt_t. This is used internally in the gorods package.
func initCollection(data *C.collEnt_t, acol *Collection) (*Collection, error) {
col := new(Collection)
col.opened = false
col.typ = CollectionType
col.col = acol
col.con = col.col.con
col.path = C.GoString(data.collName)
col.options = acol.options
col.recursive = acol.recursive
col.trimRepls = acol.trimRepls
col.parent = acol
col.ownerName = C.GoString(data.ownerName)
col.createTime = cTimeToTime(data.createTime)
col.modifyTime = cTimeToTime(data.modifyTime)
col.name = filepath.Base(col.path)
if usrs, err := col.con.Users(); err != nil {
return nil, err
} else {
if u := usrs.FindByName(col.ownerName, col.con); u != nil {
col.owner = u
} else {
return nil, newError(Fatal, -1, fmt.Sprintf("iRODS initCollection Failed: Unable to locate user in cache"))
}
}
if col.recursive {
if er := col.init(); er != nil {
return nil, er
}
}
return col, nil
}
// Stat returns a map (key/value pairs) of the system meta information. The following keys can be used with the map:
//
// "objSize"
//
// "dataMode"
//
// "dataId"
//
// "chksum"
//
// "ownerName"
//
// "ownerZone"
//
// "createTime"
//
// "modifyTime"
func (col *Collection) Stat() (map[string]interface{}, error) {
var (
err *C.char
statResult *C.rodsObjStat_t
)
path := C.CString(col.path)
defer C.free(unsafe.Pointer(path))
ccon := col.con.GetCcon()
defer col.con.ReturnCcon(ccon)
if status := C.gorods_stat_dataobject(path, &statResult, ccon, &err); status != 0 {
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Stat Failed: %v, %v", col.path, C.GoString(err)))
}
result := make(map[string]interface{})
result["objSize"] = int(statResult.objSize)
result["dataMode"] = int(statResult.dataMode)
result["dataId"] = C.GoString(&statResult.dataId[0])
result["chksum"] = C.GoString(&statResult.chksum[0])
result["ownerName"] = C.GoString(&statResult.ownerName[0])
result["ownerZone"] = C.GoString(&statResult.ownerZone[0])
result["createTime"] = C.GoString(&statResult.createTime[0])
result["modifyTime"] = C.GoString(&statResult.modifyTime[0])
//result["rescHier"] = C.GoString(&statResult.rescHier[0])
C.freeRodsObjStat(statResult)
return result, nil
}
// getCollection initializes specified collection located at startPath using gorods.connection.
// Could be considered alias of Connection.collection()
func getCollection(opts CollectionOptions, con *Connection) (*Collection, error) {
col := new(Collection)
col.options = &opts
col.con = con
return setupCollection(col)
}
// getCollectionOpts initializes specified collection located at startPath using gorods.connection.
// Could be considered alias of Connection.collection()
func getCollectionOpts(opts CollectionOptions, readOpts CollectionReadOpts, con *Connection) (*Collection, error) {
col := new(Collection)
col.options = &opts
col.con = con
col.readOpts = &readOpts
return setupCollection(col)
}
func setupCollection(col *Collection) (*Collection, error) {
col.opened = false
col.typ = CollectionType
col.path = strings.TrimRight(col.options.Path, "/")
col.name = filepath.Base(col.path)
col.recursive = col.options.Recursive
col.trimRepls = !col.options.GetRepls
if col.recursive {
if er := col.init(); er != nil {
return nil, er
}
} else {
if er := col.Open(); er != nil {
return nil, er
}
}
if info, err := col.Stat(); err == nil {
col.ownerName = info["ownerName"].(string)
cCreateTime := C.CString(info["createTime"].(string))
defer C.free(unsafe.Pointer(cCreateTime))
cModifyTime := C.CString(info["modifyTime"].(string))
defer C.free(unsafe.Pointer(cModifyTime))
col.createTime = cTimeToTime(cCreateTime)
col.modifyTime = cTimeToTime(cModifyTime)
if usrs, err := col.con.Users(); err != nil {
return nil, err
} else {
if u := usrs.FindByName(col.ownerName, col.con); u != nil {
col.owner = u
} else {
return nil, newError(Fatal, -1, fmt.Sprintf("iRODS getCollection Failed: Unable to locate user in cache"))
}
}
} else {
return nil, err
}
return col, nil
}
// CreateCollection creates a collection in the specified collection using provided options. Returns the newly created collection object.
func CreateCollection(name string, coll *Collection) (*Collection, error) {
var (
errMsg *C.char
)
newColPath := coll.path + "/" + name
path := C.CString(newColPath)
defer C.free(unsafe.Pointer(path))
ccon := coll.con.GetCcon()
if status := C.gorods_create_collection(path, ccon, &errMsg); status != 0 {
coll.con.ReturnCcon(ccon)
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Create Collection Failed: %v, Does the collection already exist?", C.GoString(errMsg)))
}
coll.con.ReturnCcon(ccon)
//coll.Refresh()
//newCol := coll.Cd(name)
return coll.Con().Collection(CollectionOptions{
Path: newColPath,
})
}
// init opens and reads collection information from iRODS if it hasn't been init'd already
func (col *Collection) init() error {
if !col.hasInit {
if err := col.Open(); err != nil {
return err
}
//if col.readOpts == nil {
if err := col.ReadCollection(); err != nil {
return err
}
// } else {
// if _, err := col.ReadCollectionOpts(*col.readOpts); err != nil {
// return err
// }
// }
}
col.hasInit = true
return nil
}
// Collections returns only the IRodsObjs that represent collections
func (col *Collection) Collections() (response IRodsObjs, err error) {
if err = col.init(); err != nil {
return
}
for i, obj := range col.dataObjects {
if obj.Type() == CollectionType {
response = append(response, col.dataObjects[i])
}
}
return
}
// DataObjs returns only the data objects contained within the collection
func (col *Collection) DataObjs() (response IRodsObjs, err error) {
if err = col.init(); err != nil {
return
}
for i, obj := range col.dataObjects {
if obj.Type() == DataObjType {
response = append(response, col.dataObjects[i])
}
}
return
}
// EachDataObj is an iterator for data objects
func (col *Collection) Each(ittr func(obj IRodsObj) error) error {
if objs, err := col.All(); err == nil {
for i := range objs {
if aErr := ittr(objs[i]); aErr != nil {
return aErr
}
}
return nil
} else {
return err
}
}
// EachDataObj is an iterator for data objects
func (col *Collection) EachDataObj(ittr func(obj *DataObj)) error {
if objs, err := col.DataObjs(); err == nil {
for _, obj := range objs {
ittr(obj.(*DataObj))
}
return nil
} else {
return err
}
}
// EachCollection is an iterator for collections
func (col *Collection) EachCollection(ittr func(obj *Collection)) error {
if cols, err := col.Collections(); err == nil {
for _, col := range cols {
ittr(col.(*Collection))
}
return nil
} else {
return err
}
}
// All returns generic interface slice containing both data objects and collections combined
func (col *Collection) All() (IRodsObjs, error) {
if err := col.init(); err != nil {
return col.dataObjects, err
}
return col.dataObjects, nil
}
func (col *Collection) Walk(callback func(IRodsObj) error) error {
all, err := col.All()
if err != nil {
return err
}
for _, item := range all {
if item.Type() == CollectionType {
if cbErr := (item.(*Collection)).Walk(callback); cbErr != nil {
return cbErr
}
} else {
if cbErr := callback(item); cbErr != nil {
return cbErr
}
}
}
return nil
}
// SetInheritance sets the inheritance option of the collection. If true, sub-collections and data objects inherit the permissions (ACL) of this collection.
func (col *Collection) SetInheritance(inherits bool, recursive bool) error {
var ih int
if inherits {
ih = Inherit
} else {
ih = NoInherit
}
return chmod(col, "", ih, recursive, false)
}
// Inheritance returns true or false, depending on the collection's inheritance setting
func (col *Collection) Inheritance() (bool, error) {
var (
enabled C.int
err *C.char
)
collName := C.CString(col.path)
defer C.free(unsafe.Pointer(collName))
ccon := col.con.GetCcon()
defer col.con.ReturnCcon(ccon)
if status := C.gorods_get_collection_inheritance(ccon, collName, &enabled, &err); status != 0 {
return false, newError(Fatal, status, fmt.Sprintf("iRODS Get Collection Inheritance Failed: %v", C.GoString(err)))
}
if int(enabled) > 0 {
return true, nil
}
return false, nil
}
// GrantAccess will add permissions (ACL) to the collection
func (col *Collection) GrantAccess(userOrGroup AccessObject, accessLevel int, recursive bool) error {
return chmod(col, userOrGroup.Name(), accessLevel, recursive, true)
}
// Chmod changes the permissions/ACL of the collection
// accessLevel: Null | Read | Write | Own
func (col *Collection) Chmod(userOrGroup string, accessLevel int, recursive bool) error {
return chmod(col, userOrGroup, accessLevel, recursive, true)
}
// ACL returns a slice of ACL structs. Example of slice in string format:
// [rods#tempZone:own
// developers#tempZone:modify object
// designers#tempZone:read object]
func (col *Collection) ACL() (ACLs, error) {
var (
result C.goRodsACLResult_t
err *C.char
zoneHint *C.char
collName *C.char
)
zone, zErr := col.con.LocalZone()
if zErr != nil {
return nil, zErr
} else {
zoneHint = C.CString(zone.Name())
}
collName = C.CString(col.path)
defer C.free(unsafe.Pointer(collName))
defer C.free(unsafe.Pointer(zoneHint))
ccon := col.con.GetCcon()
if status := C.gorods_get_collection_acl(ccon, collName, &result, zoneHint, &err); status != 0 {
col.con.ReturnCcon(ccon)
return nil, newError(Fatal, status, fmt.Sprintf("iRODS Get Collection ACL Failed: %v", C.GoString(err)))
}
col.con.ReturnCcon(ccon)
return aclSliceToResponse(&result, col.con)
}
// Size returns the total size in bytes of all contained data objects and collections, recursively
func (col *Collection) Size() int64 {
result, err := col.con.IQuest("select sum(DATA_SIZE) where COLL_NAME like '"+col.path+"%'", false)
if err != nil {
return 0
}
i, err := strconv.ParseInt(result[0]["DATA_SIZE"], 10, 64)
if err != nil {
return 0
}
return i
}
// Length returns the total number of data objects and collections contained within the collection, recursively
func (col *Collection) Length() int {
result, err := col.con.IQuest("select count(DATA_ID) where COLL_NAME like '"+col.path+"%'", false)
if err != nil {
return 0
}
i, err := strconv.Atoi(result[0]["DATA_ID"])
if err != nil {
return 0
}
return i
}
// Type gets the type
func (col *Collection) Type() int {
return col.typ
}
// IsRecursive returns true or false
func (col *Collection) IsRecursive() bool {
return col.recursive
}
// Connection returns the *Connection used to get collection
func (col *Collection) Con() *Connection {
return col.con
}
// Name returns the Name of the collection
func (col *Collection) Name() string {
return col.name
}
// Path returns the Path of the collection
func (col *Collection) Path() string {
return col.path
}
// OwnerName returns the owner name of the collection
func (col *Collection) OwnerName() string {
return col.ownerName
}
// Owner returns a User struct, representing the user who owns the collection
func (col *Collection) Owner() *User {
return col.owner
}
// CreateTime returns the create time of the collection
func (col *Collection) CreateTime() time.Time {
return col.createTime
}
func (col *Collection) Mode() os.FileMode {
return 0755
}
func (col *Collection) ModTime() time.Time {
return col.ModifyTime()
}
func (col *Collection) IsDir() bool {
return true
}
func (col *Collection) Sys() interface{} {
return nil
}
// ModifyTime returns the modify time of the collection
func (col *Collection) ModifyTime() time.Time {
return col.modifyTime
}
// Col returns the *Collection of the collection
func (col *Collection) Col() *Collection {
// This code could be in setupCollection
if col.col == nil {
pathSplit := strings.Split(col.Path(), "/")
parentColPath := strings.Join(pathSplit[:len(pathSplit)-1], "/")
if parentColPath == "" {
parentColPath = "/"
}
col.col, _ = col.con.Collection(CollectionOptions{
Path: parentColPath,
SkipCache: true,
})
}
return col.col
}
// Destroy is equivalent to irm -rf
func (col *Collection) Destroy() error {
return col.Rm(true, true)
}
// Delete is equivalent to irm -f {-r}
func (col *Collection) Delete(recursive bool) error {
return col.Rm(recursive, true)
}
// Trash is equivalent to irm {-r}
func (col *Collection) Trash(recursive bool) error {
return col.Rm(recursive, false)
}
// Rm is equivalent to irm {-r} {-f}
func (col *Collection) Rm(recursive bool, force bool) error {
var errMsg *C.char
path := C.CString(col.path)
defer C.free(unsafe.Pointer(path))
var (
cForce C.int
cRecursive C.int
)
if force {
cForce = C.int(1)
}
if recursive {
cRecursive = C.int(1)
}
ccon := col.con.GetCcon()
defer col.con.ReturnCcon(ccon)
if status := C.gorods_rm(path, C.int(1), cRecursive, cForce, C.int(0), ccon, &errMsg); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS Rm Collection Failed: %v", C.GoString(errMsg)))
}
return nil
}
// RmTrash is used (sometimes internally) by GoRODS to delete items in the trash permanently. The collection's path should be in the trash collection.
func (col *Collection) RmTrash() error {
var errMsg *C.char
path := C.CString(col.path)
defer C.free(unsafe.Pointer(path))
ccon := col.con.GetCcon()
defer col.con.ReturnCcon(ccon)
if status := C.gorods_rm(path, C.int(1), C.int(1), C.int(1), C.int(1), ccon, &errMsg); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS RmTrash Collection Failed: %v", C.GoString(errMsg)))
}
return nil
}
// Attribute gets slice of Meta AVU triples, matching by Attribute name for Collection
func (col *Collection) Attribute(attr string) (Metas, error) {
if mc, err := col.Meta(); err == nil {
return mc.Get(attr)
} else {
return nil, err
}
}
// Meta returns collection of all metadata AVU triples for Collection
func (col *Collection) Meta() (*MetaCollection, error) {
if er := col.init(); er != nil {
return nil, er
}
if col.metaCol == nil {
if mc, err := newMetaCollection(col); err == nil {
col.metaCol = mc
} else {
return nil, err
}
}
return col.metaCol, nil
}
// AddMeta adds a single Meta triple struct
func (col *Collection) AddMeta(m Meta) (newMeta *Meta, err error) {
var mc *MetaCollection
if mc, err = col.Meta(); err != nil {
return
}
newMeta, err = mc.Add(m)
return
}
// DeleteMeta deletes a single Meta triple struct, identified by Attribute field
func (col *Collection) DeleteMeta(attr string) (*MetaCollection, error) {
if mc, err := col.Meta(); err == nil {
return mc, mc.Delete(attr)
} else {
return nil, err
}
}
// DownloadTo recursively downloads all data objects and collections contained within the collection, into the path specified
func (col *Collection) DownloadTo(localPath string) error {
if dir, err := os.Stat(localPath); err == nil && dir.IsDir() {
if localPath[len(localPath)-1] != '/' {
localPath += "/"
}
if objs, er := col.DataObjs(); er == nil {
for _, obj := range objs {
if e := obj.DownloadTo(localPath + obj.Name()); e != nil {
return e
}
}
} else {
return er
}
if cols, er := col.Collections(); er == nil {
for _, col := range cols {
newDir := localPath + col.Name()
if e := os.Mkdir(newDir, 0777); e != nil {
return e
}
if e := col.DownloadTo(newDir); e != nil {
return e
}
}
} else {
return er
}
} else {
return newError(Fatal, -1, fmt.Sprintf("iRODS DownloadTo Failed: localPath doesn't exist or isn't a directory"))
}
return nil
}
// Open connects to iRODS and sets the handle for Collection.
// Usually called by Collection.init()
func (col *Collection) Open() error {
if !col.opened {
var (
errMsg *C.char
cTrimRepls C.int
)
path := C.CString(col.path)
if col.trimRepls {
cTrimRepls = C.int(1)
} else {
cTrimRepls = C.int(0)
}
defer C.free(unsafe.Pointer(path))
ccon := col.con.GetCcon()
defer col.con.ReturnCcon(ccon)
if status := C.gorods_open_collection(path, cTrimRepls, &col.cColHandle, ccon, &errMsg); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS Open Collection Failed: %v, %v", col.path, C.GoString(errMsg)))
}
col.opened = true
}
return nil
}
// Close closes the Collection connection and resets the handle
func (col *Collection) Close() error {
var errMsg *C.char
for _, c := range col.dataObjects {
if err := c.Close(); err != nil {
return err
}
}
if col.opened {
ccon := col.con.GetCcon()
defer col.con.ReturnCcon(ccon)
if status := C.gorods_close_collection(&col.cColHandle, &errMsg); status != 0 {
return newError(Fatal, status, fmt.Sprintf("iRODS Close Collection Failed: %v, %v", col.path, C.GoString(errMsg)))
}
col.opened = false
}
return nil
}
// CopyTo copies all collections and data objects contained withing the collection to the specified collection.
// Accepts string or *Collection types.
func (col *Collection) CopyTo(iRODSCollection interface{}) error {
// Get reference to destination collection (just like MoveTo)
var (
destination string
destinationCollectionString string
destinationCollection *Collection
)
switch iRODSCollection.(type) {
case string:
destinationCollectionString = iRODSCollection.(string)
// Is this a relative path?
if destinationCollectionString[0] != '/' {
destinationCollectionString = path.Dir(col.path) + "/" + destinationCollectionString
}
if destinationCollectionString[len(destinationCollectionString)-1] != '/' {
destinationCollectionString += "/"
}
destination += destinationCollectionString + col.name
case *Collection:
destinationCollectionString = (iRODSCollection.(*Collection)).path + "/"
destination = destinationCollectionString + col.name
default:
return newError(Fatal, -1, fmt.Sprintf("iRODS CopyTo Failed, unknown variable type passed as collection"))
}
var colEr error
// load destination collection into memory
if destinationCollection, colEr = col.con.Collection(CollectionOptions{
Path: destinationCollectionString,
Recursive: false,
}); colEr != nil {
return colEr
}
// Create collection with same name in destination as sub-collection
if newCol, err := destinationCollection.CreateSubCollection(col.name); err == nil {
// loop through data objects, copy each to new sub-collection
if objs, er := col.DataObjs(); er == nil {
for _, obj := range objs {
if e := obj.CopyTo(newCol); e != nil {
return e
}
}
} else {
return er
}
// Loop through collections -> run recursive copyTo util?
if cols, er := col.Collections(); er == nil {
for _, aCol := range cols {
if er := aCol.CopyTo(newCol); er != nil {
return er
}
}
} else {
return er
}
newCol.Refresh() // <- is this required?
} else {
return err
}
return nil
}
// TrimRepls recursively trims data object replicas (removes from resource servers), using the rules defined in opts.
func (col *Collection) TrimRepls(opts TrimOptions) error {
// loop through data objects
if objs, er := col.DataObjs(); er == nil {
for _, obj := range objs {
if e := obj.TrimRepls(opts); e != nil {
return e
}
}
} else {
return er
}
// Loop through collections
if cols, er := col.Collections(); er == nil {
for _, aCol := range cols {
if er := aCol.TrimRepls(opts); er != nil {
return er
}
c := aCol.(*Collection)
c.Refresh()
}
} else {
return er
}
col.Refresh()
return nil
}
// MoveToResource recursively moves all data objects contained within the collection to the specified resource.
// Accepts string or *Resource type.
func (col *Collection) MoveToResource(targetResource interface{}) error {
// loop through data objects
if objs, er := col.DataObjs(); er == nil {
for _, obj := range objs {
if e := obj.MoveToResource(targetResource); e != nil {
return e
}
}
} else {
return er
}
// Loop through collections
if cols, er := col.Collections(); er == nil {
for _, aCol := range cols {
if er := aCol.MoveToResource(targetResource); er != nil {
return er
}
c := aCol.(*Collection)
c.Refresh()
}
} else {
return er
}
col.Refresh()
return nil
}
// Replicate recursively copies all data objects contained within the collection to the specified resource.
// Accepts string or *Resource type for targetResource parameter.
func (col *Collection) Replicate(targetResource interface{}, opts DataObjOptions) error {
// loop through data objects
if objs, er := col.DataObjs(); er == nil {
for _, obj := range objs {
if e := obj.Replicate(targetResource, opts); e != nil {
return e
}
}
} else {
return er
}
// Loop through collections
if cols, er := col.Collections(); er == nil {
for _, aCol := range cols {
if er := aCol.Replicate(targetResource, opts); er != nil {
return er
}
c := aCol.(*Collection)
if !c.trimRepls {
c.Refresh()
}
}
} else {
return er
}
if !col.trimRepls {
col.Refresh()
}
return nil
}