genfiles.go 891 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // +build ignore
  2. package main
  3. import (
  4. "crypto/rand"
  5. "flag"
  6. "fmt"
  7. "io/ioutil"
  8. mr "math/rand"
  9. "os"
  10. "path/filepath"
  11. "time"
  12. )
  13. func name() string {
  14. var b [16]byte
  15. rand.Reader.Read(b[:])
  16. return fmt.Sprintf("%x", b[:])
  17. }
  18. func main() {
  19. var files int
  20. var maxexp int
  21. flag.IntVar(&files, "files", 1000, "Number of files")
  22. flag.IntVar(&maxexp, "maxexp", 20, "Maximum file size (max = 2^n + 128*1024 B)")
  23. flag.Parse()
  24. for i := 0; i < files; i++ {
  25. n := name()
  26. p0 := filepath.Join(string(n[0]), n[0:2])
  27. os.MkdirAll(p0, 0755)
  28. s := 1 << uint(mr.Intn(maxexp))
  29. a := 128 * 1024
  30. if a > s {
  31. a = s
  32. }
  33. s += mr.Intn(a)
  34. b := make([]byte, s)
  35. rand.Reader.Read(b)
  36. p1 := filepath.Join(p0, n)
  37. ioutil.WriteFile(p1, b, 0644)
  38. os.Chmod(p1, os.FileMode(mr.Intn(0777)|0400))
  39. t := time.Now().Add(-time.Duration(mr.Intn(30*86400)) * time.Second)
  40. os.Chtimes(p1, t, t)
  41. }
  42. }