genfiles.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build ignore
  5. package main
  6. import (
  7. "crypto/rand"
  8. "flag"
  9. "fmt"
  10. "io/ioutil"
  11. mr "math/rand"
  12. "os"
  13. "path/filepath"
  14. "time"
  15. )
  16. func name() string {
  17. var b [16]byte
  18. rand.Reader.Read(b[:])
  19. return fmt.Sprintf("%x", b[:])
  20. }
  21. func main() {
  22. var files int
  23. var maxexp int
  24. flag.IntVar(&files, "files", 1000, "Number of files")
  25. flag.IntVar(&maxexp, "maxexp", 20, "Maximum file size (max = 2^n + 128*1024 B)")
  26. flag.Parse()
  27. for i := 0; i < files; i++ {
  28. n := name()
  29. p0 := filepath.Join(string(n[0]), n[0:2])
  30. os.MkdirAll(p0, 0755)
  31. s := 1 << uint(mr.Intn(maxexp))
  32. a := 128 * 1024
  33. if a > s {
  34. a = s
  35. }
  36. s += mr.Intn(a)
  37. b := make([]byte, s)
  38. rand.Reader.Read(b)
  39. p1 := filepath.Join(p0, n)
  40. ioutil.WriteFile(p1, b, 0644)
  41. os.Chmod(p1, os.FileMode(mr.Intn(0777)|0400))
  42. t := time.Now().Add(-time.Duration(mr.Intn(30*86400)) * time.Second)
  43. os.Chtimes(p1, t, t)
  44. }
  45. }