2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-04-21 23:27:00 -07:00
|
|
|
// +build ignore
|
|
|
|
|
2014-03-14 02:39:37 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-07-25 23:15:59 -07:00
|
|
|
"io"
|
|
|
|
"log"
|
2014-09-27 11:09:36 -07:00
|
|
|
"math/rand"
|
2014-03-14 02:39:37 -07:00
|
|
|
"os"
|
2014-03-28 06:36:57 -07:00
|
|
|
"path/filepath"
|
2014-03-14 02:39:37 -07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-09-27 11:09:36 -07:00
|
|
|
func init() {
|
|
|
|
rand.Seed(42)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadRand(bs []byte) (int, error) {
|
|
|
|
var r uint32
|
|
|
|
for i := range bs {
|
|
|
|
if i%4 == 0 {
|
|
|
|
r = uint32(rand.Int63())
|
|
|
|
}
|
|
|
|
bs[i] = byte(r >> uint((i%4)*8))
|
|
|
|
}
|
|
|
|
return len(bs), nil
|
|
|
|
}
|
|
|
|
|
2014-03-14 02:39:37 -07:00
|
|
|
func name() string {
|
|
|
|
var b [16]byte
|
2014-09-27 11:09:36 -07:00
|
|
|
ReadRand(b[:])
|
2014-03-14 02:39:37 -07:00
|
|
|
return fmt.Sprintf("%x", b[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var files int
|
|
|
|
var maxexp int
|
2014-07-25 23:15:59 -07:00
|
|
|
var srcname string
|
2014-03-14 02:39:37 -07:00
|
|
|
|
|
|
|
flag.IntVar(&files, "files", 1000, "Number of files")
|
|
|
|
flag.IntVar(&maxexp, "maxexp", 20, "Maximum file size (max = 2^n + 128*1024 B)")
|
2014-07-25 23:15:59 -07:00
|
|
|
flag.StringVar(&srcname, "src", "/usr/share/dict/words", "Source material")
|
2014-03-14 02:39:37 -07:00
|
|
|
flag.Parse()
|
|
|
|
|
2014-07-25 23:15:59 -07:00
|
|
|
fd, err := os.Open(srcname)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-03-14 02:39:37 -07:00
|
|
|
for i := 0; i < files; i++ {
|
|
|
|
n := name()
|
2014-03-28 06:36:57 -07:00
|
|
|
p0 := filepath.Join(string(n[0]), n[0:2])
|
2014-07-25 23:15:59 -07:00
|
|
|
err = os.MkdirAll(p0, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-27 11:09:36 -07:00
|
|
|
s := 1 << uint(rand.Intn(maxexp))
|
2014-03-14 02:39:37 -07:00
|
|
|
a := 128 * 1024
|
|
|
|
if a > s {
|
|
|
|
a = s
|
|
|
|
}
|
2014-09-27 11:09:36 -07:00
|
|
|
s += rand.Intn(a)
|
2014-07-25 23:15:59 -07:00
|
|
|
|
|
|
|
src := io.LimitReader(&inifiteReader{fd}, int64(s))
|
|
|
|
|
2014-03-28 06:36:57 -07:00
|
|
|
p1 := filepath.Join(p0, n)
|
2014-07-25 23:15:59 -07:00
|
|
|
dst, err := os.Create(p1)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(dst, src)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-03-14 02:39:37 -07:00
|
|
|
|
2014-07-25 23:15:59 -07:00
|
|
|
err = dst.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-09-27 11:09:36 -07:00
|
|
|
err = os.Chmod(p1, os.FileMode(rand.Intn(0777)|0400))
|
2014-07-25 23:15:59 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2014-03-14 02:39:37 -07:00
|
|
|
|
2014-09-27 11:09:36 -07:00
|
|
|
t := time.Now().Add(-time.Duration(rand.Intn(30*86400)) * time.Second)
|
2014-07-25 23:15:59 -07:00
|
|
|
err = os.Chtimes(p1, t, t)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type inifiteReader struct {
|
|
|
|
rd io.ReadSeeker
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *inifiteReader) Read(bs []byte) (int, error) {
|
|
|
|
n, err := i.rd.Read(bs)
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
i.rd.Seek(0, 0)
|
2014-03-14 02:39:37 -07:00
|
|
|
}
|
2014-07-25 23:15:59 -07:00
|
|
|
return n, err
|
2014-03-14 02:39:37 -07:00
|
|
|
}
|