metainfo

package
v1.57.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 9, 2024 License: MPL-2.0 Imports: 24 Imported by: 3,549

README

A library for manipulating ".torrent" files.

Documentation

Index

Constants

View Source
const FileTreePropertiesKey = ""
View Source
const HashSize = infohash.Size
View Source
const NoName = "-"

The Info.Name field is "advisory". For multi-file torrents it's usually a suggested directory name. There are situations where we don't want a directory (like using the contents of a torrent as the immediate contents of a directory), or the name is invalid. Transmission will inject the name of the torrent file if it doesn't like the name, resulting in a different infohash (https://github.com/transmission/transmission/issues/1775). To work around these situations, we will use a sentinel name for compatibility with Transmission and to signal to our own client that we intended to have no directory name. By exposing it in the API we can check for references to this behaviour within this implementation.

Variables

View Source
var (
	NewHashFromHex = infohash.FromHexString
	HashBytes      = infohash.HashBytes
)
View Source
var ParseMagnetURI = ParseMagnetUri

Deprecated: Use ParseMagnetUri.

Functions

func ChoosePieceLength added in v1.47.0

func ChoosePieceLength(totalLength int64) (pieceLength int64)

Choose a good piecelength.

func GeneratePieces added in v1.16.0

func GeneratePieces(r io.Reader, pieceLength int64, b []byte) ([]byte, error)

func HashForPiecePad added in v1.56.0

func HashForPiecePad(pieceLength int64) (hash [32]byte)

Returns the padding hash for the hash layer corresponding to a piece. It can't be zero because that's the bottom-most layer (the hashes for the smallest blocks).

func ValidatePieceLayers added in v1.56.0

func ValidatePieceLayers(
	pieceLayers map[string]string,
	fileTree *FileTree,
	pieceLength int64,
) (err error)

Types

type AnnounceList

type AnnounceList [][]string

func (AnnounceList) Clone added in v1.16.0

func (al AnnounceList) Clone() AnnounceList

func (AnnounceList) DistinctValues

func (al AnnounceList) DistinctValues() (ret []string)

func (AnnounceList) OverridesAnnounce

func (al AnnounceList) OverridesAnnounce(announce string) bool

Whether the AnnounceList should be preferred over a single URL announce.

type ExtendedFileAttrs added in v1.56.0

type ExtendedFileAttrs struct {
	Attr        string   `bencode:"attr,omitempty"`
	SymlinkPath []string `bencode:"symlink path,omitempty"`
	Sha1        string   `bencode:"sha1,omitempty"`
}

See BEP 47. This is common to both Info and FileInfo.

type FileInfo

type FileInfo struct {
	// BEP3. With BEP 47 this can be optional, but we have no way to describe that without breaking
	// the API.
	Length int64    `bencode:"length"`
	Path   []string `bencode:"path"` // BEP3
	// Unofficial extension by BiglyBT? https://github.com/BiglySoftware/BiglyBT/issues/1274. Might
	// be a safer bet when available: https://github.com/anacrolix/torrent/pull/915.
	PathUtf8 []string `bencode:"path.utf-8,omitempty"`

	ExtendedFileAttrs

	// BEP 52. This isn't encoded in a v1 FileInfo, but is exposed here for APIs that expect to deal
	// v1 files.
	PiecesRoot    g.Option[[32]byte] `bencode:"-"`
	TorrentOffset int64              `bencode:"-"`
}

Information specific to a single file inside the MetaInfo structure.

func (*FileInfo) BestPath added in v1.42.0

func (fi *FileInfo) BestPath() []string

func (*FileInfo) DisplayPath

func (fi *FileInfo) DisplayPath(info *Info) string

type FileTree added in v1.55.0

type FileTree struct {
	File FileTreeFile
	Dir  map[string]FileTree
}

The fields here don't need bencode tags as the marshalling is done manually.

func (*FileTree) IsDir added in v1.56.0

func (ft *FileTree) IsDir() bool

func (*FileTree) MarshalBencode added in v1.57.0

func (ft *FileTree) MarshalBencode() (bytes []byte, err error)

func (*FileTree) NumEntries added in v1.56.0

func (ft *FileTree) NumEntries() (num int)

