summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Pointner <equinox@helsinki.at>2017-01-27 22:31:03 (GMT)
committerChristian Pointner <equinox@helsinki.at>2017-01-27 22:31:03 (GMT)
commitf1511737680385da81972bb76ec1ba1f2b6c46a1 (patch)
treeed14ac1afe969579be5a4d00a1a71e3304ad3332 /src
parenta04eca625d537ea899a4a88642c0a70e1d2032d5 (diff)
parse file-haser config as json from stdin
Diffstat (limited to 'src')
-rw-r--r--src/file-hasher/hasher.go6
-rw-r--r--src/file-hasher/main.go47
2 files changed, 34 insertions, 19 deletions
diff --git a/src/file-hasher/hasher.go b/src/file-hasher/hasher.go
index afc35fd..27574f9 100644
--- a/src/file-hasher/hasher.go
+++ b/src/file-hasher/hasher.go
@@ -90,7 +90,7 @@ func (h *Hasher) collectHashes(C <-chan string) (wg *sync.WaitGroup) {
}
func (h *Hasher) ComputeHashes(w Walker) (err error) {
- C := make(chan string, 10)
+ C := make(chan string, h.numThreads*2)
wg := h.collectHashes(C)
@@ -106,5 +106,9 @@ func (h *Hasher) ComputeHashes(w Walker) (err error) {
func NewHasher(numThreads uint, stdlog *log.Logger) (h *Hasher) {
h = &Hasher{numThreads: numThreads, stdlog: stdlog}
h.Files = make(map[string]string)
+ if h.numThreads < 1 {
+ h.numThreads = 4
+ }
+ stdlog.Printf("*** Created blake2b based hasher (%d threads)", h.numThreads)
return
}
diff --git a/src/file-hasher/main.go b/src/file-hasher/main.go
index 3e32b63..f360705 100644
--- a/src/file-hasher/main.go
+++ b/src/file-hasher/main.go
@@ -30,45 +30,56 @@ import (
"time"
)
-const (
- RD_CONF = "/etc/rd.conf"
-)
+type Request struct {
+ NumThreads uint `json:"threads"`
+ Directories []string `json:"dir"`
+ M3UPlaylists []string `json:"m3u"`
+}
func main() {
- if len(os.Args) < 2 {
- log.Fatal("Usage: file-hasher <directory> [ <directory [ .. ] ]")
- }
- directories := os.Args[1:]
-
stdlog := log.New(os.Stderr, "[std] ", log.LstdFlags)
+ var req Request
+ if err := json.NewDecoder(os.Stdin).Decode(&req); err != nil {
+ stdlog.Fatalf("Error decoding request: %v", err)
+ }
+
C := make(chan os.Signal, 1)
signal.Notify(C, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
- stdlog.Println("***************************************************************")
- stdlog.Printf("*** will hash files' from %d directories", len(directories))
- stdlog.Println("***************************************************************")
-
starttime := time.Now()
go func() {
defer func() {
C <- syscall.SIGTERM
}()
+ stdlog.Println("***************************************************************")
+ h := NewHasher(req.NumThreads, stdlog)
+ stdlog.Println("")
+ stdlog.Println("******************************")
- stdlog.Println("********************************************")
- h := NewHasher(4, stdlog) // TODO: make number of threads configurable
- for _, root := range directories {
+ for _, root := range req.Directories {
src := NewDir(root, stdlog)
stdlog.Printf("*** hashing all the files in '%s'", root)
stdlog.Println("")
if err := h.ComputeHashes(src); err != nil {
stdlog.Println("")
- stdlog.Printf("ERROR: %v", err)
- return
+ stdlog.Fatalf("ERROR: %v", err)
}
stdlog.Println("")
stdlog.Println("******************************")
}
+ for _, list := range req.M3UPlaylists {
+ src := NewM3u(list, stdlog)
+ stdlog.Printf("*** hashing all the files from '%s'", list)
+ stdlog.Println("")
+ if err := h.ComputeHashes(src); err != nil {
+ stdlog.Println("")
+ stdlog.Fatalf("ERROR: %v", err)
+ }
+ stdlog.Println("")
+ stdlog.Println("******************************")
+ }
+
stdlog.Println("")
stdlog.Println("***************************************************************")
stdlog.Printf("*** hashed %d files in %v", len(h.Files), time.Since(starttime))
@@ -76,7 +87,7 @@ func main() {
enc := json.NewEncoder(os.Stdout)
if err := enc.Encode(h.Files); err != nil {
- stdlog.Printf("Error encoding JSON: %v", err)
+ stdlog.Fatalf("Error encoding Result: %v", err)
}
}()