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