summaryrefslogtreecommitdiff
path: root/src/file-hasher/main.go
blob: c7718eed83bb587652afd6dc51116c50d2994c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
//  pool-import
//
//  Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
//  This file is part of pool-import.
//
//  pool-import is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  any later version.
//
//  pool-import is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with pool-import. If not, see <http://www.gnu.org/licenses/>.
//

package main

import (
	"encoding/json"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"
)

type Request struct {
	NumThreads uint   `json:"threads"`
	HashAlgo   string `json:"algo"`
	Sources    struct {
		Directories  []string `json:"dir"`
		M3UPlaylists []string `json:"m3u"`
	} `json:"src"`
}

func main() {
	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)

	starttime := time.Now()
	go func() {
		defer func() {
			C <- syscall.SIGTERM
		}()
		stdlog.Println("***************************************************************")
		h := NewHasher(req.HashAlgo, req.NumThreads, stdlog)
		if h == nil {
			os.Exit(1)
		}
		stdlog.Println("")
		stdlog.Println("******************************")

		for _, root := range req.Sources.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.Fatalf("ERROR: %v", err)
			}
			stdlog.Println("")
			stdlog.Println("******************************")
		}
		for _, list := range req.Sources.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))
		stdlog.Println("***************************************************************")

		enc := json.NewEncoder(os.Stdout)
		if err := enc.Encode(h.Files); err != nil {
			stdlog.Fatalf("Error encoding Result: %v", err)
		}
	}()

	<-C
}