diff --git a/Jamfile b/Jamfile index c7991f8ad..847568222 100755 --- a/Jamfile +++ b/Jamfile @@ -262,6 +262,7 @@ SOURCES = create_torrent disk_buffer_holder entry + file_storage lazy_bdecode escape_string gzip diff --git a/docs/make_torrent.rst b/docs/make_torrent.rst new file mode 100644 index 000000000..0effd2c29 --- /dev/null +++ b/docs/make_torrent.rst @@ -0,0 +1,194 @@ +================= +creating torrents +================= + +:Author: Arvid Norberg, arvid@rasterbar.com + +.. contents:: Table of contents + :depth: 2 + :backlinks: none + +overview +======== + +This section describes the functions and classes that are used +to create torrent files. It is a layered API with low level classes +and higher level convenience functions. A torrent is created in 4 +steps: + +1. first the files that will be part of the torrent are determined. +2. the torrent properties are set, such as tracker url, web seeds, + DHT nodes etc. +3. Read through all the files in the torrent, SHA-1 all the data + and set the piece hashes. +4. The torrent is bencoded into a file or buffer. + +If there are a lot of files and or deep directoy hierarchies to +traverse, step one can be time consuming. + +Typically step 3 is by far the most time consuming step, since it +requires to read all the bytes from all the files in the torrent. + +All of these classes and functions are declared by including +``libtorrent/create_torrent.hpp``. + +high level example +================== + +:: + + file_storage fs; + + // recursively adds files in directories + add_files(fs, "./my_torrent"); + + create_torrent t(fs); + t.add_tracker("http://my.tracker.com/announce"); + t.set_creator("libtorrent example"); + + // reads the files and calculates the hashes + set_piece_hashes(t, "."); + + ofstream out("my_torrent.torrent", std::ios_base::binary); + bencode(std::ostream_iterator(out), t.generate()); + +add_files +========= + + :: + + template + void add_files(file_storage& fs, boost::filesystem::path const& path, Pred p); + + void add_files(file_storage& fs, boost::filesystem::path const& path); + +Adds the file specified by ``path`` to the ``file_storage`` object. In case ``path`` +refers to a diretory, files will be added recursively from the directory. + +If specified, the predicate ``p`` is called once for every file and directory that +is encountered. files for which ``p`` returns true are added, and directories for +which ``p`` returns true are traversed. ``p`` must have the following signature:: + + bool Pred(boost::filesystem::path const& p); + +The path that is passed in to the predicate is the full path of the file or +directory. If no predicate is specified, all files are added, and all directories +are traveresed. + +The ".." directory is never traversed. + +set_piece_hashes() +================== + + :: + + template + void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f); + + void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p); + +This function will assume that the files added to the torrent file exists at path +``p``, read those files and hash the content and set the hashes in the ``create_torrent`` +object. The optional function ``f`` is called in between every hash that is set. ``f`` +must have the following signature:: + + void Fun(int); + +file_storage +============ + +The ``file_storage`` class represents a file list and the piece +size. Everything necessary to interpret a regular bittorrent storage +file structure. Its synopsis:: + + class file_storage + { + public: + + bool is_valid() const; + + void add_file(file_entry const& e); + void add_file(fs::path const& p, size_type size); + void rename_file(int index, std::string const& new_filename); + + std::vector map_block(int piece, size_type offset + , int size) const; + peer_request map_file(int file, size_type offset, int size) const; + + typedef std::vector::const_iterator iterator; + typedef std::vector::const_reverse_iterator reverse_iterator; + + iterator begin() const; + iterator end() const; + reverse_iterator rbegin(); + reverse_iterator rend() const; + int num_files() const; + + file_entry const& at(int index) const; + + size_type total_size() const; + void set_num_pieces(int n); + int num_pieces() const; + void set_piece_length(int l); + int piece_length() const; + int piece_size(int index) const; + + void set_name(std::string const& n); + const std::string& name() const; + + void swap(file_storage& ti); + } + + +create_torrent +============== + +The ``create_torrent`` class has the following synopsis:: + + + struct create_torrent + { + create_torrent(file_storage& fs, int piece_size); + create_torrent(file_storage& fs); + + entry generate() const; + + file_storage const& files() const; + + void set_comment(char const* str); + void set_creator(char const* str); + void set_hash(int index, sha1_hash const& h); + void add_url_seed(std::string const& url); + void add_node(std::pair const& node); + void add_tracker(std::string const& url, int tier = 0); + + int num_pieces() const; + int piece_length() const; + int piece_size(int i) const; + }; + +create_torrent() +---------------- + + :: + + create_torrent(file_storage& fs, int piece_size); + create_torrent(file_storage& fs); + +The contrstructor that does not take a piece_size will calculate +a piece size such that the torrent file is roughly 40 kB. + +generate() +---------- + + :: + + entry generate() const; + +This function will generate the .torrent file as a bencode tree. In order to +generate the flat file, use the bencode() function. + +It may be useful to add custom entries to the torrent file before bencoding it +and saving it to disk. + + diff --git a/docs/manual.html b/docs/manual.html index 1940d9cc7..4d003fbb8 100644 --- a/docs/manual.html +++ b/docs/manual.html @@ -3,7 +3,7 @@ - + libtorrent API Documentation @@ -16,239 +16,240 @@ Author: -Arvid Norberg, arvid@rasterbar.com +Arvid Norberg, arvid@rasterbar.com Version: 0.13
-

Table of contents

+

Table of contents

-
-

overview

+
+

overview

The interface of libtorrent consists of a few classes. The main class is the session, it contains the main loop that serves all torrents.

The basic usage is as follows:

Each class and function is described in this manual.

+

For a description on how to create torrent files, see make_torrent.

-
-

network primitives

+
+

network primitives

There are a few typedefs in the libtorrent namespace which pulls in network types from the asio namespace. These are:

@@ -266,10 +267,10 @@ udp::endpoint
 

Which are the endpoint types used in libtorrent. An endpoint is an address with an associated port.

-

For documentation on these types, please refer to the asio documentation.

+

For documentation on these types, please refer to the asio documentation.

-
-

session

+
+

session

The session class has the following synopsis:

 class session: public boost::noncopyable
@@ -368,8 +369,8 @@ class session: public boost::noncopyable
 

Once it's created, the session object will spawn the main thread that will do all the work. The main thread will be idle as long it doesn't have any torrents to participate in.

-
-

session()

+
+

session()

 session(fingerprint const& print
@@ -382,22 +383,22 @@ session(fingerprint const& print
 

If the fingerprint in the first overload is omited, the client will get a default fingerprint stating the version of libtorrent. The fingerprint is a short string that will be used in the peer-id to identify the client and the client's version. For more details see the -fingerprint class. The constructor that only takes a fingerprint will not open a +fingerprint class. The constructor that only takes a fingerprint will not open a listen port for the session, to get it running you'll have to call session::listen_on(). The other constructor, that takes a port range and an interface as well as the fingerprint will automatically try to listen on a port on the given interface. For more information about the parameters, see listen_on() function.

-
-

~session()

+
+

~session()

The destructor of session will notify all trackers that our torrents have been shut down. If some trackers are down, they will time out. All this before the destructor of session returns. So, it's advised that any kind of interface (such as windows) are closed before destructing the session object. Because it can take a few second for it to finish. The timeout can be set with set_settings().

-
-

abort()

+
+

abort()

 session_proxy abort();
 
@@ -419,8 +420,8 @@ public: };
-
-

add_torrent()

+
+

add_torrent()

 typedef storage_interface* (&storage_constructor_type)(
@@ -464,12 +465,12 @@ the torrent as long as it doesn't have metadata. See add_torrent() will throw
-duplicate_torrent which derives from std::exception unless duplicate_is_error
+duplicate_torrent which derives from std::exception unless duplicate_is_error
 is set to false. In that case, add_torrent will return the handle to the existing
 torrent.

The optional parameter, resume_data can be given if up to date fast-resume data is available. The fast-resume data can be acquired from a running torrent by calling -save_resume_data() on torrent_handle. See fast resume.

+save_resume_data() on torrent_handle. See fast resume.

The storage_mode parameter refers to the layout of the storage for this torrent. There are 3 different modes:

@@ -485,7 +486,7 @@ filesystems that don't support sparse files. are rearranged to finally be in their correct places once the entire torrent has been downloaded.
-

For more information, see storage allocation.

+

For more information, see storage allocation.

paused is a boolean that specifies whether or not the torrent is to be started in a paused state. I.e. it won't connect to the tracker or any of the peers until it's resumed. This is typically a good way of avoiding race conditions when setting @@ -493,19 +494,19 @@ configuration options on torrents before starting them.

If auto_managed is true, this torrent will be queued, started and seeded automatically by libtorrent. When this is set, the torrent should also be started as paused. The default queue order is the order the torrents were added. They -are all downloaded in that order. For more details, see queuing.

+are all downloaded in that order. For more details, see queuing.

storage can be used to customize how the data is stored. The default storage will simply write the data to the files it belongs to, but it could be overridden to save everything to a single file at a specific location or encrypt the content on disk for instance. For more information about the storage_interface -that needs to be implemented for a custom storage, see storage_interface.

+that needs to be implemented for a custom storage, see storage_interface.

The userdata parameter is optional and will be passed on to the extension -constructor functions, if any (see add_extension()).

-

The torrent_handle returned by add_torrent() can be used to retrieve information +constructor functions, if any (see add_extension()).

+

The torrent_handle returned by add_torrent() can be used to retrieve information about the torrent's progress, its peers etc. It is also used to abort a torrent.

-
-

remove_torrent()

+
+

remove_torrent()

 void remove_torrent(torrent_handle const& h, int options = none);
@@ -516,10 +517,10 @@ the tracker that we've stopped participating in the swarm. The optional second a
 options can be used to delete all the files downloaded by this torrent. To do this, pass
 in the value session::delete_files. The removal of the torrent is asyncronous, there is
 no guarantee that adding the same torrent immediately after it was removed will not throw
-a duplicate_torrent exception.

+a duplicate_torrent exception.

-
-

find_torrent() get_torrents()

+
+

find_torrent() get_torrents()

 torrent_handle find_torrent(sha_hash const& ih);
@@ -533,8 +534,8 @@ In case the torrent cannot be found, an invalid torrent_handle is returned.

get_torrents() returns a vector of torrent_handles to all the torrents currently in the session.

-
-

set_upload_rate_limit() set_download_rate_limit() upload_rate_limit() download_rate_limit()

+
+

set_upload_rate_limit() set_download_rate_limit() upload_rate_limit() download_rate_limit()

 void set_upload_rate_limit(int bytes_per_second);
@@ -551,8 +552,8 @@ of upload rate.
 download_rate_limit() and upload_rate_limit() returns the previously
 set limits.

-
-

set_max_uploads() set_max_connections()

+
+

set_max_uploads() set_max_connections()

 void set_max_uploads(int limit);
@@ -565,8 +566,8 @@ minimum of at least two connections per torrent, so if you set a too low
 connections limit, and open too many torrents, the limit will not be met. The
 number of uploads is at least one per torrent.

-
-

num_uploads() num_connections()

+
+

num_uploads() num_connections()

 int num_uploads() const;
@@ -576,8 +577,8 @@ int num_connections() const;
 

Returns the number of currently unchoked peers and the number of connections (including half-open ones) respectively.

-
-

set_max_half_open_connections() max_half_open_connections()

+
+

set_max_half_open_connections() max_half_open_connections()

 void set_max_half_open_connections(int limit);
@@ -596,8 +597,8 @@ their turn to get connected.

max_half_open_connections() returns the set limit. This limit defaults to 8 on windows.

-
-

load_asnum_db() load_country_db() int as_for_ip()

+
+

load_asnum_db() load_country_db() int as_for_ip()

 bool load_asnum_db(char const* file);
@@ -606,13 +607,13 @@ int as_for_ip(address const& adr);
 

These functions are not available if TORRENT_DISABLE_GEO_IP is defined. They -expects a path to the MaxMind ASN database and MaxMind GeoIP database +expects a path to the MaxMind ASN database and MaxMind GeoIP database respectively. This will be used to look up which AS and country peers belong to.

as_for_ip returns the AS number for the IP address specified. If the IP is not in the database or the ASN database is not loaded, 0 is returned.

-
-

load_state() state()

+
+

load_state() state()

 void load_state(entry const& ses_state);
@@ -623,8 +624,8 @@ entry state() const;
 that's stored is peak download rates for ASes. This map is used to
 determine which order to connect to peers.

-
-

set_ip_filter()

+
+

set_ip_filter()

 void set_ip_filter(ip_filter const& filter);
@@ -633,12 +634,12 @@ void set_ip_filter(ip_filter const& filter);
 

Sets a filter that will be used to reject and accept incoming as well as outgoing connections based on their originating ip address. The default filter will allow connections to any ip address. To build a set of rules for which addresses are -accepted and not, see ip_filter.

-

Each time a peer is blocked because of the IP filter, a peer_blocked_alert is +accepted and not, see ip_filter.

+

Each time a peer is blocked because of the IP filter, a peer_blocked_alert is generated.

-
-

status()

+
+

status()

 session_status status() const;
@@ -700,8 +701,8 @@ becomes unresponsive.

dht_global_nodes is an estimation of the total number of nodes in the DHT network.

-
-

get_cache_status()

+
+

get_cache_status()

 cache_status get_cache_status() const;
@@ -738,8 +739,8 @@ for the read cache.

This includes both read and write cache.

read_cache_size is the number of 16KiB blocks in the read cache.

-
-

get_cache_info()

+
+

get_cache_info()

 void get_cache_info(sha1_hash const& ih
@@ -765,8 +766,8 @@ the data for that block being in the disk cache and last_use is the time when a block was last written to this piece. The older
 a piece is, the more likely it is to be flushed to disk.

-
-

is_listening() listen_port() listen_on()

+
+

is_listening() listen_port() listen_on()

 bool is_listening() const;
@@ -790,7 +791,7 @@ the range and so on. The interface parameter can be left as 0, in that case the
 os will decide which interface to listen on, otherwise it should be the ip-address
 of the interface you want the listener socket bound to. listen_on() returns true
 if it managed to open the socket, and false if it failed. If it fails, it will also
-generate an appropriate alert (listen_failed_alert).

+generate an appropriate alert (listen_failed_alert).

The interface parameter can also be a hostname that will resolve to the device you want to listen on.

If you're also starting the DHT, it is a good idea to do that after you've called @@ -809,8 +810,8 @@ with a DHT ping packet, and connect to those that responds first. On windows one can only connect to a few peers at a time because of a built in limitation (in XP Service pack 2).

-
-

pop_alert() set_severity_level() wait_for_alert()

+
+

pop_alert() set_severity_level() wait_for_alert()

 std::auto_ptr<alert> pop_alert();
@@ -820,7 +821,7 @@ void set_severity_level(alert::severity_t s);
 

pop_alert() is used to ask the session if any errors or events has occurred. With set_severity_level() you can filter how serious the event has to be for you to -receive it through pop_alert(). For information, see alerts.

+receive it through pop_alert(). For information, see alerts.

wait_for_alert blocks until an alert is available, or for no more than max_wait time. If wait_for_alert returns because of the time-out, and no alerts are available, it returns 0. If at least one alert was generated, a pointer to that alert is returned. @@ -829,8 +830,8 @@ same pointer until the alert is popped by calling < leaving any alert dispatching mechanism independent of this blocking call, the dispatcher can be called and it can pop the alert independently.

-
-

add_extension()

+
+

add_extension()

 void add_extension(boost::function<
@@ -840,7 +841,7 @@ void add_extension(boost::function<
 

This function adds an extension to this session. The argument is a function object that is called with a torrent* and which should return a boost::shared_ptr<torrent_plugin>. To write custom plugins, see -libtorrent plugins. The main plugins implemented in libtorrent are:

+libtorrent plugins. The main plugins implemented in libtorrent are:

metadata extension
Allows peers to download the metadata (.torren files) from the swarm @@ -878,8 +879,8 @@ eliminate most problems on poisoned torrents.
ses.add_extension(&libtorrent::create_smart_ban_plugin);
-
-

set_settings() set_pe_settings()

+
+

set_settings() set_pe_settings()

 void set_settings(session_settings const& settings);
@@ -887,11 +888,11 @@ void set_pe_settings(pe_settings const& settings);
 

Sets the session settings and the packet encryption settings respectively. -See session_settings and pe_settings for more information on available +See session_settings and pe_settings for more information on available options.

-
-

set_peer_proxy() set_web_seed_proxy() set_tracker_proxy() set_dht_proxy()

+
+

set_peer_proxy() set_web_seed_proxy() set_tracker_proxy() set_dht_proxy()

 void set_peer_proxy(proxy_settings const& s);
@@ -904,16 +905,16 @@ void set_dht_proxy(proxy_settings const& s);
 sets the proxy settings for different kinds of connections, bittorrent peers,
 web seeds, trackers and the DHT traffic.

set_peer_proxy affects regular bittorrent peers. set_web_seed_proxy -affects only web seeds. see HTTP seeding.

+affects only web seeds. see HTTP seeding.

set_tracker_proxy only affects HTTP tracker connections (UDP tracker connections are affected if the given proxy supports UDP, e.g. SOCKS5).

set_dht_proxy affects the DHT messages. Since they are sent over UDP, it only has any effect if the proxy supports UDP.

For more information on what settings are available for proxies, see -proxy_settings.

+proxy_settings.

-
-

peer_proxy() web_seed_proxy() tracker_proxy() dht_proxy()

+
+

peer_proxy() web_seed_proxy() tracker_proxy() dht_proxy()

 proxy_settings const& peer_proxy() const;
@@ -925,8 +926,8 @@ proxy_settings const& dht_proxy() const;
 

These functions returns references to their respective current settings.

The dht_proxy is not available when DHT is disabled.

-
-

start_dht() stop_dht() set_dht_settings() dht_state()

+
+

start_dht() stop_dht() set_dht_settings() dht_state()

 void start_dht(entry const& startup_state);
@@ -986,8 +987,8 @@ that are ready to replace a failing node, it will be replaced immediately,
 this limit is only used to clear out nodes that don't have any node that can
 replace them.

-
-

add_dht_node() add_dht_router()

+
+

add_dht_node() add_dht_router()

 void add_dht_node(std::pair<std::string, int> const& node);
@@ -1005,8 +1006,8 @@ for bootstrapping, to keep the load off them.

An example routing node that you could typically add is router.bittorrent.com.

-
-

start_lsd() stop_lsd()

+
+

start_lsd() stop_lsd()

 void start_lsd();
@@ -1018,8 +1019,8 @@ the infohashes of all the non-private torrents on the local network to
 look for peers on the same swarm within multicast reach.

It is turned off by default.

-
-

start_upnp() stop_upnp()

+
+

start_upnp() stop_upnp()

 upnp* start_upnp();
@@ -1030,12 +1031,12 @@ void stop_upnp();
 port are attempted to be forwarded on local UPnP router devices.

The upnp object returned by start_upnp() can be used to add and remove arbitrary port mappings. Mapping status is returned through the -portmap_alert and the portmap_error_alert. The object will be valid until -stop_upnp() is called. See UPnP and NAT-PMP.

+portmap_alert and the portmap_error_alert. The object will be valid until +stop_upnp() is called. See UPnP and NAT-PMP.

It is off by default.

-
-

start_natpmp() stop_natpmp()

+
+

start_natpmp() stop_natpmp()

 natpmp* start_natpmp();
@@ -1046,13 +1047,13 @@ void stop_natpmp();
 port are attempted to be forwarded on the router through NAT-PMP.

The natpmp object returned by start_natpmp() can be used to add and remove arbitrary port mappings. Mapping status is returned through the -portmap_alert and the portmap_error_alert. The object will be valid until -stop_natpmp() is called. See UPnP and NAT-PMP.

+portmap_alert and the portmap_error_alert. The object will be valid until +stop_natpmp() is called. See UPnP and NAT-PMP.

It is off by default.

-
-

entry

+
+

entry

The entry class represents one node in a bencoded hierarchy. It works as a variant type, it can be either a list, a dictionary (std::map), an integer or a string. This is its synopsis:

@@ -1115,8 +1116,8 @@ public: };

TODO: finish documentation of entry.

-
-

integer() string() list() dict() type()

+
+

integer() string() list() dict() type()

 integer_type& integer();
@@ -1131,7 +1132,7 @@ dictionary_type const& dict() const;
 

The integer(), string(), list() and dict() functions are accessors that return the respective type. If the entry object isn't of the -type you request, the accessor will throw type_error (which derives from +type you request, the accessor will throw type_error (which derives from std::runtime_error). You can ask an entry for its type through the type() function.

The print() function is there for debug purposes only.

@@ -1165,11 +1166,11 @@ if (entry* i = torrent_file.find_key("announce")) std::cout << tracker_url << "\n"; } -

To make it easier to extract information from a torrent file, the class torrent_info +

To make it easier to extract information from a torrent file, the class torrent_info exists.

-
-

operator[]

+
+

operator[]

 entry& operator[](char const* key);
@@ -1187,8 +1188,8 @@ given key, a reference to a newly inserted element at that key.

existing element at the given key. If the key is not found, it will throw libtorrent::type_error.

-
-

find_key()

+
+

find_key()

 entry* find_key(char const* key);
@@ -1202,8 +1203,11 @@ element cannot be found, they will return 0. If an element with the given
 key is found, the return a pointer to it.

-
-

torrent_info

+
+

torrent_info

+

In previous versions of libtorrent, this class was also used for creating +torrent files. This functionality has been moved to create_torrent, see +make_torrent.

The torrent_info has the following synopsis:

 class torrent_info
@@ -1216,27 +1220,25 @@ public:
         torrent_info(char const* filename);
 
         void add_tracker(std::string const& url, int tier = 0);
+        std::vector<announce_entry> const& trackers() const;
 
-        typedef std::vector<file_entry>::const_iterator file_iterator;
-        typedef std::vector<file_entry>::const_reverse_iterator
-                reverse_file_iterator;
+        file_storage const& files() const;
 
-        bool remap_files(std::vector<file_entry> const& map);
+        typedef file_storage::iterator file_iterator;
+        typedef file_storage::reverse_iterator reverse_file_iterator;
 
-        file_iterator begin_files(bool storage = false) const;
-        file_iterator end_files(bool storage = false) const;
-        reverse_file_iterator rbegin_files(bool storage = false) const;
-        reverse_file_iterator rend_files(bool storage = false) const;
+        file_iterator begin_files() const;
+        file_iterator end_files() const;
+        reverse_file_iterator rbegin_files() const;
+        reverse_file_iterator rend_files() const;
 
-        int num_files(bool storage = false) const;
-        file_entry const& file_at(int index, bool storage = false) const;
+        int num_files() const;
+        file_entry const& file_at(int index) const;
 
         std::vector<file_slice> map_block(int piece, size_type offset
-                , int size, bool storage = false) const;
+                , int size) const;
         peer_request map_file(int file_index, size_type file_offset
-                , int size, bool storage = false) const;
-
-        std::vector<announce_entry> const& trackers() const;
+                , int size) const;
 
         bool priv() const;
 
@@ -1264,8 +1266,8 @@ public:
         int metadata_size() const;
 };
 
-
-

torrent_info()

+
+

torrent_info()

 torrent_info(sha1_hash const& info_hash);
@@ -1281,62 +1283,52 @@ from the swarm.

The constructor that takes a lazy_entry will create a torrent_info object from the information found in the given torrent_file. The lazy_entry represents a tree node in an bencoded file. To load an ordinary .torrent file -into a lazy_entry, use lazy_bdecode(), see bdecode() bencode().

+into a lazy_entry, use lazy_bdecode(), see bdecode() bencode().

The version that takes a buffer pointer and a size will decode it as a .torrent file and initialize the torrent_info object for you.

