-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.go
110 lines (88 loc) · 2.3 KB
/
lib.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
package randomfiles
import (
"fmt"
"io"
"math/rand"
"os"
"path"
)
type Options struct {
Out io.Writer // output progress
Source io.Reader // randomness source
FileSize int // the size per file.
Alphabet []rune // for filenames
FanoutDepth int // how deep the hierarchy goes
FanoutFiles int // how many files per dir
FanoutDirs int // how many dirs per dir
RandomSeed int64 // use a random seed. if 0, use a random seed
RandomSize bool // randomize file sizes
RandomFanout bool // randomize fanout numbers
}
func WriteRandomFiles(root string, depth int, opts *Options) error {
numfiles := opts.FanoutFiles
if opts.RandomFanout {
numfiles = rand.Intn(numfiles) + 1
}
for i := 0; i < numfiles; i++ {
if err := WriteRandomFile(root, opts); err != nil {
return err
}
}
if depth+1 <= opts.FanoutDepth {
numdirs := opts.FanoutDirs
if opts.RandomFanout {
numdirs = rand.Intn(numdirs) + 1
}
for i := 0; i < numdirs; i++ {
if err := WriteRandomDir(root, depth+1, opts); err != nil {
return err
}
}
}
return nil
}
var FilenameSize = 16
var RunesEasy = []rune("abcdefghijklmnopqrstuvwxyz01234567890-_")
var RunesHard = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890!@#$%^&*()-_+= ;.,<>'\"[]{}() ")
func RandomFilename(length int, alphabet []rune) string {
b := make([]rune, length)
for i := range b {
b[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(b)
}
func WriteRandomFile(root string, opts *Options) error {
filesize := int64(opts.FileSize)
if opts.RandomSize {
filesize = rand.Int63n(filesize) + 1
}
n := rand.Intn(FilenameSize-4) + 4
name := RandomFilename(n, opts.Alphabet)
filepath := path.Join(root, name)
f, err := os.Create(filepath)
if err != nil {
return err
}
if _, err := io.CopyN(f, opts.Source, filesize); err != nil {
return err
}
if opts.Out != nil {
fmt.Fprintln(opts.Out, filepath)
}
return f.Close()
}
func WriteRandomDir(root string, depth int, opts *Options) error {
if depth > opts.FanoutDepth {
return nil
}
n := rand.Intn(FilenameSize-4) + 4
name := RandomFilename(n, opts.Alphabet)
root = path.Join(root, name)
if err := os.MkdirAll(root, 0755); err != nil {
return err
}
if opts.Out != nil {
fmt.Fprintln(opts.Out, root)
}
return WriteRandomFiles(root, depth, opts)
}