func (*FileTree) PiecesRootAsByteArray added in v1.56.0

func (ft *FileTree) PiecesRootAsByteArray() (ret g.Option[[32]byte])

func (*FileTree) UnmarshalBencode added in v1.56.0

func (ft *FileTree) UnmarshalBencode(bytes []byte) (err error)

func (*FileTree) Walk added in v1.56.0

func (ft *FileTree) Walk(path []string, f func(path []string, ft *FileTree))

type FileTreeFile added in v1.56.0

type FileTreeFile struct {
	Length     int64  `bencode:"length"`
	PiecesRoot string `bencode:"pieces root"`
}

type Hash

type Hash = infohash.T

type Info

type Info struct {
	PieceLength int64 `bencode:"piece length"` // BEP3
	// BEP 3. This can be omitted because isn't needed in non-hybrid v2 infos. See BEP 52.
	Pieces   []byte `bencode:"pieces,omitempty"`
	Name     string `bencode:"name"` // BEP3
	NameUtf8 string `bencode:"name.utf-8,omitempty"`
	Length   int64  `bencode:"length,omitempty"` // BEP3, mutually exclusive with Files
	ExtendedFileAttrs
	Private *bool `bencode:"private,omitempty"` // BEP27
	// TODO: Document this field.
	Source string     `bencode:"source,omitempty"`
	Files  []FileInfo `bencode:"files,omitempty"` // BEP3, mutually exclusive with Length

	// BEP 52 (BitTorrent v2)
	MetaVersion int64    `bencode:"meta version,omitempty"`
	FileTree    FileTree `bencode:"file tree,omitempty"`
}

The info dictionary. See BEP 3 and BEP 52.

func (*Info) BestName added in v1.42.0

func (info *Info) BestName() string

func (*Info) BuildFromFilePath

func (info *Info) BuildFromFilePath(root string) (err error)

This is a helper that sets Files and Pieces from a root path and its children.

func (*Info) FilesArePieceAligned added in v1.56.0

func (info *Info) FilesArePieceAligned() bool

func (*Info) GeneratePieces

func (info *Info) GeneratePieces(open func(fi FileInfo) (io.ReadCloser, error)) (err error)

Sets Pieces (the block of piece hashes in the Info) by using the passed function to get at the torrent data.

func (*Info) HasV1 added in v1.56.0

func (info *Info) HasV1() bool

func (*Info) HasV2 added in v1.56.0

func (info *Info) HasV2() bool

Whether the Info can be used as a v2 info dict, including having a V2 infohash.

func (*Info) IsDir

func (info *Info) IsDir() bool

Whether all files share the same top-level directory name. If they don't, Info.Name is usually used.

func (*Info) NumPieces

func (info *Info) NumPieces() (num int)

func (*Info) Piece

func (info *Info) Piece(index int) Piece

func (*Info) TotalLength

func (info *Info) TotalLength() (ret int64)

func (*Info) UpvertedFiles

func (info *Info) UpvertedFiles() (files []FileInfo)

The files field, converted up from the old single-file in the parent info dict if necessary. This is a helper to avoid having to conditionally handle single and multi-file torrent infos.

func (*Info) UpvertedV1Files added in v1.56.1

func (info *Info) UpvertedV1Files() (files []FileInfo)

UpvertedFiles but specific to the files listed in the v1 info fields. This will include padding files for example that wouldn't appear in v2 file trees.

type Magnet

type Magnet struct {
	InfoHash    Hash       // Expected in this implementation
	Trackers    []string   // "tr" values
	DisplayName string     // "dn" value, if not empty
	Params      url.Values // All other values, such as "x.pe", "as", "xs" etc.
}

Magnet link components.

func ParseMagnetUri added in v1.19.0

func ParseMagnetUri(uri string) (m Magnet, err error)

ParseMagnetUri parses Magnet-formatted URIs into a Magnet instance

func (Magnet) String

func (m Magnet) String() string

type MagnetV2 added in v1.56.0

type MagnetV2 struct {
	InfoHash    g.Option[Hash] // Expected in this implementation
	V2InfoHash  g.Option[infohash_v2.T]
	Trackers    []string   // "tr" values
	DisplayName string     // "dn" value, if not empty
	Params      url.Values // All other values, such as "x.pe", "as", "xs" etc.
}

