~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/torrent_info.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (c) 2003, Arvid Norberg
 
4
All rights reserved.
 
5
 
 
6
Redistribution and use in source and binary forms, with or without
 
7
modification, are permitted provided that the following conditions
 
8
are met:
 
9
 
 
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.
 
18
 
 
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.
 
30
 
 
31
*/
 
32
 
 
33
#ifndef TORRENT_TORRENT_INFO_HPP_INCLUDE
 
34
#define TORRENT_TORRENT_INFO_HPP_INCLUDE
 
35
 
 
36
 
 
37
#include <string>
 
38
#include <vector>
 
39
#include <iosfwd>
 
40
 
 
41
#ifdef _MSC_VER
 
42
#pragma warning(push, 1)
 
43
#endif
 
44
 
 
45
#include <boost/optional.hpp>
 
46
#include <boost/filesystem/path.hpp>
 
47
#include <boost/shared_ptr.hpp>
 
48
 
 
49
#ifdef _MSC_VER
 
50
#pragma warning(pop)
 
51
#endif
 
52
 
 
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"
 
62
 
 
63
namespace libtorrent
 
64
{
 
65
        namespace pt = boost::posix_time;
 
66
        namespace gr = boost::gregorian;
 
67
 
 
68
        namespace fs = boost::filesystem;
 
69
 
 
70
        struct TORRENT_EXPORT file_entry
 
71
        {
 
72
                file_entry(): offset(0), size(0), file_base(0) {}
 
73
 
 
74
                fs::path path;
 
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.
 
80
                size_type file_base;
 
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;
 
86
        };
 
87
 
 
88
        struct TORRENT_EXPORT file_slice
 
89
        {
 
90
                int file_index;
 
91
                size_type offset;
 
92
                size_type size;
 
93
        };
 
94
 
 
95
        struct TORRENT_EXPORT announce_entry
 
96
        {
 
97
                announce_entry(std::string const& u): url(u), tier(0) {}
 
98
                std::string url;
 
99
                int tier;
 
100
        };
 
101
 
 
102
        struct TORRENT_EXPORT invalid_torrent_file: std::exception
 
103
        {
 
104
                virtual const char* what() const throw() { return "invalid torrent file"; }
 
105
        };
 
106
 
 
107
        class TORRENT_EXPORT torrent_info : public intrusive_ptr_base<torrent_info>
 
108
        {
 
109
        public:
 
110
 
 
111
                torrent_info();
 
112
                torrent_info(sha1_hash const& info_hash);
 
113
                torrent_info(entry const& torrent_file);
 
114
                ~torrent_info();
 
115
 
 
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);
 
125
 
 
126
                bool remap_files(std::vector<file_entry> const& map);
 
127
 
 
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;
 
132
                
 
133
                std::vector<std::string> const& url_seeds() const
 
134
                {
 
135
                        TORRENT_ASSERT(!m_half_metadata);
 
136
                        return m_url_seeds;
 
137
                }
 
138
 
 
139
                typedef std::vector<file_entry>::const_iterator file_iterator;
 
140
                typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
 
141
 
 
142
                // list the files in the torrent file
 
143
                file_iterator begin_files(bool storage = false) const
 
144
                {
 
145
                        if (!storage || m_remapped_files.empty())
 
146
                                return m_files.begin();
 
147
                        else
 
148
                                return m_remapped_files.begin();
 
149
                }
 
150
 
 
151
                file_iterator end_files(bool storage = false) const
 
152
                {
 
153
                        if (!storage || m_remapped_files.empty())
 
154
                                return m_files.end();
 
155
                        else
 
156
                                return m_remapped_files.end();
 
157
                }
 
158
 
 
159
                reverse_file_iterator rbegin_files(bool storage = false) const
 
160
                {
 
161
                        if (!storage || m_remapped_files.empty())
 
162
                                return m_files.rbegin();
 
163
                        else
 
164
                                return m_remapped_files.rbegin();
 
165
                }
 
166
 
 
167
                reverse_file_iterator rend_files(bool storage = false) const
 
168
                {
 
169
                        if (!storage || m_remapped_files.empty())
 
170
                                return m_files.rend();
 
171
                        else
 
172
                                return m_remapped_files.rend();
 
173
                }
 
174
 
 
175
                int num_files(bool storage = false) const
 
176
                {
 
177
                        TORRENT_ASSERT(m_piece_length > 0);
 
178
                        if (!storage || m_remapped_files.empty())
 
179
                                return (int)m_files.size();
 
180
                        else
 
181
                                return (int)m_remapped_files.size();
 
182
                }
 
183
 
 
184
                const file_entry& file_at(int index, bool storage = false) const
 
185
                {
 
186
                        if (!storage || m_remapped_files.empty())
 
187
                        {
 
188
                                TORRENT_ASSERT(index >= 0 && index < (int)m_files.size());
 
189
                                return m_files[index];
 
190
                        }
 
191
                        else
 
192
                        {
 
193
                                TORRENT_ASSERT(index >= 0 && index < (int)m_remapped_files.size());
 
194
                                return m_remapped_files[index];
 
195
                        }
 
196
                }
 
197
                
 
198
                const std::vector<announce_entry>& trackers() const { return m_urls; }
 
199
 
 
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; }
 
