summaryrefslogtreecommitdiff
path: root/src/file-hasher/main.go
blob: 3e32b6373c74b68a1e7d92b97520a46b7fadd246 (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
//
//  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"
)

const (
	RD_CONF = "/etc/rd.conf"
)

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)

	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(4, stdlog) // TODO: make number of threads configurable
		for _, root := range 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.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.Printf("Error encoding JSON: %v", err)
		}
	}()

	<-C
}