3
Copyright (c) 2003, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#ifndef TORRENT_TORRENT_INFO_HPP_INCLUDE
34
#define TORRENT_TORRENT_INFO_HPP_INCLUDE
42
#pragma warning(push, 1)
45
#include <boost/optional.hpp>
46
#include <boost/filesystem/path.hpp>
47
#include <boost/shared_ptr.hpp>
53
#include "libtorrent/entry.hpp"
54
#include "libtorrent/socket.hpp"
55
#include "libtorrent/peer_id.hpp"
56
#include "libtorrent/size_type.hpp"
57
#include "libtorrent/peer_request.hpp"
58
#include "libtorrent/config.hpp"
59
#include "libtorrent/time.hpp"
60
#include "libtorrent/intrusive_ptr_base.hpp"
61
#include "libtorrent/assert.hpp"
65
namespace pt = boost::posix_time;
66
namespace gr = boost::gregorian;
68
namespace fs = boost::filesystem;
70
struct TORRENT_EXPORT file_entry
72
file_entry(): offset(0), size(0), file_base(0) {}
75
size_type offset; // the offset of this file inside the torrent
76
size_type size; // the size of this file
77
// the offset in the file where the storage starts.
78
// This is always 0 unless parts of the torrent is
79
// compressed into a single file, such as a so-called part file.
81
// if the path was incorrectly encoded, this is
82
// the original corrupt encoded string. It is
83
// preserved in order to be able to reproduce
84
// the correct info-hash
85
boost::shared_ptr<const fs::path> orig_path;
88
struct TORRENT_EXPORT file_slice
95
struct TORRENT_EXPORT announce_entry
97
announce_entry(std::string const& u): url(u), tier(0) {}
102
struct TORRENT_EXPORT invalid_torrent_file: std::exception
104
virtual const char* what() const throw() { return "invalid torrent file"; }
107
class TORRENT_EXPORT torrent_info : public intrusive_ptr_base<torrent_info>
112
torrent_info(sha1_hash const& info_hash);
113
torrent_info(entry const& torrent_file);
116
entry create_torrent() const;
117
entry create_info_metadata() const;
118
void set_comment(char const* str);
119
void set_creator(char const* str);
120
void set_piece_size(int size);
121
void set_hash(int index, sha1_hash const& h);
122
void add_tracker(std::string const& url, int tier = 0);
123
void add_file(fs::path file, size_type size);
124
void add_url_seed(std::string const& url);
126
bool remap_files(std::vector<file_entry> const& map);
128
std::vector<file_slice> map_block(int piece, size_type offset
129
, int size, bool storage = false) const;
130
peer_request map_file(int file, size_type offset, int size
131
, bool storage = false) const;
133
std::vector<std::string> const& url_seeds() const
135
TORRENT_ASSERT(!m_half_metadata);
139
typedef std::vector<file_entry>::const_iterator file_iterator;
140
typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
142
// list the files in the torrent file
143
file_iterator begin_files(bool storage = false) const
145
if (!storage || m_remapped_files.empty())
146
return m_files.begin();
148
return m_remapped_files.begin();
151
file_iterator end_files(bool storage = false) const
153
if (!storage || m_remapped_files.empty())
154
return m_files.end();
156
return m_remapped_files.end();
159
reverse_file_iterator rbegin_files(bool storage = false) const
161
if (!storage || m_remapped_files.empty())
162
return m_files.rbegin();
164
return m_remapped_files.rbegin();
167
reverse_file_iterator rend_files(bool storage = false) const
169
if (!storage || m_remapped_files.empty())
170
return m_files.rend();
172
return m_remapped_files.rend();
175
int num_files(bool storage = false) const
177
TORRENT_ASSERT(m_piece_length > 0);
178
if (!storage || m_remapped_files.empty())
179
return (int)m_files.size();
181
return (int)m_remapped_files.size();
184
const file_entry& file_at(int index, bool storage = false) const
186
if (!storage || m_remapped_files.empty())
188
TORRENT_ASSERT(index >= 0 && index < (int)m_files.size());
189
return m_files[index];
193
TORRENT_ASSERT(index >= 0 && index < (int)m_remapped_files.size());
194
return m_remapped_files[index];
198
const std::vector<announce_entry>& trackers() const { return m_urls; }
200
size_type total_size() const { TORRENT_ASSERT(m_piece_length > 0); return m_total_size; }
201
int piece_length() const { TORRENT_ASSERT(m_piece_length > 0); return m_piece_length; }
202
int num_pieces() const { TORRENT_ASSERT(m_piece_length > 0); return m_num_pieces; }
203
const sha1_hash& info_hash() const { return m_info_hash; }
204
const std::string& name() const { TORRENT_ASSERT(m_piece_length > 0); return m_name; }
206
// ------- start deprecation -------
207
// this functionaily will be removed in a future version
208
void print(std::ostream& os) const TORRENT_DEPRECATED;
209
// ------- end deprecation -------
211
bool is_valid() const { return m_piece_length > 0; }
213
bool priv() const { return m_private; }
214
void set_priv(bool v) { m_private = v; }
216
void convert_file_names();
218
int piece_size(int index) const;
220
const sha1_hash& hash_for_piece(int index) const
222
TORRENT_ASSERT(index >= 0);
223
TORRENT_ASSERT(index < (int)m_piece_hash.size());
224
TORRENT_ASSERT(!m_half_metadata);
225
return m_piece_hash[index];
228
boost::optional<pt::ptime> creation_date() const;
230
const std::string& creator() const
231
{ return m_created_by; }
233
const std::string& comment() const
234
{ return m_comment; }
236
// dht nodes to add to the routing table/bootstrap from
237
typedef std::vector<std::pair<std::string, int> > nodes_t;
239
nodes_t const& nodes() const
241
TORRENT_ASSERT(!m_half_metadata);
245
void add_node(std::pair<std::string, int> const& node);
247
void parse_info_section(entry const& e);
249
entry const* extra(char const* key) const
250
{ return m_extra_info.find_key(key); }
252
// frees parts of the metadata that isn't
256
void swap(torrent_info& ti);
260
void read_torrent_info(const entry& libtorrent);
262
// the urls to the trackers
263
std::vector<announce_entry> m_urls;
265
std::vector<std::string> m_url_seeds;
267
// the length of one piece
268
// if this is 0, the torrent_info is
269
// in an uninitialized state
272
// the sha-1 hashes of each piece
273
std::vector<sha1_hash> m_piece_hash;
275
// the list of files that this torrent consists of
276
std::vector<file_entry> m_files;
278
// this vector is typically empty. If it is not
279
// empty, it means the user has re-mapped the
280
// files in this torrent to different names
281
// on disk. This is only used when reading and
283
std::vector<file_entry> m_remapped_files;
287
// the sum of all filesizes
288
size_type m_total_size;
290
// the number of pieces in the torrent
293
// the hash that identifies this torrent
294
// is mutable because it's calculated
296
mutable sha1_hash m_info_hash;
300
// if a creation date is found in the torrent file
301
// this will be set to that, otherwise it'll be
303
pt::ptime m_creation_date;
305
// if a comment is found in the torrent file
306
// this will be set to that comment
307
std::string m_comment;
309
// an optional string naming the software used
310
// to create the torrent file
311
std::string m_created_by;
313
// this is used when creating a torrent. If there's
314
// only one file there are cases where it's impossible
315
// to know if it should be written as a multifile torrent
316
// or not. e.g. test/test there's one file and one directory
317
// and they have the same name.
320
// this is true if the torrent is private. i.e., is should not
321
// be announced on the dht
324
// contains any non-parsed entries from the info-section
325
// these are kept in order to be able to accurately
326
// reproduce the info-section when sending the metadata
332
// this is set to true when seed_free() is called
333
bool m_half_metadata;
340
#endif // TORRENT_TORRENT_INFO_HPP_INCLUDED