The version that takes a filename will simply load the torrent file and decode it inside the constructor, for convenience. This might not be the most suitable for applications that want to be able to report detailed errors on what might go wrong.

-
-

add_tracker()

+
+

add_tracker()

 void add_tracker(std::string const& url, int tier = 0);
 

add_tracker() adds a tracker to the announce-list. The tier determines the order in -which the trackers are to be tried. For more information see trackers().

+which the trackers are to be tried. For more information see trackers().

-
-

remap_files()

+
+

files()

-bool remap_files(std::vector<file_entry> const& map);
+file_storage const& file() const;
 
-

This call will create a new mapping of the data in this torrent to other files. The -torrent_info maintains 2 views of the file storage. One that is true to the torrent -file, and one that represents what is actually saved on disk. This call will change -what the files on disk are called.

-

The each entry in the vector map is a file_entry. The only fields in this struct -that are used in this case are path, size and file_base.

-

The return value indicates if the remap was successful or not. True means success and -false means failure. The sum of all the files passed in through map has to be exactly -the same as the total_size of the torrent. If the number of bytes that are mapped do not -match, false will be returned (this is the only case this function may fail).

-

Changing this mapping for an existing torrent will not move or rename files. If some files -should be renamed, this can be done before the torrent is added.

+

The file_storage object contains the information on how to map the pieces to +files. It is separated from the torrent_info object because when creating torrents +a storage object needs to be created without having a torrent file. When renaming files +in a storage, the storage needs to make its own copy of the file_storage in order +to make its mapping differ from the one in the torrent file.

+

For more information on the file_storage object, see the separate document on how +to create torrents.

-
-

begin_files() end_files() rbegin_files() rend_files()

+
+

begin_files() end_files() rbegin_files() rend_files()

-file_iterator begin_files(bool storage = false) const;
-file_iterator end_files(bool storage = false) const;
-reverse_file_iterator rbegin_files(bool storage = false) const;
-reverse_file_iterator rend_files(bool storage = false) const;
+file_iterator begin_files() const;
+file_iterator end_files() const;
+reverse_file_iterator rbegin_files() const;
+reverse_file_iterator rend_files() const;
 

This class will need some explanation. First of all, to get a list of all files in the torrent, you can use begin_files(), end_files(), rbegin_files() and rend_files(). These will give you standard vector iterators with the type file_entry.

-

The storage parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see remap_files().

 struct file_entry
 {
@@ -1366,28 +1358,23 @@ the original string is preserved in 
-

num_files() file_at()

+
+

num_files() file_at()

-int num_files(bool storage = false) const;
-file_entry const& file_at(int index, bool storage = false) const;
+int num_files() const;
+file_entry const& file_at(int index) const;
 

If you need index-access to files you can use the num_files() and file_at() to access files using indices.

-

The storage parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see remap_files().

-
-

map_block()

+
+

map_block()

 std::vector<file_slice> map_block(int piece, size_type offset
-        , int size, bool storage = false) const;
+        , int size) const;
 

This function will map a piece index, a byte offset within that piece and @@ -1407,18 +1394,13 @@ To get the path and filename, use 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.

-

The storage parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see remap_files().

-
-

map_file()

+
+

map_file()

 peer_request map_file(int file_index, size_type file_offset
-        , int size, bool storage = false) const;
+        , int size) const;
 

This function will map a range in a specific file into a range in the torrent. @@ -1441,8 +1423,8 @@ struct peer_request + size is not allowed to be greater than the file size. file_index must refer to a valid file, i.e. it cannot be >= num_files().

-
-

url_seeds() add_url_seed()

+
+

url_seeds() add_url_seed()

 std::vector<std::string> const& url_seeds() const;
@@ -1453,15 +1435,10 @@ void add_url_seed(std::string const& url);
 vector of those urls. If you're creating a torrent file, add_url_seed()
 adds one url to the list of url-seeds. Currently, the only transport protocol
 supported for the url is http.

-

The storage parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see remap_files().

-

See HTTP seeding for more information.

+

See HTTP seeding for more information.

-
-

trackers()

+
+

trackers()

 std::vector<announce_entry> const& trackers() const;
@@ -1480,8 +1457,8 @@ struct announce_entry
 };
 
-
-

total_size() piece_length() piece_size() num_pieces()

+
+

total_size() piece_length() piece_size() num_pieces()

 size_type total_size() const;
@@ -1498,8 +1475,8 @@ the piece index as argument and gives you the exact size of that piece. It will
 be the same as piece_length() except in the case of the last piece, which may
 be smaller.

-
-

hash_for_piece() hash_for_piece_ptr() info_hash()

+
+

hash_for_piece() hash_for_piece_ptr() info_hash()

 size_type piece_size(unsigned int index) const;
@@ -1509,12 +1486,12 @@ char const* hash_for_piece_ptr(unsigned int index) const;
 

hash_for_piece() takes a piece-index and returns the 20-bytes sha1-hash for that piece and info_hash() returns the 20-bytes sha1-hash for the info-section of the -torrent file. For more information on the sha1_hash, see the big_number class. +torrent file. For more information on the sha1_hash, see the big_number class. hash_for_piece_ptr() returns a pointer to the 20 byte sha1 digest for the piece. Note that the string is not null-terminated.

-
-

name() comment() creation_date() creator()

+
+

name() comment() creation_date() creator()

 std::string const& name() const;
@@ -1524,15 +1501,15 @@ boost::optional<boost::posix_time::ptime> creation_date() const;
 

name() returns the name of the torrent.

comment() returns the comment associated with the torrent. If there's no comment, -it will return an empty string. creation_date() returns a boost::posix_time::ptime +it will return an empty string. creation_date() returns a boost::posix_time::ptime object, representing the time when this torrent file was created. If there's no time stamp in the torrent file, this will return a date of January 1:st 1970.

Both the name and the comment is UTF-8 encoded strings.

creator() returns the creator string in the torrent. If there is no creator string it will return an empty string.

-
-

priv()

+
+

priv()

 bool priv() const;
@@ -1541,8 +1518,8 @@ bool priv() const;
 

priv() returns true if this torrent is private. i.e., it should not be distributed on the trackerless network (the kademlia DHT).

-
-

nodes()

+
+

nodes()

 std::vector<std::pair<std::string, int> > const& nodes() const;
@@ -1551,8 +1528,8 @@ std::vector<std::pair<std::string, int> > const& nodes() const;
 

If this torrent contains any DHT nodes, they are put in this vector in their original form (host name and port number).

-
-

add_node()

+
+

add_node()

 void add_node(std::pair<std::string, int> const& node);
@@ -1561,8 +1538,8 @@ void add_node(std::pair<std::string, int> const& node);
 

This is used when creating torrent. Use this to add a known DHT node. It may be used, by the client, to bootstrap into the DHT network.

-
-

metadata() metadata_size()

+
+

metadata() metadata_size()

 boost::shared_array<char> metadata() const;
@@ -1573,8 +1550,8 @@ int metadata_size() const;
 of the metadata is returned by metadata_size().

-
-

torrent_handle

+
+

torrent_handle

You will usually have to store your torrent handles somewhere, since it's the object through which you retrieve information about the torrent and aborts the torrent. Its declaration looks like this:

@@ -1659,14 +1636,14 @@ valid handle. If you try to perform any operation on an uninitialized handle, it will throw invalid_handle.

Warning

-

All operations on a torrent_handle may throw invalid_handle +

All operations on a torrent_handle may throw invalid_handle exception, in case the handle is no longer refering to a torrent. There is one exception is_valid() will never throw. Since the torrents are processed by a background thread, there is no guarantee that a handle will remain valid between two calls.

-
-

piece_priority() prioritize_pieces() piece_priorities() prioritize_files()

+
+

piece_priority() prioritize_pieces() piece_priorities() prioritize_files()

 void piece_priority(int index, int priority) const;
@@ -1710,8 +1687,8 @@ torrent. Each element is the current priority of that piece.

files in the torrent. Each entry is the priority of that file. The function sets the priorities of all the pieces in the torrent based on the vector.

-
-

file_progress()

+
+

file_progress()

 void file_progress(std::vector<float>& fp);
@@ -1719,21 +1696,21 @@ void file_progress(std::vector<float>& fp);
 

This function fills in the supplied vector with the progress (a value in the range [0, 1]) describing the download progress of each file in this torrent. -The progress values are ordered the same as the files in the torrent_info. +The progress values are ordered the same as the files in the torrent_info. This operation is not very cheap.

-
-

save_path()

+
+

save_path()

 boost::filesystem::path save_path() const;
 
-

save_path() returns the path that was given to add_torrent() when this torrent +

save_path() returns the path that was given to add_torrent() when this torrent was started.

-
-

move_storage()

+
+

move_storage()

 void move_storage(boost::filesystem::path const& save_path) const;
@@ -1745,8 +1722,8 @@ the same drive as the original save path. Since disk IO is performed in a separa
 thread, this operation is also asynchronous. Once the operation completes, the
 storage_moved_alert is generated, with the new path as the message.

-
-

force_reannounce()

+
+

force_reannounce()

 void force_reannounce() const;
@@ -1757,8 +1734,8 @@ void force_reannounce(boost::posix_time::time_duration) const;
 peers. The second overload of force_reannounce that takes a time_duration as
 argument will schedule a reannounce in that amount of time from now.

-
-

scrape_tracker()

+
+

scrape_tracker()

 void scrape_tracker() const;
@@ -1768,11 +1745,11 @@ void scrape_tracker() const;
 tracker for statistics such as total number of incomplete peers, complete peers, number of
 downloads etc.

This request will specifically update the num_complete and num_incomplete fields in -the torrent_status struct once it completes. When it completes, it will generate a -scrape_reply_alert. If it fails, it will generate a scrape_failed_alert.

+the torrent_status struct once it completes. When it completes, it will generate a +scrape_reply_alert. If it fails, it will generate a scrape_failed_alert.

-
-

connect_peer()

+
+

connect_peer()

 void connect_peer(asio::ip::tcp::endpoint const& adr, int source = 0) const;
@@ -1782,12 +1759,12 @@ void connect_peer(asio::ip::tcp::endpoint const& adr, int source = 0) const;
 torrent. If the peer does not respond, or is not a member of this torrent, it will simply
 be disconnected. No harm can be done by using this other than an unnecessary connection
 attempt is made. If the torrent is uninitialized or in queued or checking mode, this
-will throw invalid_handle. The second (optional) argument will be bitwised ORed into
-the source mask of this peer. Typically this is one of the source flags in peer_info.
+will throw invalid_handle. The second (optional) argument will be bitwised ORed into
+the source mask of this peer. Typically this is one of the source flags in peer_info.
 i.e. tracker, pex, dht etc.

-
-

name()

+
+

name()

 std::string name() const;
@@ -1797,8 +1774,8 @@ std::string name() const;
 case the torrent was started without metadata, and hasn't completely received it yet,
 it returns the name given to it when added to the session. See session::add_torrent.

-
-

set_ratio()

+
+

set_ratio()

 void set_ratio(float ratio) const;
@@ -1812,8 +1789,8 @@ attempt to upload in return for each download. e.g. if set to 2, the client will
 2 bytes for every byte received. The default setting for this is 0, which will make it work
 as a standard client.

-
-

set_upload_limit() set_download_limit() upload_limit() download_limit()

+
+

set_upload_limit() set_download_limit() upload_limit() download_limit()

 void set_upload_limit(int limit) const;
@@ -1831,8 +1808,8 @@ limit.

upload_limit and download_limit will return the current limit setting, for upload and download, respectively.

-
-

set_sequential_download()

+
+

set_sequential_download()

 void set_sequential_download(bool sd);
@@ -1843,8 +1820,8 @@ instead of rarest first.

Enabling sequential download will affect the piece distribution negatively in the swarm. It should be used sparingly.

-
-

set_peer_upload_limit() set_peer_download_limit()

+
+

set_peer_upload_limit() set_peer_download_limit()

 void set_peer_upload_limit(asio::ip::tcp::endpoint ip, int limit) const;
@@ -1854,8 +1831,8 @@ void set_peer_download_limit(asio::ip::tcp::endpoint ip, int limit) const;
 

Works like set_upload_limit and set_download_limit respectively, but controls individual peer instead of the whole torrent.

-
-

pause() resume() is_paused()

+
+

pause() resume() is_paused()

 void pause() const;
@@ -1867,10 +1844,10 @@ bool is_paused() const;
 When a torrent is paused, it will however remember all share ratios to all peers and remember
 all potential (not connected) peers. You can use is_paused() to determine if a torrent
 is currently paused. Torrents may be paused automatically if there is a file error (e.g. disk full)
-or something similar. See file_error_alert.

+or something similar. See file_error_alert.

-
-

resolve_countries()

+
+

resolve_countries()

 void resolve_countries(bool r);
@@ -1878,12 +1855,12 @@ bool resolve_countries() const;
 

Sets or gets the flag that derermines if countries should be resolved for the peers of this -torrent. It defaults to false. If it is set to true, the peer_info structure for the peers -in this torrent will have their country member set. See peer_info for more information +torrent. It defaults to false. If it is set to true, the peer_info structure for the peers +in this torrent will have their country member set. See peer_info for more information on how to interpret this field.

-
-

is_seed()

+
+

is_seed()

 bool is_seed() const;
@@ -1891,8 +1868,8 @@ bool is_seed() const;
 

Returns true if the torrent is in seed mode (i.e. if it has finished downloading).

-
-

is_auto_managed() auto_managed()

+
+

is_auto_managed() auto_managed()

 bool is_auto_managed() const;
@@ -1901,10 +1878,10 @@ void auto_managed(bool m) const;
 

is_auto_managed() returns true if this torrent is currently auto managed. auto_managed() changes whether the torrent is auto managed or not. For more info, -see queuing.

+see queuing.

-
-

has_metadata()

+
+

has_metadata()

 bool has_metadata() const;
@@ -1913,10 +1890,10 @@ bool has_metadata() const;
 

Returns true if this torrent has metadata (either it was started from a .torrent file or the metadata has been downloaded). The only scenario where this can return false is when the torrent was started torrent-less (i.e. with just an info-hash and tracker ip). Note that if the torrent -doesn't have metadata, the member get_torrent_info() will throw.

+doesn't have metadata, the member get_torrent_info() will throw.

-
-

set_tracker_login()

+
+

set_tracker_login()

 void set_tracker_login(std::string const& username
@@ -1926,8 +1903,8 @@ void set_tracker_login(std::string const& username
 

set_tracker_login() sets a username and password that will be sent along in the HTTP-request of the tracker announce. Set this if the tracker requires authorization.

-
-

trackers() replace_trackers()

+
+

trackers() replace_trackers()

 std::vector<announce_entry> const& trackers() const;
@@ -1941,10 +1918,10 @@ which this tracker is tried. If you want libtorrent to use another list of
 trackers for this torrent, you can use replace_trackers() which takes
 a list of the same form as the one returned from trackers() and will
 replace it. If you want an immediate effect, you have to call
-force_reannounce().

+force_reannounce().

-
-

add_url_seed() remove_url_seed() url_seeds()

+
+

add_url_seed() remove_url_seed() url_seeds()

 void add_url_seed(std::string const& url);
@@ -1959,22 +1936,22 @@ paused, queued, checking or seeding. url_seeds() return a set of the url seeds
 currently in this torrent. Note that urls that fails may be removed
 automatically from the list.

-

See HTTP seeding for more information.

+

See HTTP seeding for more information.

-
-

use_interface()

+
+

use_interface()

 void use_interface(char const* net_interface) const;
 

use_interface() sets the network interface this torrent will use when it opens outgoing -connections. By default, it uses the same interface as the session uses to listen on. The +connections. By default, it uses the same interface as the session uses to listen on. The parameter must be a string containing an ip-address (either an IPv4 or IPv6 address). If the string does not conform to this format and exception is thrown.

-
-

info_hash()

+
+

info_hash()

 sha1_hash info_hash() const;
@@ -1982,8 +1959,8 @@ sha1_hash info_hash() const;
 

info_hash() returns the info-hash for the torrent.

-
-

set_max_uploads() set_max_connections()

+
+

set_max_uploads() set_max_connections()

 void set_max_uploads(int max_uploads) const;
@@ -1997,17 +1974,17 @@ connections are used up, incoming connections may be refused or poor connections
 This must be at least 2. The default is unlimited number of connections. If -1 is given to the
 function, it means unlimited.

-
-

save_resume_data()

+
+

save_resume_data()

 void save_resume_data() const;
 
-

save_resume_data() generates fast-resume data and returns it as an entry. This entry -is suitable for being bencoded. For more information about how fast-resume works, see fast resume.

+

save_resume_data() generates fast-resume data and returns it as an entry. This entry +is suitable for being bencoded. For more information about how fast-resume works, see fast resume.

This operation is asynchronous, save_resume_data will return immediately. The resume data -is delivered when it's done through an save_resume_data_alert.

+is delivered when it's done through an save_resume_data_alert.

The fast resume data will be empty in the following cases:

    @@ -2015,19 +1992,19 @@ is delivered when it's done through an metadata from peers extension) +(see libtorrent's metadata from peers extension)

Note that by the time you receive the fast resume data, it may already be invalid if the torrent is still downloading! The recommended practice is to first pause the torrent, then generate the -fast resume data, and then close it down. Make sure to not remove_torrent() before you receive -the save_resume_data_alert though. Only pause the torrent before you save the resume data +fast resume data, and then close it down. Make sure to not remove_torrent() before you receive +the save_resume_data_alert though. Only pause the torrent before you save the resume data if you will remove the torrent afterwards. There's no need to pause when saving intermittent resume data.

In full allocation mode the reume data is never invalidated by subsequent writes to the files, since pieces won't move around. This means that you don't need to pause before writing resume data in full or sparse mode. If you don't, however, any data written to -disk after you saved resume data and before the session closed is lost.

+disk after you saved resume data and before the session closed is lost.

It also means that if the resume data is out dated, libtorrent will not re-check the files, but assume that it is fairly recent. The assumption is that it's better to loose a little bit than to re-check the entire file.

@@ -2072,19 +2049,19 @@ while (num_resume_data > 0) }
-
-

status()

+
+

status()

 torrent_status status() const;
 

status() will return a structure with information about the status of this -torrent. If the torrent_handle is invalid, it will throw invalid_handle exception. -See torrent_status.

+torrent. If the torrent_handle is invalid, it will throw invalid_handle exception. +See torrent_status.

-
-

get_download_queue()

+
+

get_download_queue()

 void get_download_queue(std::vector<partial_piece_info>& queue) const;
@@ -2136,8 +2113,8 @@ a state (state) which
 is 0 or 1, but at the end of the torrent blocks may be requested by more peers in parallel to
 speed things up.

-
-

get_peer_info()

+
+

get_peer_info()

 void get_peer_info(std::vector<peer_info>&) const;
@@ -2145,25 +2122,25 @@ void get_peer_info(std::vector<peer_info>&) const;
 

get_peer_info() takes a reference to a vector that will be cleared and filled with one entry for each peer connected to this torrent, given the handle is valid. If the -torrent_handle is invalid, it will throw invalid_handle exception. Each entry in -the vector contains information about that particular peer. See peer_info.

+torrent_handle is invalid, it will throw invalid_handle exception. Each entry in +the vector contains information about that particular peer. See peer_info.

-
-

get_torrent_info()

+
+

get_torrent_info()

 torrent_info const& get_torrent_info() const;
 
-

Returns a const reference to the torrent_info object associated with this torrent. -This reference is valid as long as the torrent_handle is valid, no longer. If the -torrent_handle is invalid or if it doesn't have any metadata, invalid_handle +

Returns a const reference to the torrent_info object associated with this torrent. +This reference is valid as long as the torrent_handle is valid, no longer. If the +torrent_handle is invalid or if it doesn't have any metadata, invalid_handle exception will be thrown. The torrent may be in a state without metadata only if it was started without a .torrent file, i.e. by using the libtorrent extension of just supplying a tracker and info-hash.

-
-

is_valid()

+
+

is_valid()

 bool is_valid() const;
@@ -2173,12 +2150,12 @@ bool is_valid() const;
 or if the torrent it refers to has been aborted. Note that a handle may become invalid after
 it has been added to the session. Usually this is because the storage for the torrent is
 somehow invalid or if the filenames are not allowed (and hence cannot be opened/created) on
-your filesystem. If such an error occurs, a file_error_alert is generated and all handles
+your filesystem. If such an error occurs, a file_error_alert is generated and all handles
 that refers to that torrent will become invalid.

-
-

torrent_status

+
+

torrent_status

It contains the following fields:

 struct torrent_status
@@ -2354,7 +2331,7 @@ be slightly smaller than the other rates, but if projected over a long time
 

num_peers is the number of peers this torrent currently is connected to. Peer connections that are in the half-open state (is attempting to connect) or are queued for later connection attempt do not count. Although they are -visible in the peer list when you call get_peer_info().

+visible in the peer list when you call get_peer_info().

num_complete and num_incomplete are set to -1 if the tracker did not send any scrape data in its announce reply. This data is optional and may not be available from all trackers. If these are not -1, they are the total @@ -2392,7 +2369,7 @@ copies is set to -1.

block_size is the size of a block, in bytes. A block is a sub piece, it is the number of bytes that each piece request asks for and the number of bytes that each bit in the partial_piece_info's bitset represents -(see get_download_queue()). This is typically 16 kB, but it may be +(see get_download_queue()). This is typically 16 kB, but it may be larger if the pieces are larger.

num_uploads is the number of unchoked peers in this torrent.

num_connections is the number of peer connections this torrent has, including @@ -2402,7 +2379,7 @@ always <= num_peersconnections_limit is the set limit of number of connections for this torrent.

storage_mode is one of storage_mode_allocate, storage_mode_sparse or storage_mode_compact. Identifies which storage mode this torrent is being saved -with. See Storage allocation.

+with. See Storage allocation.

up_bandwidth_queue and down_bandwidth_queue are the number of peers in this torrent that are waiting for more bandwidth quota from the torrent rate limiter. This can determine if the rate you get from this torrent is bound by the torrents @@ -2419,12 +2396,12 @@ seconds it has been active while being a seed.

seed_rank is a rank of how important it is to seed the torrent, it is used to determine which torrents to seed and which to queue. It is based on the peer -to seed ratio from the tracker scrape. For more information, see queuing.

+to seed ratio from the tracker scrape. For more information, see queuing.

last_scrape is the number of seconds since this torrent acquired scrape data. If it has never done that, this value is -1.

-
-

peer_info

+
+

peer_info

It contains the following fields:

 struct peer_info
@@ -2549,7 +2526,7 @@ any combination of the enums above. The following table describes each flag:

support_extensions means that this peer supports the -extension protocol. +extension protocol. local_connection The connection was initiated by us, the peer has a @@ -2665,7 +2642,7 @@ limits.

The ip field is the IP-address to this peer. The type is an asio endpoint. For -more info, see the asio documentation.

+more info, see the asio documentation.

up_speed and down_speed contains the current upload and download speed we have to and from this peer (including any protocol messages). The transfer rates of payload data only are found in payload_up_speed and payload_down_speed. @@ -2693,12 +2670,12 @@ and used for the peer's send buffer, respectively.

allocated and used as receive buffer, respectively.

num_hashfails is the number of pieces this peer has participated in sending us that turned out to fail the hash check.

-

country is the two letter ISO 3166 country code for the country the peer +

country is the two letter ISO 3166 country code for the country the peer is connected from. If the country hasn't been resolved yet, both chars are set to 0. If the resolution failed for some reason, the field is set to "--". If the resolution service returns an invalid country code, it is set to "!!". The countries.nerd.dk service is used to look up countries. This field will -remain set to 0 unless the torrent is set to resolve countries, see resolve_countries().

