Genfiles use actual source data

This commit is contained in:
Jakob Borg 2014-07-26 08:15:59 +02:00
parent 073775e461
commit 5bf7d372f6
2 changed files with 54 additions and 9 deletions

View File

@ -21,7 +21,7 @@
<localAnnounceEnabled>true</localAnnounceEnabled> <localAnnounceEnabled>true</localAnnounceEnabled>
<localAnnouncePort>21025</localAnnouncePort> <localAnnouncePort>21025</localAnnouncePort>
<parallelRequests>16</parallelRequests> <parallelRequests>16</parallelRequests>
<maxSendKbps>50</maxSendKbps> <maxSendKbps>500</maxSendKbps>
<rescanIntervalS>10</rescanIntervalS> <rescanIntervalS>10</rescanIntervalS>
<reconnectionIntervalS>5</reconnectionIntervalS> <reconnectionIntervalS>5</reconnectionIntervalS>
<maxChangeKbps>10000</maxChangeKbps> <maxChangeKbps>10000</maxChangeKbps>

View File

@ -10,7 +10,8 @@ import (
"crypto/rand" "crypto/rand"
"flag" "flag"
"fmt" "fmt"
"io/ioutil" "io"
"log"
mr "math/rand" mr "math/rand"
"os" "os"
"path/filepath" "path/filepath"
@ -26,29 +27,73 @@ func name() string {
func main() { func main() {
var files int var files int
var maxexp int var maxexp int
var srcname string
flag.IntVar(&files, "files", 1000, "Number of files") flag.IntVar(&files, "files", 1000, "Number of files")
flag.IntVar(&maxexp, "maxexp", 20, "Maximum file size (max = 2^n + 128*1024 B)") flag.IntVar(&maxexp, "maxexp", 20, "Maximum file size (max = 2^n + 128*1024 B)")
flag.StringVar(&srcname, "src", "/usr/share/dict/words", "Source material")
flag.Parse() flag.Parse()
fd, err := os.Open(srcname)
if err != nil {
log.Fatal(err)
}
for i := 0; i < files; i++ { for i := 0; i < files; i++ {
n := name() n := name()
p0 := filepath.Join(string(n[0]), n[0:2]) p0 := filepath.Join(string(n[0]), n[0:2])
os.MkdirAll(p0, 0755) err = os.MkdirAll(p0, 0755)
if err != nil {
log.Fatal(err)
}
s := 1 << uint(mr.Intn(maxexp)) s := 1 << uint(mr.Intn(maxexp))
a := 128 * 1024 a := 128 * 1024
if a > s { if a > s {
a = s a = s
} }
s += mr.Intn(a) s += mr.Intn(a)
b := make([]byte, s)
rand.Reader.Read(b)
p1 := filepath.Join(p0, n)
ioutil.WriteFile(p1, b, 0644)
os.Chmod(p1, os.FileMode(mr.Intn(0777)|0400)) src := io.LimitReader(&inifiteReader{fd}, int64(s))
p1 := filepath.Join(p0, n)
dst, err := os.Create(p1)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(dst, src)
if err != nil {
log.Fatal(err)
}
err = dst.Close()
if err != nil {
log.Fatal(err)
}
err = os.Chmod(p1, os.FileMode(mr.Intn(0777)|0400))
if err != nil {
log.Fatal(err)
}
t := time.Now().Add(-time.Duration(mr.Intn(30*86400)) * time.Second) t := time.Now().Add(-time.Duration(mr.Intn(30*86400)) * time.Second)
os.Chtimes(p1, t, t) 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)
}
return n, err
}