@@ -11,7 +11,6 @@ import (
11
11
"strings"
12
12
13
13
cbor "github.com/fxamacker/cbor/v2"
14
- "github.com/multiformats/go-multibase"
15
14
)
16
15
17
16
type fileInfoDirEntry struct {
@@ -42,15 +41,7 @@ func newDirEntry(path string) (fs.DirEntry, error) {
42
41
return fileInfoDirEntry {fileInfo : fileInfo }, nil
43
42
}
44
43
45
- func CreateDag (path string , encoding ... multibase.Encoding ) (* Dag , error ) {
46
- var e multibase.Encoding
47
- if len (encoding ) > 0 {
48
- e = encoding [0 ]
49
- } else {
50
- e = multibase .Base64
51
- }
52
- encoder := multibase .MustNewEncoder (e )
53
-
44
+ func CreateDag (path string ) (* Dag , error ) {
54
45
dag := CreateDagBuilder ()
55
46
56
47
fileInfo , err := os .Stat (path )
@@ -68,29 +59,29 @@ func CreateDag(path string, encoding ...multibase.Encoding) (*Dag, error) {
68
59
var leaf * DagLeaf
69
60
70
61
if fileInfo .IsDir () {
71
- leaf , err = processDirectory (dirEntry , & parentPath , dag , encoder , true )
62
+ leaf , err = processDirectory (dirEntry , & parentPath , dag , true )
72
63
} else {
73
- leaf , err = processFile (dirEntry , & parentPath , dag , encoder , true )
64
+ leaf , err = processFile (dirEntry , & parentPath , dag , true )
74
65
}
75
66
76
67
if err != nil {
77
68
return nil , err
78
69
}
79
70
80
- dag .AddLeaf (leaf , encoder , nil )
71
+ dag .AddLeaf (leaf , nil )
81
72
82
73
rootHash := leaf .Hash
83
74
return dag .BuildDag (rootHash ), nil
84
75
}
85
76
86
- func processEntry (entry fs.DirEntry , path * string , dag * DagBuilder , encoder multibase. Encoder ) (* DagLeaf , error ) {
77
+ func processEntry (entry fs.DirEntry , path * string , dag * DagBuilder ) (* DagLeaf , error ) {
87
78
var result * DagLeaf
88
79
var err error
89
80
90
81
if entry .IsDir () {
91
- result , err = processDirectory (entry , path , dag , encoder , false )
82
+ result , err = processDirectory (entry , path , dag , false )
92
83
} else {
93
- result , err = processFile (entry , path , dag , encoder , false )
84
+ result , err = processFile (entry , path , dag , false )
94
85
}
95
86
96
87
if err != nil {
@@ -100,7 +91,7 @@ func processEntry(entry fs.DirEntry, path *string, dag *DagBuilder, encoder mult
100
91
return result , nil
101
92
}
102
93
103
- func processDirectory (entry fs.DirEntry , path * string , dag * DagBuilder , encoder multibase. Encoder , isRoot bool ) (* DagLeaf , error ) {
94
+ func processDirectory (entry fs.DirEntry , path * string , dag * DagBuilder , isRoot bool ) (* DagLeaf , error ) {
104
95
entryPath := filepath .Join (* path , entry .Name ())
105
96
106
97
relPath , err := filepath .Rel (* path , entryPath )
@@ -120,21 +111,21 @@ func processDirectory(entry fs.DirEntry, path *string, dag *DagBuilder, encoder
120
111
var result * DagLeaf
121
112
122
113
for _ , entry := range entries {
123
- leaf , err := processEntry (entry , & entryPath , dag , encoder )
114
+ leaf , err := processEntry (entry , & entryPath , dag )
124
115
if err != nil {
125
116
return nil , err
126
117
}
127
118
128
119
label := dag .GetNextAvailableLabel ()
129
120
builder .AddLink (label , leaf .Hash )
130
121
leaf .SetLabel (label )
131
- dag .AddLeaf (leaf , encoder , nil )
122
+ dag .AddLeaf (leaf , nil )
132
123
}
133
124
134
125
if isRoot {
135
- result , err = builder .BuildRootLeaf (dag , encoder )
126
+ result , err = builder .BuildRootLeaf (dag )
136
127
} else {
137
- result , err = builder .BuildLeaf (encoder )
128
+ result , err = builder .BuildLeaf ()
138
129
}
139
130
140
131
if err != nil {
@@ -144,7 +135,7 @@ func processDirectory(entry fs.DirEntry, path *string, dag *DagBuilder, encoder
144
135
return result , nil
145
136
}
146
137
147
- func processFile (entry fs.DirEntry , path * string , dag * DagBuilder , encoder multibase. Encoder , isRoot bool ) (* DagLeaf , error ) {
138
+ func processFile (entry fs.DirEntry , path * string , dag * DagBuilder , isRoot bool ) (* DagLeaf , error ) {
148
139
entryPath := filepath .Join (* path , entry .Name ())
149
140
150
141
relPath , err := filepath .Rel (* path , entryPath )
@@ -177,22 +168,22 @@ func processFile(entry fs.DirEntry, path *string, dag *DagBuilder, encoder multi
177
168
chunkBuilder .SetType (ChunkLeafType )
178
169
chunkBuilder .SetData (chunk )
179
170
180
- chunkLeaf , err := chunkBuilder .BuildLeaf (encoder )
171
+ chunkLeaf , err := chunkBuilder .BuildLeaf ()
181
172
if err != nil {
182
173
return nil , err
183
174
}
184
175
185
176
label := dag .GetNextAvailableLabel ()
186
177
builder .AddLink (label , chunkLeaf .Hash )
187
178
chunkLeaf .SetLabel (label )
188
- dag .AddLeaf (chunkLeaf , encoder , nil )
179
+ dag .AddLeaf (chunkLeaf , nil )
189
180
}
190
181
}
191
182
192
183
if isRoot {
193
- result , err = builder .BuildRootLeaf (dag , encoder )
184
+ result , err = builder .BuildRootLeaf (dag )
194
185
} else {
195
- result , err = builder .BuildLeaf (encoder )
186
+ result , err = builder .BuildLeaf ()
196
187
}
197
188
198
189
if err != nil {
@@ -223,7 +214,7 @@ func CreateDagBuilder() *DagBuilder {
223
214
}
224
215
}
225
216
226
- func (b * DagBuilder ) AddLeaf (leaf * DagLeaf , encoder multibase. Encoder , parentLeaf * DagLeaf ) error {
217
+ func (b * DagBuilder ) AddLeaf (leaf * DagLeaf , parentLeaf * DagLeaf ) error {
227
218
if parentLeaf != nil {
228
219
label := GetLabel (leaf .Hash )
229
220
_ , exists := parentLeaf .Links [label ]
@@ -244,27 +235,19 @@ func (b *DagBuilder) BuildDag(root string) *Dag {
244
235
}
245
236
}
246
237
247
- func (dag * Dag ) Verify (encoder multibase. Encoder ) error {
238
+ func (dag * Dag ) Verify () error {
248
239
err := dag .IterateDag (func (leaf * DagLeaf , parent * DagLeaf ) error {
249
240
if leaf .Hash == dag .Root {
250
- leafResult , err := leaf .VerifyRootLeaf (encoder )
241
+ err := leaf .VerifyRootLeaf ()
251
242
if err != nil {
252
243
return err
253
244
}
254
-
255
- if ! leafResult {
256
- return fmt .Errorf ("root leaf %s failed to verify" , leaf .Hash )
257
- }
258
245
} else {
259
- leafResult , err := leaf .VerifyLeaf (encoder )
246
+ err := leaf .VerifyLeaf ()
260
247
if err != nil {
261
248
return err
262
249
}
263
250
264
- if ! leafResult {
265
- return fmt .Errorf ("leaf %s failed to verify" , leaf .Hash )
266
- }
267
-
268
251
if ! parent .HasLink (leaf .Hash ) {
269
252
return fmt .Errorf ("parent %s does not contain link to child %s" , parent .Hash , leaf .Hash )
270
253
}
@@ -280,11 +263,11 @@ func (dag *Dag) Verify(encoder multibase.Encoder) error {
280
263
return nil
281
264
}
282
265
283
- func (dag * Dag ) CreateDirectory (path string , encoder multibase. Encoder ) error {
266
+ func (dag * Dag ) CreateDirectory (path string ) error {
284
267
rootHash := dag .Root
285
268
rootLeaf := dag .Leafs [rootHash ]
286
269
287
- err := rootLeaf .CreateDirectoryLeaf (path , dag , encoder )
270
+ err := rootLeaf .CreateDirectoryLeaf (path , dag )
288
271
if err != nil {
289
272
return err
290
273
}
0 commit comments