summaryrefslogtreecommitdiff
path: root/src/pool-import
diff options
context:
space:
mode:
Diffstat (limited to 'src/pool-import')
-rw-r--r--src/pool-import/dir-hasher.go105
-rw-r--r--src/pool-import/main.go22
2 files changed, 124 insertions, 3 deletions
diff --git a/src/pool-import/dir-hasher.go b/src/pool-import/dir-hasher.go
new file mode 100644
index 0000000..6c1bf9b
--- /dev/null
+++ b/src/pool-import/dir-hasher.go
@@ -0,0 +1,105 @@
+//
+// 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 (
+ "log"
+ "math/rand"
+ "os"
+ "path/filepath"
+ "sync"
+ "time"
+)
+
+type MusicDir struct {
+ root string
+ stdlog *log.Logger
+ Files map[string]string
+}
+
+func handleEntry(C chan<- string, path string, info os.FileInfo, err error, stdlog *log.Logger) error {
+ if err != nil {
+ return err
+ }
+ if info.IsDir() {
+ stdlog.Printf("entering directory: %s", path)
+ return nil
+ }
+ if !info.Mode().IsRegular() {
+ stdlog.Printf(" - skipping special file: %s", path)
+ return nil
+ }
+ // TODO: check file extensions
+ C <- path
+ return nil
+}
+
+func computeHash(file string) string {
+ time.Sleep(time.Duration(rand.Float64() * float64(5*time.Second))) // TODO: faking it is nice but the real hash would be nicer
+ return ""
+}
+
+func (d *MusicDir) collectHashes(C <-chan string, numThreads int) (wg *sync.WaitGroup) {
+ wg = &sync.WaitGroup{}
+
+ for i := 0; i < numThreads; i++ {
+ wg.Add(1)
+ d.stdlog.Printf("Starting hashing thread #%d", i)
+ go func(num int) {
+ defer wg.Done()
+ for {
+ file, ok := <-C
+ if !ok {
+ d.stdlog.Printf("Stopping hashing thread #%d", num)
+ return
+ }
+ d.stdlog.Printf(" - hashing: %s", file)
+ computeHash(file)
+ }
+ }(i)
+ }
+ return
+}
+
+func (d *MusicDir) ComputeHashes() (err error) {
+ C := make(chan string, 10)
+
+ wg := d.collectHashes(C, 4) // TODO: make number of hashing threads configurable
+
+ err = filepath.Walk(d.root, func(path string, info os.FileInfo, err error) error {
+ return handleEntry(C, path, info, err, d.stdlog)
+ })
+ close(C)
+
+ if err != nil {
+ return
+ }
+
+ wg.Wait()
+ return
+}
+
+func NewMusicDir(root string, stdlog *log.Logger) (dir *MusicDir) {
+ dir = &MusicDir{root: root, stdlog: stdlog}
+ dir.Files = make(map[string]string)
+ return
+}
diff --git a/src/pool-import/main.go b/src/pool-import/main.go
index a69c8cc..0a2ce7a 100644
--- a/src/pool-import/main.go
+++ b/src/pool-import/main.go
@@ -74,8 +74,8 @@ func main() {
stdlog.Fatalf("Error '%s' is not a pool group", group)
}
- c := make(chan os.Signal, 1)
- signal.Notify(c, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
+ C := make(chan os.Signal, 1)
+ signal.Notify(C, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
stdlog.Println("***************************************************************")
stdlog.Printf("*** will import into group '%s' from %d directories", group, len(directories))
@@ -90,5 +90,21 @@ func main() {
for _, cart := range carts {
stdlog.Printf(" %d: '%s' | %s / %s / %s)", cart.Number, cart.UserDefined, cart.Artist, cart.Album, cart.Title)
}
- <-c
+
+ go func() {
+ defer func() {
+ C <- syscall.SIGTERM
+ }()
+
+ for _, root := range directories {
+ stdlog.Printf("hashing all the files in '%s'", root)
+ md := NewMusicDir(root, stdlog)
+ if err := md.ComputeHashes(); err != nil {
+ return
+ }
+ }
+ stdlog.Printf("all directires hashed")
+ }()
+
+ <-C
}