Expose a wrapped metainfo type with helper methods

This commit is contained in:
Matt Joiner 2014-11-18 14:36:27 -06:00
parent 00e2d42870
commit 06e240e198
4 changed files with 27 additions and 15 deletions

3
TODO
View File

@ -6,4 +6,5 @@
* Remove non-deterministic stuff from unit tests, like the tracker UDP and fuse fs stuff.
* Expose a public Torrent type bound to a given client or similar to work with common per-torrent operations.
* Split scraping and announcing on DHT into separate routines.
* Ping nodes that stop being good.
* Ping nodes that stop being good.
* Merge duplicate magnet data.

View File

@ -12,7 +12,6 @@ import (
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
"bitbucket.org/anacrolix/go.torrent"
"github.com/anacrolix/libtorgo/metainfo"
)
const (
@ -52,7 +51,7 @@ type rootNode struct {
type node struct {
path []string
metadata *metainfo.Info
metadata *torrent.MetaInfo
FS *torrentFS
InfoHash torrent.InfoHash
}
@ -225,10 +224,6 @@ func (dn dirNode) Attr() (attr fuse.Attr) {
return
}
func isSingleFileTorrent(md *metainfo.Info) bool {
return len(md.Files) == 0
}
func (me rootNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err fuse.Error) {
for _, t := range me.fs.Client.Torrents() {
if t.Name() != name || t.Info == nil {
@ -239,7 +234,7 @@ func (me rootNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err
FS: me.fs,
InfoHash: t.InfoHash,
}
if isSingleFileTorrent(t.Info) {
if t.Info.SingleFile() {
_node = fileNode{__node, uint64(t.Info.Length), 0}
} else {
_node = dirNode{__node}
@ -253,15 +248,14 @@ func (me rootNode) Lookup(name string, intr fusefs.Intr) (_node fusefs.Node, err
}
func (me rootNode) ReadDir(intr fusefs.Intr) (dirents []fuse.Dirent, err fuse.Error) {
for _, _torrent := range me.fs.Client.Torrents() {
metaInfo := _torrent.Info
if metaInfo == nil {
for _, t := range me.fs.Client.Torrents() {
if t.Info == nil {
continue
}
dirents = append(dirents, fuse.Dirent{
Name: metaInfo.Name,
Name: t.Info.Name,
Type: func() fuse.DirentType {
if isSingleFileTorrent(metaInfo) {
if t.Info.SingleFile() {
return fuse.DT_File
} else {
return fuse.DT_Dir

17
metainfo.go Normal file
View File

@ -0,0 +1,17 @@
package torrent
import "github.com/anacrolix/libtorgo/metainfo"
type MetaInfo struct {
*metainfo.Info
}
func newMetaInfo(info *metainfo.Info) *MetaInfo {
return &MetaInfo{
Info: info,
}
}
func (me *MetaInfo) SingleFile() bool {
return len(me.Info.Files) == 0
}

View File

@ -58,7 +58,7 @@ type torrent struct {
dataLock sync.RWMutex
Data mmap_span.MMapSpan
Info *metainfo.Info
Info *MetaInfo
// Active peer connections.
Conns []*connection
// Set of addrs to which we're attempting to connect.
@ -195,7 +195,7 @@ func infoPieceHashes(info *metainfo.Info) (ret []string) {
// Called when metadata for a torrent becomes available.
func (t *torrent) setMetadata(md metainfo.Info, dataDir string, infoBytes []byte) (err error) {
t.Info = &md
t.Info = newMetaInfo(&md)
t.MetaData = infoBytes
t.metadataHave = nil
t.Data, err = mmapTorrentData(&md, dataDir)