+remain set to 0 unless the torrent is set to resolve countries, see resolve_countries().

inet_as_name is the name of the AS this peer is located in. This might be an empty string if there is no name in the geo ip database.

inet_as is the AS number the peer is located in.

@@ -2743,8 +2720,8 @@ estimated by timing the the tcp c rates seen on this connection. They are given in bytes per second. This number is reset to 0 on reconnect.

-
-

session_settings

+
+

session_settings

You have some control over tracker requests through the session_settings object. You create it and fill it with your settings and then use session::set_settings() to apply them. You have control over proxy and authorization settings and also the user-agent @@ -2934,9 +2911,9 @@ message or a time out.

are kept in memory after the torrent becomes a seed or not. If it is set to true the hashes are freed once the torrent is a seed (they're not needed anymore since the torrent won't download anything more). If it's set -to false they are not freed. If they are freed, the torrent_info returned +to false they are not freed. If they are freed, the torrent_info returned by get_torrent_info() will return an object that may be incomplete, that -cannot be passed back to add_torrent() for instance.

+cannot be passed back to add_torrent() for instance.

upnp_ignore_nonrouters indicates whether or not the UPnP implementation should ignore any broadcast response from a device whose address is not the configured router for this machine. i.e. it's a way to not talk to other @@ -2964,7 +2941,7 @@ to peers if a previous socket to that peer and port is in peer_tos determines the TOS byte set in the IP header of every packet sent to peers (including web seeds). The default value for this is 0x0 (no marking). One potentially useful TOS mark is 0x20, this represents -the QBone scavenger service. For more details, see QBSS.

+the QBone scavenger service. For more details, see QBSS.

active_downloads and active_seeds controls how many active seeding and downloading torrents the queuing mechanism allows. Seeding torrents are counted against the downloads limit but downloading torrenst are not @@ -2972,12 +2949,12 @@ counted against the seed limit.

auto_manage_interval is the number of seconds between the torrent queue is updated, and rotated.

share_ratio_limit is the upload / download ratio limit for considering a -seeding torrent have met the seed limit criteria. See queuing.

+seeding torrent have met the seed limit criteria. See queuing.

seed_time_ratio_limit is the seeding time / downloading time ratio limit -for considering a seeding torrent to have met the seed limit criteria. See queuing.

+for considering a seeding torrent to have met the seed limit criteria. See queuing.

seed_time_limit is the limit on the time a torrent has been an active seed (specified in seconds) before it is considered having met the seed limit criteria. -See queuing.

+See queuing.

close_redundant_connections specifies whether libtorrent should close connections where both ends have no utility in keeping the connection open. For instance if both ends have completed their downloads, there's no point @@ -2992,8 +2969,8 @@ automatic scrape (regardless of torrent). In case there are a large number of paused auto managed torrents, this puts a limit on how often a scrape request is sent.

-
-

pe_settings

+
+

pe_settings

The pe_settings structure is used to control the settings related to peer protocol encryption:

@@ -3049,8 +3026,8 @@ remains unchanged.
 
 

prefer_rc4 can be set to true if you want to prefer the RC4 encrypted stream.

-
-

proxy_settings

+
+

proxy_settings

The proxy_settings structs contains the information needed to direct certain traffic to a proxy.

@@ -3088,16 +3065,16 @@ options are available:

  • none - This is the default, no proxy server is used, all other fields are ignored.
  • -
  • socks4 - The server is assumed to be a SOCKS4 server that +
  • socks4 - The server is assumed to be a SOCKS4 server that requires a username.
  • -
  • socks5 - The server is assumed to be a SOCKS5 server (RFC 1928) that +
  • socks5 - The server is assumed to be a SOCKS5 server (RFC 1928) that does not require any authentication. The username and password are ignored.
  • socks5_pw - The server is assumed to be a SOCKS5 server that supports -plain text username and password authentication (RFC 1929). The username +plain text username and password authentication (RFC 1929). The username and password specified may be sent to the proxy if it requires.
  • http - The server is assumed to be an HTTP proxy. If the transport used for the connection is non-HTTP, the server is assumed to support the -CONNECT method. i.e. for web seeds and HTTP trackers, a plain proxy will +CONNECT method. i.e. for web seeds and HTTP trackers, a plain proxy will suffice. The proxy is assumed to not require authorization. The username and password will not be used.
  • http_pw - The server is assumed to be an HTTP proxy that requires @@ -3105,8 +3082,8 @@ user authorization. The username and password will be sent to the proxy.
-
-

ip_filter

+
+

ip_filter

The ip_filter class is a set of rules that uniquely categorizes all ip addresses as allowed or disallowed. The default constructor creates a single rule that allows all addresses (0.0.0.0 - 255.255.255.255 for @@ -3138,8 +3115,8 @@ public: }; -

-

ip_filter()

+
+

ip_filter()

 ip_filter()
@@ -3149,8 +3126,8 @@ ip_filter()
 

postcondition: access(x) == 0 for every x

-
-

add_rule()

+
+

add_rule()

 void add_rule(address first, address last, int flags);
@@ -3167,8 +3144,8 @@ means disallowed.

This means that in a case of overlapping ranges, the last one applied takes precedence.

-
-

access()

+
+

access()

 int access(address const& addr) const;
@@ -3179,8 +3156,8 @@ can currently be 0 or ip_filter::
 is O(log n), where n is the minimum number of non-overlapping ranges to describe
 the current filter.

-
-

export_filter()

+
+

export_filter()

 boost::tuple<std::vector<ip_range<address_v4> >
@@ -3195,8 +3172,8 @@ entry in the returned vector is a range with the access control specified in its
 and one for IPv6 addresses.

-
-

big_number

+
+

big_number

Both the peer_id and sha1_hash types are typedefs of the class big_number. It represents 20 bytes of data. Its synopsis follows:

@@ -3216,8 +3193,8 @@ public:
 

The iterators gives you access to individual bytes.

-
-

hasher

+
+

hasher

This class creates sha1-hashes. Its declaration looks like this:

 class hasher
@@ -3243,8 +3220,8 @@ call reset() to reini
 

The sha1-algorithm used was implemented by Steve Reid and released as public domain. For more info, see src/sha1.cpp.

-
-

fingerprint

+
+

fingerprint

The fingerprint class represents information about a client and its version. It is used to encode this information into the client's peer id.

This is the class declaration:

@@ -3301,13 +3278,13 @@ sure not to clash with anybody else. Here are some taken id's:

-

There's currently an informal directory of client id's here.

+

There's currently an informal directory of client id's here.

The major, minor, revision and tag parameters are used to identify the version of your client. All these numbers must be within the range [0, 9].

to_string() will generate the actual string put in the peer-id, and return it.

-
-

UPnP and NAT-PMP

+
+

UPnP and NAT-PMP

The upnp and natpmp classes contains the state for all UPnP and NAT-PMP mappings, by default 1 or two mappings are made by libtorrent, one for the listen port and one for the DHT port (UDP).

@@ -3340,8 +3317,8 @@ public:

discover_device(), close() and rebind() are for internal uses and should not be called directly by clients.

-
-

add_mapping

+
+

add_mapping

 int add_mapping(protocol_type p, int external_port, int local_port);
@@ -3352,28 +3329,28 @@ int add_mapping(protocol_type p, int external_port, int local_port);
 natpmp::udp for the NAT-PMP class.

external_port is the port on the external address that will be mapped. This is a hint, you are not guaranteed that this port will be available, and it may -end up being something else. In the portmap_alert notification, the actual +end up being something else. In the portmap_alert notification, the actual external port is reported.

local_port is the port in the local machine that the mapping should forward to.

The return value is an index that identifies this port mapping. This is used -to refer to mappings that fails or succeeds in the portmap_error_alert and -portmap_alert respectively. If The mapping fails immediately, the return value +to refer to mappings that fails or succeeds in the portmap_error_alert and +portmap_alert respectively. If The mapping fails immediately, the return value is -1, which means failure. There will not be any error alert notification for mappings that fail with a -1 return value.

-
-

delete_mapping

+
+

delete_mapping

 void delete_mapping(int mapping_index);
 

This function removes a port mapping. mapping_index is the index that refers -to the mapping you want to remove, which was returned from add_mapping.

+to the mapping you want to remove, which was returned from add_mapping.

-
-

router_model()

+
+

router_model()

 std::string router_model();
@@ -3383,10 +3360,10 @@ std::string router_model();
 the router, it can be queried through this function.

-
-

free functions

-
-

identify_client()

+
+

free functions

+
+

identify_client()

 std::string identify_client(peer_id const& id);
@@ -3396,8 +3373,8 @@ std::string identify_client(peer_id const& id);
 to extract a string describing a client version from its peer-id. It will recognize most clients
 that have this kind of identification in the peer-id.

-
-

client_fingerprint()

+
+

client_fingerprint()

 boost::optional<fingerprint> client_fingerprint(peer_id const& p);
@@ -3408,23 +3385,23 @@ to automate the identification of clients. It will not be able to identify peers
 standard encodings. Only Azureus style, Shadow's style and Mainline style. This function is
 declared in the header <libtorrent/identify_client.hpp>.

-
-

bdecode() bencode()

+
+

bdecode() bencode()

 template<class InIt> entry bdecode(InIt start, InIt end);
 template<class OutIt> void bencode(OutIt out, const entry& e);
 
-

These functions will encode data to bencoded or decode bencoded data.

-

The entry class is the internal representation of the bencoded data -and it can be used to retrieve information, an entry can also be build by +

These functions will encode data to bencoded or decode bencoded data.

+

The entry class is the internal representation of the bencoded data +and it can be used to retrieve information, an entry can also be build by the program and given to bencode() to encode it into the OutIt iterator.

The OutIt and InIt are iterators -(InputIterator and OutputIterator respectively). They -are templates and are usually instantiated as ostream_iterator, -back_insert_iterator or istream_iterator. These +(InputIterator and OutputIterator respectively). They +are templates and are usually instantiated as ostream_iterator, +back_insert_iterator or istream_iterator. These functions will assume that the iterator refers to a character (char). So, if you want to encode entry e into a buffer in memory, you can do it like this:

@@ -3444,13 +3421,13 @@ const char* buf; // ... entry e = bdecode(buf, buf + data_size); -

Now we just need to know how to retrieve information from the entry.

+

Now we just need to know how to retrieve information from the entry.

If bdecode() encounters invalid encoded data in the range given to it -it will throw invalid_encoding.

+it will throw invalid_encoding.

-
-

alerts

+
+

alerts

The pop_alert() function on session is the interface for retrieving alerts, warnings, messages and errors from libtorrent. If there hasn't occurred any errors (matching your severity level) pop_alert() will @@ -3507,7 +3484,7 @@ any events at all.

pop_alert() from time to time. If you don't do that, the alert queue will just grow.

When you get an alert, you can use typeid() or dynamic_cast<> to get more detailed information on exactly which type it is. i.e. what kind of error it is. You can also use a -dispatcher mechanism that's available in libtorrent.

+dispatcher mechanism that's available in libtorrent.

All alert types are defined in the <libtorrent/alert_types.hpp> header file.

The alert class is the base class that specific messages are derived from. This is its synopsis:

@@ -3541,8 +3518,8 @@ struct torrent_alert: alert };

The specific alerts, that all derives from alert, are:

-
-

external_ip_alert

+
+

external_ip_alert

Whenever libtorrent learns about the machines external IP, this alert is generated. The external IP address can be acquired from the tracker (if it supports that) or from peers that supports the extension protocol. @@ -3557,10 +3534,10 @@ struct external_ip_alert: alert };

-
-

listen_failed_alert

+
+

listen_failed_alert

This alert is generated when none of the ports, given in the port range, to -session can be opened for listening. This alert is generated as severity +session can be opened for listening. This alert is generated as severity level fatal.

 struct listen_failed_alert: alert
@@ -3570,8 +3547,8 @@ struct listen_failed_alert: alert
 };
 
-
-

portmap_error_alert

+
+

portmap_error_alert

This alert is generated when a NAT router was successfully found but some part of the port mapping request failed. It contains a text message that may help the user figure out what is wrong. This alert is not generated in @@ -3581,7 +3558,7 @@ mappings.

The alert is generated as severity warning, since it should be displayed to the user somehow, and could mean reduced preformance.

mapping refers to the mapping index of the port map that failed, i.e. -the index returned from add_mapping.

+the index returned from add_mapping.

type is 0 for NAT-PMP and 1 for UPnP.

 struct portmap_error_alert: alert
@@ -3593,15 +3570,15 @@ struct portmap_error_alert: alert
 };
 
-
-

portmap_alert

+
+

portmap_alert

This alert is generated when a NAT router was successfully found and a port was successfully mapped on it. On a NAT:ed network with a NAT-PMP capable router, this is typically generated once when mapping the TCP port and, if DHT is enabled, when the UDP port is mapped. This is merely an informational alert, and is generated at severity level info.

mapping refers to the mapping index of the port map that failed, i.e. -the index returned from add_mapping.

+the index returned from add_mapping.

external_port is the external port allocated for the mapping.

type is 0 for NAT-PMP and 1 for UPnP.

@@ -3615,8 +3592,8 @@ struct portmap_alert: alert
 };
 
-
-

file_error_alert

+
+

file_error_alert

If the storage fails to read or write files that it needs access to, this alert is generated and the torrent is paused. It is generated as severity level fatal.

@@ -3630,8 +3607,8 @@ struct file_error_alert: torrent_alert
 };
 
-
-

tracker_announce_alert

+
+

tracker_announce_alert

This alert is generated each time a tracker announce is sent (or attempted to be sent). It is generated at severity level info.

@@ -3645,8 +3622,8 @@ struct tracker_announce_alert: torrent_alert
 };
 
-
-

tracker_alert

+
+

tracker_alert

This is a base class for all alerts related to trackers.

 struct tracker_alert: torrent_alert
@@ -3660,8 +3637,8 @@ struct tracker_alert: torrent_alert
 };
 
-
-

tracker_error_alert

+
+

tracker_error_alert

This alert is generated on tracker time outs, premature disconnects, invalid response or a HTTP response other than "200 OK". From the alert you can get the handle to the torrent the tracker belongs to. This alert is generated as severity level warning.

@@ -3681,8 +3658,8 @@ struct tracker_error_alert: tracker_alert };
-
-

tracker_reply_alert

+
+

tracker_reply_alert

This alert is only for informational purpose. It is generated when a tracker announce succeeds. It is generated regardless what kind of tracker was used, be it UDP, HTTP or the DHT. It is generated with severity level info.

@@ -3702,8 +3679,8 @@ struct tracker_reply_alert: tracker_alert

The num_peers tells how many peers were returned from the tracker. This is not necessarily all new peers, some of them may already be connected.

-
-

tracker_warning_alert

+
+

tracker_warning_alert

This alert is triggered if the tracker reply contains a warning field. Usually this means that the tracker announce was successful, but the tracker has a message to the client. The message string in the alert will contain the warning message from @@ -3719,8 +3696,8 @@ struct tracker_warning_alert: tracker_alert };

-
-

scrape_reply_alert

+
+

scrape_reply_alert

 struct scrape_reply_alert: tracker_alert
 {
@@ -3740,8 +3717,8 @@ struct scrape_reply_alert: tracker_alert
 and complete is the data returned in the scrape response. These numbers
 may be -1 if the reponse was malformed.

-
-

scrape_failed_alert

+
+

scrape_failed_alert

 struct scrape_failed_alert: tracker_alert
 {
@@ -3756,8 +3733,8 @@ struct scrape_failed_alert: tracker_alert
 to the tracker timing out, refusing connection or returning an http response
 code indicating an error.

-
-

url_seed_alert

+
+

url_seed_alert

This alert is generated when a HTTP seed name lookup fails. This alert is generated as severity level warning.

It contains url to the HTTP seed that failed along with an error message.

@@ -3772,8 +3749,8 @@ struct url_seed_alert: torrent_alert };
-
-

hash_failed_alert

+
+

hash_failed_alert

This alert is generated when a finished piece fails its hash check. You can get the handle to the torrent which got the failed piece and the index of the piece itself from the alert. This alert is generated as severity level info.

@@ -3791,10 +3768,10 @@ struct hash_failed_alert: torrent_alert };
-
-

peer_ban_alert

+
+

peer_ban_alert

This alert is generated when a peer is banned because it has sent too many corrupt pieces -to us. It is generated at severity level info. The handle member is a torrent_handle +to us. It is generated at severity level info. The handle member is a torrent_handle to the torrent that this peer was a member of.

 struct peer_ban_alert: torrent_alert
@@ -3810,8 +3787,8 @@ struct peer_ban_alert: torrent_alert
 };
 
-
-

peer_error_alert

+
+

peer_error_alert

This alert is generated when a peer sends invalid data over the peer-peer protocol. The peer will be disconnected, but you get its ip address from the alert, to identify it. This alert is generated as severity level debug.

@@ -3829,8 +3806,8 @@ struct peer_error_alert: alert };
-
-

invalid_request_alert

+
+

invalid_request_alert

This is a debug alert that is generated by an incoming invalid piece request. The handle is a handle to the torrent the peer is a member of. ìp is the address of the peer and the request is the actual incoming request from the peer. The alert is generated as severity level @@ -3865,8 +3842,8 @@ struct peer_request the index of the piece it want data from, start is the offset within the piece where the data should be read, and length is the amount of data it wants.

-
-

torrent_finished_alert

+
+

torrent_finished_alert

This alert is generated when a torrent switches from being a downloader to a seed. It will only be generated once per torrent. It contains a torrent_handle to the torrent in question. This alert is generated as severity level info.

@@ -3881,8 +3858,8 @@ struct torrent_finished_alert: torrent_alert };
-
-

metadata_failed_alert

+
+

metadata_failed_alert

This alert is generated when the metadata has been completely received and the info-hash failed to match it. i.e. the metadata that was received was corrupt. libtorrent will automatically retry to fetch it in this case. This is only relevant when running a @@ -3899,8 +3876,8 @@ struct metadata_failed_alert: torrent_alert };

-
-

metadata_received_alert

+
+

metadata_received_alert

This alert is generated when the metadata has been completely received and the torrent can start downloading. It is not generated on torrents that are started with metadata, but only those that needs to download it from peers (when utilizing the libtorrent extension). @@ -3916,8 +3893,8 @@ struct metadata_received_alert: torrent_alert };

-
-

fastresume_rejected_alert

+
+

fastresume_rejected_alert

This alert is generated when a fastresume file has been passed to add_torrent but the files on disk did not match the fastresume file. The string explains the reason why the resume file was rejected. It is generated at severity level warning.

@@ -3931,8 +3908,8 @@ struct fastresume_rejected_alert: torrent_alert };
-
-

peer_blocked_alert

+
+

peer_blocked_alert

This alert is generated when a peer is blocked by the IP filter. It has the severity leve info. The ip member is the address that was blocked.

@@ -3947,8 +3924,8 @@ struct peer_blocked_alert: alert
 };
 
-
-

storage_moved_alert

+
+

storage_moved_alert

The storage_moved_alert is generated when all the disk IO has completed and the files have been moved, as an effect of a call to torrent_handle::move_storage. This is useful to synchronize with the actual disk.

@@ -3960,8 +3937,8 @@ struct storage_moved_alert: torrent_alert };
-
-

torrent_paused_alert

+
+

torrent_paused_alert

This alert is generated as a response to a torrent_handle::pause request. It is generated once all disk IO is complete and the files in the torrent have been closed. This is useful for synchronizing with the disk.

@@ -3973,8 +3950,8 @@ struct torrent_paused_alert: torrent_alert };
-
-

save_resume_data_alert

+
+

save_resume_data_alert

This alert is generated as a response to a torrent_handle::save_resume_data request. It is generated once the disk IO thread is done writing the state for this torrent. The resume_data member points to the resume data or is 0 on errors.

@@ -3989,8 +3966,8 @@ struct save_resume_data_alert: torrent_alert };
-
-

dispatcher

+
+

dispatcher

The handle_alert class is defined in <libtorrent/alert.hpp>.

Examples usage:

@@ -4035,13 +4012,13 @@ parameters to select between more types. If the number of types are more than
 including <libtorrent/alert.hpp>.

-
-

exceptions

+
+

exceptions

There are a number of exceptions that can be thrown from different places in libtorrent, here's a complete list with description.

-
-

invalid_handle

-

This exception is thrown when querying information from a torrent_handle that hasn't +

+

invalid_handle

+

This exception is thrown when querying information from a torrent_handle that hasn't been initialized or that has become invalid.

 struct invalid_handle: std::exception
@@ -4050,10 +4027,10 @@ struct invalid_handle: std::exception
 };
 
-
-

duplicate_torrent

-

This is thrown by add_torrent() if the torrent already has been added to -the session. Since remove_torrent() is asynchronous, this exception may +

+

duplicate_torrent

+

This is thrown by add_torrent() if the torrent already has been added to +the session. Since remove_torrent() is asynchronous, this exception may be thrown if the torrent is removed and then immediately added again.

 struct duplicate_torrent: std::exception
@@ -4062,8 +4039,8 @@ struct duplicate_torrent: std::exception
 };
 
-
-

invalid_encoding

+
+

invalid_encoding

This is thrown by bdecode() if the input data is not a valid bencoding.

 struct invalid_encoding: std::exception
@@ -4072,8 +4049,8 @@ struct invalid_encoding: std::exception
 };
 
-
-

type_error

+
+

type_error

This is thrown from the accessors of entry if the data type of the entry doesn't match the type you want to extract from it.

@@ -4083,8 +4060,8 @@ struct type_error: std::runtime_error
 };
 
-
-

invalid_torrent_file

+
+

invalid_torrent_file

This exception is thrown from the constructor of torrent_info if the given bencoded information doesn't meet the requirements on what information has to be present in a torrent file.

@@ -4095,8 +4072,8 @@ struct invalid_torrent_file: std::exception
 
-
-

storage_interface

+
+

storage_interface

The storage interface is a pure virtual class that can be implemented to change the behavior of the actual file storage. The interface looks like this:

@@ -4118,8 +4095,8 @@ struct storage_interface virtual ~storage_interface() {} }; -
-

initialize()

+
+

initialize()

 void initialize(bool allocate_files) = 0;
@@ -4129,8 +4106,8 @@ void initialize(bool allocate_files) = 0;
 will create directories and empty files at this point. If allocate_files is true,
 it will also ftruncate all files to their target size.

-
-

read()

+
+

read()

 size_type read(char* buf, int slot, int offset, int size) = 0;
@@ -4140,8 +4117,8 @@ size_type read(char* buf, int slot, int offset, int size) = 0;
 and size number of bytes. The data is to be copied to buf.

The return value is the number of bytes actually read.

-
-

write()

+
+

write()

 void write(const char* buf, int slot, int offset, int size) = 0;
@@ -4150,8 +4127,8 @@ void write(const char* buf, int slot, int offset, int size) = 0;
 

This function should write the data in buf to the given slot (slot) at offset offset in that slot. The buffer size is size.

-
-

move_storage()

+
+

move_storage()

 bool move_storage(fs::path save_path) = 0;
@@ -4162,8 +4139,8 @@ The default storage moves the single file or the directory of the torrent.

Before moving the files, any open file handles may have to be closed, like release_files().

-
-

verify_resume_data()

+
+

verify_resume_data()

 bool verify_resume_data(entry& rd, std::string& error) = 0;
@@ -4174,8 +4151,8 @@ on disk. If the resume data seems to be up-to-date, return true. If
 not, set error to a description of what mismatched and return false.

The default storage may compare file sizes and time stamps of the files.

-
-

write_resume_data()

+
+

write_resume_data()

 void write_resume_data(entry& rd) const = 0;
@@ -4185,8 +4162,8 @@ void write_resume_data(entry& rd) const = 0;
 storage, in rd. The default storage adds file timestamps and
 sizes.

