-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtacle_voodoo.go
More file actions
1552 lines (1431 loc) · 37.5 KB
/
tacle_voodoo.go
File metadata and controls
1552 lines (1431 loc) · 37.5 KB
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
/*
This software is not permitted to use in:
* United Kingdom
* Australia and New Zealand
* United States
* Canada
* Estonia
* Lithuania
* Latvia
* Ukraine
* France
* Germany
* Spain
* Italy
* Finland
Also not permitted to use or distrubetd by or for the
interest of any resident citizen or resident company of
the any country specified.
Otherwise you are free to use and distribute the
software as specified AGPLv3 untill you keep
this policy note
*/
package main
import (
"bufio"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
)
var not_impl = fmt.Errorf("not_implemented")
var unmatched = fmt.Errorf("unmatched")
var not_found = fmt.Errorf("unfound")
const magic_version_ofset = 83
const MB = 1024 * 1024
type ObjectAddress struct {
osdnum int
pool int64
pg int
prefix string
name string
size int64
snap_ref int64
version int64
}
type Volume struct {
id int64
Parent *Snapshot
pool int64
name string
order int
size int64
Snapshots map[string]*Snapshot
}
type Snapshot struct {
snap_ref int64
name string
size int64
Volume *Volume
}
type AtomicOperation struct {
objects []*ObjectAddress
size int64
object_offset int64
}
// For OLDEST go's
func max(a int64, b int64) int64 {
if a > b {
return a
} else {
return b
}
}
// For OLDEST go's
func min(a int64, b int64) int64 {
if a < b {
return a
} else {
return b
}
}
func (op *AtomicOperation) ToString() string {
if len(op.objects) > 0 {
snap_ref := max(op.objects[0].snap_ref, 0)
return fmt.Sprintf(
"%s#%d range [%d - %d]",
op.objects[0].name, snap_ref, op.object_offset, op.object_offset+op.size-1,
)
} else {
return fmt.Sprintf("zero %d bytes", op.size)
}
}
func NewAtomicOperation(
address []*ObjectAddress,
object_offset int64,
operation_size int64,
) *AtomicOperation {
return &AtomicOperation{
objects: append(make([]*ObjectAddress, 0, len(address)), address...),
object_offset: object_offset,
size: operation_size,
}
}
type DataSource interface {
BlockSize() int64
GetSize() int64
GetParent() *Snapshot
GetBaseName() string
GetSnapRef() int64
GetPool() int64
GetName() string
}
func (v *Volume) GetName() string {
return v.name
}
func (v *Volume) GetPool() int64 {
return v.pool
}
func (v *Volume) GetBaseName() string {
return fmt.Sprintf("rbd_data.%x", v.id)
}
func (v *Volume) GetSnapRef() int64 {
return -1
}
func (v *Volume) BlockSize() int64 {
return 2 << (v.order - 1)
}
func (v *Volume) GetParent() *Snapshot {
return v.Parent
}
func (v *Volume) GetSize() int64 {
return v.size
}
func (s *Snapshot) GetBaseName() string {
if s.Volume == nil {
fmt.Println("Snap", s.name, "has no volume(?)")
}
return s.Volume.GetBaseName()
}
func (v *Snapshot) GetPool() int64 {
return v.Volume.pool
}
func (s *Snapshot) GetName() string {
return fmt.Sprintf("%s@%s", s.Volume.name, s.name)
}
func (s *Snapshot) GetSnapRef() int64 {
return s.snap_ref
}
func (v *Snapshot) BlockSize() int64 {
return 2 << (v.Volume.order - 1)
}
func (s *Snapshot) GetParent() *Snapshot {
if s.Volume == nil {
fmt.Println("Snapshot", s.name, "has no volume (?)")
}
return s.Volume.Parent
}
func (s *Snapshot) GetSize() int64 {
return s.size
}
func GetGranularity(src DataSource) int64 {
g := src.BlockSize()
parent := src.GetParent()
if parent != nil {
pg := GetGranularity(parent)
if pg < g {
g = pg
}
}
return g
}
var is_debug = false
func Debugf(f string, args ...any) {
if is_debug {
fmt.Print("DEBUG: ")
fmt.Printf(f, args...)
fmt.Println()
}
}
func GetBlockOps(src DataSource, offset int64, size int64, ctx *FullContext) ([]*AtomicOperation, error) {
ret := make([]*AtomicOperation, 0)
blocksize := src.BlockSize()
obj_name := fmt.Sprintf("%s.%016x", src.GetBaseName(), offset/blocksize)
snap_ref := src.GetSnapRef()
if offset > src.GetSize() {
ret = append(ret, NewAtomicOperation(nil, 0, size))
return ret, nil
}
objlist, err := FindObject(ctx.Store, src.GetPool(), obj_name, snap_ref)
if err != nil {
return nil, err
}
if len(objlist) == 0 {
if src.GetParent() != nil {
if ops, err := GetBlockOps(src.GetParent(), offset, size, ctx); err != nil {
return nil, err
} else {
ret = append(ret, ops...)
return ret, nil
}
} else {
ret = append(ret, NewAtomicOperation(nil, 0, size))
return ret, nil
}
}
object_size := objlist[0].size
delta := offset % src.BlockSize()
usable := object_size - delta
to_use := min(usable, size)
Debugf(
"object %s#%d(%d) offset %d size %d delta %d usable %d to_use %d",
objlist[0].name, objlist[0].snap_ref, object_size, offset, size, delta, usable, to_use,
)
if to_use > 0 {
ret = append(ret, NewAtomicOperation(objlist, delta, to_use))
size -= to_use
offset += to_use
}
if size > 0 {
if src.GetParent() != nil {
if ops, err := GetBlockOps(src.GetParent(), offset, size, ctx); err != nil {
return nil, err
} else {
ret = append(ret, ops...)
}
} else {
ret = append(ret, NewAtomicOperation(objlist, 0, size))
}
}
return ret, nil
}
func GetPlan2(src DataSource, ctx *FullContext) ([]*AtomicOperation, error) {
ret := make([]*AtomicOperation, 0)
offset := int64(0)
size := src.GetSize()
gran := GetGranularity(src)
for offset < size {
opsize := gran
if offset+opsize > size {
opsize = size - offset
}
if ops, err := GetBlockOps(src, offset, opsize, ctx); err != nil {
return nil, err
} else {
ret = append(ret, ops...)
}
offset += opsize
}
sum := int64(0)
for _, op := range ret {
Debugf("Entry: %s", op.ToString())
sum += op.size
}
Debugf("Total size: %dMB", sum/MB)
return ret, nil
}
func (v *Volume) GetSnapByRef(snapref int64) (*Snapshot, error) {
for _, snap := range v.Snapshots {
if snap.snap_ref == snapref {
return snap, nil
}
}
return nil, not_found
}
func StartsWith(s string, sub string) bool {
return strings.Index(s, sub) == 0
}
func ReadVolume(path string) (*Volume, error) {
if _, err := os.ReadFile(path); err != nil {
return nil, err
} else {
return nil, not_impl
}
}
func UnpackPg(path string) (int64, int) {
tokens := strings.Split(path, "_")
tokens = strings.Split(tokens[0], ".")
pool, _ := strconv.ParseInt(tokens[0], 10, 64)
pg, _ := strconv.ParseInt(tokens[1], 16, 32)
return pool, int(pg)
}
func ScanOsd(osdpath string, osdnum int) ([]*ObjectAddress, error) {
ret := make([]*ObjectAddress, 0, 10000)
if entries, err := os.ReadDir(osdpath); err != nil {
return nil, err
} else {
for _, item := range entries {
if !strings.Contains(item.Name(), "_head") {
continue
}
pool, pg := UnpackPg(item.Name())
if objects, err := ScanPg(osdpath, osdnum, pool, pg); err != nil {
return nil, err
} else {
ret = append(ret, objects...)
}
}
return ret, nil
}
}
func ScanPg(osdpath string, osdnum int, pool int64, pg int) ([]*ObjectAddress, error) {
ret := make([]*ObjectAddress, 0, 10000)
opath := fmt.Sprintf("%s/%d.%x_head/all", osdpath, pool, pg)
if entries, err := os.ReadDir(opath); err != nil {
return nil, err
} else {
for _, item := range entries {
if obj, err := ParseObjectByName(pool, pg, item.Name()); err == nil {
obj.osdnum = osdnum
ret = append(ret, obj)
} else {
fmt.Printf("Failed parse %s/%s - %s\n", opath, item.Name(), err)
}
}
return ret, nil
}
}
func ParseObjectByName(pool int64, pg int, basename string) (*ObjectAddress, error) {
tokens := strings.Split(basename, ":")
if len(tokens) != 6 {
return nil, unmatched
}
if tokens[4] == "" {
return nil, unmatched
}
snap_ref := int64(0)
var err error
if tokens[5] == "head#" {
snap_ref = -1
} else if snap_ref, err = strconv.ParseInt(tokens[5][0:len(tokens[5])-1], 16, 64); err != nil {
return nil, unmatched
}
ret := &ObjectAddress{
pool: pool, pg: pg,
prefix: fmt.Sprintf("%s:%s:::", tokens[0], tokens[1]),
name: tokens[4],
snap_ref: snap_ref,
}
return ret, nil
}
func ByteSliceToInt64(data []byte) int64 {
ret := int64(0)
scale := int64(1)
for i := 0; i < 8; i++ {
ret += scale * int64(data[i])
if i < 7 {
scale = scale * 256
}
}
return ret
}
func ByteSliceToInt(data []byte) int {
ret := 0
scale := 1
for i := 0; i < 4; i++ {
ret += scale * int(data[i])
if i < 3 {
scale = scale * 256
}
}
return ret
}
func ReadObjectMeta(osdpath string, obj *ObjectAddress) error {
suffix := "head#"
if obj.snap_ref > 0 {
obj.version = 0
suffix = fmt.Sprintf("%x#", obj.snap_ref)
}
xdir := fmt.Sprintf("%s/%d.%x_head/all/%s%s:%s", osdpath, obj.pool, obj.pg, obj.prefix, obj.name, suffix)
path := fmt.Sprintf("%s/attr/_", xdir)
if _data, err := os.ReadFile(path); err == nil {
base := magic_version_ofset + len(obj.name)
vdata := _data[base : base+8]
obj.version = ByteSliceToInt64(vdata)
}
path = fmt.Sprintf("%s/data", xdir)
if sd, err := os.Stat(path); err == nil {
obj.size = sd.Size()
} else {
fmt.Printf("failed to stat %s/data\n", xdir)
}
return nil
}
type ObjectLister interface {
ListObjects(pool int64, name_or_prefix string, suggest_prefix bool) ([]*ObjectAddress, error)
Load() error
Save() error
Size() int
}
type ObjectStore interface {
ObjectLister
AddObject(*ObjectAddress) error
}
type MultiStore struct {
listers []ObjectLister
}
func NewMultiStore() *MultiStore {
return &MultiStore{
listers: make([]ObjectLister, 0, 100),
}
}
func (ms *MultiStore) Load() error {
for _, l := range ms.listers {
if err := l.Load(); err != nil {
return err
}
}
return nil
}
func (ms *MultiStore) Save() error {
for _, l := range ms.listers {
if err := l.Save(); err != nil {
return err
}
}
return nil
}
func (ms *MultiStore) Size() int {
ret := 0
for _, l := range ms.listers {
ret += l.Size()
}
return ret
}
func (ms *MultiStore) ListObjects(pool int64, name_or_prefix string, suggest_prefix bool) ([]*ObjectAddress, error) {
ret := make([]*ObjectAddress, 0, 100)
for _, ls := range ms.listers {
if s, err := ls.ListObjects(pool, name_or_prefix, suggest_prefix); err != nil {
return nil, err
} else {
ret = append(ret, s...)
}
}
return ret, nil
}
type GenericObjectStore struct{}
func (*GenericObjectStore) Size() int { return 0 }
func (*GenericObjectStore) AddObject(obj *ObjectAddress) error { return not_impl }
func (*GenericObjectStore) ListObjects(pool int64, name_or_prefix string, suggest_prefix bool) ([]*ObjectAddress, error) {
return nil, not_impl
}
func SortFunc(a []*ObjectAddress, cfunc func(l *ObjectAddress, r *ObjectAddress) int) {
for i := 0; i < len(a)-1; i++ {
for j := i + 1; j < len(a); j++ {
x := cfunc(a[i], a[j])
if x > 0 {
v := a[i]
a[i] = a[j]
a[j] = v
}
}
}
}
func FindObject(store ObjectLister, pool int64, name string, snapref int64) ([]*ObjectAddress, error) {
if matched, err := store.ListObjects(pool, name, false); err != nil {
return nil, err
} else {
all := make([]*ObjectAddress, 0, 10)
for _, addr := range matched {
if addr.pool != pool {
continue
} else if addr.name != name {
continue
} else {
if snapref < 0 {
if addr.snap_ref == -1 {
all = append(all, addr)
}
} else {
if addr.snap_ref >= snapref || addr.snap_ref == -1 {
all = append(all, addr)
}
}
}
}
SortFunc(all, ObjectCompare)
if len(all) > 1 {
maxver := all[0].version
isnapref := all[0].snap_ref
for _, addr := range all {
if addr.version > maxver && isnapref == addr.snap_ref {
maxver = addr.version
}
}
nall := make([]*ObjectAddress, 0, len(all))
for _, addr := range all {
if addr.version == maxver && addr.snap_ref == isnapref {
nall = append(nall, addr)
}
}
all = nall
}
return all, nil
}
}
type InMemoryObjectStore struct {
GenericObjectStore
objects []*ObjectAddress
}
func (store *InMemoryObjectStore) Size() int { return len(store.objects) }
func (store *InMemoryObjectStore) AddObject(obj *ObjectAddress) error {
store.objects = append(store.objects, obj)
return nil
}
func (store *InMemoryObjectStore) ListObjects(pool int64, name_or_prefix string, suggest_prefix bool) ([]*ObjectAddress, error) {
ret := make([]*ObjectAddress, 0, 100)
slen := len(name_or_prefix)
for _, addr := range store.objects {
if !suggest_prefix {
if name_or_prefix == addr.name {
ret = append(ret, addr)
}
}
if addr.pool != pool {
continue
} else if !suggest_prefix {
if name_or_prefix == addr.name {
ret = append(ret, addr)
}
} else if len(addr.name) < slen {
continue
} else if addr.name[0:slen] == name_or_prefix {
ret = append(ret, addr)
}
}
return ret, nil
}
func ObjectCompare(l *ObjectAddress, r *ObjectAddress) int {
if l.snap_ref < 0 && r.snap_ref < 0 {
if l.version > r.version {
return -1
} else if l.version == r.version {
return 0
} else {
return -1
}
} else if l.snap_ref < 0 && r.snap_ref > 0 {
return 1
} else if l.snap_ref > 0 && r.snap_ref < 0 {
return -1
} else if l.snap_ref < r.snap_ref {
return -1
} else if l.snap_ref > r.snap_ref {
return 1
} else {
return 0
}
}
func (store *InMemoryObjectStore) Load() error {
return nil
}
func (store *InMemoryObjectStore) Save() error {
return not_impl
}
func InitInMemoryObjectStore(store *InMemoryObjectStore) *InMemoryObjectStore {
store.objects = make([]*ObjectAddress, 0, 100000)
return store
}
func NewInMemoryObjectStore() ObjectStore {
return InitInMemoryObjectStore(&InMemoryObjectStore{})
}
type DebugObjectStore struct {
InMemoryObjectStore
}
func (dos *DebugObjectStore) AddObject(obj *ObjectAddress) error {
return dos.InMemoryObjectStore.AddObject(obj)
}
func NewDebugObjectStore() ObjectStore {
ret := &DebugObjectStore{}
InitInMemoryObjectStore(&ret.InMemoryObjectStore)
return ret
}
type CSVObjectStore struct {
InMemoryObjectStore
filename string
}
func (dos *CSVObjectStore) AddObject(obj *ObjectAddress) error {
return dos.InMemoryObjectStore.AddObject(obj)
}
func NewCSVObjectStore(path string) ObjectStore {
ret := &CSVObjectStore{filename: path}
InitInMemoryObjectStore(&ret.InMemoryObjectStore)
return ret
}
func (store *CSVObjectStore) Load() error {
if f, err := os.Open(store.filename); err != nil {
return nil
} else {
defer f.Close()
reader := csv.NewReader(f)
for {
tokens, err := reader.Read()
if err == io.EOF {
return nil
} else if err != nil {
return err
}
objaddr := &ObjectAddress{}
if objaddr.osdnum, err = strconv.Atoi(tokens[0]); err != nil {
return err
}
if objaddr.pool, err = strconv.ParseInt(tokens[1], 10, 64); err != nil {
return err
}
if objaddr.pg, err = strconv.Atoi(tokens[2]); err != nil {
return err
}
objaddr.prefix = tokens[3]
objaddr.name = tokens[4]
if objaddr.snap_ref, err = strconv.ParseInt(tokens[5], 10, 64); err != nil {
return err
}
if objaddr.version, err = strconv.ParseInt(tokens[6], 10, 64); err != nil {
return err
}
if objaddr.size, err = strconv.ParseInt(tokens[7], 10, 64); err != nil {
return err
}
store.AddObject(objaddr)
}
}
}
func Itoa(n int) string { return fmt.Sprintf("%d", n) }
func Ltoa(n int64) string { return fmt.Sprintf("%d", n) }
func (store *CSVObjectStore) Save() error {
if f, err := os.OpenFile(store.filename, os.O_WRONLY|os.O_CREATE, 0644); err != nil {
return err
} else {
defer f.Close()
writer := csv.NewWriter(f)
for _, e := range store.objects {
vals := make([]string, 0, 8)
vals = append(vals, Itoa(e.osdnum), Ltoa(e.pool), Itoa(e.pg))
vals = append(vals, e.prefix, e.name)
vals = append(vals, Ltoa(e.snap_ref), Ltoa(e.version), Ltoa(e.size))
if err := writer.Write(vals); err != nil {
return err
}
}
writer.Flush()
return nil
}
}
type OSDAccessor interface {
ListOmaps(obj *ObjectAddress) ([]string, error)
GetOmap(obj *ObjectAddress, name string) ([]byte, error)
GetData(obj *ObjectAddress) ([]byte, error)
}
type LocalOSDAccessor struct {
basepath string
}
func (oa *LocalOSDAccessor) ListOmaps(obj *ObjectAddress) ([]string, error) {
snap_ref := "head#"
if obj.snap_ref > 0 {
snap_ref = fmt.Sprintf("%x", obj.snap_ref)
}
fpath := fmt.Sprintf(
"%s/%d.%x_head/all/%s%s:%s/omap",
oa.basepath, obj.pool, obj.pg, obj.prefix, obj.name, snap_ref,
)
if ret, err := os.ReadDir(fpath); err != nil {
return nil, not_found
} else {
aret := make([]string, 0, len(ret))
for _, s := range ret {
if s.Name() != "." && s.Name() != ".." {
aret = append(aret, s.Name())
}
}
return aret, nil
}
}
func (oa *LocalOSDAccessor) GetOmap(obj *ObjectAddress, name string) ([]byte, error) {
snap_ref := "head#"
if obj.snap_ref > 0 {
snap_ref = fmt.Sprintf("%x", obj.snap_ref)
}
path := fmt.Sprintf(
"%s/%d.%x_head/all/%s%s:%s/omap/%s",
oa.basepath, obj.pool, obj.pg, obj.prefix, obj.name, snap_ref, name,
)
if !FileExists(path) {
return nil, not_found
} else if fd, err := os.Open(path); err != nil {
return nil, not_found
} else {
defer fd.Close()
if data, err := io.ReadAll(fd); err != nil {
return nil, not_found
} else {
return data, nil
}
}
}
func FileExists(fpath string) bool {
if _, err := os.Stat(fpath); err != nil {
return false
} else {
return true
}
}
func (oa *LocalOSDAccessor) GetData(obj *ObjectAddress) ([]byte, error) {
snap_ref := "head#"
if obj.snap_ref > 0 {
snap_ref = fmt.Sprintf("%x#", obj.snap_ref)
}
path := fmt.Sprintf(
"%s/%d.%x_head/all/%s%s:%s/data",
oa.basepath, obj.pool, obj.pg, obj.prefix, obj.name, snap_ref,
)
if !FileExists(path) {
fmt.Println("ERROR: file not found", path)
return nil, not_found
} else if fd, err := os.Open(path); err != nil {
return nil, err
} else {
defer fd.Close()
if data, err := io.ReadAll(fd); err != nil {
return nil, err
} else {
return data, nil
}
}
}
func NewLocalOSDAccessor(basepath string) OSDAccessor {
return &LocalOSDAccessor{
basepath: basepath,
}
}
type HttpOSDAccessor struct {
baseurl string
}
func (oa *HttpOSDAccessor) ListOmaps(obj *ObjectAddress) ([]string, error) {
snap_ref := "head#"
if obj.snap_ref > 0 {
snap_ref = fmt.Sprintf("%x#", obj.snap_ref)
}
url := fmt.Sprintf(
"%s/%d.%x_head/all/%s%s:%s/omap",
oa.baseurl, obj.pool, obj.pg,
url.PathEscape(obj.prefix), url.PathEscape(obj.name), url.PathEscape(snap_ref),
)
if resp, err := http.Get(url); err != nil {
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("Remote error: %d", resp.StatusCode)
} else {
ret := make([]byte, 0, 1024*1024)
for {
buf := make([]byte, 1024*1024)
cnt, err := resp.Body.Read(buf)
if cnt > 0 {
ret = append(ret, buf[0:cnt]...)
}
if err == io.EOF {
break
} else {
return nil, err
}
}
lret := make([]string, 0, 128)
if err := json.Unmarshal(ret, &lret); err != nil {
return nil, err
}
return lret, nil
}
}
func (oa *HttpOSDAccessor) GetOmap(obj *ObjectAddress, name string) ([]byte, error) {
snap_ref := "head#"
if obj.snap_ref > 0 {
snap_ref = fmt.Sprintf("%x#", obj.snap_ref)
}
url := fmt.Sprintf(
"%s/%d.%x_head/all/%s%s:%s/omap/%s",
oa.baseurl, obj.pool, obj.pg,
url.PathEscape(obj.prefix), url.PathEscape(obj.name), url.PathEscape(snap_ref),
name,
)
if resp, err := http.Get(url); err != nil {
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("Remote error: %d", resp.StatusCode)
} else {
ret := make([]byte, 0, 4*1024*1024)
for {
buf := make([]byte, 1024*1024)
cnt, err := resp.Body.Read(buf)
if cnt > 0 {
ret = append(ret, buf[0:cnt]...)
}
if err == io.EOF {
break
} else {
return nil, err
}
}
return ret, nil
}
}
func (oa *HttpOSDAccessor) GetData(obj *ObjectAddress) ([]byte, error) {
snap_ref := "head#"
if obj.snap_ref > 0 {
snap_ref = fmt.Sprintf("%x#", obj.snap_ref)
}
url := fmt.Sprintf(
"%s/%d.%x_head/all/%s%s:%s/data",
oa.baseurl, obj.pool, obj.pg,
url.PathEscape(obj.prefix), url.PathEscape(obj.name), url.PathEscape(snap_ref),
)
if resp, err := http.Get(url); err != nil {
return nil, err
} else if resp.StatusCode != 200 {
return nil, fmt.Errorf("Remote error: %d", resp.StatusCode)
} else {
ret := make([]byte, 0, 4*1024*1024)
for {
buf := make([]byte, 1024*1024)
cnt, err := resp.Body.Read(buf)
if cnt > 0 {
ret = append(ret, buf[0:cnt]...)
}
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
}
return ret, nil
}
}
func NewHttpOSDAccessor(basepath string) OSDAccessor {
return &HttpOSDAccessor{
baseurl: basepath,
}
}
func run_scan(objectstore ObjectStore, xargs []string) {
for _, arg := range xargs {
tokens := strings.Split(arg, "=")
if len(tokens) != 2 {
Debugf("Omit arg %s", arg)
continue
} else if osdnum, err := strconv.Atoi(tokens[0]); err != nil {
Debugf("Omit arg %s", arg)
continue
} else if items, err := ScanOsd(tokens[1], osdnum); err != nil {
Debugf("Error scanning OSD: %s", err)
} else {
for _, obj := range items {
if err := ReadObjectMeta(tokens[1], obj); err != nil {
Debugf("Failed get meta for osd %d object %d.%x %s:%x - %s", obj.osdnum, obj.pool, obj.pg, obj.name, obj.snap_ref, err)
}
}
for _, obj := range items {
objectstore.AddObject(obj)
}
}
}
if err := objectstore.Save(); err != nil {
fmt.Println("error saving objects:", err)
} else {
fmt.Printf("%d objects stored\n", objectstore.Size())
}
}
func SimplifyPath(path string) (string, error) {
ptokens := strings.Split(path, "/")
ret := make([]string, 0, len(ptokens))
for _, i := range ptokens {
if i == "." {
continue
} else if i == ".." {
if len(ret) > 1 {
ret = ret[0 : len(ret)-1]
} else if len(ret) > 0 {
ret = make([]string, 0, len(ptokens))
}
} else if i == "" {
continue
} else {
if s, err := url.PathUnescape(i); err != nil {
return "", err
} else {
ret = append(ret, s)
}
}
}
return strings.Join(ret, "/"), nil
}
func run_server(wg *sync.WaitGroup, paddr string, proot string) {
defer wg.Done()
handler := func(w http.ResponseWriter, req *http.Request) {
spath, err := SimplifyPath(req.URL.Path)
if err != nil {
w.Header().Add("Content-Type", "application/text")
w.WriteHeader(400)
} else {
apath := fmt.Sprintf("%s/%s", proot, spath)
if files, err := os.ReadDir(apath); err == nil {
fmt.Println("Request list", spath, "from", apath, "OK")
names := make([]string, 0, len(files))
for _, s := range files {
names = append(names, s.Name())
}