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-08-22 07:31:03 +00:00
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent/util"
|
|
|
|
|
|
|
|
"bitbucket.org/anacrolix/go.torrent"
|
|
|
|
"github.com/anacrolix/libtorgo/metainfo"
|
|
|
|
"github.com/go-fsnotify/fsnotify"
|
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
|
|
|
|
InfoHash torrent.InfoHash
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:31:03 +00:00
|
|
|
type entity struct {
|
|
|
|
torrent.InfoHash
|
|
|
|
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
|
|
|
|
dirState map[torrent.InfoHash]entity
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Instance) Close() {
|
|
|
|
me.w.Close()
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Instance) handleEvents() {
|
2014-08-22 07:31:03 +00:00
|
|
|
defer close(me.Events)
|
2014-07-22 15:54:11 +00:00
|
|
|
for e := range me.w.Events {
|
|
|
|
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 {
|
|
|
|
me.refresh()
|
|
|
|
}
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me *Instance) handleErrors() {
|
|
|
|
for err := range me.w.Errors {
|
|
|
|
log.Printf("error in torrent directory watcher: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func torrentFileInfoHash(fileName string) (ih torrent.InfoHash, ok bool) {
|
|
|
|
mi, _ := metainfo.LoadFromFile(fileName)
|
|
|
|
if mi == nil {
|
|
|
|
return
|
|
|
|
}
|
2014-08-22 07:31:03 +00:00
|
|
|
util.CopyExact(ih[:], mi.Info.Hash)
|
2014-07-22 15:54:11 +00:00
|
|
|
ok = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:31:03 +00:00
|
|
|
func scanDir(dirName string) (ee map[torrent.InfoHash]entity) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
ee = make(map[torrent.InfoHash]entity, len(names))
|
|
|
|
addEntity := func(e entity) {
|
|
|
|
e0, ok := ee[e.InfoHash]
|
|
|
|
if ok {
|
|
|
|
if e0.MagnetURI != "" && len(e.MagnetURI) < len(e0.MagnetURI) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ee[e.InfoHash] = e
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
util.CopyExact(e.InfoHash, ih)
|
|
|
|
addEntity(e)
|
|
|
|
case ".magnet":
|
|
|
|
uris, err := magnetFileURIs(fullName)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for _, uri := range uris {
|
|
|
|
m, err := torrent.ParseMagnetURI(uri)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
addEntity(entity{
|
|
|
|
InfoHash: m.InfoHash,
|
|
|
|
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)
|
|
|
|
for scanner.Scan() {
|
|
|
|
uris = append(uris, scanner.Text())
|
|
|
|
}
|
|
|
|
err = scanner.Err()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:31:03 +00:00
|
|
|
func (me *Instance) torrentRemoved(ih torrent.InfoHash) {
|
|
|
|
me.Events <- Event{
|
|
|
|
InfoHash: ih,
|
|
|
|
Change: Removed,
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
2014-07-24 03:44:23 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 07:31:03 +00:00
|
|
|
func (me *Instance) torrentAdded(e entity) {
|
2014-07-24 03:44:23 +00:00
|
|
|
me.Events <- Event{
|
2014-08-22 07:31:03 +00:00
|
|
|
InfoHash: e.InfoHash,
|
|
|
|
Change: Added,
|
|
|
|
MagnetURI: e.MagnetURI,
|
|
|
|
TorrentFilePath: e.TorrentFilePath,
|
2014-07-24 03:44:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 07:31:03 +00:00
|
|
|
func (me *Instance) refresh() {
|
|
|
|
_new := scanDir(me.dirName)
|
|
|
|
old := me.dirState
|
|
|
|
for ih, _ := range old {
|
|
|
|
_, ok := _new[ih]
|
|
|
|
if !ok {
|
|
|
|
me.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
|
|
|
|
}
|
2014-08-22 07:31:03 +00:00
|
|
|
me.torrentRemoved(ih)
|
2014-07-24 03:44:23 +00:00
|
|
|
}
|
2014-08-22 07:31:03 +00:00
|
|
|
me.torrentAdded(newE)
|
2014-07-22 15:54:11 +00:00
|
|
|
}
|
2014-08-22 07:31:03 +00:00
|
|
|
me.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),
|
|
|
|
dirState: make(map[torrent.InfoHash]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
|
|
|
|
}
|