-
-

move_slot()

+
+

move_slot()

 void move_slot(int src_slot, int dst_slot) = 0;
@@ -4197,8 +4174,8 @@ the slot dst_slot. Th
 

If the storage caches slots, this could be implemented more efficient than reading and writing the data.

-
-

swap_slots()

+
+

swap_slots()

 void swap_slots(int slot1, int slot2) = 0;
@@ -4209,8 +4186,8 @@ storage uses a scratch buffer to read the data into, then moving the other
 slot and finally writing back the temporary slot's data

This is only used in compact mode.

-
-

swap_slots3()

+
+

swap_slots3()

 void swap_slots3(int slot1, int slot2, int slot3) = 0;
@@ -4221,8 +4198,8 @@ should move to slot2,
 should be moved to slot1.

This is only used in compact mode.

-
-

hash_for_slot()

+
+

hash_for_slot()

 sha1_hash hash_for_slot(int slot, partial_hash& h, int piece_size) = 0;
@@ -4243,8 +4220,8 @@ struct partial_hash
 that is stored in the given slot.

The function should return the hash of the piece stored in the slot.

-
-

release_files()

+
+

release_files()

 void release_files() = 0;
@@ -4254,8 +4231,8 @@ void release_files() = 0;
 belonging to this storage. The default implementation just calls
 file_pool::release_files(this).

-
-

delete_files()

+
+

delete_files()

 void delete_files() = 0;
@@ -4264,16 +4241,16 @@ void delete_files() = 0;
 

This function should delete all files and directories belonging to this storage.

-
-

queuing

+
+

queuing

libtorrent supports queuing. Which means it makes sure that a limited number of torrents are being downloaded at any given time, and once a torrent is completely downloaded, the next in line is started.

Torrents that are auto managed are subject to the queuing and the active torrents limits. To make a torrent auto managed, set auto_managed to true when adding the -torrent (see add_torrent()).

+torrent (see add_torrent()).

The limits of the number of downloading and seeding torrents are controlled via -active_downloads and active_seeds in session_settings. These limits takes +active_downloads and active_seeds in session_settings. These limits takes non auto managed torrents into account as well. If there are are more non-auto managed torrents being downloaded than the active_downloads setting, any auto managed torrents will be queued until torrents are removed so that the number drops below @@ -4283,40 +4260,40 @@ seeding. So, active_downloads

At a regular interval, torrents are checked if there needs to be any re-ordering of which torrents are active and which are queued. This interval can be controlled via -auto_manage_interval in session_settings. It defaults to every 30 seconds.

+auto_manage_interval in session_settings. It defaults to every 30 seconds.

For queuing to work, resume data needs to be saved and restored for all torrents. -See save_resume_data().

-
-

downloading

+See save_resume_data().

+
+

downloading

TODO: finish

-
-

seeding

+
+

seeding

Auto managed seeding torrents are rotated, so that all of them are allocated a fair amount of seeding. Torrents with fewer completed seed cycles are prioritized for seeding. A seed cycle is completed when a torrent meets either the share ratio limit (uploaded bytes / downloaded bytes), the share time ratio (time seeding / time downloaing) or seed time limit (time seeded).

The relevant settings to control these limits are share_ratio_limit, -seed_time_ratio_limit and seed_time_limit in session_settings.

+seed_time_ratio_limit and seed_time_limit in session_settings.

-
-

fast resume

+
+

fast resume

The fast resume mechanism is a way to remember which pieces are downloaded and where they are put between sessions. You can generate fast resume data by -calling save_resume_data() on torrent_handle. You can +calling save_resume_data() on torrent_handle. You can then save this data to disk and use it when resuming the torrent. libtorrent will not check the piece hashes then, and rely on the information given in the fast-resume data. The fast-resume data also contains information about which blocks, in the unfinished pieces, were downloaded, so it will not have to start from scratch on the partially downloaded pieces.

-

To use the fast-resume data you simply give it to add_torrent(), and it +

To use the fast-resume data you simply give it to add_torrent(), and it will skip the time consuming checks. It may have to do the checking anyway, if the fast-resume data is corrupt or doesn't fit the storage for that torrent, then it will not trust the fast-resume data and just do the checking.

-
-

file format

+
+

file format

The file format is a bencoded dictionary containing the following fields:

@@ -4425,8 +4402,8 @@ last resume data checkpoint.
-
-

threads

+
+

threads

libtorrent starts 2 or 3 threads.

    @@ -4444,8 +4421,8 @@ non-blocking host name resolution to simulate non-blocking behavior.
-
-

storage allocation

+
+

storage allocation

There are three modes in which storage (files on disk) are allocated in libtorrent.

  1. The traditional full allocation mode, where the entire files are filled up with @@ -4459,11 +4436,11 @@ pieces that have been downloaded. This is the default allocation mode in libtorr to where they belong. This is the recommended (and default) mode.

The allocation mode is selected when a torrent is started. It is passed as an -argument to session::add_torrent() (see add_torrent()).

+argument to session::add_torrent() (see add_torrent()).

The decision to use full allocation or compact allocation typically depends on whether any files are filtered and if the filesystem supports sparse files.

-
-

sparse allocation

+
+

sparse allocation

On filesystems that supports sparse files, this allocation mode will only use as much space as has been downloaded.

@@ -4474,9 +4451,9 @@ as much space as has been downloaded.

-
-

full allocation

-

When a torrent is started in full allocation mode, the checker thread (see threads) +

+

full allocation

+

When a torrent is started in full allocation mode, the checker thread (see threads) will make sure that the entire storage is allocated, and fill any gaps with zeros. This will be skipped if the filesystem supports sparse files or automatic zero filling. It will of course still check for existing pieces and fast resume data. The main @@ -4506,8 +4483,8 @@ out of date data, since pieces won't move around.

-
-

compact allocation

+
+

compact allocation

The compact allocation will only allocate as much storage as it needs to keep the pieces downloaded so far. This means that pieces will be moved around to be placed at their final position in the files while downloading (to make sure the completed @@ -4555,9 +4532,9 @@ contain any piece), return that slot index.

-
-

extensions

-

These extensions all operates within the extension protocol. The +

+

extensions

+

These extensions all operates within the extension protocol. The name of the extension is the name used in the extension-list packets, and the payload is the data in the extended message (not counting the length-prefix, message-id nor extension-id).

@@ -4565,8 +4542,8 @@ length-prefix, message-id nor extension-id).

handshake, it may be incompatible with future versions of the mainline bittorrent client.

These are the extensions that are currently implemented.

-
-

metadata from peers

+
+

metadata from peers

Extension name: "LT_metadata"

The point with this extension is that you don't have to distribute the metadata (.torrent-file) separately. The metadata can be distributed @@ -4684,9 +4661,9 @@ doesn't have any metadata.

-
-

HTTP seeding

-

The HTTP seed extension implements this specification.

+
+

HTTP seeding

+

The HTTP seed extension implements this specification.

The libtorrent implementation assumes that, if the URL ends with a slash ('/'), the filename should be appended to it in order to request pieces from that file. The way this works is that if the torrent is a single-file torrent, @@ -4695,8 +4672,8 @@ torrent's name '/' the file name is appended. This is the same directory structure that libtorrent will download torrents into.

-
-

filename checks

+
+

filename checks

Boost.Filesystem will by default check all its paths to make sure they conform to filename requirements on many platforms. If you don't want this check, you can set it to either only check for native filesystem requirements or turn it off @@ -4704,19 +4681,19 @@ altogether. You can use:

 boost::filesystem::path::default_name_check(boost::filesystem::native);
 
-

for example. For more information, see the Boost.Filesystem docs.

+

for example. For more information, see the Boost.Filesystem docs.

-
-

acknowledgments

+
+

acknowledgments

Written by Arvid Norberg. Copyright © 2003-2006

Contributions by Magnus Jonsson, Daniel Wallin and Cory Nelson

Lots of testing, suggestions and contributions by Massaroddel and Tianhao Qiu.

Big thanks to Michael Wojciechowski and Peter Koeleman for making the autotools scripts.

Thanks to Reimond Retz for bugfixes, suggestions and testing

-

Thanks to University of Umeå for providing development and test hardware.

+

Thanks to University of Umeå for providing development and test hardware.

Project is hosted by sourceforge.

-

sf_logo

+

sf_logo

