7
7
"math/big"
8
8
"sort"
9
9
"strings"
10
- "testing"
11
10
12
11
"github.com/ipfs/go-cid"
13
12
"github.com/ipfs/go-unixfsnode/data/builder"
@@ -23,7 +22,7 @@ import (
23
22
// GenerateFile generates a random unixfs file of the given size, storing the
24
23
// blocks in the provided LinkSystem and returns a DirEntry representation of
25
24
// the file.
26
- func GenerateFile (t * testing. T , linkSys * linking.LinkSystem , randReader io.Reader , size int ) DirEntry {
25
+ func GenerateFile (t require. TestingT , linkSys * linking.LinkSystem , randReader io.Reader , size int ) DirEntry {
27
26
// a file of `size` random bytes, packaged into unixfs DAGs, stored in the remote blockstore
28
27
delimited := io .LimitReader (randReader , int64 (size ))
29
28
var buf bytes.Buffer
@@ -54,15 +53,15 @@ func GenerateFile(t *testing.T, linkSys *linking.LinkSystem, randReader io.Reade
54
53
// the root directory will be built as HAMT sharded (with a low "width" to
55
54
// maximise the chance of collisions and therefore greater depth for smaller
56
55
// number of files).
57
- func GenerateDirectory (t * testing. T , linkSys * linking.LinkSystem , randReader io.Reader , targetSize int , rootSharded bool ) DirEntry {
56
+ func GenerateDirectory (t require. TestingT , linkSys * linking.LinkSystem , randReader io.Reader , targetSize int , rootSharded bool ) DirEntry {
58
57
return GenerateDirectoryFrom (t , linkSys , randReader , targetSize , "" , rootSharded )
59
58
}
60
59
61
60
// GenerateDirectoryFrom is the same as GenerateDirectory but allows the caller
62
61
// to specify a directory path to start from. This is useful for generating
63
62
// nested directories.
64
63
func GenerateDirectoryFrom (
65
- t * testing. T ,
64
+ t require. TestingT ,
66
65
linkSys * linking.LinkSystem ,
67
66
randReader io.Reader ,
68
67
targetSize int ,
@@ -91,7 +90,8 @@ func GenerateDirectoryFrom(
91
90
break
92
91
}
93
92
}
94
- child := GenerateDirectoryFrom (t , linkSys , randReader , targetSize - curSize , dir + "/" + newDir , false )
93
+ sharded := rndInt (randReader , 6 ) == 0
94
+ child := GenerateDirectoryFrom (t , linkSys , randReader , targetSize - curSize , dir + "/" + newDir , sharded )
95
95
children = append (children , child )
96
96
curSize += int (child .TSize )
97
97
default : // 4 in 6 chance of making a new file
@@ -124,12 +124,46 @@ func GenerateDirectoryFrom(
124
124
return dirEntry
125
125
}
126
126
127
+ // GenerateDirectoryFor will build a directory with name `dir` whose children
128
+ // are generated by the caller using the provided mkchild function. This is
129
+ // useful for generating directories with a specific structure where you need
130
+ // to have greater control over the children.
131
+ func GenerateDirectoryFor (
132
+ t require.TestingT ,
133
+ linkSys * linking.LinkSystem ,
134
+ randReader io.Reader ,
135
+ dir string ,
136
+ sharded bool ,
137
+ mkchild func (name string ) * DirEntry ,
138
+ ) DirEntry {
139
+ children := make ([]DirEntry , 0 )
140
+ for {
141
+ var name string
142
+ for {
143
+ var err error
144
+ name , err = namegen .RandomDirectoryName (randReader )
145
+ require .NoError (t , err )
146
+ if ! isDupe (children , name ) {
147
+ break
148
+ }
149
+ }
150
+ child := mkchild (dir + "/" + name )
151
+ if child == nil {
152
+ break
153
+ }
154
+ children = append (children , * child )
155
+ }
156
+ dirEntry := BuildDirectory (t , linkSys , children , sharded )
157
+ dirEntry .Path = dir
158
+ return dirEntry
159
+ }
160
+
127
161
// BuildDirectory builds a directory from the given children, storing the
128
162
// blocks in the provided LinkSystem and returns a DirEntry representation of
129
163
// the directory. If sharded is true, the root directory will be built as HAMT
130
164
// sharded (with a low "width" to maximise the chance of collisions and
131
165
// therefore greater depth for smaller number of files).
132
- func BuildDirectory (t * testing. T , linkSys * linking.LinkSystem , children []DirEntry , sharded bool ) DirEntry {
166
+ func BuildDirectory (t require. TestingT , linkSys * linking.LinkSystem , children []DirEntry , sharded bool ) DirEntry {
133
167
// create stable sorted children, which should match the encoded form
134
168
// in dag-pb
135
169
sort .Slice (children , func (i , j int ) bool {
@@ -197,8 +231,16 @@ func cidCollector(ls *ipld.LinkSystem, cids *[]cid.Cid) (ipld.BlockWriteOpener,
197
231
}
198
232
199
233
func isDupe (children []DirEntry , name string ) bool {
234
+ if strings .Contains (name , "." ) {
235
+ name = name [:strings .LastIndex (name , "." )]
236
+ }
200
237
for _ , child := range children {
201
- if strings .HasSuffix (child .Path , "/" + name ) {
238
+ childName := child .Path [strings .LastIndex (child .Path , "/" )+ 1 :]
239
+ // remove suffix
240
+ if strings .Contains (childName , "." ) {
241
+ childName = childName [:strings .LastIndex (childName , "." )]
242
+ }
243
+ if childName == name {
202
244
return true
203
245
}
204
246
}
0 commit comments