205
 
 
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 -------
 
210
 
 
211
                bool is_valid() const { return m_piece_length > 0; }
 
212
 
 
213
                bool priv() const { return m_private; }
 
214
                void set_priv(bool v) { m_private = v; }
 
215
 
 
216
                void convert_file_names();
 
217
 
 
218
                int piece_size(int index) const;
 
219
 
 
220
                const sha1_hash& hash_for_piece(int index) const
 
221
                {
 
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];
 
226
                }
 
227
 
 
228
                boost::optional<pt::ptime> creation_date() const;
 
229
 
 
230
                const std::string& creator() const
 
231
                { return m_created_by; }
 
232
 
 
233
                const std::string& comment() const
 
234
                { return m_comment; }
 
235
 
 
236
                // dht nodes to add to the routing table/bootstrap from
 
237
                typedef std::vector<std::pair<std::string, int> > nodes_t;
 
238
                
 
239
                nodes_t const& nodes() const
 
240
                {
 
241
                        TORRENT_ASSERT(!m_half_metadata);
 
242
                        return m_nodes;
 
243
                }
 
244
                
 
245
                void add_node(std::pair<std::string, int> const& node);
 
246
 
 
247
                void parse_info_section(entry const& e);
 
248
 
 
249
                entry const* extra(char const* key) const
 
250
                { return m_extra_info.find_key(key); }
 
251
 
 
252
                // frees parts of the metadata that isn't
 
253
                // used by seeds
 
254
                void seed_free();
 
255
 
 
256
                void swap(torrent_info& ti);
 
257
 
 
258
        private:
 
259
 
 
260
                void read_torrent_info(const entry& libtorrent);
 
261
 
 
262
                // the urls to the trackers
 
263
                std::vector<announce_entry> m_urls;
 
264
 
 
265
                std::vector<std::string> m_url_seeds;
 
266
 
 
267
                // the length of one piece
 
268
                // if this is 0, the torrent_info is
 
269
                // in an uninitialized state
 
270
                int m_piece_length;
 
271
 
 
272
                // the sha-1 hashes of each piece
 
273
                std::vector<sha1_hash> m_piece_hash;
 
274
 
 
275
                // the list of files that this torrent consists of
 
276
                std::vector<file_entry> m_files;
 
277
 
 
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
 
282
                // writing the disk.
 
283
                std::vector<file_entry> m_remapped_files;
 
284
 
 
285
                nodes_t m_nodes;
 
286
 
 
287
                // the sum of all filesizes
 
288
                size_type m_total_size;
 
289
 
 
290
                // the number of pieces in the torrent
 
291
                int m_num_pieces;
 
292
 
 
293
                // the hash that identifies this torrent
 
294
                // is mutable because it's calculated
 
295
                // lazily
 
296
                mutable sha1_hash m_info_hash;
 
297
 
 
298
                std::string m_name;
 
299
                
 
300
                // if a creation date is found in the torrent file
 
301
                // this will be set to that, otherwise it'll be
 
302
                // 1970, Jan 1
 
303
                pt::ptime m_creation_date;
 
304
 
 
305
                // if a comment is found in the torrent file
 
306
                // this will be set to that comment
 
307
                std::string m_comment;
 
308
 
 
309
                // an optional string naming the software used
 
310
                // to create the torrent file
 
311
                std::string m_created_by;
 
312
 
 
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.
 
318
                bool m_multifile;
 
319
                
 
320
                // this is true if the torrent is private. i.e., is should not
 
321
                // be announced on the dht
 
322
                bool m_private;
 
323
 
 
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
 
327
                // to peers.
 
328
                entry m_extra_info;
 
329
 
 
330
#ifndef NDEBUG
 
331
        public:
 
332
                // this is set to true when seed_free() is called
 
333
                bool m_half_metadata;
 
334
        private:
 
335
#endif
 
336
        };
 
337
 
 
338
}
 
339
 
 
340
#endif // TORRENT_TORRENT_INFO_HPP_INCLUDED
 
341