-
Notifications
You must be signed in to change notification settings - Fork 0
/
sindri.go
904 lines (732 loc) · 19.1 KB
/
sindri.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
package sindri
import (
"archive/tar"
"archive/zip"
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/frantjc/go-steamcmd"
"github.com/frantjc/sindri/thunderstore"
xtar "github.com/frantjc/x/archive/tar"
xzip "github.com/frantjc/x/archive/zip"
xio "github.com/frantjc/x/io"
xslice "github.com/frantjc/x/slice"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/tarball"
"golang.org/x/exp/maps"
)
// ModMetadata stores metadata about an added mod.
type ModMetadata struct {
LayerDigest string `json:"layerDigest,omitempty"`
Version string `json:"version,omitempty"`
}
type SteamAppMetadata struct {
LayerDigest string `json:"layerDigest,omitempty"`
}
// Metadata stores metadata about a downloaded game
// and added mods.
type Metadata struct {
SteamApps map[string]SteamAppMetadata `json:"steamApps,omitempty"`
Mods map[string]ModMetadata `json:"mods,omitempty"`
}
// Sindri manages the files of a game and its mods.
type Sindri struct {
BepInEx *thunderstore.Package
ThunderstoreClient *thunderstore.Client
mu *sync.Mutex
stateDir, rootDir string
img v1.Image
tag *name.Tag
metadata *Metadata
initialized bool
beta, betaPassword string
}
// Opt is an option to pass when creating
// a new Sindri instance.
type Opt func(*Sindri)
// WithRootDir sets a *Sindri's root directory
// where it will store any persistent data.
func WithRootDir(dir string) Opt {
return func(s *Sindri) {
s.rootDir = dir
}
}
// WithStateDir sets a *Sindri's state directory
// where it will store any ephemeral data.
func WithStateDir(dir string) Opt {
return func(s *Sindri) {
s.stateDir = dir
}
}
// WithBeta makes Sindri use the given Steam beta.
func WithBeta(beta string, password string) Opt {
return func(s *Sindri) {
s.beta = beta
s.betaPassword = password
}
}
const (
// ImageRef is the image reference that Sindri
// stores a game and its mods' files at inside
// of it's .tar file.
ImageRef = "frantj.cc/sindri"
// MetadataLayerDigestLabel is the image config file label
// that Sindri stores Metadata at.
MetadataLayerDigestLabel = "cc.frantj.sindri.metadata-layer-digest"
)
// New creates a new Sindri instance with the given
// required arguments and options. Sindri can also be
// safely created directly so long as the exported
// fields are set to non-nil values.
func New(bepInEx *thunderstore.Package, thunderstoreClient *thunderstore.Client, opts ...Opt) (*Sindri, error) {
s := &Sindri{
BepInEx: bepInEx,
ThunderstoreClient: thunderstoreClient,
}
return s, s.init(opts...)
}
// Mods returns the installed thunderstore.io packages.
func (s *Sindri) Mods() ([]thunderstore.Package, error) {
pkgs := []thunderstore.Package{}
for k, v := range s.metadata.Mods {
pkg, err := thunderstore.ParsePackage(k + "-" + v.Version)
if err != nil {
return nil, err
}
pkgs = append(pkgs, *pkg)
}
return pkgs, nil
}
type readCloser struct {
io.Reader
io.Closer
}
func reproducibleBuildLayerFromDir(dir string) (v1.Layer, error) {
return tarball.LayerFromOpener(func() (io.ReadCloser, error) {
var (
rc1 = xtar.Compress(dir)
rc2 = xtar.ModifyHeaders(tar.NewReader(rc1), func(h *tar.Header) {
h.ModTime = sourceDateEpoch
})
)
return &readCloser{
Reader: rc2,
Closer: xio.CloserFunc(func() error {
return errors.Join(rc2.Close(), rc1.Close())
}),
}, nil
})
}
// AppUpdate uses `steamcmd` to installed or update
// the game that *Sindri is managing.
func (s *Sindri) AppUpdate(ctx context.Context, steamAppID string) error {
if steamAppID == "" {
return fmt.Errorf("empty SteamAppID")
}
s.mu.Lock()
defer s.mu.Unlock()
if err := s.init(); err != nil {
return err
}
steamcmdForceInstallDir, err := os.MkdirTemp(s.stateDir, "steamapp-*")
if err != nil {
return err
}
defer os.RemoveAll(steamcmdForceInstallDir)
if err := steamcmd.Command("steamcmd").AppUpdate(ctx, &steamcmd.AppUpdateCombined{
ForceInstallDir: steamcmdForceInstallDir,
AppUpdate: &steamcmd.AppUpdate{
AppID: steamAppID,
Beta: s.beta,
BetaPassword: s.betaPassword,
Validate: true,
},
}); err != nil {
return err
}
steamAppLayer, err := reproducibleBuildLayerFromDir(steamcmdForceInstallDir)
if err != nil {
return err
}
steamAppLayerDigest, err := steamAppLayer.Digest()
if err != nil {
return err
}
if s.metadata.SteamApps == nil {
s.metadata.SteamApps = map[string]SteamAppMetadata{}
}
if steamAppMetadata, ok := s.metadata.SteamApps[steamAppID]; ok {
// If the digest hasn't changed, we don't need to spend
// any more time on this.
if steamAppMetadata.LayerDigest == steamAppLayerDigest.String() {
return nil
}
}
layers, err := s.img.Layers()
if err != nil {
return err
}
filteredLayers := []v1.Layer{steamAppLayer}
for _, layer := range layers {
digest, err := layer.Digest()
if err != nil {
return err
}
if s.metadata.SteamApps[steamAppID].LayerDigest != digest.String() {
filteredLayers = append(filteredLayers, layer)
}
}
if s.img, err = mutate.AppendLayers(empty.Image, filteredLayers...); err != nil {
return err
}
s.metadata.SteamApps[steamAppID] = SteamAppMetadata{LayerDigest: steamAppLayerDigest.String()}
return s.save()
}
// AddMods installs or updates the given mods and their
// dependencies using thunderstore.io.
func (s *Sindri) AddMods(ctx context.Context, mods ...string) error {
switch {
case len(mods) == 0:
return nil
case s.BepInEx == nil:
return fmt.Errorf("nil BepInEx Package")
case s.ThunderstoreClient == nil:
return fmt.Errorf("nil ThunderstoreClient")
}
s.mu.Lock()
defer s.mu.Unlock()
if err := s.init(); err != nil {
return err
}
layers, err := s.img.Layers()
if err != nil {
return err
}
for _, mod := range append(
xslice.Unique(mods),
s.BepInEx.Versionless().String(),
) {
pkg, err := thunderstore.ParsePackage(mod)
if err != nil {
return err
}
var (
modKey = pkg.Versionless().String()
modMeta, ok = s.metadata.Mods[modKey]
)
// If the mod version hasn't changed, no need to
// spend any time on it.
if ok {
if modMeta.Version == pkg.Version {
continue
}
}
pkgUnzipDir, err := os.MkdirTemp(s.stateDir, pkg.Fullname()+"-*")
if err != nil {
return err
}
defer os.RemoveAll(pkgUnzipDir)
if err := s.extractModsAndDependenciesToDir(ctx, pkgUnzipDir, mod); err != nil {
return err
}
modLayer, err := reproducibleBuildLayerFromDir(pkgUnzipDir)
if err != nil {
return err
}
modLayerDigest, err := modLayer.Digest()
if err != nil {
return err
}
// If the digest hasn't changed, we don't need to spend
// any more time on this.
if ok && modMeta.LayerDigest == modLayerDigest.String() {
continue
}
fileteredLayers := []v1.Layer{}
for _, layer := range layers {
digest, err := layer.Digest()
if err != nil {
return err
}
if digest.String() != modMeta.LayerDigest {
fileteredLayers = append(fileteredLayers, layer)
}
}
layers = fileteredLayers
layers = append(layers, modLayer)
s.metadata.Mods[modKey] = ModMetadata{
Version: pkg.Version,
LayerDigest: modLayerDigest.String(),
}
}
if s.img, err = mutate.AppendLayers(empty.Image, layers...); err != nil {
return err
}
return s.save()
}
// RemoveMods removes the given mods.
func (s *Sindri) RemoveMods(_ context.Context, mods ...string) error {
if len(mods) == 0 {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
if err := s.init(); err != nil {
return err
}
layers, err := s.img.Layers()
if err != nil {
return err
}
for _, mod := range mods {
pkg, err := thunderstore.ParsePackage(mod)
if err != nil {
return err
}
if pkg.Versionless().String() == s.BepInEx.Versionless().String() {
return fmt.Errorf("cannot remove BepInEx")
}
var (
modKey = pkg.Versionless().String()
modMeta, ok = s.metadata.Mods[modKey]
)
if !ok {
continue
}
fileteredLayers := []v1.Layer{}
for _, layer := range layers {
digest, err := layer.Digest()
if err != nil {
return err
}
if digest.String() != modMeta.LayerDigest {
fileteredLayers = append(fileteredLayers, layer)
}
}
delete(s.metadata.Mods, modKey)
layers = fileteredLayers
}
if s.img, err = mutate.AppendLayers(empty.Image, layers...); err != nil {
return err
}
return s.save()
}
var sourceDateEpoch = time.Date(1970, time.January, 1, 0, 0, 0, 0, time.UTC).
Add(func() time.Duration {
sourceDateEpoch := os.Getenv("SOURCE_DATE_EPOCH")
if sourceDateEpoch == "" {
return 0
}
if seconds, err := strconv.Atoi(sourceDateEpoch); err == nil {
return time.Second * time.Duration(seconds)
}
return 0
}())
// Extract returns an io.ReadCloser of a tarball
// containing the files of the game and the given mods.
func (s *Sindri) Extract(steamAppIDs []string, mods ...string) (io.ReadCloser, error) {
s.mu.Lock()
defer s.mu.Unlock()
layers, err := s.steamAppLayers(steamAppIDs...)
if err != nil {
return nil, err
}
modLayers, err := s.modLayers(mods...)
if err != nil {
return nil, err
}
layers = append(layers, modLayers...)
img, err := mutate.AppendLayers(empty.Image, layers...)
if err != nil {
return nil, err
}
now := time.Now()
return xtar.ModifyHeaders(tar.NewReader(mutate.Extract(img)), func(h *tar.Header) {
h.ModTime = now
}), nil
}
// ExtractMods returns an io.ReadCloser containing a tarball
// containing the files just the game's mods.
func (s *Sindri) ExtractMods(mods ...string) (io.ReadCloser, error) {
s.mu.Lock()
defer s.mu.Unlock()
modLayers, err := s.modLayers(mods...)
if err != nil {
return nil, err
}
img, err := mutate.AppendLayers(empty.Image, modLayers...)
if err != nil {
return nil, err
}
now := time.Now()
return xtar.ModifyHeaders(tar.NewReader(mutate.Extract(img)), func(h *tar.Header) {
h.ModTime = now
}), nil
}
func (s *Sindri) save() error {
metadataLayer, err := tarball.LayerFromOpener(func() (io.ReadCloser, error) {
var (
buf = new(bytes.Buffer)
tw = tar.NewWriter(buf)
)
b, err := json.Marshal(s.metadata)
if err != nil {
return nil, err
}
if err = tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeReg,
Name: s.metadataName(),
Size: int64(len(b)),
Mode: 0644,
ModTime: sourceDateEpoch,
}); err != nil {
return nil, err
}
if _, err = tw.Write(b); err != nil {
return nil, err
}
if err = tw.Close(); err != nil {
return nil, err
}
return io.NopCloser(buf), nil
})
if err != nil {
return err
}
metadataLayerDigest, err := metadataLayer.Digest()
if err != nil {
return err
}
configFile, err := s.img.ConfigFile()
if err != nil {
configFile = &v1.ConfigFile{
Config: v1.Config{
Labels: make(map[string]string),
},
}
}
oldMetadataLayerDigest := configFile.Config.Labels[MetadataLayerDigestLabel]
layers, err := s.img.Layers()
if err != nil {
return err
}
newLayers := []v1.Layer{metadataLayer}
if oldMetadataLayerDigest != "" {
for _, layer := range layers {
digest, err := layer.Digest()
if err != nil {
return err
}
if digest.String() != oldMetadataLayerDigest {
newLayers = append(newLayers, layer)
}
}
} else {
newLayers = append(newLayers, layers...)
}
if s.img, err = mutate.AppendLayers(empty.Image, newLayers...); err != nil {
return err
}
configFile, err = s.img.ConfigFile()
if err != nil {
configFile = &v1.ConfigFile{
Config: v1.Config{
Labels: make(map[string]string),
},
}
} else if configFile.Config.Labels == nil {
configFile.Config.Labels = map[string]string{}
}
maps.Copy(configFile.Config.Labels, map[string]string{
MetadataLayerDigestLabel: metadataLayerDigest.String(),
})
if s.img, err = mutate.ConfigFile(s.img, configFile); err != nil {
return err
}
if err := tarball.WriteToFile(s.tmpDbPath(), name.MustParseReference(ImageRef), s.img); err != nil {
return err
}
if err = os.Rename(s.tmpDbPath(), s.dbPath()); err != nil {
return err
}
img, err := tarball.ImageFromPath(s.dbPath(), s.tag)
if err != nil {
return err
}
s.img = img
return nil
}
func (s *Sindri) extractModsAndDependenciesToDir(ctx context.Context, dir string, mods ...string) error {
errC := make(chan error, 1)
for _, m := range mods {
go func(mod string) {
pkg, err := thunderstore.ParsePackage(mod)
if err != nil {
errC <- err
return
}
var (
modKey = pkg.Versionless().String()
modMeta, ok = s.metadata.Mods[modKey]
)
if ok {
if modMeta.Version == pkg.Version {
return
}
}
// The pkg doesn't need a version to get the metadata
// or the archive, but we want the version so we know
// what version is installed, so we make sure that we
// have it. We also need to know its dependencies.
pkgMeta, err := s.ThunderstoreClient.GetPackageMetadata(ctx, pkg)
if err != nil {
errC <- err
return
}
var (
bepInExKey = s.BepInEx.Versionless().String()
isBepInEx = modKey == bepInExKey
)
dependencies := pkgMeta.Dependencies
if pkg.Version == "" && pkgMeta.Latest != nil {
pkg = &pkgMeta.Latest.Package
dependencies = append(dependencies, pkgMeta.Latest.Dependencies...)
}
dependencies = xslice.Unique(xslice.Filter(dependencies, func(dependency string, _ int) bool {
return !strings.HasPrefix(dependency, bepInExKey)
}))
if err := s.extractModsAndDependenciesToDir(ctx, dir, dependencies...); err != nil {
errC <- err
return
}
pkgZip, err := s.ThunderstoreClient.GetPackageZip(ctx, pkg)
if err != nil {
errC <- err
return
}
defer pkgZip.Close()
pkgZipRdr, err := zip.NewReader(pkgZip, pkgZip.Size())
if err != nil {
errC <- err
return
}
pkgZipRdr.File = xslice.Reduce(pkgZipRdr.File, func(acc []*zip.File, cur *zip.File, _ int) []*zip.File {
norm := strings.ReplaceAll(cur.Name, "\\", "/")
if isBepInEx {
name, err := filepath.Rel(s.BepInEx.Name, norm)
if err != nil {
return acc
}
if strings.Contains(name, "..") {
return acc
}
cur.Name = name
} else {
cur.Name = filepath.Join("BepInEx/plugins", pkg.Fullname(), norm)
}
return append(acc, cur)
}, []*zip.File{})
if err := xzip.Extract(pkgZipRdr, dir); err != nil {
errC <- err
return
}
errC <- nil
}(m)
}
for i := 0; i < len(mods); i++ {
if err := <-errC; err != nil {
return err
}
}
return nil
}
func (s *Sindri) dbPath() string {
return filepath.Join(s.rootDir, "sindri.db")
}
func (s *Sindri) tmpDbPath() string {
return filepath.Join(s.rootDir, "sindri.tmp.db")
}
func (s *Sindri) metadataName() string {
return "sindri.metadata.json"
}
func (s *Sindri) steamAppLayers(steamAppIDs ...string) ([]v1.Layer, error) {
if len(steamAppIDs) == 0 {
return []v1.Layer{}, nil
}
var (
extractLayerDigests = []string{}
lenSteamAppIDs = len(steamAppIDs)
)
for _, steamAppID := range steamAppIDs {
if steamAppMeta, ok := s.metadata.SteamApps[steamAppID]; ok {
extractLayerDigests = append(extractLayerDigests, steamAppMeta.LayerDigest)
if len(extractLayerDigests) == lenSteamAppIDs {
// Found them all
break
}
} else {
return nil, fmt.Errorf("could not find steamapp " + steamAppID + " layer digest")
}
}
if len(extractLayerDigests) != lenSteamAppIDs {
return nil, fmt.Errorf("unable to find all steamapp layer digests")
}
return s.layerDigests(extractLayerDigests...)
}
func (s *Sindri) modLayers(mods ...string) ([]v1.Layer, error) {
if len(mods) == 0 {
return []v1.Layer{}, nil
}
mods = xslice.Unique(append(mods, s.BepInEx.Versionless().String()))
var (
extractLayerDigests = []string{}
lenMods = len(mods)
)
for _, mod := range mods {
pkg, err := thunderstore.ParsePackage(mod)
if err != nil {
return nil, err
}
if modMeta, ok := s.metadata.Mods[pkg.Versionless().String()]; ok {
extractLayerDigests = append(extractLayerDigests, modMeta.LayerDigest)
if len(extractLayerDigests) == lenMods {
// Found them all
break
}
} else {
return nil, fmt.Errorf("could not find mod " + mod + " layer digest")
}
}
if len(extractLayerDigests) != lenMods {
return nil, fmt.Errorf("unable to find all mod layer digests")
}
return s.layerDigests(extractLayerDigests...)
}
func (s *Sindri) layerDigests(layerDigests ...string) ([]v1.Layer, error) {
layers, err := s.img.Layers()
if err != nil {
return nil, err
}
var (
filteredLayers = []v1.Layer{}
lenLayerDigests = len(layerDigests)
)
for _, layer := range layers {
digest, err := layer.Digest()
if err != nil {
return nil, err
}
if xslice.Includes(layerDigests, digest.String()) {
filteredLayers = append(filteredLayers, layer)
if len(filteredLayers) == lenLayerDigests {
// Found them all
break
}
}
}
if len(filteredLayers) != lenLayerDigests {
return nil, fmt.Errorf("unable to find all layers by digest")
}
return filteredLayers, nil
}
func (s *Sindri) init(opts ...Opt) error {
if s.initialized {
return nil
}
s.img = empty.Image
s.mu = new(sync.Mutex)
s.metadata = &Metadata{
Mods: map[string]ModMetadata{},
}
for _, opt := range opts {
opt(s)
}
tag, err := name.NewTag(ImageRef)
if err != nil {
return err
}
s.tag = &tag
if s.rootDir == "" || s.stateDir == "" {
wd, err := os.Getwd()
if err != nil {
return err
}
if s.rootDir == "" {
s.rootDir = filepath.Join(wd, "root")
}
if s.stateDir == "" {
s.stateDir = filepath.Join(wd, "state")
}
}
if err := os.MkdirAll(s.stateDir, 0755); err != nil {
return err
}
if err := os.MkdirAll(s.rootDir, 0755); err != nil {
return err
}
if fi, err := os.Stat(s.dbPath()); err == nil && !fi.IsDir() && fi.Size() > 0 {
if s.img, err = tarball.ImageFromPath(s.dbPath(), s.tag); err != nil {
return err
}
}
configFile, err := s.img.ConfigFile()
if err != nil {
return err
}
if metadataLayerDigest, ok := configFile.Config.Labels[MetadataLayerDigestLabel]; ok {
layers, err := s.img.Layers()
if err != nil {
return err
}
var (
found = false
metadataName = s.metadataName()
)
for _, layer := range layers {
digest, err := layer.Digest()
if err != nil {
return err
}
if digest.String() == metadataLayerDigest {
found = true
rc, err := layer.Uncompressed()
if err != nil {
return err
}
defer rc.Close()
metadataTarReader := tar.NewReader(rc)
for {
hdr, err := metadataTarReader.Next()
if errors.Is(err, io.EOF) {
return fmt.Errorf("unable to find metadata in metadata layer")
} else if err != nil {
return err
}
if hdr.Name == metadataName {
if err = json.NewDecoder(metadataTarReader).Decode(s.metadata); err != nil {
return err
}
break
}
}
}
}
if !found {
return fmt.Errorf("unable to find metadata layer")
}
}
s.initialized = true
return nil
}