Showing the changes to cmd/test (1 of 2 files). Show all
Unified · Split
| @@ -3,12 +3,12 @@ · 2 unmodified lines | ||
| 3 | 3 | package makefiles |
| 4 | 4 | |
| 5 | 5 | import ( |
| 6 | − cryptrand "crypto/rand" | |
| 7 | 6 | "io" |
| 8 | 7 | "log" |
| 9 | 8 | "math/rand" |
| 10 | 9 | "os" |
| 11 | 10 | "path/filepath" |
| 11 | + "time" | |
| 12 | 12 | |
| 13 | 13 | "github.com/rclone/rclone/cmd" |
| 14 | 14 | "github.com/rclone/rclone/cmd/test" |
| @@ -27,8 +27,10 @@ var ( · 12 unmodified lines | ||
| 27 | 27 | maxFileSize = fs.SizeSuffix(100) |
| 28 | 28 | minFileNameLength = 4 |
| 29 | 29 | maxFileNameLength = 12 |
| 30 | + seed = int64(1) | |
| 30 | 31 | |
| 31 | 32 | // Globals |
| 33 | + randSource *rand.Rand | |
| 32 | 34 | directoriesToCreate int |
| 33 | 35 | totalDirectories int |
| 34 | 36 | fileNames = map[string]struct{}{} // keep a note of which file name we've used already |
| @@ -44,6 +46,7 @@ func init() { · 9 unmodified lines | ||
| 44 | 46 | flags.FVarP(cmdFlags, &maxFileSize, "max-file-size", "", "Maximum size of files to create") |
| 45 | 47 | flags.IntVarP(cmdFlags, &minFileNameLength, "min-name-length", "", minFileNameLength, "Minimum size of file names") |
| 46 | 48 | flags.IntVarP(cmdFlags, &maxFileNameLength, "max-name-length", "", maxFileNameLength, "Maximum size of file names") |
| 49 | + flags.Int64VarP(cmdFlags, &seed, "seed", "", seed, "Seed for the random number generator (0 for random)") | |
| 47 | 50 | } |
| 48 | 51 | |
| 49 | 52 | var commandDefinition = &cobra.Command{ |
| @@ -51,6 +54,11 @@ var commandDefinition = &cobra.Command{ · 1 unmodified line | ||
| 51 | 54 | Short: `Make a random file hierarchy in <dir>`, |
| 52 | 55 | Run: func(command *cobra.Command, args []string) { |
| 53 | 56 | cmd.CheckArgs(1, 1, command, args) |
| 57 | + if seed == 0 { | |
| 58 | + seed = time.Now().UnixNano() | |
| 59 | + log.Printf("Using random seed = %d", seed) | |
| 60 | + } | |
| 61 | + randSource = rand.New(rand.NewSource(seed)) | |
| 54 | 62 | outputDirectory := args[0] |
| 55 | 63 | directoriesToCreate = numberOfFiles / averageFilesPerDirectory |
| 56 | 64 | averageSize := (minFileSize + maxFileSize) / 2 |
| @@ -61,7 +69,7 @@ var commandDefinition = &cobra.Command{ · 4 unmodified lines | ||
| 61 | 69 | } |
| 62 | 70 | dirs := root.list("", []string{}) |
| 63 | 71 | for i := 0; i < numberOfFiles; i++ { |
| 64 | − dir := dirs[rand.Intn(len(dirs))] | |
| 72 | + dir := dirs[randSource.Intn(len(dirs))] | |
| 65 | 73 | writeFile(dir, fileName()) |
| 66 | 74 | } |
| 67 | 75 | log.Printf("Done.") |
| @@ -71,8 +79,8 @@ var commandDefinition = &cobra.Command{ · 3 unmodified lines | ||
| 71 | 79 | // fileName creates a unique random file or directory name |
| 72 | 80 | func fileName() (name string) { |
| 73 | 81 | for { |
| 74 | − length := rand.Intn(maxFileNameLength-minFileNameLength) + minFileNameLength | |
| 75 | − name = random.String(length) | |
| 82 | + length := randSource.Intn(maxFileNameLength-minFileNameLength) + minFileNameLength | |
| 83 | + name = random.StringFn(length, randSource.Intn) | |
| 76 | 84 | if _, found := fileNames[name]; !found { |
| 77 | 85 | break |
| 78 | 86 | } |
| @@ -99,7 +107,7 @@ func (d *dir) createDirectories() { · 20 unmodified lines | ||
| 99 | 107 | } |
| 100 | 108 | d.children = append(d.children, newDir) |
| 101 | 109 | totalDirectories++ |
| 102 | − switch rand.Intn(4) { | |
| 110 | + switch randSource.Intn(4) { | |
| 103 | 111 | case 0: |
| 104 | 112 | if d.depth < maxDepth { |
| 105 | 113 | newDir.createDirectories() |
| @@ -132,8 +140,8 @@ func writeFile(dir, name string) { · 26 unmodified lines | ||
| 132 | 140 | if err != nil { |
| 133 | 141 | log.Fatalf("Failed to open file %q: %v", path, err) |
| 134 | 142 | } |
| 135 | − size := rand.Int63n(int64(maxFileSize-minFileSize)) + int64(minFileSize) | |
| 136 | − _, err = io.CopyN(fd, cryptrand.Reader, size) | |
| 143 | + size := randSource.Int63n(int64(maxFileSize-minFileSize)) + int64(minFileSize) | |
| 144 | + _, err = io.CopyN(fd, randSource, size) | |
| 137 | 145 | if err != nil { |
| 138 | 146 | log.Fatalf("Failed to write %v bytes to file %q: %v", size, path, err) |
| 139 | 147 | } |