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