Version: 2.1.0

home

[report issue]

storage_params

Declared in "libtorrent/storage_defs.hpp"

a parameter pack used to construct the storage for a torrent, used in disk_interface

struct storage_params
{
   storage_params (file_storage const& f, lt::renamed_files const& mf
      , string_view sp, string_view pfd, storage_mode_t const sm
      , aux::vector<download_priority_t, file_index_t> const& prio
      , sha1_hash const& ih, bool v1_torrent, bool v2_torrent);

   file_storage const& files;
   lt::renamed_files const& renamed_files;
   string_view path;
   string_view part_file_dir;
   storage_mode_t mode {storage_mode_sparse};
   aux::vector<download_priority_t, file_index_t> const& priorities;
   sha1_hash info_hash;
   bool v1;
   bool v2;
};
[report issue]

storage_params()

storage_params (file_storage const& f, lt::renamed_files const& mf
      , string_view sp, string_view pfd, storage_mode_t const sm
      , aux::vector<download_priority_t, file_index_t> const& prio
      , sha1_hash const& ih, bool v1_torrent, bool v2_torrent);

constructs a parameter pack from the layout, save path, mode, per-file priorities, info-hash and protocol-version flags. v1 indicates that the torrent has v1 metadata (a SHA-1 info-hash); v2 that it has v2 metadata (BEP 52). A hybrid torrent has both set.

[report issue]
files
the file layout (sizes, paths, flags) of the torrent
[report issue]
renamed_files
rename overlay applied on top of files; on-disk paths resolved through this object take precedence over the originals
[report issue]
path
the directory where the torrent's files are stored
[report issue]
part_file_dir
directory in which the part file is created. If empty, the part file is placed under path.
[report issue]
mode
the storage allocation mode (sparse vs. pre-allocated)
[report issue]
priorities
per-file download priorities, indexed by file index
[report issue]
info_hash
the SHA-1 info-hash of the torrent (zero for v2-only torrents)
[report issue]
v1 v2
indicates which BitTorrent protocol version(s) this storage is for. v1 is true if the torrent has v1 metadata; v2 is true if the torrent has v2 metadata (BEP 52). A hybrid torrent has both flags set.
[report issue]

file_slice

Declared in "libtorrent/file_storage.hpp"

represents a window of a file in a torrent.

The file_index refers to the index of the file (in the torrent_info). To get the path and filename, use file_path() and give the file_index as argument. The offset is the byte offset in the file where the range starts, and size is the number of bytes this range is. The size + offset will never be greater than the file size.

struct file_slice
{
   file_index_t file_index;
   std::int64_t offset;
   std::int64_t size;
};
[report issue]
file_index
the index of the file
[report issue]
offset
the offset from the start of the file, in bytes
[report issue]
size
the size of the window, in bytes
[report issue]

file_storage

Declared in "libtorrent/file_storage.hpp"

The file_storage class represents a file list and the piece size. Everything necessary to interpret a regular bittorrent storage file structure.

class file_storage
{
   bool is_valid () const;
   void reserve (int num_files);
   void add_file_borrow (string_view filename
      , std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}, std::int64_t mtime = 0
      , string_view symlink_path = string_view()
      , char const* root_hash = nullptr);
   void add_file (std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}
      , std::time_t mtime = 0, string_view symlink_path = string_view()
      , char const* root_hash = nullptr);
   void add_file_borrow (error_code& ec, string_view filename
      , std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}, std::int64_t mtime = 0
      , string_view symlink_path = string_view()
      , char const* root_hash = nullptr);
   void add_file (error_code& ec, std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}
      , std::time_t mtime = 0, string_view symlink_path = string_view()
      , char const* root_hash = nullptr);
   std::vector<file_slice> map_block (piece_index_t piece, std::int64_t offset
      , std::int64_t size) const;
   peer_request map_file (file_index_t file, std::int64_t offset, int size) const;
   int num_files () const noexcept;
   file_index_t end_file () const noexcept;
   index_range<file_index_t> file_range () const noexcept;
   std::int64_t total_size () const;
   std::int64_t size_on_disk () const;
   void set_num_pieces (int n);
   int num_pieces () const;
   piece_index_t end_piece () const;
   piece_index_t last_piece () const;
   index_range<piece_index_t> piece_range () const noexcept;
   int piece_length () const;
   void set_piece_length (int l);
   int piece_size (piece_index_t index) const;
   int piece_size2 (piece_index_t index) const;
   int blocks_in_piece2 (piece_index_t index) const;
   int blocks_per_piece () const;
   void set_name (std::string const& n);
   std::string const& name () const;
   void swap (file_storage& ti) noexcept;
   std::time_t mtime (file_index_t index) const;
   std::string symlink (file_index_t index) const;
   string_view file_name (file_index_t index) const;
   std::int64_t file_offset (file_index_t index) const;
   std::int64_t file_size (file_index_t index) const;
   bool pad_file_at (file_index_t index) const;
   char const* root_ptr (file_index_t const index) const;
   sha256_hash root (file_index_t index) const;
   std::string file_path (file_index_t index, std::string const& save_path = "") const;
   int file_num_blocks (file_index_t index) const;
   int file_num_pieces (file_index_t index) const;
   index_range<piece_index_t::diff_type> file_piece_range (file_index_t) const;
   int file_first_block_node (file_index_t index) const;
   int file_first_piece_node (file_index_t index) const;
   std::uint32_t file_path_hash (file_index_t index, std::string const& save_path) const;
   void all_path_hashes (std::unordered_set<std::uint32_t>& table) const;
   file_flags_t file_flags (file_index_t index) const;
   bool file_absolute_path (file_index_t index) const;
   file_index_t file_index_at_offset (std::int64_t offset) const;
   file_index_t file_index_at_piece (piece_index_t piece) const;
   file_index_t last_file_index_at_piece (piece_index_t piece) const;
   file_index_t file_index_for_root (sha256_hash const& root_hash) const;
   piece_index_t piece_index_at_file (file_index_t f) const;
   piece_index_t last_piece_index_at_file (file_index_t f) const;
   void sanitize_symlinks ();
   bool v2 () const;

   static inline constexpr file_flags_t flag_pad_file  = 0_bit;
   static inline constexpr file_flags_t flag_hidden  = 1_bit;
   static inline constexpr file_flags_t flag_executable  = 2_bit;
   static inline constexpr file_flags_t flag_symlink  = 3_bit;
};
[report issue]

is_valid()

bool is_valid () const;

returns true if the piece length has been initialized on the file_storage. This is typically taken as a proxy of whether the file_storage as a whole is initialized or not.

[report issue]

reserve()

void reserve (int num_files);

allocates space for num_files in the internal file list. This can be used to avoid reallocating the internal file list when the number of files to be added is known up-front.

[report issue]

add_file_borrow() add_file()

void add_file_borrow (string_view filename
      , std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}, std::int64_t mtime = 0
      , string_view symlink_path = string_view()
      , char const* root_hash = nullptr);
void add_file (std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}
      , std::time_t mtime = 0, string_view symlink_path = string_view()
      , char const* root_hash = nullptr);

Adds a file to the file storage. The add_file_borrow version expects that filename is the file name (without a path) of the file that's being added. This memory is borrowed, i.e. it is the caller's responsibility to make sure it stays valid throughout the lifetime of this file_storage object or any copy of it. The same thing applies to filehash, which is an optional pointer to a 20 byte binary SHA-1 hash of the file.

if filename is empty, the filename from path is used and not borrowed.

The path argument is the full path (in the torrent file) to the file to add. Note that this is not supposed to be an absolute path, but it is expected to include the name of the torrent as the first path element.

file_size is the size of the file in bytes.

The file_flags argument sets attributes on the file. The file attributes is an extension and may not work in all bittorrent clients.

For possible file attributes, see file_storage::flags_t.

The mtime argument is optional and can be set to 0. If non-zero, it is the posix time of the last modification time of this file.

symlink_path is the path the file is a symlink to. To make this a symlink you also need to set the file_storage::flag_symlink file flag.

root_hash is an optional pointer to a 32 byte SHA-256 hash, being the merkle tree root hash for this file. This is only used for v2 torrents. If the root hash is specified for one file, it has to be specified for all, otherwise this function will fail. Note that the buffer root_hash points to must out-live the file_storage object, it will not be copied. This parameter is only used when loading torrents, that already have their file hashes computed. When creating torrents, the file hashes will be computed by the piece hashes.

If more files than one are added, certain restrictions to their paths apply. In a multi-file file storage (torrent), all files must share the same root directory.

That is, the first path element of all files must be the same. This shared path element is also set to the name of the torrent. It can be changed by calling set_name.

The overloads that take an error_code reference will report failures via that variable, otherwise system_error is thrown.

[report issue]

add_file_borrow() add_file()

void add_file_borrow (error_code& ec, string_view filename
      , std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}, std::int64_t mtime = 0
      , string_view symlink_path = string_view()
      , char const* root_hash = nullptr);
void add_file (error_code& ec, std::string const& path, std::int64_t file_size
      , file_flags_t file_flags = {}
      , std::time_t mtime = 0, string_view symlink_path = string_view()
      , char const* root_hash = nullptr);

these overloads report failures through the ec reference rather than throwing. add_file_borrow() does not copy the filename string; it is borrowed by reference and the caller must keep it alive for the lifetime of this file_storage object. This is useful when constructing the file list from a memory-mapped .torrent file.

[report issue]

map_block()

std::vector<file_slice> map_block (piece_index_t piece, std::int64_t offset
      , std::int64_t size) const;

returns a list of file_slice objects representing the portions of files the specified piece index, byte offset and size range overlaps. this is the inverse mapping of map_file().

Preconditions of this function is that the input range is within the torrents address space. piece may not be negative and

piece * piece_size + offset + size

may not exceed the total size of the torrent.

[report issue]

map_file()

peer_request map_file (file_index_t file, std::int64_t offset, int size) const;

returns a peer_request representing the piece index, byte offset and size the specified file range overlaps. This is the inverse mapping over map_block(). Note that the peer_request return type is meant to hold bittorrent block requests, which may not be larger than 16 kiB. Mapping a range larger than that may return an overflown integer.

[report issue]

num_files()

int num_files () const noexcept;

returns the number of files in the file_storage

[report issue]

end_file()

file_index_t end_file () const noexcept;

returns the index of the one-past-end file in the file storage

[report issue]

file_range()

index_range<file_index_t> file_range () const noexcept;

returns an implementation-defined type that can be used as the container in a range-for loop. Where the values are the indices of all files in the file_storage.

[report issue]

total_size()

std::int64_t total_size () const;

returns the total number of bytes all the files in this torrent spans including pad files

[report issue]

size_on_disk()

std::int64_t size_on_disk () const;

returns the sum of all non pad file sizes

[report issue]

num_pieces() set_num_pieces()

void set_num_pieces (int n);
int num_pieces () const;

set and get the number of pieces in the torrent

[report issue]

end_piece()

piece_index_t end_piece () const;

returns the index of the one-past-end piece in the file storage

[report issue]

last_piece()

piece_index_t last_piece () const;

returns the index of the last piece in the torrent. The last piece is special in that it may be smaller than the other pieces (and the other pieces are all the same size).

[report issue]

piece_range()

index_range<piece_index_t> piece_range () const noexcept;

returns an implementation-defined type that can be used as the container in a range-for loop. Where the values are the indices of all pieces in the file_storage.

[report issue]

piece_length() set_piece_length()

int piece_length () const;
void set_piece_length (int l);

set and get the size of each piece in this torrent. It must be a power of two and at least 16 kiB.

[report issue]

piece_size()

int piece_size (piece_index_t index) const;

returns the piece size of index. This will be the same as piece_length(), except for the last piece, which may be shorter.

[report issue]

piece_size2()

int piece_size2 (piece_index_t index) const;

Returns the size of the given piece. If the piece spans multiple files, only the first file is considered part of the piece. This is used for v2 torrents, where all files are piece aligned and padded. i.e. The pad files are not considered part of the piece for this purpose.

[report issue]

blocks_in_piece2()

int blocks_in_piece2 (piece_index_t index) const;

returns the number of blocks in the specified piece, for v2 torrents.

[report issue]

blocks_per_piece()

int blocks_per_piece () const;

returns the number of blocks there are in the typical piece. There may be fewer in the last piece)

[report issue]

name() set_name()

void set_name (std::string const& n);
std::string const& name () const;

set and get the name of this torrent. For multi-file torrents, this is also the name of the root directory all the files are stored in.

[report issue]

swap()

void swap (file_storage& ti) noexcept;

swap all content of this with ti.

[report issue]

file_num_pieces() file_piece_range() file_num_blocks()

int file_num_blocks (file_index_t index) const;
int file_num_pieces (file_index_t index) const;
index_range<piece_index_t::diff_type> file_piece_range (file_index_t) const;

Returns the number of pieces or blocks the file at index spans, under the assumption that the file is aligned to the start of a piece. This is only meaningful for v2 torrents, where files are guaranteed such alignment. These numbers are used to size and navigate the merkle hash tree for each file.

[report issue]

file_first_piece_node() file_first_block_node()

int file_first_block_node (file_index_t index) const;
int file_first_piece_node (file_index_t index) const;

index of first piece node in the merkle tree

[report issue]

file_path_hash()

std::uint32_t file_path_hash (file_index_t index, std::string const& save_path) const;

returns the crc32 hash of file_path(index)

[report issue]

all_path_hashes()

void all_path_hashes (std::unordered_set<std::uint32_t>& table) const;

this will add the CRC32 hash of all directory entries to the table. No filename will be included, just directories. Every depth of directories are added separately to allow test for collisions with files at all levels. i.e. if one path in the torrent is foo/bar/baz, the CRC32 hashes for foo, foo/bar and foo/bar/baz will be added to the set.

[report issue]

file_flags()

file_flags_t file_flags (file_index_t index) const;

returns a bitmask of flags from file_flags_t that apply to file at index.

[report issue]

file_absolute_path()

bool file_absolute_path (file_index_t index) const;

returns true if the file at the specified index has been renamed to have an absolute path, i.e. is not anchored in the save path of the torrent.

[report issue]

file_index_at_piece() last_file_index_at_piece() file_index_at_offset()

file_index_t file_index_at_offset (std::int64_t offset) const;
file_index_t file_index_at_piece (piece_index_t piece) const;
file_index_t last_file_index_at_piece (piece_index_t piece) const;

returns the index of the file at the given offset or piece in the torrent

[report issue]

file_index_for_root()

file_index_t file_index_for_root (sha256_hash const& root_hash) const;

finds the file with the given root hash and returns its index if there is no file with the root hash, file_index_t{-1} is returned

[report issue]

last_piece_index_at_file() piece_index_at_file()

piece_index_t piece_index_at_file (file_index_t f) const;
piece_index_t last_piece_index_at_file (file_index_t f) const;

returns the first or last piece index of the given file in the torrent

[report issue]

v2()

bool v2 () const;

returns true if this torrent contains v2 metadata.

[report issue]
flag_pad_file
the file is a pad file. It's required to contain zeros at it will not be saved to disk. Its purpose is to make the following file start on a piece boundary.
[report issue]
flag_hidden
this file has the hidden attribute set. This is primarily a windows attribute
[report issue]
flag_executable
this file has the executable attribute set.
[report issue]
flag_symlink
this file is a symbolic link. It should have a link target string associated with it.
[report issue]

renamed_files

Declared in "libtorrent/file_storage.hpp"

this is a wrapper around file_storage for renaming files without having to mutate the file_storage object itself.

struct renamed_files
{
   std::string file_path (file_storage const& fs, file_index_t index, std::string const& save_path = "") const;
   string_view file_name (file_storage const& fs, file_index_t index) const;
   bool file_absolute_path (file_storage const& fs, file_index_t index) const;
   void rename_file (file_storage const& fs, file_index_t index, std::string const& new_filename);
   void import_filenames (file_storage const& fs, std::map<file_index_t, std::string> const& renamed_files);
   std::map<file_index_t, std::string> export_filenames (file_storage const& fs) const;
};
[report issue]

file_name() file_absolute_path() file_path()

std::string file_path (file_storage const& fs, file_index_t index, std::string const& save_path = "") const;
string_view file_name (file_storage const& fs, file_index_t index) const;
bool file_absolute_path (file_storage const& fs, file_index_t index) const;

returns information about the file at index in fs, honoring any rename recorded in this object. file_path() returns the full on-disk path (prepending save_path to relative paths). file_name() returns just the leaf filename. file_absolute_path() returns true if the recorded rename is an absolute path (in which case save_path is ignored).

[report issue]

rename_file()

void rename_file (file_storage const& fs, file_index_t index, std::string const& new_filename);

records that the file at index in fs should be renamed to new_filename on disk. The underlying file_storage is not modified.

[report issue]

export_filenames() import_filenames()

void import_filenames (file_storage const& fs, std::map<file_index_t, std::string> const& renamed_files);
std::map<file_index_t, std::string> export_filenames (file_storage const& fs) const;

bulk import or export the set of file renames recorded in this object. import_filenames() adds the given renames (dropping entries whose file index is out of range). export_filenames() returns the current renames as a map suitable for serializing and later passing back to import_filenames().

[report issue]

filenames

Declared in "libtorrent/file_storage.hpp"

a lightweight view that pairs a file_storage with the rename overrides in a renamed_files. It exposes the read-only subset of file_storage that downstream code (e.g. the disk I/O backends) needs, with file paths automatically resolved through the rename layer.

struct filenames
{
   filenames (file_storage const& fs, renamed_files const& rf);
   file_flags_t file_flags (file_index_t const index) const;
   file_index_t end_file () const noexcept;
   bool pad_file_at (file_index_t const index) const;
   std::int64_t file_size (file_index_t const index) const;
   std::int64_t file_offset (file_index_t const index) const;
   std::string file_path (file_index_t const index, std::string const& save_path = "") const;
   bool file_absolute_path (file_index_t const index) const;
   std::string symlink (file_index_t const index) const;
   index_range<file_index_t> file_range () const noexcept;
   piece_index_t end_piece () const;
   int num_files () const noexcept;
   int num_pieces () const;
   std::vector<file_slice> map_block (piece_index_t const piece, std::int64_t const offset
      , std::int64_t const size) const;
   peer_request map_file (file_index_t const file, std::int64_t const offset, int const size) const;
   sha256_hash root (file_index_t const index) const;
   int piece_length () const;
};
[report issue]

filenames()

filenames (file_storage const& fs, renamed_files const& rf);

construct a view over fs with the renames recorded in rf applied. Both references must outlive the view.

[report issue]

file_flags()

file_flags_t file_flags (file_index_t const index) const;

returns the file flags for the file at index

[report issue]

end_file()

file_index_t end_file () const noexcept;

one past-the-end file index

[report issue]

pad_file_at()

bool pad_file_at (file_index_t const index) const;

returns true if the file at index is a pad file

[report issue]

file_size() file_offset()

std::int64_t file_size (file_index_t const index) const;
std::int64_t file_offset (file_index_t const index) const;

returns the file size or file offset for the file at index. The file offset is in the torrent space, of all files in the torrent, in the order they are defined.

[report issue]

file_absolute_path() file_path()

std::string file_path (file_index_t const index, std::string const& save_path = "") const;
bool file_absolute_path (file_index_t const index) const;

returns the on-disk path for the file at index, applying any rename recorded in the underlying renamed_files. If save_path is non-empty it is prepended to relative paths. file_absolute_path() returns true if the recorded rename is an absolute path (in which case save_path is ignored).

[report issue]

file_range()

index_range<file_index_t> file_range () const noexcept;

all file indices in the underlying file_storage. Convenient in range-for loops.

[report issue]

num_pieces() end_piece() num_files()

piece_index_t end_piece () const;
int num_files () const noexcept;
int num_pieces () const;

the number of files in the underlying file_storage, the number of pieces, and the one-past-the-end piece index.

[report issue]

map_block()

std::vector<file_slice> map_block (piece_index_t const piece, std::int64_t const offset
      , std::int64_t const size) const;

returns the list of file slices that make up the piece at piece, starting at offset into the piece and covering size bytes. Each entry identifies one file and the byte range within it that contributes to the requested block.

[report issue]

map_file()

peer_request map_file (file_index_t const file, std::int64_t const offset, int const size) const;

inverse of map_block(): maps a byte range within the file at file (starting at offset, of length size) to a peer_request identifying the piece, piece-offset and length in torrent space.

[report issue]

root()

sha256_hash root (file_index_t const index) const;

returns the v2 merkle root (pieces root) for the file at index. Returns a zero hash for v1-only torrents.

[report issue]

piece_length()

int piece_length () const;

the piece size, in bytes. All pieces have this size except the last, which may be shorter.

[report issue]

mmap_disk_io_constructor()

Declared in "libtorrent/mmap_disk_io.hpp"

std::unique_ptr<disk_interface> mmap_disk_io_constructor (
   io_context& ios, settings_interface const&, counters& cnt);

constructs a memory mapped file disk I/O object.

[report issue]

default_disk_io_constructor()

Declared in "libtorrent/session.hpp"

std::unique_ptr<disk_interface> default_disk_io_constructor (
   io_context& ios, settings_interface const&, counters& cnt);

the constructor function for the default storage. On systems that support memory mapped files (and a 64 bit address space) the memory mapped storage will be constructed, otherwise the portable posix storage.

[report issue]

disabled_disk_io_constructor()

Declared in "libtorrent/disabled_disk_io.hpp"

std::unique_ptr<disk_interface> disabled_disk_io_constructor (
   io_context& ios, settings_interface const&, counters& cnt);

creates a disk io object that discards all data written to it, and only returns zero-buffers when read from. May be useful for testing and benchmarking.

[report issue]

posix_disk_io_constructor()

Declared in "libtorrent/posix_disk_io.hpp"

std::unique_ptr<disk_interface> posix_disk_io_constructor (
   io_context& ios, settings_interface const&, counters& cnt);

this is a simple posix disk I/O back-end, used for systems that don't have a 64 bit virtual address space or don't support memory mapped files. It's implemented using portable C file functions and is single-threaded.

[report issue]

enum storage_mode_t

Declared in "libtorrent/storage_defs.hpp"

name value description
storage_mode_allocate 0 All pieces will be written to their final position, all files will be allocated in full when the torrent is first started. This mode minimizes fragmentation but could be a costly operation.
storage_mode_sparse 1 All pieces will be written to the place where they belong and sparse files will be used. This is the recommended, and default mode.
[report issue]

enum move_flags_t

Declared in "libtorrent/storage_defs.hpp"

name value description
always_replace_files 0 replace any files in the destination when copying or moving the storage
fail_if_exist 1 if any files that we want to copy exist in the destination exist, fail the whole operation and don't perform any copy or move. There is an inherent race condition in this mode. The files are checked for existence before the operation starts. In between the check and performing the copy, the destination files may be created, in which case they are replaced.
dont_replace 2 if any file exist in the target, take those files instead of the ones we may have in the source.
reset_save_path 3 don't move any source files, just forget about them and begin checking files at new save path
reset_save_path_unchecked 4 don't move any source files, just change save path and continue working without any checks