diff --git a/docs/manual.rst b/docs/manual.rst index 778d43f20..6cac6b883 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -33,6 +33,10 @@ The basic usage is as follows: Each class and function is described in this manual. +For a description on how to create torrent files, see make_torrent_. + +.. _make_torrent: make_torrent.html + network primitives ================== @@ -221,8 +225,7 @@ add_torrent() :: typedef storage_interface* (&storage_constructor_type)( - boost::intrusive_ptr, fs::path const& - , file_pool&); + file_storage const&, fs::path const&, file_pool&); struct add_torrent_params { @@ -1090,6 +1093,10 @@ key is found, the return a pointer to it. torrent_info ============ +In previous versions of libtorrent, this class was also used for creating +torrent files. This functionality has been moved to ``create_torrent``, see +make_torrent_. + The ``torrent_info`` has the following synopsis:: class torrent_info @@ -1102,27 +1109,25 @@ The ``torrent_info`` has the following synopsis:: torrent_info(char const* filename); void add_tracker(std::string const& url, int tier = 0); + std::vector const& trackers() const; - typedef std::vector::const_iterator file_iterator; - typedef std::vector::const_reverse_iterator - reverse_file_iterator; + file_storage const& files() const; - bool remap_files(std::vector const& map); + typedef file_storage::iterator file_iterator; + typedef file_storage::reverse_iterator reverse_file_iterator; - file_iterator begin_files(bool storage = false) const; - file_iterator end_files(bool storage = false) const; - reverse_file_iterator rbegin_files(bool storage = false) const; - reverse_file_iterator rend_files(bool storage = false) const; + file_iterator begin_files() const; + file_iterator end_files() const; + reverse_file_iterator rbegin_files() const; + reverse_file_iterator rend_files() const; - int num_files(bool storage = false) const; - file_entry const& file_at(int index, bool storage = false) const; + int num_files() const; + file_entry const& file_at(int index) const; std::vector map_block(int piece, size_type offset - , int size, bool storage = false) const; + , int size) const; peer_request map_file(int file_index, size_type file_offset - , int size, bool storage = false) const; - - std::vector const& trackers() const; + , int size) const; bool priv() const; @@ -1188,52 +1193,37 @@ add_tracker() ``add_tracker()`` adds a tracker to the announce-list. The ``tier`` determines the order in which the trackers are to be tried. For more information see `trackers()`_. - -remap_files() -------------- +files() +------- :: - bool remap_files(std::vector const& map); + file_storage const& file() const; -This call will create a new mapping of the data in this torrent to other files. The -``torrent_info`` maintains 2 views of the file storage. One that is true to the torrent -file, and one that represents what is actually saved on disk. This call will change -what the files on disk are called. - -The each entry in the vector ``map`` is a ``file_entry``. The only fields in this struct -that are used in this case are ``path``, ``size`` and ``file_base``. - -The return value indicates if the remap was successful or not. True means success and -false means failure. The sum of all the files passed in through ``map`` has to be exactly -the same as the total_size of the torrent. If the number of bytes that are mapped do not -match, false will be returned (this is the only case this function may fail). - -Changing this mapping for an existing torrent will not move or rename files. If some files -should be renamed, this can be done before the torrent is added. +The ``file_storage`` object contains the information on how to map the pieces to +files. It is separated from the ``torrent_info`` object because when creating torrents +a storage object needs to be created without having a torrent file. When renaming files +in a storage, the storage needs to make its own copy of the ``file_storage`` in order +to make its mapping differ from the one in the torrent file. +For more information on the ``file_storage`` object, see the separate document on how +to create torrents. begin_files() end_files() rbegin_files() rend_files() ----------------------------------------------------- :: - file_iterator begin_files(bool storage = false) const; - file_iterator end_files(bool storage = false) const; - reverse_file_iterator rbegin_files(bool storage = false) const; - reverse_file_iterator rend_files(bool storage = false) const; + file_iterator begin_files() const; + file_iterator end_files() const; + reverse_file_iterator rbegin_files() const; + reverse_file_iterator rend_files() const; This class will need some explanation. First of all, to get a list of all files in the torrent, you can use ``begin_files()``, ``end_files()``, ``rbegin_files()`` and ``rend_files()``. These will give you standard vector iterators with the type ``file_entry``. -The ``storage`` parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see `remap_files()`_. - :: struct file_entry @@ -1274,27 +1264,20 @@ num_files() file_at() :: - int num_files(bool storage = false) const; - file_entry const& file_at(int index, bool storage = false) const; + int num_files() const; + file_entry const& file_at(int index) const; If you need index-access to files you can use the ``num_files()`` and ``file_at()`` to access files using indices. -The ``storage`` parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see `remap_files()`_. - - map_block() ----------- :: std::vector map_block(int piece, size_type offset - , int size, bool storage = false) const; + , int size) const; This function will map a piece index, a byte offset within that piece and a size (in bytes) into the corresponding files with offsets where that data @@ -1316,12 +1299,6 @@ 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. -The ``storage`` parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see `remap_files()`_. - map_file() ---------- @@ -1329,7 +1306,7 @@ map_file() :: peer_request map_file(int file_index, size_type file_offset - , int size, bool storage = false) const; + , int size) const; This function will map a range in a specific file into a range in the torrent. The ``file_offset`` parameter is the offset in the file, given in bytes, where @@ -1366,12 +1343,6 @@ vector of those urls. If you're creating a torrent file, ``add_url_seed()`` adds one url to the list of url-seeds. Currently, the only transport protocol supported for the url is http. -The ``storage`` parameter specifies which view of the files you want. The default -is false, which means you will see the content of the torrent file. If set to -true, you will see the file that the storage class uses to save the files to -disk. Typically these views are the same, but in case the files have been -remapped, they may differ. For more info, see `remap_files()`_. - See `HTTP seeding`_ for more information. diff --git a/examples/make_torrent.cpp b/examples/make_torrent.cpp index 3e687ae68..241455c0c 100644 --- a/examples/make_torrent.cpp +++ b/examples/make_torrent.cpp @@ -41,38 +41,38 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/file.hpp" #include "libtorrent/storage.hpp" #include "libtorrent/hasher.hpp" -#include "libtorrent/file_pool.hpp" #include "libtorrent/create_torrent.hpp" #include #include #include +#include using namespace boost::filesystem; using namespace libtorrent; -void add_files(create_torrent& t, path const& p, path const& l) +// do not include files and folders whose +// name starts with a . +bool file_filter(boost::filesystem::path const& filename) { - if (l.leaf()[0] == '.') return; - path f(p / l); - if (is_directory(f)) - { - for (directory_iterator i(f), end; i != end; ++i) - add_files(t, p, l / i->leaf()); - } - else - { - std::cerr << "adding \"" << l.string() << "\"\n"; - t.add_file(l, file_size(f)); - } + if (filename.leaf()[0] == '.') return false; + std::cerr << filename << std::endl; + return true; } +void print_progress(int i, int num) +{ + std::cerr << "\r" << (i+1) << "/" << num; +} int main(int argc, char* argv[]) { using namespace libtorrent; using namespace boost::filesystem; + int piece_size = 256 * 1024; + char const* creator_str = "libtorrent"; + path::default_name_check(no_check); if (argc != 4 && argc != 5) @@ -87,40 +87,20 @@ int main(int argc, char* argv[]) try { #endif - create_torrent t; + file_storage fs; + file_pool fp; path full_path = complete(path(argv[3])); - int piece_size = 256 * 1024; - char const* creator_str = "libtorrent"; + add_files(fs, full_path, file_filter); - add_files(t, full_path.branch_path(), full_path.leaf()); - t.set_piece_size(piece_size); + create_torrent t(fs, piece_size); t.add_tracker(argv[2]); - - std::vector tmp; - bencode(std::back_inserter(tmp), t.generate()); - boost::intrusive_ptr info(new torrent_info(&tmp[0], tmp.size())); - - file_pool fp; - boost::scoped_ptr st( - default_storage_constructor(info, full_path.branch_path(), fp)); - - // calculate the hash for all pieces - int num = t.num_pieces(); - std::vector buf(piece_size); - for (int i = 0; i < num; ++i) - { - st->read(&buf[0], i, 0, t.piece_size(i)); - hasher h(&buf[0], t.piece_size(i)); - t.set_hash(i, h.final()); - std::cerr << "\r" << (i+1) << "/" << num; - } + set_piece_hashes(t, full_path.branch_path() + , boost::bind(&print_progress, _1, t.num_pieces())); std::cerr << std::endl; - t.set_creator(creator_str); - if (argc == 5) - t.add_url_seed(argv[4]); + if (argc == 5) t.add_url_seed(argv[4]); // create the torrent and print it to out ofstream out(complete(path(argv[1])), std::ios_base::binary); diff --git a/include/Makefile.am b/include/Makefile.am index 166e9ea34..383f012c3 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -20,6 +20,7 @@ libtorrent/escape_string.hpp \ libtorrent/extensions.hpp \ libtorrent/file.hpp \ libtorrent/file_pool.hpp \ +libtorrent/file_storage.hpp \ libtorrent/fingerprint.hpp \ libtorrent/GeoIP.h \ libtorrent/gzip.hpp \ diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index cb72cc6aa..042ec1683 100755 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -53,6 +53,21 @@ namespace libtorrent torrent_handle handle; }; + struct TORRENT_EXPORT file_renamed_alert: torrent_alert + { + file_renamed_alert(torrent_handle const& h + , std::string const& name_ + , std::string const& msg) + : torrent_alert(h, alert::warning, msg) + , name(name_) + {} + + virtual std::auto_ptr clone() const + { return std::auto_ptr(new file_renamed_alert(*this)); } + + std::string name; + }; + struct TORRENT_EXPORT tracker_alert: torrent_alert { tracker_alert(torrent_handle const& h diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index c97174d4c..32c7a41f2 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -35,6 +35,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bencode.hpp" #include "libtorrent/peer_id.hpp" +#include "libtorrent/file_storage.hpp" +#include "libtorrent/file_pool.hpp" +#include "libtorrent/config.hpp" +#include "libtorrent/storage.hpp" +#include "libtorrent/hasher.hpp" + #include #include #include @@ -44,8 +50,10 @@ POSSIBILITY OF SUCH DAMAGE. #endif #include +#include #include #include +#include #ifdef _MSC_VER #pragma warning(pop) @@ -56,26 +64,29 @@ namespace libtorrent namespace fs = boost::filesystem; namespace pt = boost::posix_time; - struct create_torrent + struct TORRENT_EXPORT create_torrent { - create_torrent(); + create_torrent(file_storage& fs, int piece_size); + create_torrent(file_storage& fs); entry generate() const; + file_storage const& files() const { return m_files; } + void set_comment(char const* str); void set_creator(char const* str); - void set_piece_size(int size); void set_hash(int index, sha1_hash const& h); - void add_file(fs::path file, size_type size); void add_url_seed(std::string const& url); void add_node(std::pair const& node); void add_tracker(std::string const& url, int tier = 0); - int num_pieces() const { return m_num_pieces; } - int piece_length() const { return m_piece_length; } - int piece_size(int i) const; + int num_pieces() const { return m_files.num_pieces(); } + int piece_length() const { return m_files.piece_length(); } + int piece_size(int i) const { return m_files.piece_size(i); } private: + file_storage& m_files; + // the urls to the trackers typedef std::pair announce_entry; std::vector m_urls; @@ -84,32 +95,15 @@ namespace libtorrent std::vector m_piece_hash; - // the length of one piece - // if this is 0, the torrent_info is - // in an uninitialized state - int m_piece_length; - - typedef std::pair file_entry; - // the list of files that this torrent consists of - std::vector m_files; - // dht nodes to add to the routing table/bootstrap from typedef std::vector > nodes_t; nodes_t m_nodes; - // the sum of all filesizes - size_type m_total_size; - - // the number of pieces in the torrent - int m_num_pieces; - // the hash that identifies this torrent // is mutable because it's calculated // lazily mutable sha1_hash m_info_hash; - std::string m_name; - // if a creation date is found in the torrent file // this will be set to that, otherwise it'll be // 1970, Jan 1 @@ -134,6 +128,72 @@ namespace libtorrent // be announced on the dht bool m_private; }; + + namespace detail + { + inline bool default_pred(boost::filesystem::path const&) { return true; } + + inline void nop(int i) {} + + template + void add_files_impl(file_storage& fs, boost::filesystem::path const& p + , boost::filesystem::path const& l, Pred pred) + { + using boost::filesystem::path; + using boost::filesystem::directory_iterator; + std::string const& leaf = l.leaf(); + if (leaf == ".." || leaf == ".") return; + if (!pred(l)) return; + path f(p / l); + if (is_directory(f)) + { + for (directory_iterator i(f), end; i != end; ++i) + add_files_impl(fs, p, l / i->leaf(), pred); + } + else + { + fs.add_file(l, file_size(f)); + } + } + } + + template + void add_files(file_storage& fs, boost::filesystem::path const& file, Pred p) + { + detail::add_files_impl(fs, complete(file).branch_path(), file.leaf(), p); + } + + inline void add_files(file_storage& fs, boost::filesystem::path const& file) + { + detail::add_files_impl(fs, complete(file).branch_path(), file.leaf(), detail::default_pred); + } + + template + void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f) + { + file_pool fp; + boost::scoped_ptr st( + default_storage_constructor(const_cast(t.files()), p, fp)); + + // calculate the hash for all pieces + int num = t.num_pieces(); + std::vector buf(t.piece_length()); + for (int i = 0; i < num; ++i) + { + // read hits the disk and will block. Progress should + // be updated in between reads + st->read(&buf[0], i, 0, t.piece_size(i)); + hasher h(&buf[0], t.piece_size(i)); + t.set_hash(i, h.final()); + f(i); + } + } + + inline void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p) + { + set_piece_hashes(t, p, detail::nop); + } + } #endif diff --git a/include/libtorrent/disk_io_thread.hpp b/include/libtorrent/disk_io_thread.hpp index b66cc6014..198915588 100644 --- a/include/libtorrent/disk_io_thread.hpp +++ b/include/libtorrent/disk_io_thread.hpp @@ -82,6 +82,7 @@ namespace libtorrent , check_fastresume , check_files , save_resume_data + , rename_file }; action_t action; @@ -91,7 +92,7 @@ namespace libtorrent boost::intrusive_ptr storage; // arguments used for read and write int piece, offset; - // used for move_storage. On errors, this is set + // used for move_storage and rename_file. On errors, this is set // to the error message std::string str; diff --git a/include/libtorrent/file_pool.hpp b/include/libtorrent/file_pool.hpp index d57f6def5..69116d675 100644 --- a/include/libtorrent/file_pool.hpp +++ b/include/libtorrent/file_pool.hpp @@ -68,6 +68,7 @@ namespace libtorrent boost::shared_ptr open_file(void* st, fs::path const& p , file::open_mode m, std::string& error); void release(void* st); + void release(fs::path const& p); void resize(int size); private: diff --git a/include/libtorrent/file_storage.hpp b/include/libtorrent/file_storage.hpp new file mode 100644 index 000000000..bbb606bf6 --- /dev/null +++ b/include/libtorrent/file_storage.hpp @@ -0,0 +1,146 @@ +/* + +Copyright (c) 2003-2008, Arvid Norberg +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 + +#include +#include + +#ifdef _MSC_VER +#pragma warning(push, 1) +#endif + +#include + +#ifdef _MSC_VER +#pragma warning(pop) +#endif + +#include "libtorrent/size_type.hpp" +#include "libtorrent/assert.hpp" +#include "libtorrent/peer_request.hpp" + +namespace libtorrent +{ + namespace fs = boost::filesystem; + + struct TORRENT_EXPORT file_entry + { + file_entry(): offset(0), size(0), file_base(0) {} + + fs::path path; + size_type offset; // the offset of this file inside the torrent + size_type size; // the size of this file + // the offset in the file where the storage starts. + // This is always 0 unless parts of the torrent is + // compressed into a single file, such as a so-called part file. + size_type file_base; + }; + + struct TORRENT_EXPORT file_slice + { + int file_index; + size_type offset; + size_type size; + }; + + class TORRENT_EXPORT file_storage + { + friend class torrent_info; + public: + file_storage(); + ~file_storage() {} + + bool is_valid() const { return m_piece_length > 0; } + + void add_file(file_entry const& e); + void add_file(fs::path const& p, size_type size); + void rename_file(int index, std::string const& new_filename); + + std::vector map_block(int piece, size_type offset + , int size) const; + peer_request map_file(int file, size_type offset, int size) const; + + typedef std::vector::const_iterator iterator; + typedef std::vector::const_reverse_iterator reverse_iterator; + + iterator begin() const { return m_files.begin(); } + iterator end() const { return m_files.end(); } + reverse_iterator rbegin() const { return m_files.rbegin(); } + reverse_iterator rend() const { return m_files.rend(); } + int num_files() const + { return int(m_files.size()); } + + file_entry const& at(int index) const + { + TORRENT_ASSERT(index >= 0 && index < int(m_files.size())); + return m_files[index]; + } + + size_type total_size() const { TORRENT_ASSERT(m_piece_length > 0); return m_total_size; } + void set_num_pieces(int n) { m_num_pieces = n; } + int num_pieces() const { TORRENT_ASSERT(m_piece_length > 0); return m_num_pieces; } + void set_piece_length(int l) { m_piece_length = l; } + int piece_length() const { TORRENT_ASSERT(m_piece_length > 0); return m_piece_length; } + int piece_size(int index) const; + + void set_name(std::string const& n) { m_name = n; } + const std::string& name() const { TORRENT_ASSERT(m_piece_length > 0); return m_name; } + + void swap(file_storage& ti) + { + using std::swap; + swap(ti.m_piece_length, m_piece_length); + swap(ti.m_files, m_files); + swap(ti.m_total_size, m_total_size); + swap(ti.m_num_pieces, m_num_pieces); + swap(ti.m_name, m_name); + } + + private: + int m_piece_length; + + // the list of files that this torrent consists of + std::vector m_files; + + // the sum of all filesizes + size_type m_total_size; + + // the number of pieces in the torrent + int m_num_pieces; + std::string m_name; + }; +} + +#endif // TORRENT_FILE_STORAGE_HPP_INCLUDED + diff --git a/include/libtorrent/storage.hpp b/include/libtorrent/storage.hpp index 51c7b3c01..1c27388f7 100755 --- a/include/libtorrent/storage.hpp +++ b/include/libtorrent/storage.hpp @@ -87,11 +87,11 @@ namespace libtorrent #endif TORRENT_EXPORT std::vector > get_filesizes( - torrent_info const& t + file_storage const& t , fs::path p); TORRENT_EXPORT bool match_filesizes( - torrent_info const& t + file_storage const& t , fs::path p , std::vector > const& sizes , bool compact_mode @@ -157,6 +157,9 @@ namespace libtorrent // non-zero return value indicates an error virtual bool release_files() = 0; + // this will rename the file specified by index. + virtual bool rename_file(int index, std::string const& new_filename) = 0; + // this will close all open files and delete them // non-zero return value indicates an error virtual bool delete_files() = 0; @@ -178,16 +181,12 @@ namespace libtorrent }; typedef storage_interface* (&storage_constructor_type)( - boost::intrusive_ptr, fs::path const& - , file_pool&); + file_storage const&, fs::path const&, file_pool&); TORRENT_EXPORT storage_interface* default_storage_constructor( - boost::intrusive_ptr ti - , fs::path const& path, file_pool& fp); - + file_storage const&, fs::path const&, file_pool&); TORRENT_EXPORT storage_interface* mapped_storage_constructor( - boost::intrusive_ptr ti - , fs::path const& path, file_pool& fp); + file_storage const&, fs::path const&, file_pool&); struct disk_io_thread; @@ -201,7 +200,7 @@ namespace libtorrent piece_manager( boost::shared_ptr const& torrent - , boost::intrusive_ptr ti + , boost::intrusive_ptr info , fs::path const& path , file_pool& fp , disk_io_thread& io @@ -210,8 +209,7 @@ namespace libtorrent ~piece_manager(); - torrent_info const* info() const { return m_info.get(); } - + boost::intrusive_ptr info() const { return m_info; } void write_resume_data(entry& rd) const; void async_check_fastresume(entry const* resume_data @@ -219,6 +217,9 @@ namespace libtorrent void async_check_files(boost::function const& handler); + void async_rename_file(int index, std::string const& name + , boost::function const& handler); + void async_read( peer_request const& r , boost::function const& handler @@ -316,6 +317,8 @@ namespace libtorrent int release_files_impl() { return m_storage->release_files(); } int delete_files_impl() { return m_storage->delete_files(); } + int rename_file_impl(int index, std::string const& new_filename) + { return m_storage->rename_file(index, new_filename); } bool move_storage_impl(fs::path const& save_path); @@ -326,12 +329,13 @@ namespace libtorrent void debug_log() const; #endif #endif + boost::intrusive_ptr m_info; + file_storage const& m_files; + boost::scoped_ptr m_storage; storage_mode_t m_storage_mode; - boost::intrusive_ptr m_info; - // slots that haven't had any file storage allocated std::vector m_unallocated_slots; // slots that have file storage, but isn't assigned to a piece diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index f679d40f0..75d78e3bf 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -565,8 +565,14 @@ namespace libtorrent int max_uploads() const { return m_max_uploads; } void set_max_connections(int limit); int max_connections() const { return m_max_connections; } + void move_storage(fs::path const& save_path); + // renames the file with the given index to the new name + // the name may include a directory path + // returns false on failure + bool rename_file(int index, std::string const& name); + // unless this returns true, new connections must wait // with their initialization. bool ready_for_connections() const @@ -590,6 +596,7 @@ namespace libtorrent void on_torrent_paused(int ret, disk_io_job const& j); void on_storage_moved(int ret, disk_io_job const& j); void on_save_resume_data(int ret, disk_io_job const& j); + void on_file_renamed(int ret, disk_io_job const& j); void on_piece_verified(int ret, disk_io_job const& j , boost::function f); diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index a4ec19055..19f7dd6ca 100755 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -442,6 +442,7 @@ namespace libtorrent // post condition: save_path() == save_path if true is returned void move_storage(fs::path const& save_path) const; + void rename_file(int index, fs::path const& new_name) const; sha1_hash info_hash() const; diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index 46e9cd653..148d77c8f 100755 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -1,6 +1,6 @@ /* -Copyright (c) 2003, Arvid Norberg +Copyright (c) 2003-2008, Arvid Norberg All rights reserved. Redistribution and use in source and binary forms, with or without @@ -30,9 +30,8 @@ POSSIBILITY OF SUCH DAMAGE. */ -#ifndef TORRENT_TORRENT_INFO_HPP_INCLUDE -#define TORRENT_TORRENT_INFO_HPP_INCLUDE - +#ifndef TORRENT_TORRENT_INFO_HPP_INCLUDED +#define TORRENT_TORRENT_INFO_HPP_INCLUDED #include #include @@ -44,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include #include #ifdef _MSC_VER @@ -56,44 +54,17 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/peer_id.hpp" #include "libtorrent/size_type.hpp" -#include "libtorrent/peer_request.hpp" #include "libtorrent/config.hpp" #include "libtorrent/time.hpp" #include "libtorrent/intrusive_ptr_base.hpp" #include "libtorrent/assert.hpp" +#include "libtorrent/file_storage.hpp" namespace libtorrent { namespace pt = boost::posix_time; namespace gr = boost::gregorian; - namespace fs = boost::filesystem; - - struct TORRENT_EXPORT file_entry - { - file_entry(): offset(0), size(0), file_base(0) {} - - fs::path path; - size_type offset; // the offset of this file inside the torrent - size_type size; // the size of this file - // the offset in the file where the storage starts. - // This is always 0 unless parts of the torrent is - // compressed into a single file, such as a so-called part file. - size_type file_base; - // if the path was incorrectly encoded, this is - // the original corrupt encoded string. It is - // preserved in order to be able to reproduce - // the correct info-hash - boost::shared_ptr orig_path; - }; - - struct TORRENT_EXPORT file_slice - { - int file_index; - size_type offset; - size_type size; - }; - struct TORRENT_EXPORT announce_entry { announce_entry(std::string const& u): url(u), tier(0) {} @@ -101,10 +72,12 @@ namespace libtorrent int tier; }; +#ifndef BOOST_NO_EXCEPTIONS struct TORRENT_EXPORT invalid_torrent_file: std::exception { virtual const char* what() const throw() { return "invalid torrent file"; } }; +#endif class TORRENT_EXPORT torrent_info : public intrusive_ptr_base { @@ -116,108 +89,56 @@ namespace libtorrent torrent_info(char const* filename); ~torrent_info(); + file_storage const& files() const { return m_files; } + void add_tracker(std::string const& url, int tier = 0); + std::vector const& trackers() const { return m_urls; } - bool remap_files(std::vector const& map); - - std::vector map_block(int piece, size_type offset - , int size, bool storage = false) const; - peer_request map_file(int file, size_type offset, int size - , bool storage = false) const; - std::vector const& url_seeds() const { return m_url_seeds; } void add_url_seed(std::string const& url) { m_url_seeds.push_back(url); } - typedef std::vector::const_iterator file_iterator; - typedef std::vector::const_reverse_iterator reverse_file_iterator; - - // list the files in the torrent file - file_iterator begin_files(bool storage = false) const - { - if (!storage || m_remapped_files.empty()) - return m_files.begin(); - else - return m_remapped_files.begin(); - } - - file_iterator end_files(bool storage = false) const - { - if (!storage || m_remapped_files.empty()) - return m_files.end(); - else - return m_remapped_files.end(); - } - - reverse_file_iterator rbegin_files(bool storage = false) const - { - if (!storage || m_remapped_files.empty()) - return m_files.rbegin(); - else - return m_remapped_files.rbegin(); - } - - reverse_file_iterator rend_files(bool storage = false) const - { - if (!storage || m_remapped_files.empty()) - return m_files.rend(); - else - return m_remapped_files.rend(); - } - - int num_files(bool storage = false) const - { - TORRENT_ASSERT(m_piece_length > 0); - if (!storage || m_remapped_files.empty()) - return int(m_files.size()); - else - return int(m_remapped_files.size()); - } - - file_entry const& file_at(int index, bool storage = false) const - { - if (!storage || m_remapped_files.empty()) - { - TORRENT_ASSERT(index >= 0 && index < (int)m_files.size()); - return m_files[index]; - } - else - { - TORRENT_ASSERT(index >= 0 && index < (int)m_remapped_files.size()); - return m_remapped_files[index]; - } - } - - std::vector const& trackers() const { return m_urls; } - - size_type total_size() const { TORRENT_ASSERT(m_piece_length > 0); return m_total_size; } - int piece_length() const { TORRENT_ASSERT(m_piece_length > 0); return m_piece_length; } - int num_pieces() const { TORRENT_ASSERT(m_piece_length > 0); return m_num_pieces; } + size_type total_size() const { return m_files.total_size(); } + int piece_length() const { return m_files.piece_length(); } + int num_pieces() const { return m_files.num_pieces(); } const sha1_hash& info_hash() const { return m_info_hash; } - const std::string& name() const { TORRENT_ASSERT(m_piece_length > 0); return m_name; } + const std::string& name() const { return m_files.name(); } + typedef file_storage::iterator file_iterator; + typedef file_storage::reverse_iterator reverse_file_iterator; + + file_iterator begin_files() const { return m_files.begin(); } + file_iterator end_files() const { return m_files.end(); } + reverse_file_iterator rbegin_files() const { return m_files.rbegin(); } + reverse_file_iterator rend_files() const { return m_files.rend(); } + int num_files() const { return m_files.num_files(); } + file_entry const& file_at(int index) const { return m_files.at(index); } + + std::vector map_block(int piece, size_type offset, int size) const + { return m_files.map_block(piece, offset, size); } + peer_request map_file(int file, size_type offset, int size) const + { return m_files.map_file(file, offset, size); } + // ------- start deprecation ------- // these functions will be removed in a future version torrent_info(entry const& torrent_file) TORRENT_DEPRECATED; void print(std::ostream& os) const TORRENT_DEPRECATED; // ------- end deprecation ------- - bool is_valid() const { return m_piece_length > 0; } + bool is_valid() const { return m_files.is_valid(); } bool priv() const { return m_private; } - int piece_size(int index) const; + int piece_size(int index) const { return m_files.piece_size(index); } sha1_hash hash_for_piece(int index) const - { - return sha1_hash(hash_for_piece_ptr(index)); - } + { return sha1_hash(hash_for_piece_ptr(index)); } char const* hash_for_piece_ptr(int index) const { TORRENT_ASSERT(index >= 0); - TORRENT_ASSERT(index < m_num_pieces); + TORRENT_ASSERT(index < m_files.num_pieces()); TORRENT_ASSERT(m_piece_hashes); TORRENT_ASSERT(m_piece_hashes >= m_info_section.get()); TORRENT_ASSERT(m_piece_hashes < m_info_section.get() + m_info_section_size); @@ -261,41 +182,18 @@ namespace libtorrent bool parse_torrent_file(lazy_entry const& libtorrent, std::string& error); + file_storage m_files; + // the urls to the trackers std::vector m_urls; - std::vector m_url_seeds; - - // the length of one piece - // if this is 0, the torrent_info is - // in an uninitialized state - int m_piece_length; - - // the list of files that this torrent consists of - std::vector m_files; - - // this vector is typically empty. If it is not - // empty, it means the user has re-mapped the - // files in this torrent to different names - // on disk. This is only used when reading and - // writing the disk. - std::vector m_remapped_files; - nodes_t m_nodes; - // the sum of all filesizes - size_type m_total_size; - - // the number of pieces in the torrent - int m_num_pieces; - // the hash that identifies this torrent // is mutable because it's calculated // lazily sha1_hash m_info_hash; - std::string m_name; - // if a creation date is found in the torrent file // this will be set to that, otherwise it'll be // 1970, Jan 1 diff --git a/src/Makefile.am b/src/Makefile.am index 50e3ccadc..856eeebdc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,7 +24,7 @@ logger.cpp file_pool.cpp ut_pex.cpp lsd.cpp upnp.cpp instantiate_connection.cpp socks5_stream.cpp socks4_stream.cpp http_stream.cpp connection_queue.cpp \ disk_io_thread.cpp ut_metadata.cpp magnet_uri.cpp udp_socket.cpp smart_ban.cpp \ http_parser.cpp gzip.cpp disk_buffer_holder.cpp create_torrent.cpp GeoIP.c \ -parse_url.cpp $(kademlia_sources) +parse_url.cpp file_storage.cpp $(kademlia_sources) noinst_HEADERS = \ $(top_srcdir)/include/libtorrent/alert.hpp \ @@ -51,6 +51,7 @@ $(top_srcdir)/include/libtorrent/extensions/logger.hpp \ $(top_srcdir)/include/libtorrent/extensions/ut_pex.hpp \ $(top_srcdir)/include/libtorrent/file.hpp \ $(top_srcdir)/include/libtorrent/file_pool.hpp \ +$(top_srcdir)/include/libtorrent/file_storage.hpp \ $(top_srcdir)/include/libtorrent/fingerprint.hpp \ $(top_srcdir)/include/libtorrent/gzip.hpp \ $(top_srcdir)/include/libtorrent/hasher.hpp \ diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index 8d408fe2c..98f590d20 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -31,7 +31,8 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/create_torrent.hpp" -#include "libtorrent/hasher.hpp" +#include "libtorrent/file_pool.hpp" +#include "libtorrent/storage.hpp" #include #include @@ -41,22 +42,61 @@ namespace gr = boost::gregorian; namespace libtorrent { - create_torrent::create_torrent() - : m_piece_length(0) - , m_total_size(0) - , m_num_pieces(0) - , m_info_hash() - , m_name() + create_torrent::create_torrent(file_storage& fs, int size) + : m_files(fs) , m_creation_date(pt::second_clock::universal_time()) - , m_multifile(false) + , m_multifile(fs.num_files() > 1) , m_private(false) - {} + { + TORRENT_ASSERT(fs.num_files() > 0); + if (!m_multifile && m_files.at(0).path.has_branch_path()) m_multifile = true; + // make sure the size is an even power of 2 +#ifndef NDEBUG + for (int i = 0; i < 32; ++i) + { + if (size & (1 << i)) + { + TORRENT_ASSERT((size & ~(1 << i)) == 0); + break; + } + } +#endif + m_files.set_piece_length(size); + m_files.set_num_pieces(static_cast( + (m_files.total_size() + m_files.piece_length() - 1) / m_files.piece_length())); + m_piece_hash.resize(m_files.num_pieces()); + } + + create_torrent::create_torrent(file_storage& fs) + : m_files(fs) + , m_creation_date(pt::second_clock::universal_time()) + , m_multifile(fs.num_files() > 1) + , m_private(false) + { + TORRENT_ASSERT(fs.num_files() > 0); + if (!m_multifile && m_files.at(0).path.has_branch_path()) m_multifile = true; + + const int target_size = 40 * 1024; + int size = fs.total_size() / (target_size / 20); + + for (int i = 4*1024*1024; i > 16*1024; i /= 2) + { + if (size < i) continue; + size = i; + break; + } + + m_files.set_piece_length(size); + m_files.set_num_pieces(static_cast( + (m_files.total_size() + m_files.piece_length() - 1) / m_files.piece_length())); + m_piece_hash.resize(m_files.num_pieces()); + } entry create_torrent::generate() const { - TORRENT_ASSERT(m_piece_length > 0); + TORRENT_ASSERT(m_files.piece_length() > 0); - if (m_files.empty()) + if (m_files.num_files() == 0) { // TODO: throw something here // throw @@ -128,15 +168,13 @@ namespace libtorrent } entry& info = dict["info"]; - - info["name"] = m_name; - + info["name"] = m_files.name(); if (m_private) info["private"] = 1; if (!m_multifile) { - info["length"] = m_files.front().second; + info["length"] = m_files.at(0).size; } else { @@ -144,19 +182,19 @@ namespace libtorrent { entry& files = info["files"]; - for (std::vector::const_iterator i = m_files.begin(); + for (file_storage::iterator i = m_files.begin(); i != m_files.end(); ++i) { files.list().push_back(entry()); entry& file_e = files.list().back(); - file_e["length"] = i->second; + file_e["length"] = i->size; entry& path_e = file_e["path"]; - TORRENT_ASSERT(i->first.has_branch_path()); - TORRENT_ASSERT(*i->first.begin() == m_name); + TORRENT_ASSERT(i->path.has_branch_path()); + TORRENT_ASSERT(*i->path.begin() == m_files.name()); - for (fs::path::iterator j = boost::next(i->first.begin()); - j != i->first.end(); ++j) + for (fs::path::iterator j = boost::next(i->path.begin()); + j != i->path.end(); ++j) { path_e.list().push_back(entry(*j)); } @@ -164,7 +202,7 @@ namespace libtorrent } } - info["piece length"] = m_piece_length; + info["piece length"] = m_files.piece_length(); entry& pieces = info["pieces"]; std::string& p = pieces.string(); @@ -183,21 +221,6 @@ namespace libtorrent } - int create_torrent::piece_size(int index) const - { - TORRENT_ASSERT(index >= 0 && index < num_pieces()); - if (index == num_pieces()-1) - { - int size = int(m_total_size - - (num_pieces() - 1) * piece_length()); - TORRENT_ASSERT(size > 0); - TORRENT_ASSERT(size <= piece_length()); - return int(size); - } - else - return piece_length(); - } - void create_torrent::add_tracker(std::string const& url, int tier) { m_urls.push_back(announce_entry(url, tier)); @@ -207,32 +230,6 @@ namespace libtorrent , bind(&announce_entry::second, _1) < bind(&announce_entry::second, _2)); } - void create_torrent::set_piece_size(int size) - { - // make sure the size is an even power of 2 -#ifndef NDEBUG - for (int i = 0; i < 32; ++i) - { - if (size & (1 << i)) - { - TORRENT_ASSERT((size & ~(1 << i)) == 0); - break; - } - } -#endif - m_piece_length = size; - - m_num_pieces = static_cast( - (m_total_size + m_piece_length - 1) / m_piece_length); - int old_num_pieces = static_cast(m_piece_hash.size()); - - m_piece_hash.resize(m_num_pieces); - for (int i = old_num_pieces; i < m_num_pieces; ++i) - { - m_piece_hash[i].clear(); - } - } - void create_torrent::set_hash(int index, sha1_hash const& h) { TORRENT_ASSERT(index >= 0); @@ -240,46 +237,6 @@ namespace libtorrent m_piece_hash[index] = h; } - void create_torrent::add_file(fs::path file, size_type size) - { -// TORRENT_ASSERT(file.begin() != file.end()); - - if (!file.has_branch_path()) - { - // you have already added at least one file with a - // path to the file (branch_path), which means that - // all the other files need to be in the same top - // directory as the first file. - TORRENT_ASSERT(m_files.empty()); - TORRENT_ASSERT(!m_multifile); - m_name = file.string(); - } - else - { -#ifndef NDEBUG - if (!m_files.empty()) - TORRENT_ASSERT(m_name == *file.begin()); -#endif - m_multifile = true; - m_name = *file.begin(); - } - - m_files.push_back(file_entry(file, size)); - - m_total_size += size; - - if (m_piece_length == 0) - m_piece_length = 256 * 1024; - - m_num_pieces = int((m_total_size + m_piece_length - 1) / m_piece_length); - int old_num_pieces = int(m_piece_hash.size()); - - m_piece_hash.resize(m_num_pieces); - if (m_num_pieces > old_num_pieces) - std::for_each(m_piece_hash.begin() + old_num_pieces - , m_piece_hash.end(), boost::bind(&sha1_hash::clear, _1)); - } - void create_torrent::add_node(std::pair const& node) { m_nodes.push_back(node); @@ -299,5 +256,6 @@ namespace libtorrent { m_created_by = str; } + } diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index ac76263ce..57aa52877 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -1049,6 +1049,13 @@ namespace libtorrent ret = 0; break; } + case disk_io_job::rename_file: + { +#ifdef TORRENT_DISK_STATS + m_log << log_time() << " rename file" << std::endl; +#endif + ret = j.storage->rename_file_impl(j.piece, j.str); + } } #ifndef BOOST_NO_EXCEPTIONS } catch (std::exception& e) diff --git a/src/file_pool.cpp b/src/file_pool.cpp index 6baeb64d0..19bdb4162 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -115,6 +115,16 @@ namespace libtorrent return e.file_ptr; } + void file_pool::release(fs::path const& p) + { + boost::mutex::scoped_lock l(m_mutex); + + typedef nth_index::type path_view; + path_view& pt = get<0>(m_files); + path_view::iterator i = pt.find(p); + if (i != pt.end()) pt.erase(i); + } + void file_pool::release(void* st) { boost::mutex::scoped_lock l(m_mutex); diff --git a/src/file_storage.cpp b/src/file_storage.cpp new file mode 100644 index 000000000..69e88d25d --- /dev/null +++ b/src/file_storage.cpp @@ -0,0 +1,150 @@ +/* + +Copyright (c) 2003-2008, Arvid Norberg +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. + +*/ + +#include "libtorrent/pch.hpp" + +#include "libtorrent/file_storage.hpp" + + +namespace libtorrent +{ + file_storage::file_storage() + : m_piece_length(0) + , m_total_size(0) + , m_num_pieces(0) + {} + + int file_storage::piece_size(int index) const + { + TORRENT_ASSERT(index >= 0 && index < num_pieces()); + if (index == num_pieces()-1) + { + int size = int(total_size() + - size_type(num_pieces() - 1) * piece_length()); + TORRENT_ASSERT(size > 0); + TORRENT_ASSERT(size <= piece_length()); + return int(size); + } + else + return piece_length(); + } + + void file_storage::rename_file(int index, std::string const& new_filename) + { + TORRENT_ASSERT(index >= 0 && index < int(m_files.size())); + m_files[index].path = new_filename; + } + + std::vector file_storage::map_block(int piece, size_type offset + , int size_) const + { + TORRENT_ASSERT(num_files() > 0); + std::vector ret; + + size_type start = piece * (size_type)m_piece_length + offset; + size_type size = size_; + TORRENT_ASSERT(start + size <= m_total_size); + + // find the file iterator and file offset + // TODO: make a vector that can map piece -> file index in O(1) + size_type file_offset = start; + std::vector::const_iterator file_iter; + + int counter = 0; + for (file_iter = begin();; ++counter, ++file_iter) + { + TORRENT_ASSERT(file_iter != end()); + if (file_offset < file_iter->size) + { + file_slice f; + f.file_index = counter; + f.offset = file_offset + file_iter->file_base; + f.size = (std::min)(file_iter->size - file_offset, (size_type)size); + size -= f.size; + file_offset += f.size; + ret.push_back(f); + } + + TORRENT_ASSERT(size >= 0); + if (size <= 0) break; + + file_offset -= file_iter->size; + } + return ret; + } + + peer_request file_storage::map_file(int file_index, size_type file_offset + , int size) const + { + TORRENT_ASSERT(file_index < num_files()); + TORRENT_ASSERT(file_index >= 0); + size_type offset = file_offset + at(file_index).offset; + + peer_request ret; + ret.piece = int(offset / piece_length()); + ret.start = int(offset - ret.piece * piece_length()); + ret.length = size; + return ret; + } + + void file_storage::add_file(fs::path const& file, size_type size) + { + TORRENT_ASSERT(size >= 0); + if (!file.has_branch_path()) + { + // you have already added at least one file with a + // path to the file (branch_path), which means that + // all the other files need to be in the same top + // directory as the first file. + TORRENT_ASSERT(m_files.empty()); + m_name = file.string(); + } + else + { + if (m_files.empty()) + m_name = *file.begin(); + } + TORRENT_ASSERT(m_name == *file.begin()); + file_entry e; + m_files.push_back(e); + m_files.back().size = size; + m_files.back().path = file; + m_files.back().offset = m_total_size; + m_total_size += size; + } + + void file_storage::add_file(file_entry const& e) + { + add_file(e.path, e.size); + } +} + diff --git a/src/mapped_storage.cpp b/src/mapped_storage.cpp index 7b8637064..97d961ab9 100644 --- a/src/mapped_storage.cpp +++ b/src/mapped_storage.cpp @@ -262,8 +262,8 @@ namespace libtorrent struct mapped_storage: storage_interface { - mapped_storage(boost::intrusive_ptr const& info, fs::path save_path) - : m_info(info) + mapped_storage(file_storage const& fs, fs::path save_path) + : m_files(fs) , m_save_path(save_path) {} @@ -272,9 +272,9 @@ namespace libtorrent int read(char* buf, int slot, int offset, int size) { TORRENT_ASSERT(buf != 0); - TORRENT_ASSERT(slot >= 0 && slot < m_info->num_pieces()); + TORRENT_ASSERT(slot >= 0 && slot < m_files.num_pieces()); TORRENT_ASSERT(offset >= 0); - TORRENT_ASSERT(offset < m_info->piece_size(slot)); + TORRENT_ASSERT(offset < m_files.piece_size(slot)); TORRENT_ASSERT(size > 0); size_type result = -1; @@ -283,17 +283,17 @@ namespace libtorrent #ifndef NDEBUG std::vector slices - = m_info->map_block(slot, offset, size, true); + = files().map_block(slot, offset, size); TORRENT_ASSERT(!slices.empty()); #endif - size_type start = slot * (size_type)m_info->piece_length() + offset; - TORRENT_ASSERT(start + size <= m_info->total_size()); + size_type start = slot * (size_type)m_files.piece_length() + offset; + TORRENT_ASSERT(start + size <= m_files.total_size()); // find the file iterator and file offset size_type file_offset = start; std::vector::const_iterator file_iter; - for (file_iter = m_info->begin_files(true);;) + for (file_iter = files().begin();;) { if (file_offset < file_iter->size) break; @@ -333,7 +333,7 @@ namespace libtorrent TORRENT_ASSERT(int(slices.size()) > counter); size_type slice_size = slices[counter].size; TORRENT_ASSERT(slice_size == read_bytes); - TORRENT_ASSERT(m_info->file_at(slices[counter].file_index, true).path + TORRENT_ASSERT(files().at(slices[counter].file_index).path == file_iter->path); #endif @@ -353,7 +353,7 @@ namespace libtorrent { ++file_iter; // skip empty files - while (file_iter != m_info->end_files(true) && file_iter->size == 0) + while (file_iter != files().end() && file_iter->size == 0) ++file_iter; #ifndef NDEBUG @@ -390,24 +390,24 @@ namespace libtorrent int write(const char* buf, int slot, int offset, int size) { TORRENT_ASSERT(buf != 0); - TORRENT_ASSERT(slot >= 0 && slot < m_info->num_pieces()); + TORRENT_ASSERT(slot >= 0 && slot < m_files.num_pieces()); TORRENT_ASSERT(offset >= 0); - TORRENT_ASSERT(offset < m_info->piece_size(slot)); + TORRENT_ASSERT(offset < m_files.piece_size(slot)); TORRENT_ASSERT(size > 0); #ifndef NDEBUG std::vector slices - = m_info->map_block(slot, offset, size, true); + = files().map_block(slot, offset, size); TORRENT_ASSERT(!slices.empty()); #endif - size_type start = slot * (size_type)m_info->piece_length() + offset; - TORRENT_ASSERT(start + size <= m_info->total_size()); + size_type start = slot * (size_type)m_files.piece_length() + offset; + TORRENT_ASSERT(start + size <= m_files.total_size()); // find the file iterator and file offset size_type file_offset = start; std::vector::const_iterator file_iter; - for (file_iter = m_info->begin_files(true);;) + for (file_iter = files().begin();;) { if (file_offset < file_iter->size) break; @@ -449,7 +449,7 @@ namespace libtorrent TORRENT_ASSERT(int(slices.size()) > counter); size_type slice_size = slices[counter].size; TORRENT_ASSERT(slice_size == write_bytes); - TORRENT_ASSERT(m_info->file_at(slices[counter].file_index, true).path + TORRENT_ASSERT(files().at(slices[counter].file_index).path == file_iter->path); #endif @@ -468,7 +468,7 @@ namespace libtorrent if (left_to_write > 0) { ++file_iter; - while (file_iter != m_info->end_files(true) && file_iter->size == 0) + while (file_iter != files().end() && file_iter->size == 0) ++file_iter; #ifndef NDEBUG // empty files are not returned by map_block, so if @@ -533,11 +533,11 @@ namespace libtorrent m_pool.release(this); #if defined(_WIN32) && defined(UNICODE) && BOOST_VERSION >= 103400 - old_path = safe_convert((m_save_path / m_info->name()).string()); - new_path = safe_convert((save_path / m_info->name()).string()); + old_path = safe_convert((m_save_path / files().name()).string()); + new_path = safe_convert((save_path / files().name()).string()); #else - old_path = m_save_path / m_info->name(); - new_path = save_path / m_info->name(); + old_path = m_save_path / files().name(); + new_path = save_path / files().name(); #endif try @@ -604,7 +604,7 @@ namespace libtorrent } entry::list_type const& slots = slots_ent->list(); - bool seed = int(slots.size()) == m_info->num_pieces() + bool seed = int(slots.size()) == files().num_pieces() && std::find_if(slots.begin(), slots.end() , boost::bind(std::less() , boost::bind((size_type const& (entry::*)() const) @@ -617,11 +617,11 @@ namespace libtorrent if (seed) { - if (m_info->num_files(true) != (int)file_sizes.size()) + if (files().num_files() != (int)file_sizes.size()) { error = "the number of files does not match the torrent (num: " + boost::lexical_cast(file_sizes.size()) + " actual: " - + boost::lexical_cast(m_info->num_files(true)) + ")"; + + boost::lexical_cast(files().num_files()) + ")"; return false; } @@ -629,8 +629,8 @@ namespace libtorrent fs = file_sizes.begin(); // the resume data says we have the entire torrent // make sure the file sizes are the right ones - for (torrent_info::file_iterator i = m_info->begin_files(true) - , end(m_info->end_files(true)); i != end; ++i, ++fs) + for (file_storage::iterator i = files().begin() + , end(files().end()); i != end; ++i, ++fs) { if (i->size != fs->first) { @@ -643,7 +643,7 @@ namespace libtorrent return true; } - return match_filesizes(*m_info, m_save_path, file_sizes + return match_filesizes(files(), m_save_path, file_sizes , !full_allocation_mode, &error); } @@ -655,7 +655,7 @@ namespace libtorrent return true; } std::vector > file_sizes - = get_filesizes(*m_info, m_save_path); + = get_filesizes(m_files, m_save_path); entry::list_type& fl = rd["file sizes"].list(); for (std::vector >::iterator i @@ -672,7 +672,7 @@ namespace libtorrent bool move_slot(int src_slot, int dst_slot) { // TODO: this can be optimized by mapping both slots and do a straight memcpy - int piece_size = m_info->piece_size(dst_slot); + int piece_size = m_files.piece_size(dst_slot); m_scratch_buffer.resize(piece_size); size_type ret1 = read(&m_scratch_buffer[0], src_slot, 0, piece_size); size_type ret2 = write(&m_scratch_buffer[0], dst_slot, 0, piece_size); @@ -683,9 +683,9 @@ namespace libtorrent { // TODO: this can be optimized by mapping both slots and do a straight memcpy // the size of the target slot is the size of the piece - int piece_size = m_info->piece_length(); - int piece1_size = m_info->piece_size(slot2); - int piece2_size = m_info->piece_size(slot1); + int piece_size = m_files.piece_length(); + int piece1_size = m_files.piece_size(slot2); + int piece2_size = m_files.piece_size(slot1); m_scratch_buffer.resize(piece_size * 2); size_type ret1 = read(&m_scratch_buffer[0], slot1, 0, piece1_size); size_type ret2 = read(&m_scratch_buffer[piece_size], slot2, 0, piece2_size); @@ -699,10 +699,10 @@ namespace libtorrent { // TODO: this can be optimized by mapping both slots and do a straight memcpy // the size of the target slot is the size of the piece - int piece_size = m_info->piece_length(); - int piece1_size = m_info->piece_size(slot2); - int piece2_size = m_info->piece_size(slot3); - int piece3_size = m_info->piece_size(slot1); + int piece_size = m_files.piece_length(); + int piece1_size = m_files.piece_size(slot2); + int piece2_size = m_files.piece_size(slot3); + int piece3_size = m_files.piece_size(slot1); m_scratch_buffer.resize(piece_size * 2); size_type ret1 = read(&m_scratch_buffer[0], slot1, 0, piece1_size); size_type ret2 = read(&m_scratch_buffer[piece_size], slot2, 0, piece2_size); @@ -745,6 +745,39 @@ namespace libtorrent #endif } + bool rename_file(int index, std::string const& new_filename) + { + if (index < 0 || index >= m_files.num_files()) return true; + fs::path old_name = m_save_path / files().at(index).path; + m_pool.release(this); + +#if defined(_WIN32) && defined(UNICODE) && BOOST_VERSION >= 103400 + fs::wpath old_path = safe_convert(old_name); + fs::wpath new_path = safe_convert(m_save_path / new_filename); +#else + fs::path const& old_path = old_name; + fs::path new_path = m_save_path / new_filename; +#endif + +#ifndef BOOST_NO_EXCEPTIONS + try + { +#endif + rename(old_path, new_path); + if (!m_mapped_files) + { m_mapped_files.reset(new file_storage(m_files)); } + m_mapped_files->rename_file(index, new_filename); +#ifndef BOOST_NO_EXCEPTIONS + } + catch (std::exception& e) + { + set_error(old_name.string(), e.what()); + return true; + } +#endif + return false; + } + bool release_files() { m_pool.release(this); @@ -764,8 +797,8 @@ namespace libtorrent // delete the files from disk std::set directories; typedef std::set::iterator iter_t; - for (torrent_info::file_iterator i = m_info->begin_files(true) - , end(m_info->end_files(true)); i != end; ++i) + for (file_storage::iterator i = m_files.begin() + , end(m_files.end()); i != end; ++i) { std::string p = (m_save_path / i->path).string(); fs::path bp = i->path.branch_path(); @@ -804,7 +837,10 @@ namespace libtorrent private: - boost::intrusive_ptr m_info; + file_storage const& files() const { return m_mapped_files?*m_mapped_files:m_files; } + + boost::scoped_ptr m_mapped_files; + file_storage const& m_files; fs::path m_save_path; // temporary storage for moving pieces @@ -813,10 +849,10 @@ namespace libtorrent static mapped_file_pool m_pool; }; - storage_interface* mapped_storage_constructor(boost::intrusive_ptr ti + storage_interface* mapped_storage_constructor(file_storage const& fs , fs::path const& path, file_pool& fp) { - return new mapped_storage(ti, path); + return new mapped_storage(fs, path); } mapped_file_pool mapped_storage::m_pool; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 2615d0211..3d8589077 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1661,7 +1661,7 @@ namespace aux { { TORRENT_ASSERT(!params.save_path.empty()); - if (params.ti && params.ti->num_files() == 0) + if (params.ti && params.ti->files().num_files() == 0) { #ifndef BOOST_NO_EXCEPTIONS throw std::runtime_error("no files in torrent"); diff --git a/src/storage.cpp b/src/storage.cpp index e86d3d58d..bb9c49264 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -251,12 +251,12 @@ namespace libtorrent { std::vector > get_filesizes( - torrent_info const& t, fs::path p) + file_storage const& s, fs::path p) { p = complete(p); std::vector > sizes; - for (torrent_info::file_iterator i = t.begin_files(true); - i != t.end_files(true); ++i) + for (file_storage::iterator i = s.begin() + , end(s.end());i != end; ++i) { size_type size = 0; std::time_t time = 0; @@ -289,13 +289,13 @@ namespace libtorrent // pieces, so any older version of the resume data will // still be a correct subset of the actual data on disk. bool match_filesizes( - torrent_info const& t + file_storage const& fs , fs::path p , std::vector > const& sizes , bool compact_mode , std::string* error) { - if ((int)sizes.size() != t.num_files(true)) + if ((int)sizes.size() != fs.num_files()) { if (error) *error = "mismatching number of files"; return false; @@ -304,8 +304,8 @@ namespace libtorrent std::vector >::const_iterator s = sizes.begin(); - for (torrent_info::file_iterator i = t.begin_files(true); - i != t.end_files(true); ++i, ++s) + for (file_storage::iterator i = fs.begin() + , end(fs.end());i != end; ++i, ++s) { size_type size = 0; std::time_t time = 0; @@ -354,15 +354,16 @@ namespace libtorrent class storage : public storage_interface, boost::noncopyable { public: - storage(boost::intrusive_ptr info, fs::path const& path, file_pool& fp) - : m_info(info) - , m_files(fp) + storage(file_storage const& fs, fs::path const& path, file_pool& fp) + : m_files(fs) + , m_pool(fp) { - TORRENT_ASSERT(info->begin_files(true) != info->end_files(true)); + TORRENT_ASSERT(m_files.begin() != m_files.end()); m_save_path = fs::complete(path); TORRENT_ASSERT(m_save_path.is_complete()); } + bool rename_file(int index, std::string const& new_filename); bool release_files(); bool delete_files(); bool initialize(bool allocate_files); @@ -379,14 +380,18 @@ namespace libtorrent int read_impl(char* buf, int slot, int offset, int size, bool fill_zero); ~storage() - { m_files.release(this); } + { m_pool.release(this); } + + file_storage const& files() const { return m_mapped_files?*m_mapped_files:m_files; } + + boost::scoped_ptr m_mapped_files; + file_storage const& m_files; - boost::intrusive_ptr m_info; fs::path m_save_path; // the file pool is typically stored in // the session, to make all storage // instances use the same pool - file_pool& m_files; + file_pool& m_pool; // temporary storage for moving pieces buffer m_scratch_buffer; @@ -399,7 +404,7 @@ namespace libtorrent hasher whole; int slot_size1 = piece_size; m_scratch_buffer.resize(slot_size1); - int read_result = read_impl(&m_scratch_buffer[0], slot, 0, slot_size1, true); + read_impl(&m_scratch_buffer[0], slot, 0, slot_size1, true); if (ph.offset > 0) partial.update(&m_scratch_buffer[0], ph.offset); whole.update(&m_scratch_buffer[0], slot_size1); @@ -426,8 +431,8 @@ namespace libtorrent { // first, create all missing directories fs::path last_path; - for (torrent_info::file_iterator file_iter = m_info->begin_files(true), - end_iter = m_info->end_files(true); file_iter != end_iter; ++file_iter) + for (file_storage::iterator file_iter = files().begin(), + end_iter = files().end(); file_iter != end_iter; ++file_iter) { fs::path dir = (m_save_path / file_iter->path).branch_path(); @@ -475,7 +480,7 @@ namespace libtorrent if (allocate_files) { std::string error; - boost::shared_ptr f = m_files.open_file(this + boost::shared_ptr f = m_pool.open_file(this , m_save_path / file_iter->path, file::in | file::out , error); if (f && f->error().empty()) @@ -491,13 +496,46 @@ namespace libtorrent #endif } // close files that were opened in write mode - m_files.release(this); + m_pool.release(this); + return false; + } + + bool storage::rename_file(int index, std::string const& new_filename) + { + if (index < 0 || index >= m_files.num_files()) return true; + fs::path old_name = m_save_path / files().at(index).path; + m_pool.release(old_name); + +#if defined(_WIN32) && defined(UNICODE) && BOOST_VERSION >= 103400 + fs::wpath old_path = safe_convert(old_name); + fs::wpath new_path = safe_convert(m_save_path / new_filename); +#else + fs::path const& old_path = old_name; + fs::path new_path = m_save_path / new_filename; +#endif + +#ifndef BOOST_NO_EXCEPTIONS + try + { +#endif + rename(old_path, new_path); + if (!m_mapped_files) + { m_mapped_files.reset(new file_storage(m_files)); } + m_mapped_files->rename_file(index, new_filename); +#ifndef BOOST_NO_EXCEPTIONS + } + catch (std::exception& e) + { + set_error(old_name.string(), e.what()); + return true; + } +#endif return false; } bool storage::release_files() { - m_files.release(this); + m_pool.release(this); buffer().swap(m_scratch_buffer); return false; } @@ -505,7 +543,7 @@ namespace libtorrent bool storage::delete_files() { // make sure we don't have the files open - m_files.release(this); + m_pool.release(this); buffer().swap(m_scratch_buffer); int result = 0; @@ -515,8 +553,8 @@ namespace libtorrent // delete the files from disk std::set directories; typedef std::set::iterator iter_t; - for (torrent_info::file_iterator i = m_info->begin_files(true) - , end(m_info->end_files(true)); i != end; ++i) + for (file_storage::iterator i = files().begin() + , end(files().end()); i != end; ++i) { std::string p = (m_save_path / i->path).string(); fs::path bp = i->path.branch_path(); @@ -584,7 +622,7 @@ namespace libtorrent TORRENT_ASSERT(rd.type() == entry::dictionary_t); std::vector > file_sizes - = get_filesizes(*m_info, m_save_path); + = get_filesizes(files(), m_save_path); entry::list_type& fl = rd["file sizes"].list(); for (std::vector >::iterator i @@ -642,7 +680,7 @@ namespace libtorrent } entry::list_type const& slots = slots_ent->list(); - bool seed = int(slots.size()) == m_info->num_pieces() + bool seed = int(slots.size()) == m_files.num_pieces() && std::find_if(slots.begin(), slots.end() , boost::bind(std::less() , boost::bind((size_type const& (entry::*)() const) @@ -655,11 +693,11 @@ namespace libtorrent if (seed) { - if (m_info->num_files(true) != (int)file_sizes.size()) + if (m_files.num_files() != (int)file_sizes.size()) { error = "the number of files does not match the torrent (num: " + boost::lexical_cast(file_sizes.size()) + " actual: " - + boost::lexical_cast(m_info->num_files(true)) + ")"; + + boost::lexical_cast(m_files.num_files()) + ")"; return false; } @@ -667,8 +705,8 @@ namespace libtorrent fs = file_sizes.begin(); // the resume data says we have the entire torrent // make sure the file sizes are the right ones - for (torrent_info::file_iterator i = m_info->begin_files(true) - , end(m_info->end_files(true)); i != end; ++i, ++fs) + for (file_storage::iterator i = files().begin() + , end(files().end()); i != end; ++i, ++fs) { if (i->size != fs->first) { @@ -680,7 +718,7 @@ namespace libtorrent } } - return match_filesizes(*m_info, m_save_path, file_sizes + return match_filesizes(files(), m_save_path, file_sizes , !full_allocation_mode, &error); } @@ -716,14 +754,14 @@ namespace libtorrent return false; #endif - m_files.release(this); + m_pool.release(this); #if defined(_WIN32) && defined(UNICODE) && BOOST_VERSION >= 103400 - old_path = safe_convert((m_save_path / m_info->name()).string()); - new_path = safe_convert((save_path / m_info->name()).string()); + old_path = safe_convert((m_save_path / files().name()).string()); + new_path = safe_convert((save_path / files().name()).string()); #else - old_path = m_save_path / m_info->name(); - new_path = save_path / m_info->name(); + old_path = m_save_path / files().name(); + new_path = save_path / files().name(); #endif #ifndef BOOST_NO_EXCEPTIONS @@ -740,16 +778,20 @@ namespace libtorrent return true; #ifndef BOOST_NO_EXCEPTIONS } - catch (std::exception&) {} - return false; + catch (std::exception& e) + { + set_error((m_save_path / files().name()).string(), e.what()); + return true; + } #endif + return false; } #ifndef NDEBUG /* void storage::shuffle() { - int num_pieces = m_info->num_pieces(); + int num_pieces = files().num_pieces(); std::vector pieces(num_pieces); for (std::vector::iterator i = pieces.begin(); @@ -766,7 +808,7 @@ namespace libtorrent { const int slot_index = targets[i]; const int piece_index = pieces[i]; - const int slot_size =static_cast(m_info->piece_size(slot_index)); + const int slot_size =static_cast(m_files.piece_size(slot_index)); std::vector buf(slot_size); read(&buf[0], piece_index, 0, slot_size); write(&buf[0], slot_index, 0, slot_size); @@ -777,7 +819,7 @@ namespace libtorrent bool storage::move_slot(int src_slot, int dst_slot) { - int piece_size = m_info->piece_size(dst_slot); + int piece_size = m_files.piece_size(dst_slot); m_scratch_buffer.resize(piece_size); int ret1 = read_impl(&m_scratch_buffer[0], src_slot, 0, piece_size, true); int ret2 = write(&m_scratch_buffer[0], dst_slot, 0, piece_size); @@ -787,9 +829,9 @@ namespace libtorrent bool storage::swap_slots(int slot1, int slot2) { // the size of the target slot is the size of the piece - int piece_size = m_info->piece_length(); - int piece1_size = m_info->piece_size(slot2); - int piece2_size = m_info->piece_size(slot1); + int piece_size = m_files.piece_length(); + int piece1_size = m_files.piece_size(slot2); + int piece2_size = m_files.piece_size(slot1); m_scratch_buffer.resize(piece_size * 2); int ret1 = read_impl(&m_scratch_buffer[0], slot1, 0, piece1_size, true); int ret2 = read_impl(&m_scratch_buffer[piece_size], slot2, 0, piece2_size, true); @@ -802,10 +844,10 @@ namespace libtorrent bool storage::swap_slots3(int slot1, int slot2, int slot3) { // the size of the target slot is the size of the piece - int piece_size = m_info->piece_length(); - int piece1_size = m_info->piece_size(slot2); - int piece2_size = m_info->piece_size(slot3); - int piece3_size = m_info->piece_size(slot1); + int piece_size = m_files.piece_length(); + int piece1_size = m_files.piece_size(slot2); + int piece2_size = m_files.piece_size(slot3); + int piece3_size = m_files.piece_size(slot1); m_scratch_buffer.resize(piece_size * 2); int ret1 = read_impl(&m_scratch_buffer[0], slot1, 0, piece1_size, true); int ret2 = read_impl(&m_scratch_buffer[piece_size], slot2, 0, piece2_size, true); @@ -835,25 +877,25 @@ namespace libtorrent , bool fill_zero) { TORRENT_ASSERT(buf != 0); - TORRENT_ASSERT(slot >= 0 && slot < m_info->num_pieces()); + TORRENT_ASSERT(slot >= 0 && slot < m_files.num_pieces()); TORRENT_ASSERT(offset >= 0); - TORRENT_ASSERT(offset < m_info->piece_size(slot)); + TORRENT_ASSERT(offset < m_files.piece_size(slot)); TORRENT_ASSERT(size > 0); #ifndef NDEBUG std::vector slices - = m_info->map_block(slot, offset, size, true); + = files().map_block(slot, offset, size); TORRENT_ASSERT(!slices.empty()); #endif - size_type start = slot * (size_type)m_info->piece_length() + offset; - TORRENT_ASSERT(start + size <= m_info->total_size()); + size_type start = slot * (size_type)m_files.piece_length() + offset; + TORRENT_ASSERT(start + size <= m_files.total_size()); // find the file iterator and file offset size_type file_offset = start; std::vector::const_iterator file_iter; - for (file_iter = m_info->begin_files(true);;) + for (file_iter = files().begin();;) { if (file_offset < file_iter->size) break; @@ -864,7 +906,7 @@ namespace libtorrent int buf_pos = 0; std::string error; - boost::shared_ptr in(m_files.open_file( + boost::shared_ptr in(m_pool.open_file( this, m_save_path / file_iter->path, file::in , error)); if (!in) @@ -899,7 +941,7 @@ namespace libtorrent #endif int left_to_read = size; - int slot_size = static_cast(m_info->piece_size(slot)); + int slot_size = static_cast(m_files.piece_size(slot)); if (offset + left_to_read > slot_size) left_to_read = slot_size - offset; @@ -924,7 +966,7 @@ namespace libtorrent TORRENT_ASSERT(int(slices.size()) > counter); size_type slice_size = slices[counter].size; TORRENT_ASSERT(slice_size == read_bytes); - TORRENT_ASSERT(m_info->file_at(slices[counter].file_index, true).path + TORRENT_ASSERT(files().at(slices[counter].file_index).path == file_iter->path); #endif @@ -961,7 +1003,7 @@ namespace libtorrent file_offset = 0; std::string error; - in = m_files.open_file( + in = m_pool.open_file( this, path, file::in, error); if (!in) { @@ -997,35 +1039,35 @@ namespace libtorrent { TORRENT_ASSERT(buf != 0); TORRENT_ASSERT(slot >= 0); - TORRENT_ASSERT(slot < m_info->num_pieces()); + TORRENT_ASSERT(slot < m_files.num_pieces()); TORRENT_ASSERT(offset >= 0); TORRENT_ASSERT(size > 0); #ifndef NDEBUG std::vector slices - = m_info->map_block(slot, offset, size, true); + = files().map_block(slot, offset, size); TORRENT_ASSERT(!slices.empty()); #endif - size_type start = slot * (size_type)m_info->piece_length() + offset; + size_type start = slot * (size_type)m_files.piece_length() + offset; // find the file iterator and file offset size_type file_offset = start; std::vector::const_iterator file_iter; - for (file_iter = m_info->begin_files(true);;) + for (file_iter = files().begin();;) { if (file_offset < file_iter->size) break; file_offset -= file_iter->size; ++file_iter; - TORRENT_ASSERT(file_iter != m_info->end_files(true)); + TORRENT_ASSERT(file_iter != files().end()); } fs::path p(m_save_path / file_iter->path); std::string error; - boost::shared_ptr out = m_files.open_file( + boost::shared_ptr out = m_pool.open_file( this, p, file::out | file::in, error); if (!out) @@ -1050,7 +1092,7 @@ namespace libtorrent } int left_to_write = size; - int slot_size = static_cast(m_info->piece_size(slot)); + int slot_size = static_cast(m_files.piece_size(slot)); if (offset + left_to_write > slot_size) left_to_write = slot_size - offset; @@ -1074,7 +1116,7 @@ namespace libtorrent { TORRENT_ASSERT(int(slices.size()) > counter); TORRENT_ASSERT(slices[counter].size == write_bytes); - TORRENT_ASSERT(m_info->file_at(slices[counter].file_index, true).path + TORRENT_ASSERT(files().at(slices[counter].file_index).path == file_iter->path); TORRENT_ASSERT(buf_pos >= 0); @@ -1101,11 +1143,11 @@ namespace libtorrent #endif ++file_iter; - TORRENT_ASSERT(file_iter != m_info->end_files(true)); + TORRENT_ASSERT(file_iter != files().end()); fs::path p = m_save_path / file_iter->path; file_offset = 0; std::string error; - out = m_files.open_file( + out = m_pool.open_file( this, p, file::out | file::in, error); if (!out) @@ -1131,25 +1173,26 @@ namespace libtorrent return size; } - storage_interface* default_storage_constructor(boost::intrusive_ptr ti + storage_interface* default_storage_constructor(file_storage const& fs , fs::path const& path, file_pool& fp) { - return new storage(ti, path, fp); + return new storage(fs, path, fp); } // -- piece_manager ----------------------------------------------------- piece_manager::piece_manager( boost::shared_ptr const& torrent - , boost::intrusive_ptr ti + , boost::intrusive_ptr info , fs::path const& save_path , file_pool& fp , disk_io_thread& io , storage_constructor_type sc , storage_mode_t sm) - : m_storage(sc(ti, save_path, fp)) + : m_info(info) + , m_files(m_info->files()) + , m_storage(sc(m_files, save_path, fp)) , m_storage_mode(sm) - , m_info(ti) , m_save_path(complete(save_path)) , m_state(state_none) , m_current_slot(0) @@ -1213,6 +1256,17 @@ namespace libtorrent m_io_thread.add_job(j, handler); } + void piece_manager::async_rename_file(int index, std::string const& name + , boost::function const& handler) + { + disk_io_job j; + j.storage = this; + j.piece = index; + j.str = name; + j.action = disk_io_job::rename_file; + m_io_thread.add_job(j, handler); + } + void piece_manager::async_check_files( boost::function const& handler) { @@ -1241,6 +1295,8 @@ namespace libtorrent m_io_thread.add_job(j, handler); #ifndef NDEBUG boost::recursive_mutex::scoped_lock l(m_mutex); + // if this assert is hit, it suggests + // that check_files was not successful TORRENT_ASSERT(slot_for(r.piece) >= 0); #endif } @@ -1295,7 +1351,7 @@ namespace libtorrent int slot = slot_for(piece); TORRENT_ASSERT(slot != has_no_slot); - return m_storage->hash_for_slot(slot, ph, m_info->piece_size(piece)); + return m_storage->hash_for_slot(slot, ph, m_files.piece_size(piece)); } bool piece_manager::move_storage_impl(fs::path const& save_path) @@ -1373,7 +1429,7 @@ namespace libtorrent TORRENT_ASSERT(buf); TORRENT_ASSERT(offset >= 0); TORRENT_ASSERT(size > 0); - TORRENT_ASSERT(piece_index >= 0 && piece_index < m_info->num_pieces()); + TORRENT_ASSERT(piece_index >= 0 && piece_index < m_files.num_pieces()); int slot = allocate_slot_for_piece(piece_index); int ret = m_storage->write(buf, slot, offset, size); @@ -1455,9 +1511,9 @@ namespace libtorrent { // INVARIANT_CHECK; - const int piece_size = static_cast(m_info->piece_length()); - const int last_piece_size = static_cast(m_info->piece_size( - m_info->num_pieces() - 1)); + const int piece_size = static_cast(m_files.piece_length()); + const int last_piece_size = static_cast(m_files.piece_size( + m_files.num_pieces() - 1)); TORRENT_ASSERT((int)piece_data.size() >= last_piece_size); @@ -1579,8 +1635,8 @@ namespace libtorrent int piece_manager::check_no_fastresume(std::string& error) { - torrent_info::file_iterator i = m_info->begin_files(true); - torrent_info::file_iterator end = m_info->end_files(true); + file_storage::iterator i = m_files.begin(); + file_storage::iterator end = m_files.end(); for (; i != end; ++i) { @@ -1612,9 +1668,9 @@ namespace libtorrent { m_state = state_full_check; m_piece_to_slot.clear(); - m_piece_to_slot.resize(m_info->num_pieces(), has_no_slot); + m_piece_to_slot.resize(m_files.num_pieces(), has_no_slot); m_slot_to_piece.clear(); - m_slot_to_piece.resize(m_info->num_pieces(), unallocated); + m_slot_to_piece.resize(m_files.num_pieces(), unallocated); if (m_storage_mode == storage_mode_compact) { m_unallocated_slots.clear(); @@ -1629,12 +1685,12 @@ namespace libtorrent // in compact mode without checking, we need to // populate the unallocated list TORRENT_ASSERT(m_unallocated_slots.empty()); - for (int i = 0, end(m_info->num_pieces()); i < end; ++i) + for (int i = 0, end(m_files.num_pieces()); i < end; ++i) m_unallocated_slots.push_back(i); m_piece_to_slot.clear(); - m_piece_to_slot.resize(m_info->num_pieces(), has_no_slot); + m_piece_to_slot.resize(m_files.num_pieces(), has_no_slot); m_slot_to_piece.clear(); - m_slot_to_piece.resize(m_info->num_pieces(), unallocated); + m_slot_to_piece.resize(m_files.num_pieces(), unallocated); } return check_init_storage(error); @@ -1675,7 +1731,7 @@ namespace libtorrent INVARIANT_CHECK; - TORRENT_ASSERT(m_info->piece_length() > 0); + TORRENT_ASSERT(m_files.piece_length() > 0); // if we don't have any resume data, return if (rd.type() == entry::undefined_t) return check_no_fastresume(error); @@ -1686,38 +1742,11 @@ namespace libtorrent return check_no_fastresume(error); } - entry const* file_format = rd.find_key("file-format"); - - if (file_format == 0 || file_format->type() != entry::string_t) - { - error = "missing file format tag"; - return check_no_fastresume(error); - } - - if (file_format->string() != "libtorrent resume file") - { - error = "invalid file format tag"; - return check_no_fastresume(error); - } - - entry const* info_hash = rd.find_key("info-hash"); - if (info_hash == 0 || info_hash->type() != entry::string_t) - { - error = "missing info-hash"; - return check_no_fastresume(error); - } - - if (sha1_hash(info_hash->string()) != m_info->info_hash()) - { - error = "mismatching info-hash"; - return check_no_fastresume(error); - } - - int block_size = (std::min)(16 * 1024, m_info->piece_length()); + int block_size = (std::min)(16 * 1024, m_files.piece_length()); entry const* blocks_per_piece_ent = rd.find_key("blocks per piece"); if (blocks_per_piece_ent != 0 && blocks_per_piece_ent->type() == entry::int_t - && blocks_per_piece_ent->integer() != m_info->piece_length() / block_size) + && blocks_per_piece_ent->integer() != m_files.piece_length() / block_size) { error = "invalid 'blocks per piece' entry"; return check_no_fastresume(error); @@ -1746,17 +1775,17 @@ namespace libtorrent return check_no_fastresume(error); } - if ((int)slots->list().size() > m_info->num_pieces()) + if ((int)slots->list().size() > m_files.num_pieces()) { error = "file has more slots than torrent (slots: " + boost::lexical_cast(slots->list().size()) + " size: " - + boost::lexical_cast(m_info->num_pieces()) + " )"; + + boost::lexical_cast(m_files.num_pieces()) + " )"; return check_no_fastresume(error); } if (storage_mode == storage_mode_compact) { - int num_pieces = int(m_info->num_pieces()); + int num_pieces = int(m_files.num_pieces()); m_slot_to_piece.resize(num_pieces, unallocated); m_piece_to_slot.resize(num_pieces, has_no_slot); int slot = 0; @@ -1877,7 +1906,7 @@ namespace libtorrent // is finished int piece_manager::check_files(int& current_slot, int& have_piece, std::string& error) { - TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_info->num_pieces()); + TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_files.num_pieces()); current_slot = m_current_slot; have_piece = -1; @@ -1894,9 +1923,9 @@ namespace libtorrent if (other_piece >= 0) { if (m_scratch_buffer2.empty()) - m_scratch_buffer2.resize(m_info->piece_length()); + m_scratch_buffer2.resize(m_files.piece_length()); - int piece_size = m_info->piece_size(other_piece); + int piece_size = m_files.piece_size(other_piece); if (m_storage->read(&m_scratch_buffer2[0], piece, 0, piece_size) != piece_size) { @@ -1910,7 +1939,7 @@ namespace libtorrent // the slot where this piece belongs is // free. Just move the piece there. - int piece_size = m_info->piece_size(piece); + int piece_size = m_files.piece_size(piece); if (m_storage->write(&m_scratch_buffer[0], piece, 0, piece_size) != piece_size) { error = m_storage->error(); @@ -1926,19 +1955,19 @@ namespace libtorrent return need_full_check; } - while (m_current_slot < m_info->num_pieces() + while (m_current_slot < m_files.num_pieces() && (m_slot_to_piece[m_current_slot] == m_current_slot || m_slot_to_piece[m_current_slot] < 0)) { ++m_current_slot; } - if (m_current_slot == m_info->num_pieces()) + if (m_current_slot == m_files.num_pieces()) { return check_init_storage(error); } - TORRENT_ASSERT(m_current_slot < m_info->num_pieces()); + TORRENT_ASSERT(m_current_slot < m_files.num_pieces()); int piece = m_slot_to_piece[m_current_slot]; TORRENT_ASSERT(piece >= 0); @@ -1949,9 +1978,9 @@ namespace libtorrent // where this one goes. Store it in the scratch // buffer until next iteration. if (m_scratch_buffer.empty()) - m_scratch_buffer.resize(m_info->piece_length()); + m_scratch_buffer.resize(m_files.piece_length()); - int piece_size = m_info->piece_size(other_piece); + int piece_size = m_files.piece_size(other_piece); if (m_storage->read(&m_scratch_buffer[0], piece, 0, piece_size) != piece_size) { error = m_storage->error(); @@ -1975,7 +2004,7 @@ namespace libtorrent TORRENT_ASSERT(m_state == state_full_check); bool skip = check_one_piece(have_piece); - TORRENT_ASSERT(m_current_slot <= m_info->num_pieces()); + TORRENT_ASSERT(m_current_slot <= m_files.num_pieces()); if (skip) { @@ -1984,9 +2013,9 @@ namespace libtorrent // completely. We should skip all pieces belonging to that file. // find the file that failed, and skip all the pieces in that file size_type file_offset = 0; - size_type current_offset = size_type(m_current_slot) * m_info->piece_length(); - for (torrent_info::file_iterator i = m_info->begin_files(true); - i != m_info->end_files(true); ++i) + size_type current_offset = size_type(m_current_slot) * m_files.piece_length(); + for (file_storage::iterator i = m_files.begin() + , end(m_files.end()); i != end; ++i) { file_offset += i->size; if (file_offset > current_offset) break; @@ -1994,8 +2023,8 @@ namespace libtorrent TORRENT_ASSERT(file_offset > current_offset); int skip_blocks = static_cast( - (file_offset - current_offset + m_info->piece_length() - 1) - / m_info->piece_length()); + (file_offset - current_offset + m_files.piece_length() - 1) + / m_files.piece_length()); if (m_storage_mode == storage_mode_compact) { @@ -2008,15 +2037,15 @@ namespace libtorrent // current slot will increase by one at the end of the for-loop too m_current_slot += skip_blocks - 1; - TORRENT_ASSERT(m_current_slot <= m_info->num_pieces()); + TORRENT_ASSERT(m_current_slot <= m_files.num_pieces()); } ++m_current_slot; current_slot = m_current_slot; - if (m_current_slot >= m_info->num_pieces()) + if (m_current_slot >= m_files.num_pieces()) { - TORRENT_ASSERT(m_current_slot == m_info->num_pieces()); + TORRENT_ASSERT(m_current_slot == m_files.num_pieces()); // clear the memory we've been using std::vector().swap(m_piece_data); @@ -2059,19 +2088,19 @@ namespace libtorrent // DO THE FULL CHECK // ------------------------ - TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_info->num_pieces()); - TORRENT_ASSERT(int(m_slot_to_piece.size()) == m_info->num_pieces()); + TORRENT_ASSERT(int(m_piece_to_slot.size()) == m_files.num_pieces()); + TORRENT_ASSERT(int(m_slot_to_piece.size()) == m_files.num_pieces()); TORRENT_ASSERT(have_piece == -1); // initialization for the full check if (m_hash_to_piece.empty()) { - for (int i = 0; i < m_info->num_pieces(); ++i) + for (int i = 0; i < m_files.num_pieces(); ++i) m_hash_to_piece.insert(std::make_pair(m_info->hash_for_piece(i), i)); } - m_piece_data.resize(int(m_info->piece_length())); - int piece_size = m_info->piece_size(m_current_slot); + m_piece_data.resize(int(m_files.piece_length())); + int piece_size = m_files.piece_size(m_current_slot); int num_read = m_storage->read(&m_piece_data[0] , m_current_slot, 0, piece_size); @@ -2348,7 +2377,7 @@ namespace libtorrent // special case to make sure we don't use the last slot // when we shouldn't, since it's smaller than ordinary slots - if (*iter == m_info->num_pieces() - 1 && piece_index != *iter) + if (*iter == m_files.num_pieces() - 1 && piece_index != *iter) { if (m_free_slots.size() == 1) allocate_slots(1); @@ -2481,14 +2510,14 @@ namespace libtorrent { boost::recursive_mutex::scoped_lock lock(m_mutex); - TORRENT_ASSERT(m_current_slot <= m_info->num_pieces()); + TORRENT_ASSERT(m_current_slot <= m_files.num_pieces()); if (m_unallocated_slots.empty() && m_free_slots.empty() && m_state == state_finished) { TORRENT_ASSERT(m_storage_mode != storage_mode_compact - || m_info->num_pieces() == 0); + || m_files.num_pieces() == 0); } if (m_storage_mode != storage_mode_compact) @@ -2508,8 +2537,8 @@ namespace libtorrent { if (m_piece_to_slot.empty()) return; - TORRENT_ASSERT((int)m_piece_to_slot.size() == m_info->num_pieces()); - TORRENT_ASSERT((int)m_slot_to_piece.size() == m_info->num_pieces()); + TORRENT_ASSERT((int)m_piece_to_slot.size() == m_files.num_pieces()); + TORRENT_ASSERT((int)m_slot_to_piece.size() == m_files.num_pieces()); for (std::vector::const_iterator i = m_free_slots.begin(); i != m_free_slots.end(); ++i) @@ -2531,7 +2560,7 @@ namespace libtorrent == m_unallocated_slots.end()); } - for (int i = 0; i < m_info->num_pieces(); ++i) + for (int i = 0; i < m_files.num_pieces(); ++i) { // Check domain of piece_to_slot's elements if (m_piece_to_slot[i] != has_no_slot) @@ -2620,7 +2649,7 @@ namespace libtorrent s << "index\tslot\tpiece\n"; - for (int i = 0; i < m_info->num_pieces(); ++i) + for (int i = 0; i < m_files.num_pieces(); ++i) { s << i << "\t" << m_slot_to_piece[i] << "\t"; s << m_piece_to_slot[i] << "\n"; diff --git a/src/torrent.cpp b/src/torrent.cpp index 2a0431f1d..3a09fe9ab 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -424,8 +424,34 @@ namespace libtorrent m_state = torrent_status::queued_for_checking; - if (m_resume_data.type() == entry::dictionary_t) read_resume_data(m_resume_data); - + if (m_resume_data.type() == entry::dictionary_t) + { + char const* error = 0; + entry const* file_format = m_resume_data.find_key("file-format"); + if (file_format->string() != "libtorrent resume file") + error = "invalid file format tag"; + + entry const* info_hash = m_resume_data.find_key("info-hash"); + if (!error && (info_hash == 0 || info_hash->type() != entry::string_t)) + error = "missing info-hash"; + + if (!error && sha1_hash(info_hash->string()) != m_torrent_file->info_hash()) + error = "mismatching info-hash"; + + if (error && m_ses.m_alerts.should_post(alert::warning)) + { + m_ses.m_alerts.post_alert(fastresume_rejected_alert(get_handle(), error)); +#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING + (*m_ses.m_logger) << "fastresume data for " + << torrent_file().name() << " rejected: " + << error << "\n"; +#endif + } + + if (error) m_resume_data = entry(); + else read_resume_data(m_resume_data); + } + m_storage->async_check_fastresume(&m_resume_data , bind(&torrent::on_resume_data_checked , shared_from_this(), _1, _2)); @@ -1425,6 +1451,21 @@ namespace libtorrent } } + void torrent::on_file_renamed(int ret, disk_io_job const& j) + { + session_impl::mutex_t::scoped_lock l(m_ses.m_mutex); + + if (alerts().should_post(alert::warning)) + { + + if (ret == 0) + alerts().post_alert(file_renamed_alert(get_handle(), j.str + , "renamed file: " + j.str)); + else + alerts().post_alert(file_renamed_alert(get_handle(), "", j.str)); + } + } + void torrent::on_torrent_paused(int ret, disk_io_job const& j) { session_impl::mutex_t::scoped_lock l(m_ses.m_mutex); @@ -1619,7 +1660,7 @@ namespace libtorrent for (int i = 0; i < int(files.size()); ++i) { size_type start = position; - size_type size = m_torrent_file->file_at(i).size; + size_type size = m_torrent_file->files().at(i).size; if (size == 0) continue; position += size; // mark all pieces of the file with this file's priority @@ -1750,7 +1791,7 @@ namespace libtorrent for (int i = 0; i < (int)bitmask.size(); ++i) { size_type start = position; - position += m_torrent_file->file_at(i).size; + position += m_torrent_file->files().at(i).size; // is the file selected for download? if (!bitmask[i]) { @@ -3148,6 +3189,20 @@ namespace libtorrent return m_save_path; } + bool torrent::rename_file(int index, std::string const& name) + { + INVARIANT_CHECK; + + TORRENT_ASSERT(index >= 0); + TORRENT_ASSERT(index < m_torrent_file->num_files()); + + if (!m_owning_storage.get()) return false; + + m_owning_storage->async_rename_file(index, name + , bind(&torrent::on_file_renamed, shared_from_this(), _1, _2)); + return true; + } + void torrent::move_storage(fs::path const& save_path) { INVARIANT_CHECK; @@ -3770,8 +3825,8 @@ namespace libtorrent for (int i = 0; i < m_torrent_file->num_files(); ++i) { - peer_request ret = m_torrent_file->map_file(i, 0, 0); - size_type size = m_torrent_file->file_at(i).size; + peer_request ret = m_torrent_file->files().map_file(i, 0, 0); + size_type size = m_torrent_file->files().at(i).size; // zero sized files are considered // 100% done all the time @@ -3793,7 +3848,7 @@ namespace libtorrent } TORRENT_ASSERT(size == 0); - fp[i] = static_cast(done) / m_torrent_file->file_at(i).size; + fp[i] = static_cast(done) / m_torrent_file->files().at(i).size; } } diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 79cac2e7d..adb242124 100755 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -215,6 +215,12 @@ namespace libtorrent TORRENT_FORWARD(move_storage(save_path)); } + void torrent_handle::rename_file(int index, fs::path const& new_name) const + { + INVARIANT_CHECK; + TORRENT_FORWARD(rename_file(index, new_name.string())); + } + void torrent_handle::add_extension( boost::function(torrent*, void*)> const& ext , void* userdata) diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index c7dd28096..8afd2e408 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -1,6 +1,6 @@ /* -Copyright (c) 2008, Arvid Norberg +Copyright (c) 2003-2008, Arvid Norberg All rights reserved. Redistribution and use in source and binary forms, with or without @@ -153,11 +153,7 @@ namespace // save the original encoding and replace the // commonly used path with the correctly // encoded string - if (!valid_encoding) - { - target.orig_path.reset(new fs::path(target.path)); - target.path = tmp_path; - } + if (!valid_encoding) target.path = tmp_path; } bool extract_single_file(lazy_entry const& dict, file_entry& target @@ -194,40 +190,26 @@ namespace return true; } - bool extract_files(lazy_entry const& list, std::vector& target + bool extract_files(lazy_entry const& list, file_storage& target , std::string const& root_dir) { - size_type offset = 0; if (list.type() != lazy_entry::list_t) return false; for (int i = 0, end(list.list_size()); i < end; ++i) { - target.push_back(file_entry()); - if (!extract_single_file(*list.list_at(i), target.back(), root_dir)) + file_entry e; + if (!extract_single_file(*list.list_at(i), e, root_dir)) return false; - target.back().offset = offset; - offset += target.back().size; + target.add_file(e); } return true; } -/* - void remove_dir(fs::path& p) - { - TORRENT_ASSERT(p.begin() != p.end()); - path tmp; - for (path::iterator i = boost::next(p.begin()); i != p.end(); ++i) - tmp /= *i; - p = tmp; - } -*/ } namespace libtorrent { - // standard constructor that parses a torrent file torrent_info::torrent_info(entry const& torrent_file) - : m_num_pieces(0) - , m_creation_date(pt::ptime(pt::not_a_date_time)) + : m_creation_date(pt::ptime(pt::not_a_date_time)) , m_multifile(false) , m_private(false) , m_info_section_size(0) @@ -249,8 +231,7 @@ namespace libtorrent } torrent_info::torrent_info(lazy_entry const& torrent_file) - : m_num_pieces(0) - , m_creation_date(pt::ptime(pt::not_a_date_time)) + : m_creation_date(pt::ptime(pt::not_a_date_time)) , m_multifile(false) , m_private(false) , m_info_section_size(0) @@ -266,8 +247,7 @@ namespace libtorrent } torrent_info::torrent_info(char const* buffer, int size) - : m_num_pieces(0) - , m_creation_date(pt::ptime(pt::not_a_date_time)) + : m_creation_date(pt::ptime(pt::not_a_date_time)) , m_multifile(false) , m_private(false) , m_info_section_size(0) @@ -289,22 +269,16 @@ namespace libtorrent // just the necessary to use it with piece manager // used for torrents with no metadata torrent_info::torrent_info(sha1_hash const& info_hash) - : m_piece_length(0) - , m_total_size(0) - , m_num_pieces(0) - , m_info_hash(info_hash) - , m_name() + : m_info_hash(info_hash) , m_creation_date(pt::second_clock::universal_time()) , m_multifile(false) , m_private(false) , m_info_section_size(0) , m_piece_hashes(0) - { - } + {} torrent_info::torrent_info(char const* filename) - : m_num_pieces(0) - , m_creation_date(pt::ptime(pt::not_a_date_time)) + : m_creation_date(pt::ptime(pt::not_a_date_time)) , m_multifile(false) , m_private(false) { @@ -335,11 +309,8 @@ namespace libtorrent m_urls.swap(ti.m_urls); m_url_seeds.swap(ti.m_url_seeds); m_files.swap(ti.m_files); - m_files.swap(ti.m_remapped_files); m_nodes.swap(ti.m_nodes); - swap(m_num_pieces, ti.m_num_pieces); swap(m_info_hash, ti.m_info_hash); - m_name.swap(ti.m_name); swap(m_creation_date, ti.m_creation_date); m_comment.swap(ti.m_comment); m_created_by.swap(ti.m_created_by); @@ -373,27 +344,27 @@ namespace libtorrent TORRENT_ASSERT(section.first[m_info_section_size-1] == 'e'); // extract piece length - m_piece_length = info.dict_find_int_value("piece length", -1); - if (m_piece_length <= 0) + int piece_length = info.dict_find_int_value("piece length", -1); + if (piece_length <= 0) { error = "invalid or missing 'piece length' entry in torrent file"; return false; } + m_files.set_piece_length(piece_length); // extract file name (or the directory name if it's a multifile libtorrent) - m_name = info.dict_find_string_value("name.utf-8"); - if (m_name.empty()) m_name = info.dict_find_string_value("name"); - - if (m_name.empty()) + std::string name = info.dict_find_string_value("name.utf-8"); + if (name.empty()) name = info.dict_find_string_value("name"); + if (name.empty()) { - error = "invalid name in torrent file"; + error = "missing name in torrent file"; return false; } - fs::path tmp = m_name; + fs::path tmp = name; if (tmp.is_complete()) { - m_name = tmp.leaf(); + name = tmp.leaf(); } else if (tmp.has_branch_path()) { @@ -404,9 +375,9 @@ namespace libtorrent if (*i == "." || *i == "..") continue; p /= *i; } - m_name = p.string(); + name = p.string(); } - if (m_name == ".." || m_name == ".") + if (name == ".." || name == ".") { error = "invalid 'name' of torrent (possible exploit attempt)"; return false; @@ -419,7 +390,7 @@ namespace libtorrent // if there's no list of files, there has to be a length // field. file_entry e; - e.path = m_name; + e.path = name; e.offset = 0; e.size = info.dict_find_int_value("length", -1); if (e.size < 0) @@ -427,29 +398,26 @@ namespace libtorrent error = "invalid length of torrent"; return false; } - m_files.push_back(e); + m_files.add_file(e); m_multifile = false; } else { - if (!extract_files(*i, m_files, m_name)) + if (!extract_files(*i, m_files, name)) { error = "failed to parse files from torrent file"; return false; } m_multifile = true; } - - // calculate total size of all pieces - m_total_size = 0; - for (std::vector::iterator i = m_files.begin(); i != m_files.end(); ++i) - m_total_size += i->size; + m_files.set_name(name); // extract sha-1 hashes for all pieces // we want this division to round upwards, that's why we have the // extra addition - m_num_pieces = static_cast((m_total_size + m_piece_length - 1) / m_piece_length); + m_files.set_num_pieces(int((m_files.total_size() + m_files.piece_length() - 1) + / m_files.piece_length())); lazy_entry const* pieces = info.dict_find("pieces"); if (pieces == 0 || pieces->type() != lazy_entry::string_t) @@ -458,7 +426,7 @@ namespace libtorrent return false; } - if (pieces->string_length() != m_num_pieces * 20) + if (pieces->string_length() != m_files.num_pieces() * 20) { error = "incorrect number of piece hashes in torrent file"; return false; @@ -616,122 +584,11 @@ namespace libtorrent os << "number of pieces: " << num_pieces() << "\n"; os << "piece length: " << piece_length() << "\n"; os << "files:\n"; - for (file_iterator i = begin_files(); i != end_files(); ++i) + for (file_storage::iterator i = m_files.begin(); i != m_files.end(); ++i) os << " " << std::setw(11) << i->size << " " << i->path.string() << "\n"; } // ------- end deprecation ------- - int torrent_info::piece_size(int index) const - { - TORRENT_ASSERT(index >= 0 && index < num_pieces()); - if (index == num_pieces()-1) - { - int size = int(total_size() - - size_type(num_pieces() - 1) * piece_length()); - TORRENT_ASSERT(size > 0); - TORRENT_ASSERT(size <= piece_length()); - return int(size); - } - else - return piece_length(); - } - - bool torrent_info::remap_files(std::vector const& map) - { - size_type offset = 0; - m_remapped_files.resize(map.size()); - - for (int i = 0; i < int(map.size()); ++i) - { - file_entry& fe = m_remapped_files[i]; - fe.path = map[i].path; - fe.offset = offset; - fe.size = map[i].size; - fe.file_base = map[i].file_base; - fe.orig_path.reset(); - offset += fe.size; - } - if (offset != total_size()) - { - m_remapped_files.clear(); - return false; - } - -#ifndef NDEBUG - std::vector map2(m_remapped_files); - std::sort(map2.begin(), map2.end() - , bind(&file_entry::file_base, _1) < bind(&file_entry::file_base, _2)); - std::stable_sort(map2.begin(), map2.end() - , bind(&file_entry::path, _1) < bind(&file_entry::path, _2)); - fs::path last_path; - size_type last_end = 0; - for (std::vector::iterator i = map2.begin(), end(map2.end()); - i != end; ++i) - { - if (last_path == i->path) - { - assert(last_end <= i->file_base); - } - last_end = i->file_base + i->size; - last_path = i->path; - } -#endif - - return true; - } - - std::vector torrent_info::map_block(int piece, size_type offset - , int size_, bool storage) const - { - TORRENT_ASSERT(num_files() > 0); - std::vector ret; - - size_type start = piece * (size_type)m_piece_length + offset; - size_type size = size_; - TORRENT_ASSERT(start + size <= m_total_size); - - // find the file iterator and file offset - // TODO: make a vector that can map piece -> file index in O(1) - size_type file_offset = start; - std::vector::const_iterator file_iter; - - int counter = 0; - for (file_iter = begin_files(storage);; ++counter, ++file_iter) - { - TORRENT_ASSERT(file_iter != end_files(storage)); - if (file_offset < file_iter->size) - { - file_slice f; - f.file_index = counter; - f.offset = file_offset + file_iter->file_base; - f.size = (std::min)(file_iter->size - file_offset, (size_type)size); - size -= f.size; - file_offset += f.size; - ret.push_back(f); - } - - TORRENT_ASSERT(size >= 0); - if (size <= 0) break; - - file_offset -= file_iter->size; - } - return ret; - } - - peer_request torrent_info::map_file(int file_index, size_type file_offset - , int size, bool storage) const - { - TORRENT_ASSERT(file_index < num_files(storage)); - TORRENT_ASSERT(file_index >= 0); - size_type offset = file_offset + file_at(file_index, storage).offset; - - peer_request ret; - ret.piece = int(offset / piece_length()); - ret.start = int(offset - ret.piece * piece_length()); - ret.length = size; - return ret; - } - } diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index ff96a163a..d641948b0 100755 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -242,7 +242,7 @@ namespace libtorrent } else { - std::vector files = info.map_block(r.piece, r.start + std::vector files = info.files().map_block(r.piece, r.start , r.length); for (std::vector::iterator i = files.begin(); @@ -254,13 +254,13 @@ namespace libtorrent if (using_proxy) { request += m_url; - std::string path = info.file_at(f.file_index).path.string(); + std::string path = info.files().at(f.file_index).path.string(); request += escape_path(path.c_str(), path.length()); } else { std::string path = m_path; - path += info.file_at(f.file_index).path.string(); + path += info.files().at(f.file_index).path.string(); request += escape_path(path.c_str(), path.length()); } request += " HTTP/1.1\r\n"; @@ -426,7 +426,7 @@ namespace libtorrent int file_index = m_file_requests.front(); torrent_info const& info = t->torrent_file(); - std::string path = info.file_at(file_index).path.string(); + std::string path = info.files().at(file_index).path.string(); path = escape_path(path.c_str(), path.length()); size_t i = location.rfind(path); if (i == std::string::npos) @@ -511,7 +511,7 @@ namespace libtorrent } int file_index = m_file_requests.front(); - peer_request in_range = info.map_file(file_index, range_start + peer_request in_range = info.files().map_file(file_index, range_start , int(range_end - range_start)); peer_request front_request = m_requests.front(); diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 018130f7c..6334b448a 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -170,10 +170,10 @@ boost::intrusive_ptr create_torrent(std::ostream* file) using namespace boost::filesystem; - libtorrent::create_torrent t; + file_storage fs; int total_size = 2 * 1024 * 1024; - t.add_file(path("temporary"), total_size); - t.set_piece_size(16 * 1024); + fs.add_file(path("temporary"), total_size); + libtorrent::create_torrent t(fs, 16 * 1024); t.add_tracker(tracker_url); std::vector piece(16 * 1024); diff --git a/test/test_storage.cpp b/test/test_storage.cpp index e2e72d1f4..1bcbc5f42 100644 --- a/test/test_storage.cpp +++ b/test/test_storage.cpp @@ -51,9 +51,11 @@ void on_check_files(int ret, disk_io_job const& j) } void run_storage_tests(boost::intrusive_ptr info + , file_storage& fs , path const& test_path , libtorrent::storage_mode_t storage_mode) { + TORRENT_ASSERT(fs.num_files() > 0); create_directory(test_path / "temp_storage"); int num_pieces = (1 + 612 + 17 + piece_size - 1) / piece_size; @@ -64,7 +66,7 @@ void run_storage_tests(boost::intrusive_ptr info { // avoid having two storages use the same files file_pool fp; boost::scoped_ptr s( - default_storage_constructor(info, test_path, fp)); + default_storage_constructor(fs, test_path, fp)); // write piece 1 (in slot 0) s->write(piece1, 0, 0, half); @@ -100,12 +102,17 @@ void run_storage_tests(boost::intrusive_ptr info entry frd; pm->async_check_fastresume(&frd, &on_check_resume_data); + ios.reset(); ios.run(); pm->async_check_files(&on_check_files); for (int i = 0; i < 4; ++i) + { + ios.reset(); ios.run_one(); + } + // test move_storage boost::function none; TEST_CHECK(exists(test_path / "temp_storage")); pm->async_move_storage(test_path / "temp_storage2", none); @@ -117,6 +124,15 @@ void run_storage_tests(boost::intrusive_ptr info TEST_CHECK(!exists(test_path / "temp_storage2/temp_storage")); remove_all(test_path / "temp_storage2"); + // test rename_file + remove(test_path / "part0"); + TEST_CHECK(exists(test_path / "temp_storage/test1.tmp")); + TEST_CHECK(!exists(test_path / "part0")); + pm->async_rename_file(0, "part0", none); + test_sleep(2000); + TEST_CHECK(!exists(test_path / "temp_storage/test1.tmp")); + TEST_CHECK(exists(test_path / "part0")); + peer_request r; r.piece = 0; r.start = 0; @@ -128,6 +144,10 @@ void run_storage_tests(boost::intrusive_ptr info pm->async_read(r, bind(&on_read_piece, _1, _2, piece2, piece_size)); pm->async_release_files(none); + pm->async_rename_file(0, "temp_storage/test1.tmp", none); + test_sleep(1000); + TEST_CHECK(!exists(test_path / "part0")); + ios.run(); io.join(); @@ -136,13 +156,13 @@ void run_storage_tests(boost::intrusive_ptr info void test_remove(path const& test_path) { - libtorrent::create_torrent t; - t.set_piece_size(4); - t.add_file("temp_storage/test1.tmp", 8); - t.add_file("temp_storage/folder1/test2.tmp", 8); - t.add_file("temp_storage/folder2/test3.tmp", 0); - t.add_file("temp_storage/_folder3/test4.tmp", 0); - t.add_file("temp_storage/_folder3/subfolder/test5.tmp", 8); + file_storage fs; + fs.add_file("temp_storage/test1.tmp", 8); + fs.add_file("temp_storage/folder1/test2.tmp", 8); + fs.add_file("temp_storage/folder2/test3.tmp", 0); + fs.add_file("temp_storage/_folder3/test4.tmp", 0); + fs.add_file("temp_storage/_folder3/subfolder/test5.tmp", 8); + libtorrent::create_torrent t(fs, 4); char buf[4] = {0, 0, 0, 0}; sha1_hash h = hasher(buf, 4).final(); @@ -152,7 +172,7 @@ void test_remove(path const& test_path) file_pool fp; boost::scoped_ptr s( - default_storage_constructor(info, test_path, fp)); + default_storage_constructor(fs, test_path, fp)); // allocate the files and create the directories s->initialize(true); @@ -172,14 +192,15 @@ void run_test(path const& test_path) boost::intrusive_ptr info; { - libtorrent::create_torrent t; - t.set_piece_size(piece_size); - t.add_file("temp_storage/test1.tmp", 17); - t.add_file("temp_storage/test2.tmp", 612); - t.add_file("temp_storage/test3.tmp", 0); - t.add_file("temp_storage/test4.tmp", 0); - t.add_file("temp_storage/test5.tmp", 1); + remove_all(test_path / "temp_storage"); + file_storage fs; + fs.add_file("temp_storage/test1.tmp", 17); + fs.add_file("temp_storage/test2.tmp", 612); + fs.add_file("temp_storage/test3.tmp", 0); + fs.add_file("temp_storage/test4.tmp", 0); + fs.add_file("temp_storage/test5.tmp", 1); + libtorrent::create_torrent t(fs, piece_size); t.set_hash(0, hasher(piece0, piece_size).final()); t.set_hash(1, hasher(piece1, piece_size).final()); t.set_hash(2, hasher(piece2, piece_size).final()); @@ -188,7 +209,7 @@ void run_test(path const& test_path) info = new torrent_info(t.generate()); std::cerr << "=== test 1 ===" << std::endl; - run_storage_tests(info, test_path, storage_mode_compact); + run_storage_tests(info, fs, test_path, storage_mode_compact); // make sure the files have the correct size std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl; @@ -200,63 +221,39 @@ void run_test(path const& test_path) remove_all(test_path / "temp_storage"); } -// ============================================== - - // make sure remap_files works - std::vector map; - file_entry fe; - fe.path = "temp_storage/test.tmp"; - fe.size = 17; - fe.file_base = 612 + 1; - map.push_back(fe); - fe.path = "temp_storage/test.tmp"; - fe.size = 612 + 1; - fe.file_base = 0; - map.push_back(fe); - - bool ret = info->remap_files(map); - TEST_CHECK(ret); - - std::cerr << "=== test 2 ===" << std::endl; - - run_storage_tests(info, test_path, storage_mode_compact); - - std::cerr << file_size(test_path / "temp_storage" / "test.tmp") << std::endl; - TEST_CHECK(file_size(test_path / "temp_storage" / "test.tmp") == 17 + 612 + 1); - - remove_all(test_path / "temp_storage"); - // ============================================== { - libtorrent::create_torrent t; - t.set_piece_size(piece_size); - t.add_file("temp_storage/test1.tmp", 17 + 612 + 1); + file_storage fs; + fs.add_file("temp_storage/test1.tmp", 17 + 612 + 1); + libtorrent::create_torrent t(fs, piece_size); + TEST_CHECK(fs.begin()->path == "temp_storage/test1.tmp"); t.set_hash(0, hasher(piece0, piece_size).final()); t.set_hash(1, hasher(piece1, piece_size).final()); t.set_hash(2, hasher(piece2, piece_size).final()); + info = new torrent_info(t.generate()); std::cerr << "=== test 3 ===" << std::endl; - run_storage_tests(info, test_path, storage_mode_compact); + run_storage_tests(info, fs, test_path, storage_mode_compact); // 48 = piece_size * 3 TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 48); remove_all(test_path / "temp_storage"); - } // ============================================== std::cerr << "=== test 4 ===" << std::endl; - run_storage_tests(info, test_path, storage_mode_allocate); + run_storage_tests(info, fs, test_path, storage_mode_allocate); std::cerr << file_size(test_path / "temp_storage" / "test1.tmp") << std::endl; TEST_CHECK(file_size(test_path / "temp_storage" / "test1.tmp") == 17 + 612 + 1); remove_all(test_path / "temp_storage"); + } // ============================================== diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index f1e1704aa..aaa66a520 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -14,12 +14,12 @@ int test_main() session ses(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48130, 48140)); - libtorrent::create_torrent t; + file_storage fs; size_type file_size = 1 * 1024 * 1024 * 1024; - t.add_file("test_torrent/tmp1", file_size); - t.add_file("test_torrent/tmp2", file_size); - t.add_file("test_torrent/tmp3", file_size); - t.set_piece_size(4 * 1024 * 1024); + fs.add_file("test_torrent/tmp1", file_size); + fs.add_file("test_torrent/tmp2", file_size); + fs.add_file("test_torrent/tmp3", file_size); + libtorrent::create_torrent t(fs, 4 * 1024 * 1024); t.add_tracker("http://non-existing.com/announce"); std::vector piece(4 * 1024 * 1024); diff --git a/test/test_web_seed.cpp b/test/test_web_seed.cpp index 0e3b6b8ca..73192f428 100644 --- a/test/test_web_seed.cpp +++ b/test/test_web_seed.cpp @@ -15,22 +15,6 @@ using namespace boost::filesystem; using namespace libtorrent; -void add_files(libtorrent::create_torrent& t, path const& p, path const& l) -{ - if (l.leaf()[0] == '.') return; - path f(p / l); - if (is_directory(f)) - { - for (directory_iterator i(f), end; i != end; ++i) - add_files(t, p, l / i->leaf()); - } - else - { - std::cerr << "adding \"" << l.string() << "\"\n"; - t.add_file(l, file_size(f)); - } -} - // proxy: 0=none, 1=socks4, 2=socks5, 3=socks5_pw 4=http 5=http_pw void test_transfer(boost::intrusive_ptr torrent_file, int proxy) { @@ -87,9 +71,6 @@ int test_main() using namespace libtorrent; using namespace boost::filesystem; - libtorrent::create_torrent t; - t.add_url_seed("http://127.0.0.1:8000/"); - create_directory("test_torrent"); char random_data[300000]; std::srand(std::time(0)); @@ -102,7 +83,11 @@ int test_main() std::ofstream("./test_torrent/test6").write(random_data, 300000); std::ofstream("./test_torrent/test7").write(random_data, 300000); - add_files(t, complete("."), "test_torrent"); + file_storage fs; + add_files(fs, path("test_torrent")); + + libtorrent::create_torrent t(fs, 16 * 1024); + t.add_url_seed("http://127.0.0.1:8000/"); start_web_server(8000); @@ -111,20 +96,17 @@ int test_main() std::vector buf(t.piece_length()); file_pool fp; - boost::intrusive_ptr torrent_file(new torrent_info(t.generate())); boost::scoped_ptr s(default_storage_constructor( - torrent_file, ".", fp)); + fs, ".", fp)); for (int i = 0; i < num; ++i) { - s->read(&buf[0], i, 0, t.piece_size(i)); - hasher h(&buf[0], t.piece_size(i)); + s->read(&buf[0], i, 0, fs.piece_size(i)); + hasher h(&buf[0], fs.piece_size(i)); t.set_hash(i, h.final()); } - entry e = t.generate(); - torrent_file = new torrent_info(e); - s.reset(default_storage_constructor(torrent_file, ".", fp)); + boost::intrusive_ptr torrent_file(new torrent_info(t.generate())); for (int i = 0; i < 6; ++i) test_transfer(torrent_file, i);