-
Notifications
You must be signed in to change notification settings - Fork 338
/
simple.go
159 lines (128 loc) · 3.75 KB
/
simple.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
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package manifest
import (
"context"
"errors"
"fmt"
"github.com/ethersphere/bee/v2/pkg/file"
"github.com/ethersphere/bee/v2/pkg/manifest/simple"
"github.com/ethersphere/bee/v2/pkg/swarm"
)
const (
// ManifestSimpleContentType represents content type used for noting that
// specific file should be processed as 'simple' manifest
ManifestSimpleContentType = "application/bzz-manifest-simple+json"
)
type simpleManifest struct {
manifest simple.Manifest
reference swarm.Address
ls file.LoadSaver
}
// NewSimpleManifest creates a new simple manifest.
func NewSimpleManifest(ls file.LoadSaver) (Interface, error) {
return &simpleManifest{
manifest: simple.NewManifest(),
ls: ls,
}, nil
}
// NewSimpleManifestReference loads existing simple manifest.
func NewSimpleManifestReference(ref swarm.Address, l file.LoadSaver) (Interface, error) {
m := &simpleManifest{
manifest: simple.NewManifest(),
reference: ref,
ls: l,
}
err := m.load(context.Background(), ref)
return m, err
}
func (m *simpleManifest) Type() string {
return ManifestSimpleContentType
}
func (m *simpleManifest) Add(_ context.Context, path string, entry Entry) error {
e := entry.Reference().String()
return m.manifest.Add(path, e, entry.Metadata())
}
func (m *simpleManifest) Remove(_ context.Context, path string) error {
err := m.manifest.Remove(path)
if err != nil {
if errors.Is(err, simple.ErrNotFound) {
return ErrNotFound
}
return err
}
return nil
}
func (m *simpleManifest) Lookup(_ context.Context, path string) (Entry, error) {
n, err := m.manifest.Lookup(path)
if err != nil {
return nil, ErrNotFound
}
address, err := swarm.ParseHexAddress(n.Reference())
if err != nil {
return nil, fmt.Errorf("parse swarm address: %w", err)
}
entry := NewEntry(address, n.Metadata())
return entry, nil
}
func (m *simpleManifest) HasPrefix(_ context.Context, prefix string) (bool, error) {
return m.manifest.HasPrefix(prefix), nil
}
func (m *simpleManifest) Store(ctx context.Context, storeSizeFn ...StoreSizeFunc) (swarm.Address, error) {
data, err := m.manifest.MarshalBinary()
if err != nil {
return swarm.ZeroAddress, fmt.Errorf("manifest marshal error: %w", err)
}
if len(storeSizeFn) > 0 {
dataLen := int64(len(data))
for i := range storeSizeFn {
err = storeSizeFn[i](dataLen)
if err != nil {
return swarm.ZeroAddress, fmt.Errorf("manifest store size func: %w", err)
}
}
}
ref, err := m.ls.Save(ctx, data)
if err != nil {
return swarm.ZeroAddress, fmt.Errorf("manifest save error: %w", err)
}
m.reference = swarm.NewAddress(ref)
return m.reference, nil
}
func (m *simpleManifest) IterateAddresses(ctx context.Context, fn swarm.AddressIterFunc) error {
if swarm.ZeroAddress.Equal(m.reference) {
return ErrMissingReference
}
// NOTE: making it behave same for all manifest implementation
err := fn(m.reference)
if err != nil {
return fmt.Errorf("manifest iterate addresses: %w", err)
}
walker := func(path string, entry simple.Entry, err error) error {
if err != nil {
return err
}
ref, err := swarm.ParseHexAddress(entry.Reference())
if err != nil {
return err
}
return fn(ref)
}
err = m.manifest.WalkEntry("", walker)
if err != nil {
return fmt.Errorf("manifest iterate addresses: %w", err)
}
return nil
}
func (m *simpleManifest) load(ctx context.Context, reference swarm.Address) error {
buf, err := m.ls.Load(ctx, reference.Bytes())
if err != nil {
return fmt.Errorf("manifest load error: %w", err)
}
err = m.manifest.UnmarshalBinary(buf)
if err != nil {
return fmt.Errorf("manifest unmarshal error: %w", err)
}
return nil
}