// // pool-import // // Copyright (C) 2016 Christian Pointner // // 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 . // 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 }