// Handle a .torrent file. func handleFile(path string) { torrent, err := metadata.ReadMetadataFile(path) if err != nil { statsd.Incr("torrent.error", 1, 1) glog.Errorf("error reading torrent (%q); %v", path, err) return } // Find the first handler matching the supplied torrent. for _, handler := range handlers { if handler.Match(torrent) { name := "torrent.match." + handler.Name statsd.Incr(name, 1, 1) glog.Infof("match file:%q handler:%q watch:%q", torrent.Info.Name, handler.Name, handler.Watch, ) mvpath := filepath.Join(handler.Watch, filepath.Base(path)) if err := os.Rename(path, mvpath); err != nil { glog.Errorf("watch import failed (%q); %v", torrent.Info.Name, err) } return } } statsd.Incr("torrent.no-match", 1, 1) glog.Warningf("no handler matched torrent: %q", torrent.Info.Name) }
func main() { opt = parseFlags() // Read the deamon configuration. flag overrides default (~/.config/gutterd.json) var err error defconfig := &Config{} if opt.ConfigPath == "" { home, err := HomeDirectory() if err != nil { glog.Fatalf("unable to locate home directory: %v", err) } opt.ConfigPath = filepath.Join(home, ".config", "gutterd.json") } if config, err = LoadConfig(opt.ConfigPath, defconfig); err != nil { glog.Fatalf("unable to load configuration: %v", err) } if config.Statsd != "" { err := statsd.Init(config.Statsd, "gutterd") if err != nil { glog.Warningf("statsd init error (no stats will be recorded); %v", err) } statsd.Incr("proc.start", 1, 1) } handlers = config.MakeHandlers() // command line flag overrides if opt.Watch != nil { config.Watch = opt.Watch } statsd.Incr("proc.boot", 1, 1) if err := fsInit(); err != nil { glog.Fatalf("error initializing file system watcher; %v", err) } for event := range fs.Event { statsd.Incr("torrents.matches", 1, 1) handleFile(event.Name) } }
func fsInit() (err error) { fs, err = watcher.NewInstr( func(event *fsnotify.FileEvent) bool { statsd.Incr("watcher.fs.events", 1, 1) // filter sees all events return event.IsCreate() && strings.HasSuffix(event.Name, ".torrent") }, func(err error) { statsd.Incr("watcher.fs.errors", 1, 1) glog.Warningf("watcher error: %v", err) }) if err != nil { return } if err = fs.Watch(config.Watch...); err != nil { return } return }