Magnet link components.

func ParseMagnetV2Uri added in v1.56.0

func ParseMagnetV2Uri(uri string) (m MagnetV2, err error)

ParseMagnetUri parses Magnet-formatted URIs into a Magnet instance

func (MagnetV2) String added in v1.56.0

func (m MagnetV2) String() string

type MetaInfo

type MetaInfo struct {
	InfoBytes    bencode.Bytes `bencode:"info,omitempty"`                              // BEP 3
	Announce     string        `bencode:"announce,omitempty"`                          // BEP 3
	AnnounceList AnnounceList  `bencode:"announce-list,omitempty"`                     // BEP 12
	Nodes        []Node        `bencode:"nodes,omitempty,ignore_unmarshal_type_error"` // BEP 5
	// Where's this specified? Mentioned at
	// https://wiki.theory.org/index.php/BitTorrentSpecification: (optional) the creation time of
	// the torrent, in standard UNIX epoch format (integer, seconds since 1-Jan-1970 00:00:00 UTC)
	CreationDate int64   `bencode:"creation date,omitempty,ignore_unmarshal_type_error"`
	Comment      string  `bencode:"comment,omitempty"`
	CreatedBy    string  `bencode:"created by,omitempty"`
	Encoding     string  `bencode:"encoding,omitempty"`
	UrlList      UrlList `bencode:"url-list,omitempty"` // BEP 19 WebSeeds
	// BEP 52 (BitTorrent v2): Keys are file merkle roots ("pieces root"s), and the values are the
	// concatenated hashes of the merkle tree layer that corresponds to the piece length.
	PieceLayers map[string]string `bencode:"piece layers,omitempty"`
}

Also known as a torrent file.

func Load

func Load(r io.Reader) (*MetaInfo, error)

Load a MetaInfo from an io.Reader. Returns a non-nil error in case of failure.

func LoadFromFile

func LoadFromFile(filename string) (*MetaInfo, error)

Convenience function for loading a MetaInfo from a file.

func (*MetaInfo) HashInfoBytes

func (mi *MetaInfo) HashInfoBytes() (infoHash Hash)

func (MetaInfo) Magnet deprecated

func (mi MetaInfo) Magnet(infoHash *Hash, info *Info) (m Magnet)

Deprecated: Use MagnetV2. Creates a Magnet from a MetaInfo. Optional infohash and parsed info can be provided.

func (*MetaInfo) MagnetV2 added in v1.56.0

func (mi *MetaInfo) MagnetV2() (m MagnetV2, err error)

Creates a MagnetV2 from a MetaInfo. This supports v1, hybrid, and v2 magnet links.

func (*MetaInfo) SetDefaults

func (mi *MetaInfo) SetDefaults()

Set good default values in preparation for creating a new MetaInfo file.

func (*MetaInfo) UnmarshalInfo

func (mi *MetaInfo) UnmarshalInfo() (info Info, err error)

func (*MetaInfo) UpvertedAnnounceList

func (mi *MetaInfo) UpvertedAnnounceList() AnnounceList

Returns the announce-list converted from the old single announce field if necessary.

func (*MetaInfo) Write

func (mi *MetaInfo) Write(w io.Writer) error

Encode to bencoded form.

type Node

type Node string

func (*Node) UnmarshalBencode

func (n *Node) UnmarshalBencode(b []byte) (err error)

type Piece

type Piece struct {
	Info *Info // Can we embed the fields here instead, or is it something to do with saving memory?
	// contains filtered or unexported fields
}

func (Piece) Index

func (p Piece) Index() int

func (Piece) Length

func (p Piece) Length() int64

func (Piece) Offset

func (p Piece) Offset() int64

func (Piece) V1Hash added in v1.56.0

func (p Piece) V1Hash() (ret g.Option[Hash])

func (Piece) V1Length added in v1.56.0

func (p Piece) V1Length() int64

type PieceKey

type PieceKey struct {
	InfoHash Hash
	Index    pieceIndex
}

Uniquely identifies a piece.

type UrlList

type UrlList []string

func (*UrlList) UnmarshalBencode

func (me *UrlList) UnmarshalBencode(b []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL