2008-05-28 10:44:40 +02:00
|
|
|
/*
|
|
|
|
|
2018-04-09 09:04:33 +02:00
|
|
|
Copyright (c) 2003-2018, Arvid Norberg
|
2008-05-28 10:44:40 +02:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_FILE_STORAGE_HPP_INCLUDED
|
|
|
|
#define TORRENT_FILE_STORAGE_HPP_INCLUDED
|
|
|
|
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2016-04-30 22:53:20 +02:00
|
|
|
#include <unordered_set>
|
2009-10-20 18:44:11 +02:00
|
|
|
#include <ctime>
|
2016-06-18 20:01:38 +02:00
|
|
|
#include <cstdint>
|
2015-04-18 04:33:39 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
#include "libtorrent/peer_request.hpp"
|
2016-09-21 07:06:07 +02:00
|
|
|
#include "libtorrent/sha1_hash.hpp"
|
2016-08-13 13:04:53 +02:00
|
|
|
#include "libtorrent/string_view.hpp"
|
2016-12-22 16:42:33 +01:00
|
|
|
#include "libtorrent/aux_/vector.hpp"
|
2018-06-02 19:29:40 +02:00
|
|
|
#include "libtorrent/index_range.hpp"
|
2017-08-03 11:31:03 +02:00
|
|
|
#include "libtorrent/flags.hpp"
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2017-04-12 19:00:57 +02:00
|
|
|
namespace libtorrent {
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2013-08-17 09:01:03 +02:00
|
|
|
// information about a file in a file_storage
|
2017-03-18 01:41:47 +01:00
|
|
|
struct TORRENT_DEPRECATED_EXPORT file_entry
|
2010-11-29 06:44:29 +01:00
|
|
|
{
|
2013-08-17 09:01:03 +02:00
|
|
|
// hidden
|
2012-03-19 07:06:52 +01:00
|
|
|
file_entry();
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2012-03-19 07:06:52 +01:00
|
|
|
~file_entry();
|
2015-09-01 05:23:10 +02:00
|
|
|
file_entry(file_entry const&) = default;
|
|
|
|
file_entry& operator=(file_entry const&) = default;
|
2017-05-28 13:14:58 +02:00
|
|
|
file_entry(file_entry&&) noexcept = default;
|
|
|
|
file_entry& operator=(file_entry&&) noexcept = default;
|
2010-11-29 06:44:29 +01:00
|
|
|
|
2013-07-26 08:11:10 +02:00
|
|
|
// the full path of this file. The paths are unicode strings
|
|
|
|
// encoded in UTF-8.
|
2010-11-29 06:44:29 +01:00
|
|
|
std::string path;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
2013-11-26 05:39:33 +01:00
|
|
|
// the path which this is a symlink to, or empty if this is
|
|
|
|
// not a symlink. This field is only used if the ``symlink_attribute`` is set.
|
|
|
|
std::string symlink_path;
|
|
|
|
|
2013-07-26 08:11:10 +02:00
|
|
|
// the offset of this file inside the torrent
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t offset;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// the size of the file (in bytes) and ``offset`` is the byte offset
|
|
|
|
// of the file within the torrent. i.e. the sum of all the sizes of the files
|
|
|
|
// before it in the list.
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t size;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// the modification time of this file specified in posix time.
|
2010-11-29 06:44:29 +01:00
|
|
|
std::time_t mtime;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
2017-06-18 20:39:10 +02:00
|
|
|
// a SHA-1 hash of the content of the file, or zeros, if no
|
2013-07-26 08:11:10 +02:00
|
|
|
// file hash was present in the torrent file. It can be used to potentially
|
|
|
|
// find alternative sources for the file.
|
2010-11-29 06:44:29 +01:00
|
|
|
sha1_hash filehash;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// set to true for files that are not part of the data of the torrent.
|
|
|
|
// They are just there to make sure the next file is aligned to a particular byte offset
|
2016-09-21 07:06:07 +02:00
|
|
|
// or piece boundary. These files should typically be hidden from an end user. They are
|
2013-07-26 08:11:10 +02:00
|
|
|
// not written to disk.
|
2010-11-29 06:44:29 +01:00
|
|
|
bool pad_file:1;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// true if the file was marked as hidden (on windows).
|
2010-11-29 06:44:29 +01:00
|
|
|
bool hidden_attribute:1;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// true if the file was marked as executable (posix)
|
2010-11-29 06:44:29 +01:00
|
|
|
bool executable_attribute:1;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// true if the file was a symlink. If this is the case
|
|
|
|
// the ``symlink_index`` refers to a string which specifies the original location
|
|
|
|
// where the data for this file was found.
|
2010-11-29 06:44:29 +01:00
|
|
|
bool symlink_attribute:1;
|
|
|
|
};
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2010-11-29 06:44:29 +01:00
|
|
|
|
2013-07-21 17:47:30 +02:00
|
|
|
// internal
|
2018-06-02 19:29:40 +02:00
|
|
|
struct internal_file_entry
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2010-11-25 00:49:22 +01:00
|
|
|
friend class file_storage;
|
2016-07-02 01:46:59 +02:00
|
|
|
#if TORRENT_USE_INVARIANT_CHECKS
|
2011-06-06 09:47:29 +02:00
|
|
|
// for torrent_info::invariant_check
|
|
|
|
friend class torrent_info;
|
|
|
|
#endif
|
2014-06-11 10:32:24 +02:00
|
|
|
|
2016-08-13 13:04:53 +02:00
|
|
|
internal_file_entry();
|
2010-11-29 06:44:29 +01:00
|
|
|
internal_file_entry(internal_file_entry const& fe);
|
|
|
|
internal_file_entry& operator=(internal_file_entry const& fe);
|
2017-05-28 13:14:58 +02:00
|
|
|
internal_file_entry(internal_file_entry&& fe) noexcept;
|
|
|
|
internal_file_entry& operator=(internal_file_entry&& fe) noexcept;
|
2010-11-29 06:44:29 +01:00
|
|
|
~internal_file_entry();
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2013-10-05 06:18:24 +02:00
|
|
|
void set_name(char const* n, bool borrow_string = false, int string_len = 0);
|
2016-08-13 13:04:53 +02:00
|
|
|
string_view filename() const;
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2013-10-05 06:18:24 +02:00
|
|
|
enum {
|
2016-12-18 16:59:41 +01:00
|
|
|
name_is_owned = (1 << 12) - 1,
|
|
|
|
not_a_symlink = (1 << 15) - 1
|
2013-10-05 06:18:24 +02:00
|
|
|
};
|
2013-11-29 06:29:39 +01:00
|
|
|
|
2010-08-23 10:48:02 +02:00
|
|
|
// the offset of this file inside the torrent
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t offset:48;
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2013-11-17 09:02:16 +01:00
|
|
|
// index into file_storage::m_symlinks or not_a_symlink
|
|
|
|
// if this is not a symlink
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t symlink_index:15;
|
2013-11-17 09:02:16 +01:00
|
|
|
|
2013-11-26 05:39:33 +01:00
|
|
|
// if this is true, don't include m_name as part of the
|
|
|
|
// path to this file
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t no_root_dir:1;
|
2013-11-26 05:39:33 +01:00
|
|
|
|
|
|
|
// the size of this file
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t size:48;
|
2013-11-26 05:39:33 +01:00
|
|
|
|
2010-11-25 00:49:22 +01:00
|
|
|
// the number of characters in the name. If this is
|
2016-06-20 17:32:06 +02:00
|
|
|
// name_is_owned, name is 0-terminated and owned by this object
|
2010-11-25 00:49:22 +01:00
|
|
|
// (i.e. it should be freed in the destructor). If
|
2016-12-13 05:53:03 +01:00
|
|
|
// the len is not name_is_owned, the name pointer does not belong
|
2016-06-20 17:32:06 +02:00
|
|
|
// to this object, and it's not 0-terminated
|
2016-06-18 20:01:38 +02:00
|
|
|
std::uint64_t name_len:12;
|
|
|
|
std::uint64_t pad_file:1;
|
|
|
|
std::uint64_t hidden_attribute:1;
|
|
|
|
std::uint64_t executable_attribute:1;
|
|
|
|
std::uint64_t symlink_attribute:1;
|
2013-11-17 09:02:16 +01:00
|
|
|
|
2013-11-26 05:39:33 +01:00
|
|
|
// make it available for logging
|
|
|
|
private:
|
2016-06-20 17:32:06 +02:00
|
|
|
// This string is not necessarily 0-terminated!
|
2013-11-26 05:39:33 +01:00
|
|
|
// that's why it's private, to keep people away from it
|
|
|
|
char const* name;
|
|
|
|
public:
|
2013-11-17 09:02:16 +01:00
|
|
|
|
2010-11-25 00:49:22 +01:00
|
|
|
// the index into file_storage::m_paths. To get
|
|
|
|
// the full path to this file, concatenate the path
|
|
|
|
// from that array with the 'name' field in
|
|
|
|
// this struct
|
2013-11-17 09:02:16 +01:00
|
|
|
// values for path_index include:
|
|
|
|
// -1 means no path (i.e. single file torrent)
|
|
|
|
// -2, it means the filename
|
2013-06-10 00:30:02 +02:00
|
|
|
// in this field contains the full, absolute path
|
|
|
|
// to the file
|
2010-11-25 00:49:22 +01:00
|
|
|
int path_index;
|
2008-05-28 10:44:40 +02:00
|
|
|
};
|
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
|
2013-07-26 08:11:10 +02:00
|
|
|
// represents a window of a file in a torrent.
|
|
|
|
//
|
|
|
|
// The ``file_index`` refers to the index of the file (in the torrent_info).
|
2015-04-25 06:22:51 +02:00
|
|
|
// To get the path and filename, use ``file_path()`` and give the ``file_index``
|
2013-07-26 08:11:10 +02:00
|
|
|
// 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.
|
2008-05-28 10:44:40 +02:00
|
|
|
struct TORRENT_EXPORT file_slice
|
|
|
|
{
|
2013-07-26 08:11:10 +02:00
|
|
|
// the index of the file
|
2016-12-22 16:42:33 +01:00
|
|
|
file_index_t file_index;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// the offset from the start of the file, in bytes
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t offset;
|
2013-07-26 08:11:10 +02:00
|
|
|
|
|
|
|
// the size of the window, in bytes
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t size;
|
2008-05-28 10:44:40 +02:00
|
|
|
};
|
|
|
|
|
2017-08-03 11:31:03 +02:00
|
|
|
// hidden
|
2018-03-22 23:28:55 +01:00
|
|
|
using file_flags_t = flags::bitfield_flag<std::uint8_t, struct file_flags_tag>;
|
2017-08-03 11:31:03 +02:00
|
|
|
|
2013-08-12 05:18:43 +02:00
|
|
|
// The ``file_storage`` class represents a file list and the piece
|
|
|
|
// size. Everything necessary to interpret a regular bittorrent storage
|
|
|
|
// file structure.
|
2008-05-28 10:44:40 +02:00
|
|
|
class TORRENT_EXPORT file_storage
|
|
|
|
{
|
|
|
|
friend class torrent_info;
|
|
|
|
public:
|
2013-08-17 09:01:03 +02:00
|
|
|
// hidden
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage();
|
2013-09-01 19:34:05 +02:00
|
|
|
// hidden
|
2015-01-01 21:25:39 +01:00
|
|
|
~file_storage();
|
2016-08-13 13:04:53 +02:00
|
|
|
file_storage(file_storage const&);
|
2015-04-11 16:17:33 +02:00
|
|
|
file_storage& operator=(file_storage const&);
|
2017-05-28 13:14:58 +02:00
|
|
|
file_storage(file_storage&&) noexcept;
|
2018-02-01 01:06:15 +01:00
|
|
|
file_storage& operator=(file_storage&&) = default;
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// 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.
|
2008-05-28 10:44:40 +02:00
|
|
|
bool is_valid() const { return m_piece_length > 0; }
|
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2017-08-03 11:31:03 +02:00
|
|
|
static constexpr file_flags_t TORRENT_DEPRECATED_MEMBER pad_file = 0_bit;
|
|
|
|
static constexpr file_flags_t TORRENT_DEPRECATED_MEMBER attribute_hidden = 1_bit;
|
|
|
|
static constexpr file_flags_t TORRENT_DEPRECATED_MEMBER attribute_executable = 2_bit;
|
|
|
|
static constexpr file_flags_t TORRENT_DEPRECATED_MEMBER attribute_symlink = 3_bit;
|
|
|
|
#endif
|
2009-01-11 23:27:43 +01:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// 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.
|
2009-11-26 07:54:52 +01:00
|
|
|
void reserve(int num_files);
|
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
// Adds a file to the file storage. The ``add_file_borrow`` version
|
2017-09-02 23:58:36 +02:00
|
|
|
// 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
|
2015-01-21 16:46:12 +01:00
|
|
|
// responsibility to make sure it stays valid throughout the lifetime
|
|
|
|
// of this file_storage object or any copy of it. The same thing applies
|
2016-04-21 21:39:53 +02:00
|
|
|
// to ``filehash``, which is an optional pointer to a 20 byte binary
|
2015-01-21 16:46:12 +01:00
|
|
|
// SHA-1 hash of the file.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2017-09-02 23:58:36 +02:00
|
|
|
// if ``filename`` is empty, the filename from ``path`` is used and not
|
|
|
|
// borrowed.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2015-01-21 16:46:12 +01:00
|
|
|
// 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.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2015-01-21 16:46:12 +01:00
|
|
|
// ``file_size`` is the size of the file in bytes.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2015-01-21 16:46:12 +01:00
|
|
|
// The ``file_flags`` argument sets attributes on the file. The file
|
|
|
|
// attributes is an extension and may not work in all bittorrent clients.
|
2013-08-12 05:18:43 +02:00
|
|
|
//
|
|
|
|
// For possible file attributes, see file_storage::flags_t.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2015-01-21 16:46:12 +01:00
|
|
|
// 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.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2015-02-16 22:53:23 +01:00
|
|
|
// ``symlink_path`` is the path the file is a symlink to. To make this a
|
2015-01-21 16:46:12 +01:00
|
|
|
// symlink you also need to set the file_storage::flag_symlink file flag.
|
2013-08-12 05:18:43 +02:00
|
|
|
//
|
2014-12-17 04:19:42 +01:00
|
|
|
// 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.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-08-12 05:18:43 +02:00
|
|
|
// 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``.
|
2017-09-02 23:58:36 +02:00
|
|
|
void add_file_borrow(string_view filename
|
2016-06-18 20:01:38 +02:00
|
|
|
, std::string const& path, std::int64_t file_size
|
2017-09-28 10:11:20 +02:00
|
|
|
, file_flags_t file_flags = {}, char const* filehash = nullptr
|
2016-12-22 05:23:45 +01:00
|
|
|
, std::int64_t mtime = 0, string_view symlink_path = string_view());
|
2017-08-03 11:31:03 +02:00
|
|
|
void add_file(std::string const& path, std::int64_t file_size
|
|
|
|
, file_flags_t file_flags = {}
|
2016-08-13 13:04:53 +02:00
|
|
|
, std::time_t mtime = 0, string_view symlink_path = string_view());
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// renames the file at ``index`` to ``new_filename``. Keep in mind
|
|
|
|
// that filenames are expected to be UTF-8 encoded.
|
2016-12-22 16:42:33 +01:00
|
|
|
void rename_file(file_index_t index, std::string const& new_filename);
|
2009-03-31 10:12:35 +02:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2017-09-02 23:58:36 +02:00
|
|
|
void add_file_borrow(char const* filename, int filename_len
|
|
|
|
, std::string const& path, std::int64_t file_size
|
2017-09-28 10:11:20 +02:00
|
|
|
, file_flags_t file_flags = {}, char const* filehash = nullptr
|
2017-09-02 23:58:36 +02:00
|
|
|
, std::int64_t mtime = 0, string_view symlink_path = string_view());
|
|
|
|
TORRENT_DEPRECATED
|
2016-06-20 17:32:06 +02:00
|
|
|
void add_file(file_entry const& fe, char const* filehash = nullptr);
|
2015-01-21 16:46:12 +01:00
|
|
|
|
2013-08-02 07:03:22 +02:00
|
|
|
// all wstring APIs are deprecated since 0.16.11
|
|
|
|
// instead, use the wchar -> utf8 conversion functions
|
|
|
|
// and pass in utf8 strings
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2017-08-03 11:31:03 +02:00
|
|
|
void add_file(std::wstring const& p, std::int64_t size
|
|
|
|
, file_flags_t flags = {}
|
2016-08-13 13:04:53 +02:00
|
|
|
, std::time_t mtime = 0, string_view s_p = "");
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2016-12-22 16:42:33 +01:00
|
|
|
void rename_file(file_index_t index, std::wstring const& new_filename);
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
|
|
|
void set_name(std::wstring const& n);
|
2014-07-05 16:10:25 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
void rename_file_deprecated(file_index_t index, std::wstring const& new_filename);
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// 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().
|
2016-01-21 23:46:03 +01:00
|
|
|
//
|
|
|
|
// Preconditions of this function is that the input range is within the
|
|
|
|
// torrents address space. ``piece`` may not be negative and
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2016-01-21 23:46:03 +01:00
|
|
|
// ``piece`` * piece_size + ``offset`` + ``size``
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2016-01-21 23:46:03 +01:00
|
|
|
// may not exceed the total size of the torrent.
|
2016-12-22 16:42:33 +01:00
|
|
|
std::vector<file_slice> map_block(piece_index_t piece, std::int64_t offset
|
2008-05-28 10:44:40 +02:00
|
|
|
, int size) const;
|
2013-08-17 09:01:03 +02:00
|
|
|
|
|
|
|
// returns a peer_request representing the piece index, byte offset
|
|
|
|
// and size the specified file range overlaps. This is the inverse
|
2017-06-17 17:35:53 +02:00
|
|
|
// mapping over map_block(). Note that the ``peer_request`` return type
|
2015-04-10 05:25:02 +02:00
|
|
|
// 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.
|
2016-12-22 16:42:33 +01:00
|
|
|
peer_request map_file(file_index_t file, std::int64_t offset, int size) const;
|
2013-08-12 09:30:57 +02:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2013-08-12 05:18:43 +02:00
|
|
|
// all functions depending on internal_file_entry
|
|
|
|
// were deprecated in 1.0. Use the variants that take an
|
|
|
|
// index instead
|
2017-05-28 13:14:58 +02:00
|
|
|
using iterator = std::vector<internal_file_entry>::const_iterator;
|
|
|
|
using reverse_iterator = std::vector<internal_file_entry>::const_reverse_iterator;
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2016-06-18 20:01:38 +02:00
|
|
|
iterator file_at_offset(std::int64_t offset) const;
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2015-04-26 03:45:23 +02:00
|
|
|
iterator begin() const { return m_files.begin(); }
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2015-04-26 03:45:23 +02:00
|
|
|
iterator end() const { return m_files.end(); }
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2015-04-26 03:45:23 +02:00
|
|
|
reverse_iterator rbegin() const { return m_files.rbegin(); }
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2015-04-26 03:45:23 +02:00
|
|
|
reverse_iterator rend() const { return m_files.rend(); }
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
2018-06-02 19:29:40 +02:00
|
|
|
internal_file_entry const& internal_at(int const index) const;
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
|
|
|
file_entry at(iterator i) const;
|
2013-10-03 17:45:37 +02:00
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
// returns a file_entry with information about the file
|
|
|
|
// at ``index``. Index must be in the range [0, ``num_files()`` ).
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
|
|
|
file_entry at(int index) const;
|
2015-01-21 16:46:12 +01:00
|
|
|
|
2013-10-03 17:45:37 +02:00
|
|
|
iterator begin_deprecated() const { return m_files.begin(); }
|
|
|
|
iterator end_deprecated() const { return m_files.end(); }
|
|
|
|
reverse_iterator rbegin_deprecated() const { return m_files.rbegin(); }
|
|
|
|
reverse_iterator rend_deprecated() const { return m_files.rend(); }
|
2016-06-18 20:01:38 +02:00
|
|
|
iterator file_at_offset_deprecated(std::int64_t offset) const;
|
2015-04-25 06:22:51 +02:00
|
|
|
file_entry at_deprecated(int index) const;
|
2018-04-26 09:01:14 +02:00
|
|
|
#endif // TORRENT_ABI_VERSION
|
2013-08-12 05:18:43 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// returns the number of files in the file_storage
|
2018-06-02 19:29:40 +02:00
|
|
|
int num_files() const noexcept;
|
2013-08-12 05:18:43 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
// returns the index of the one-past-end file in the file storage
|
2018-06-02 19:29:40 +02:00
|
|
|
file_index_t end_file() const noexcept;
|
2016-12-22 16:42:33 +01:00
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
// 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.
|
|
|
|
index_range<file_index_t> file_range() const noexcept;
|
2016-12-22 16:42:33 +01:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// returns the total number of bytes all the files in this torrent spans
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t total_size() const { return m_total_size; }
|
2013-08-17 09:01:03 +02:00
|
|
|
|
|
|
|
// set and get the number of pieces in the torrent
|
2008-05-28 10:44:40 +02:00
|
|
|
void set_num_pieces(int n) { m_num_pieces = n; }
|
|
|
|
int num_pieces() const { TORRENT_ASSERT(m_piece_length > 0); return m_num_pieces; }
|
2013-08-17 09:01:03 +02:00
|
|
|
|
2016-12-22 16:42:33 +01:00
|
|
|
// returns the index of the one-past-end piece in the file storage
|
|
|
|
piece_index_t end_piece() const
|
|
|
|
{ return piece_index_t(m_num_pieces); }
|
|
|
|
|
|
|
|
piece_index_t last_piece() const
|
|
|
|
{ return piece_index_t(m_num_pieces - 1); }
|
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
// 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.
|
|
|
|
index_range<piece_index_t> piece_range() const noexcept;
|
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// set and get the size of each piece in this torrent. This size is typically an even power
|
2017-06-17 17:35:53 +02:00
|
|
|
// of 2. It doesn't have to be though. It should be divisible by 16 kiB however.
|
2008-05-28 10:44:40 +02:00
|
|
|
void set_piece_length(int l) { m_piece_length = l; }
|
|
|
|
int piece_length() const { TORRENT_ASSERT(m_piece_length > 0); return m_piece_length; }
|
2013-08-17 09:01:03 +02:00
|
|
|
|
|
|
|
// returns the piece size of ``index``. This will be the same as piece_length(), except
|
|
|
|
// for the last piece, which may be shorter.
|
2016-12-22 16:42:33 +01:00
|
|
|
int piece_size(piece_index_t index) const;
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// 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.
|
2008-05-28 10:44:40 +02:00
|
|
|
void set_name(std::string const& n) { m_name = n; }
|
2015-01-21 16:46:12 +01:00
|
|
|
std::string const& name() const { return m_name; }
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2013-08-17 09:01:03 +02:00
|
|
|
// swap all content of *this* with *ti*.
|
2018-06-02 19:29:40 +02:00
|
|
|
void swap(file_storage& ti) noexcept;
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2014-06-18 08:22:01 +02:00
|
|
|
// if pad_file_limit >= 0, files larger than that limit will be padded,
|
|
|
|
// default is to not add any padding (-1). The alignment specifies the
|
|
|
|
// alignment files should be padded to. This defaults to the piece size
|
|
|
|
// (-1) but it may also make sense to set it to 16 kiB, or something
|
|
|
|
// divisible by 16 kiB.
|
|
|
|
// If pad_file_limit is 0, every file will be padded (except empty ones).
|
2015-03-21 01:12:40 +01:00
|
|
|
// ``tail_padding`` indicates whether aligned files also are padded at
|
|
|
|
// the end to make them end aligned. This is required for mutable
|
|
|
|
// torrents, since piece hashes are compared
|
|
|
|
void optimize(int pad_file_limit = -1, int alignment = -1
|
|
|
|
, bool tail_padding = false);
|
2009-01-11 11:32:57 +01:00
|
|
|
|
2013-08-12 05:18:43 +02:00
|
|
|
// These functions are used to query attributes of files at
|
|
|
|
// a given index.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2017-06-18 20:39:10 +02:00
|
|
|
// The ``hash()`` is a SHA-1 hash of the file, or 0 if none was
|
2013-08-12 05:18:43 +02:00
|
|
|
// provided in the torrent file. This can potentially be used to
|
|
|
|
// join a bittorrent network with other file sharing networks.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-08-12 05:18:43 +02:00
|
|
|
// The ``mtime()`` is the modification time is the posix
|
|
|
|
// time when a file was last modified when the torrent
|
|
|
|
// was created, or 0 if it was not included in the torrent file.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-08-12 05:18:43 +02:00
|
|
|
// ``file_path()`` returns the full path to a file.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-08-12 05:18:43 +02:00
|
|
|
// ``file_size()`` returns the size of a file.
|
2016-10-22 17:47:24 +02:00
|
|
|
//
|
2013-08-12 05:18:43 +02:00
|
|
|
// ``pad_file_at()`` returns true if the file at the given
|
|
|
|
// index is a pad-file.
|
|
|
|
//
|
|
|
|
// ``file_name()`` returns *just* the name of the file, whereas
|
|
|
|
// ``file_path()`` returns the path (inside the torrent file) with
|
|
|
|
// the filename appended.
|
|
|
|
//
|
|
|
|
// ``file_offset()`` returns the byte offset within the torrent file
|
|
|
|
// where this file starts. It can be used to map the file to a piece
|
|
|
|
// index (given the piece size).
|
2016-12-22 16:42:33 +01:00
|
|
|
sha1_hash hash(file_index_t index) const;
|
|
|
|
std::string const& symlink(file_index_t index) const;
|
|
|
|
std::time_t mtime(file_index_t index) const;
|
|
|
|
std::string file_path(file_index_t index, std::string const& save_path = "") const;
|
|
|
|
string_view file_name(file_index_t index) const;
|
|
|
|
std::int64_t file_size(file_index_t index) const;
|
|
|
|
bool pad_file_at(file_index_t index) const;
|
|
|
|
std::int64_t file_offset(file_index_t index) const;
|
2012-03-26 18:07:55 +02:00
|
|
|
|
2015-02-17 03:08:47 +01:00
|
|
|
// returns the crc32 hash of file_path(index)
|
2016-12-22 16:42:33 +01:00
|
|
|
std::uint32_t file_path_hash(file_index_t index, std::string const& save_path) const;
|
2015-02-17 03:08:47 +01:00
|
|
|
|
2015-03-21 17:55:23 +01:00
|
|
|
// 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.
|
2016-06-18 20:01:38 +02:00
|
|
|
void all_path_hashes(std::unordered_set<std::uint32_t>& table) const;
|
2015-02-17 03:08:47 +01:00
|
|
|
|
2017-08-03 11:31:03 +02:00
|
|
|
// 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.
|
|
|
|
static constexpr file_flags_t flag_pad_file = 0_bit;
|
|
|
|
|
|
|
|
// this file has the hidden attribute set. This is primarily
|
|
|
|
// a windows attribute
|
|
|
|
static constexpr file_flags_t flag_hidden = 1_bit;
|
|
|
|
|
|
|
|
// this file has the executable attribute set.
|
|
|
|
static constexpr file_flags_t flag_executable = 2_bit;
|
|
|
|
|
|
|
|
// this file is a symbolic link. It should have a link
|
|
|
|
// target string associated with it.
|
|
|
|
static constexpr file_flags_t flag_symlink = 3_bit;
|
2013-08-12 09:30:57 +02:00
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
std::vector<std::string> const& paths() const { return m_paths; }
|
|
|
|
|
2013-08-12 09:30:57 +02:00
|
|
|
// returns a bitmask of flags from file_flags_t that apply
|
|
|
|
// to file at ``index``.
|
2017-08-03 11:31:03 +02:00
|
|
|
file_flags_t file_flags(file_index_t index) const;
|
2013-08-12 09:30:57 +02:00
|
|
|
|
2016-05-01 18:23:02 +02:00
|
|
|
// 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.
|
2016-12-22 16:42:33 +01:00
|
|
|
bool file_absolute_path(file_index_t index) const;
|
2016-05-01 18:23:02 +02:00
|
|
|
|
2013-08-12 09:30:57 +02:00
|
|
|
// returns the index of the file at the given offset in the torrent
|
2016-12-22 16:42:33 +01:00
|
|
|
file_index_t file_index_at_offset(std::int64_t offset) const;
|
2013-08-12 09:30:57 +02:00
|
|
|
|
|
|
|
// low-level function. returns a pointer to the internal storage for
|
2016-06-20 17:32:06 +02:00
|
|
|
// the filename. This string may not be 0-terminated!
|
2013-08-12 09:30:57 +02:00
|
|
|
// the ``file_name_len()`` function returns the length of the filename.
|
2017-10-01 03:17:10 +02:00
|
|
|
// prefer to use ``file_name()`` instead, which returns a ``string_view``.
|
2016-12-22 16:42:33 +01:00
|
|
|
char const* file_name_ptr(file_index_t index) const;
|
|
|
|
int file_name_len(file_index_t index) const;
|
2013-08-12 09:30:57 +02:00
|
|
|
|
2018-04-26 09:01:14 +02:00
|
|
|
#if TORRENT_ABI_VERSION == 1
|
2013-08-12 05:18:43 +02:00
|
|
|
// these were deprecated in 1.0. Use the versions that take an index instead
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
|
|
|
sha1_hash hash(internal_file_entry const& fe) const;
|
|
|
|
TORRENT_DEPRECATED
|
|
|
|
std::string const& symlink(internal_file_entry const& fe) const;
|
|
|
|
TORRENT_DEPRECATED
|
2016-12-11 02:51:55 +01:00
|
|
|
std::time_t mtime(internal_file_entry const& fe) const;
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
|
|
|
int file_index(internal_file_entry const& fe) const;
|
|
|
|
TORRENT_DEPRECATED
|
|
|
|
std::string file_path(internal_file_entry const& fe, std::string const& save_path = "") const;
|
|
|
|
TORRENT_DEPRECATED
|
|
|
|
std::string file_name(internal_file_entry const& fe) const;
|
|
|
|
TORRENT_DEPRECATED
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_size(internal_file_entry const& fe) const;
|
2015-04-17 03:49:49 +02:00
|
|
|
TORRENT_DEPRECATED
|
|
|
|
bool pad_file_at(internal_file_entry const& fe) const;
|
|
|
|
TORRENT_DEPRECATED
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t file_offset(internal_file_entry const& fe) const;
|
2013-08-12 05:18:43 +02:00
|
|
|
#endif
|
2010-11-29 06:44:29 +01:00
|
|
|
|
2014-12-17 06:33:16 +01:00
|
|
|
// if the backing buffer changed for this storage, this is the pointer
|
|
|
|
// offset to add to any pointers to make them point into the new buffer
|
2017-01-24 20:03:17 +01:00
|
|
|
void apply_pointer_offset(std::ptrdiff_t off);
|
2014-12-17 06:33:16 +01:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
private:
|
2010-11-15 06:10:36 +01:00
|
|
|
|
2018-06-02 19:29:40 +02:00
|
|
|
file_index_t last_file() const noexcept;
|
|
|
|
|
2017-08-03 10:38:58 +02:00
|
|
|
int get_or_add_path(string_view path);
|
|
|
|
|
2015-03-21 01:12:40 +01:00
|
|
|
void add_pad_file(int size
|
|
|
|
, std::vector<internal_file_entry>::iterator& i
|
2016-06-18 20:01:38 +02:00
|
|
|
, std::int64_t& offset
|
2015-03-21 01:12:40 +01:00
|
|
|
, int& pad_file_counter);
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
// the number of bytes in a regular piece
|
|
|
|
// (i.e. not the potentially truncated last piece)
|
|
|
|
int m_piece_length;
|
|
|
|
|
|
|
|
// the number of pieces in the torrent
|
|
|
|
int m_num_pieces;
|
|
|
|
|
2015-02-17 03:08:47 +01:00
|
|
|
void update_path_index(internal_file_entry& e, std::string const& path
|
|
|
|
, bool set_name = true);
|
2010-11-25 00:49:22 +01:00
|
|
|
void reorder_file(int index, int dst);
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
// the list of files that this torrent consists of
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<internal_file_entry, file_index_t> m_files;
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2015-01-21 16:46:12 +01:00
|
|
|
// if there are sha1 hashes for each individual file there are as many
|
|
|
|
// entries in this array as the m_files array. Each entry in m_files has
|
|
|
|
// a corresponding hash pointer in this array. The reason to split it up
|
|
|
|
// in separate arrays is to save memory in case the torrent doesn't have
|
|
|
|
// file hashes
|
|
|
|
// the pointers in this vector are pointing into the .torrent file in
|
|
|
|
// memory which is _not_ owned by this file_storage object. It's simply
|
|
|
|
// a non-owning pointer. It is the user's responsibility that the hash
|
|
|
|
// stays valid throughout the lifetime of this file_storage object.
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<char const*, file_index_t> m_file_hashes;
|
2010-11-15 06:10:36 +01:00
|
|
|
|
|
|
|
// for files that are symlinks, the symlink
|
2010-11-29 06:44:29 +01:00
|
|
|
// path_index in the internal_file_entry indexes
|
2010-11-15 06:10:36 +01:00
|
|
|
// this vector of strings
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<std::string, file_index_t> m_symlinks;
|
2010-11-15 06:10:36 +01:00
|
|
|
|
2010-11-25 00:49:22 +01:00
|
|
|
// the modification times of each file. This vector
|
|
|
|
// is empty if no file have a modification time.
|
|
|
|
// each element corresponds to the file with the same
|
|
|
|
// index in m_files
|
2016-12-22 16:42:33 +01:00
|
|
|
aux::vector<std::time_t, file_index_t> m_mtime;
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2010-11-29 06:44:29 +01:00
|
|
|
// all unique paths files have. The internal_file_entry::path_index
|
2013-11-17 09:02:16 +01:00
|
|
|
// points into this array. The paths don't include the root directory
|
|
|
|
// name for multi-file torrents. The m_name field need to be
|
|
|
|
// prepended to these paths, and the filename of a specific file
|
|
|
|
// entry appended, to form full file paths
|
2017-01-13 07:55:29 +01:00
|
|
|
aux::vector<std::string> m_paths;
|
2010-11-25 00:49:22 +01:00
|
|
|
|
2010-11-15 06:10:36 +01:00
|
|
|
// name of torrent. For multi-file torrents
|
|
|
|
// this is always the root directory
|
2018-02-01 01:06:15 +01:00
|
|
|
std::string m_name;
|
2010-08-22 00:10:16 +02:00
|
|
|
|
2016-03-15 01:43:52 +01:00
|
|
|
// the sum of all file sizes
|
2016-06-18 20:01:38 +02:00
|
|
|
std::int64_t m_total_size;
|
2008-05-28 10:44:40 +02:00
|
|
|
};
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
namespace aux {
|
|
|
|
|
|
|
|
// returns the piece range that entirely falls within the specified file. the
|
2016-12-13 05:53:03 +01:00
|
|
|
// end piece is one-past the last piece that entirely falls within the file.
|
2016-10-03 08:08:03 +02:00
|
|
|
// i.e. They can conveniently be used as loop boundaries. No edge partial
|
|
|
|
// pieces will be included.
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_EXTRA_EXPORT std::tuple<piece_index_t, piece_index_t>
|
|
|
|
file_piece_range_exclusive(file_storage const& fs, file_index_t file);
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
// returns the piece range of pieces that overlaps with the specified file.
|
|
|
|
// the end piece is one-past the last piece. i.e. They can conveniently be
|
|
|
|
// used as loop boundaries.
|
2016-12-22 16:42:33 +01:00
|
|
|
TORRENT_EXTRA_EXPORT std::tuple<piece_index_t, piece_index_t>
|
|
|
|
file_piece_range_inclusive(file_storage const& fs, file_index_t file);
|
2016-10-03 08:08:03 +02:00
|
|
|
|
|
|
|
} // namespace aux
|
|
|
|
} // namespace libtorrent
|
2008-05-28 10:44:40 +02:00
|
|
|
|
|
|
|
#endif // TORRENT_FILE_STORAGE_HPP_INCLUDED
|