genfiles.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // +build ignore
  16. package main
  17. import (
  18. "flag"
  19. "fmt"
  20. "io"
  21. "log"
  22. "math/rand"
  23. "os"
  24. "path/filepath"
  25. "time"
  26. )
  27. func ReadRand(bs []byte) (int, error) {
  28. var r uint32
  29. for i := range bs {
  30. if i%4 == 0 {
  31. r = uint32(rand.Int63())
  32. }
  33. bs[i] = byte(r >> uint((i%4)*8))
  34. }
  35. return len(bs), nil
  36. }
  37. func name() string {
  38. var b [16]byte
  39. ReadRand(b[:])
  40. return fmt.Sprintf("%x", b[:])
  41. }
  42. func main() {
  43. var files int
  44. var maxexp int
  45. var srcname string
  46. var random bool
  47. flag.IntVar(&files, "files", 1000, "Number of files")
  48. flag.IntVar(&maxexp, "maxexp", 20, "Maximum file size (max = 2^n + 128*1024 B)")
  49. flag.StringVar(&srcname, "src", "/usr/share/dict/words", "Source material")
  50. flag.BoolVar(&random, "random", true, "When false, always generate the same set of file")
  51. flag.Parse()
  52. if random {
  53. rand.Seed(time.Now().UnixNano())
  54. } else {
  55. rand.Seed(42)
  56. }
  57. fd, err := os.Open(srcname)
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. for i := 0; i < files; i++ {
  62. n := name()
  63. p0 := filepath.Join(string(n[0]), n[0:2])
  64. err = os.MkdirAll(p0, 0755)
  65. if err != nil {
  66. log.Fatal(err)
  67. }
  68. s := 1 << uint(rand.Intn(maxexp))
  69. a := 128 * 1024
  70. if a > s {
  71. a = s
  72. }
  73. s += rand.Intn(a)
  74. src := io.LimitReader(&inifiteReader{fd}, int64(s))
  75. p1 := filepath.Join(p0, n)
  76. dst, err := os.Create(p1)
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. _, err = io.Copy(dst, src)
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. err = dst.Close()
  85. if err != nil {
  86. log.Fatal(err)
  87. }
  88. err = os.Chmod(p1, os.FileMode(rand.Intn(0777)|0400))
  89. if err != nil {
  90. log.Fatal(err)
  91. }
  92. t := time.Now().Add(-time.Duration(rand.Intn(30*86400)) * time.Second)
  93. err = os.Chtimes(p1, t, t)
  94. if err != nil {
  95. log.Fatal(err)
  96. }
  97. }
  98. }
  99. type inifiteReader struct {
  100. rd io.ReadSeeker
  101. }
  102. func (i *inifiteReader) Read(bs []byte) (int, error) {
  103. n, err := i.rd.Read(bs)
  104. if err == io.EOF {
  105. err = nil
  106. i.rd.Seek(0, 0)
  107. }
  108. return n, err
  109. }