piece

package
v0.0.0-...-8abdde7 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package piece implements a data structure used for storing the actual contents of a torrent.

Index

Constants

This section is empty.

Variables

View Source
var ErrDeleted = errors.New("pieces deleted")

ErrDeleted indicates that the pieces structure has been deleted.

View Source
var ErrHashMismatch = errors.New("hash mismatch")

ErrHashMismatch is returned by AddData when hash validation fails.

Functions

This section is empty.

Types

type Piece

type Piece struct {
	// contains filtered or unexported fields
}

A Piece represents a single piece of a torrent. It can be in one of three states: incomplete, busy (hash is being computed), and complete.

func (*Piece) Busy

func (p *Piece) Busy() bool

Busy returns true if the piece's hash is currently being verified.

func (*Piece) BusyOrComplete

func (p *Piece) BusyOrComplete() bool

BusyOrComplete returns true if the piece is either busy or complete.

func (*Piece) Complete

func (p *Piece) Complete() bool

Complete returns true if the piece is complete and its hash has been verified.

func (*Piece) SetTime

func (p *Piece) SetTime(tm mono.Time)

Time sets the piece's access time.

func (*Piece) Time

func (p *Piece) Time() mono.Time

Time returns the piece's access time.

type Pieces

type Pieces struct {
	// contains filtered or unexported fields
}

Pieces represents the contents of a torrent.

func (*Pieces) AddData

func (ps *Pieces) AddData(index uint32, begin uint32, data []byte, peer uint32) (count uint32, complete bool, err error)

AddData adds data to a torrent.

func (*Pieces) All

func (ps *Pieces) All() bool

All returns true if all pieces are complete.

func (*Pieces) Bitmap

func (ps *Pieces) Bitmap() bitmap.Bitmap

Bitmap returns the a bitmap with a bit set for each complete piece.

func (*Pieces) Bytes

func (ps *Pieces) Bytes() int64

Bytes returns an overestimate the amount of memory used up by ps.

func (*Pieces) Complete

func (ps *Pieces) Complete(n uint32) bool

Complete returns true if a given piece is complete.

func (*Pieces) Count

func (ps *Pieces) Count() int

Count returns the number of non-empty pieces.

func (*Pieces) Del

func (ps *Pieces) Del()

Del discards the contents of a torrent from memory.

func (*Pieces) Expire

func (ps *Pieces) Expire(bytes int64, available []uint16, f func(index uint32)) int

Expire deletes pieces until the amount of space consumed is smaller than the value given by bytes. It calls f whenever it deletes a complete piece.

func (*Pieces) Finalise

func (ps *Pieces) Finalise(index uint32, h hash.Hash) (done bool, peers []uint32, err error)

Finalise validates the hash of a piece and either marks it as complete or discards its data. This is a time-consuming operation, and should be called from its own goroutine.

func (*Pieces) Hole

func (ps *Pieces) Hole(index, offset uint32) (uint32, uint32)

Hole returns the size of the hole starting at a given position.

func (*Pieces) Length

func (ps *Pieces) Length() int64

Length returns the length in bytes of a torrent's contents.

func (*Pieces) MetadataComplete

func (ps *Pieces) MetadataComplete(psize uint32, length int64)

MetadataComplete must be called when a torrent's metadata becomes known.

func (*Pieces) Num

func (ps *Pieces) Num() int

Num returns the number of pieces in a torrent.

func (*Pieces) PieceBitmap

func (ps *Pieces) PieceBitmap(n uint32) (int, bitmap.Bitmap)

PieceBitmap returns a bitmap of complete pieces.

func (*Pieces) PieceEmpty

func (ps *Pieces) PieceEmpty(n uint32) bool

PieceEmpty returns true if no data is available for a given piece.

func (*Pieces) PieceLength

func (ps *Pieces) PieceLength(index uint32) uint32

PieceLength returns the length in bytes of a given piece.

func (*Pieces) PieceSize

func (ps *Pieces) PieceSize() uint32

PieceSize returns a torrent's piece size.

func (*Pieces) ReadAt

func (ps *Pieces) ReadAt(p []byte, off int64) (int, error)

ReadAt copies a range of bytes out of a torrent. It only returns data whose hash has been validated.

func (*Pieces) UpdateTime

func (ps *Pieces) UpdateTime(index uint32) bool

Update sets the last accessed time of a piece. It returns true if the piece is complete.

Jump to

Keyboard shortcuts

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