2014-08-22 07:31:03 +00:00
|
|
|
// Package dirwatch provides filesystem-notification based tracking of torrent
|
|
|
|
// info files and magnet URIs in a directory.
|
2014-07-22 15:54:11 +00:00
|
|
|
package dirwatch
|
|
|
|
|
|
|
|
import (
|
2014-07-24 03:44:23 +00:00
|
|
|
"bufio"
|
2014-07-22 15:54:11 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-09-11 04:18:59 +00:00
|
|
|
"strings"
|
2014-08-22 07:31:03 +00:00
|
|
|
|
2015-08-03 14:29:01 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
2014-08-22 07:31:03 +00:00
|
|
|
"github.com/go-fsnotify/fsnotify"
|
2015-03-26 06:18:08 +00:00
|
|
|
|
2015-04-29 14:31:34 +00:00
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2014-07-22 15:54:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Change uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
Added Change = iota
|
|
|
|
Removed
|
|
|
|
)
|
|
|
|
|
|
|
|
type Event struct {
|
2014-08-22 07:31:03 +00:00
|
|
|
MagnetURI string
|
2014-07-22 15:54:11 +00:00
|
|
|
Change
|
|
|
|
TorrentFilePath string
|
2016-04-04 03:01:31 +00:00
|
|
|
InfoHash metainfo.Hash
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 07:31:03 +00:00
|
|
|
type entity struct {
|
2016-04-04 03:01:31 +00:00
|
|
|
metainfo.Hash
|
2014-08-22 07:31:03 +00:00
|
|
|
MagnetURI string
|
|
|
|
TorrentFilePath string
|
|
|
|
}
|
|
|
|
|
2014-07-22 15:54:11 +00:00
|
|
|
type Instance struct {
|
2014-08-22 07:31:03 +00:00
|
|
|
w *fsnotify.Watcher
|
|
|
|
dirName string
|
|
|
|
Events chan Event
|
2016-04-04 03:01:31 +00:00
|
|
|
dirState map[metainfo.Hash]entity
|
2014-08-22 07:31:03 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (i *Instance) Close() {
|
|
|
|
i.w.Close()
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (i *Instance) handleEvents() {
|
|
|
|
defer close(i.Events)
|
|
|
|
for e := range i.w.Events {
|
2014-07-22 15:54:11 +00:00
|
|
|
log.Printf("event: %s", e)
|
2014-08-22 07:31:03 +00:00
|
|
|
if e.Op == fsnotify.Write {
|
|
|
|
// TODO: Special treatment as an existing torrent may have changed.
|
|
|
|
} else {
|
2016-04-19 04:11:11 +00:00
|
|
|
i.refresh()
|
2014-08-22 07:31:03 +00:00
|
|
|
}
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (i *Instance) handleErrors() {
|
|
|
|
for err := range i.w.Errors {
|
2014-07-22 15:54:11 +00:00
|
|
|
log.Printf("error in torrent directory watcher: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-04 03:01:31 +00:00
|
|
|
func torrentFileInfoHash(fileName string) (ih metainfo.Hash, ok bool) {
|
2014-07-22 15:54:11 +00:00
|
|
|
mi, _ := metainfo.LoadFromFile(fileName)
|
|
|
|
if mi == nil {
|
|
|
|
return
|
|
|
|
}
|
2016-08-26 10:29:05 +00:00
|
|
|
ih = mi.HashInfoBytes()
|
2014-07-22 15:54:11 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-04 03:01:31 +00:00
|
|
|
func scanDir(dirName string) (ee map[metainfo.Hash]entity) {
|
2014-08-22 07:31:03 +00:00
|
|
|
d, err := os.Open(dirName)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer d.Close()
|
|
|
|
names, err := d.Readdirnames(-1)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
return
|
|
|
|
}
|
2016-04-04 03:01:31 +00:00
|
|
|
ee = make(map[metainfo.Hash]entity, len(names))
|
2014-08-22 07:31:03 +00:00
|
|
|
addEntity := func(e entity) {
|
2016-04-04 03:01:31 +00:00
|
|
|
e0, ok := ee[e.Hash]
|
2014-08-22 07:31:03 +00:00
|
|
|
if ok {
|
2014-08-27 22:05:06 +00:00
|
|
|
if e0.MagnetURI == "" || len(e.MagnetURI) < len(e0.MagnetURI) {
|
2014-08-22 07:31:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-04-04 03:01:31 +00:00
|
|
|
ee[e.Hash] = e
|
2014-08-22 07:31:03 +00:00
|
|
|
}
|
|
|
|
for _, n := range names {
|
|
|
|
fullName := filepath.Join(dirName, n)
|
|
|
|
switch filepath.Ext(n) {
|
|
|
|
case ".torrent":
|
|
|
|
ih, ok := torrentFileInfoHash(fullName)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
e := entity{
|
|
|
|
TorrentFilePath: fullName,
|
|
|
|
}
|
2016-04-04 03:01:31 +00:00
|
|
|
missinggo.CopyExact(&e.Hash, ih)
|
2014-08-22 07:31:03 +00:00
|
|
|
addEntity(e)
|
|
|
|
case ".magnet":
|
|
|
|
uris, err := magnetFileURIs(fullName)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, uri := range uris {
|
2016-04-04 03:48:39 +00:00
|
|
|
m, err := metainfo.ParseMagnetURI(uri)
|
2014-08-22 07:31:03 +00:00
|
|
|
if err != nil {
|
2014-09-11 04:18:59 +00:00
|
|
|
log.Printf("error parsing %q in file %q: %s", uri, fullName, err)
|
2014-08-22 07:31:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
addEntity(entity{
|
2016-04-04 03:01:31 +00:00
|
|
|
Hash: m.InfoHash,
|
2014-08-22 07:31:03 +00:00
|
|
|
MagnetURI: uri,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-07-24 03:44:23 +00:00
|
|
|
func magnetFileURIs(name string) (uris []string, err error) {
|
|
|
|
f, err := os.Open(name)
|
|
|
|
if err != nil {
|
2014-07-22 15:54:11 +00:00
|
|
|
return
|
|
|
|
}
|
2014-07-24 03:44:23 +00:00
|
|
|
defer f.Close()
|
|
|
|
scanner := bufio.NewScanner(f)
|
2014-08-27 22:05:06 +00:00
|
|
|
scanner.Split(bufio.ScanWords)
|
2014-07-24 03:44:23 +00:00
|
|
|
for scanner.Scan() {
|
2014-09-11 04:18:59 +00:00
|
|
|
// Allow magnet URIs to be "commented" out.
|
|
|
|
if strings.HasPrefix(scanner.Text(), "#") {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-24 03:44:23 +00:00
|
|
|
uris = append(uris, scanner.Text())
|
|
|
|
}
|
|
|
|
err = scanner.Err()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (i *Instance) torrentRemoved(ih metainfo.Hash) {
|
|
|
|
i.Events <- Event{
|
2014-08-22 07:31:03 +00:00
|
|
|
InfoHash: ih,
|
|
|
|
Change: Removed,
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
2014-07-24 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (i *Instance) torrentAdded(e entity) {
|
|
|
|
i.Events <- Event{
|
2016-04-04 03:01:31 +00:00
|
|
|
InfoHash: e.Hash,
|
2014-08-22 07:31:03 +00:00
|
|
|
Change: Added,
|
|
|
|
MagnetURI: e.MagnetURI,
|
|
|
|
TorrentFilePath: e.TorrentFilePath,
|
2014-07-24 03:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 04:11:11 +00:00
|
|
|
func (i *Instance) refresh() {
|
|
|
|
_new := scanDir(i.dirName)
|
|
|
|
old := i.dirState
|
2016-11-22 03:01:09 +00:00
|
|
|
for ih := range old {
|
2014-08-22 07:31:03 +00:00
|
|
|
_, ok := _new[ih]
|
|
|
|
if !ok {
|
2016-04-19 04:11:11 +00:00
|
|
|
i.torrentRemoved(ih)
|
2014-07-24 03:44:23 +00:00
|
|
|
}
|
2014-08-22 07:31:03 +00:00
|
|
|
}
|
|
|
|
for ih, newE := range _new {
|
|
|
|
oldE, ok := old[ih]
|
2014-07-24 03:44:23 +00:00
|
|
|
if ok {
|
2014-08-22 07:31:03 +00:00
|
|
|
if newE == oldE {
|
2014-07-24 03:44:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-04-19 04:11:11 +00:00
|
|
|
i.torrentRemoved(ih)
|
2014-07-24 03:44:23 +00:00
|
|
|
}
|
2016-04-19 04:11:11 +00:00
|
|
|
i.torrentAdded(newE)
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
2016-04-19 04:11:11 +00:00
|
|
|
i.dirState = _new
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(dirName string) (i *Instance, err error) {
|
|
|
|
w, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = w.Add(dirName)
|
|
|
|
if err != nil {
|
|
|
|
w.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i = &Instance{
|
2014-08-22 07:31:03 +00:00
|
|
|
w: w,
|
|
|
|
dirName: dirName,
|
|
|
|
Events: make(chan Event),
|
2016-04-04 03:01:31 +00:00
|
|
|
dirState: make(map[metainfo.Hash]entity, 0),
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
go func() {
|
2014-08-22 07:31:03 +00:00
|
|
|
i.refresh()
|
2014-07-22 15:54:11 +00:00
|
|
|
go i.handleEvents()
|
|
|
|
go i.handleErrors()
|
|
|
|
}()
|
|
|
|
return
|
|
|
|
}
|