00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef TORRENT_TORRENT_INFO_HPP_INCLUDE
00034 #define TORRENT_TORRENT_INFO_HPP_INCLUDE
00035
00036
00037 #include <string>
00038 #include <vector>
00039
00040 #ifdef _MSC_VER
00041 #pragma warning(push, 1)
00042 #endif
00043
00044 #include <boost/date_time/posix_time/posix_time.hpp>
00045 #include <boost/date_time/gregorian/gregorian_types.hpp>
00046 #include <boost/optional.hpp>
00047 #include <boost/filesystem/path.hpp>
00048 #include <boost/shared_ptr.hpp>
00049
00050 #ifdef _MSC_VER
00051 #pragma warning(pop)
00052 #endif
00053
00054 #include "libtorrent/entry.hpp"
00055 #include "libtorrent/socket.hpp"
00056 #include "libtorrent/peer_id.hpp"
00057 #include "libtorrent/size_type.hpp"
00058 #include "libtorrent/peer_request.hpp"
00059 #include "libtorrent/config.hpp"
00060
00061 namespace libtorrent
00062 {
00063
00064 struct TORRENT_EXPORT file_entry
00065 {
00066 boost::filesystem::path path;
00067 size_type offset;
00068 size_type size;
00069
00070
00071
00072
00073 boost::shared_ptr<const boost::filesystem::path> orig_path;
00074 };
00075
00076 struct TORRENT_EXPORT file_slice
00077 {
00078 int file_index;
00079 size_type offset;
00080 size_type size;
00081 };
00082
00083 struct TORRENT_EXPORT announce_entry
00084 {
00085 announce_entry(std::string const& u): url(u), tier(0) {}
00086 std::string url;
00087 int tier;
00088 };
00089
00090 struct TORRENT_EXPORT invalid_torrent_file: std::exception
00091 {
00092 virtual const char* what() const throw() { return "invalid torrent file"; }
00093 };
00094
00095 class TORRENT_EXPORT torrent_info
00096 {
00097 public:
00098
00099 torrent_info();
00100 torrent_info(sha1_hash const& info_hash);
00101 torrent_info(entry const& torrent_file);
00102 ~torrent_info();
00103
00104 entry create_torrent() const;
00105 entry create_info_metadata() const;
00106 void set_comment(char const* str);
00107 void set_creator(char const* str);
00108 void set_piece_size(int size);
00109 void set_hash(int index, sha1_hash const& h);
00110 void add_tracker(std::string const& url, int tier = 0);
00111 void add_file(boost::filesystem::path file, size_type size);
00112 void add_url_seed(std::string const& url);
00113
00114 std::vector<file_slice> map_block(int piece, size_type offset, int size) const;
00115 peer_request map_file(int file, size_type offset, int size) const;
00116
00117 std::vector<std::string> const& url_seeds() const { return m_url_seeds; }
00118
00119 typedef std::vector<file_entry>::const_iterator file_iterator;
00120 typedef std::vector<file_entry>::const_reverse_iterator reverse_file_iterator;
00121
00122
00123 file_iterator begin_files() const { return m_files.begin(); }
00124 file_iterator end_files() const { return m_files.end(); }
00125 reverse_file_iterator rbegin_files() const { return m_files.rbegin(); }
00126 reverse_file_iterator rend_files() const { return m_files.rend(); }
00127
00128 int num_files() const
00129 { assert(m_piece_length > 0); return (int)m_files.size(); }
00130 const file_entry& file_at(int index) const
00131 { assert(index >= 0 && index < (int)m_files.size()); return m_files[index]; }
00132
00133 const std::vector<announce_entry>& trackers() const { return m_urls; }
00134
00135 size_type total_size() const { assert(m_piece_length > 0); return m_total_size; }
00136 size_type piece_length() const { assert(m_piece_length > 0); return m_piece_length; }
00137 int num_pieces() const { assert(m_piece_length > 0); return (int)m_piece_hash.size(); }
00138 const sha1_hash& info_hash() const { return m_info_hash; }
00139 const std::string& name() const { assert(m_piece_length > 0); return m_name; }
00140 void print(std::ostream& os) const;
00141 bool is_valid() const { return m_piece_length > 0; }
00142
00143 bool priv() const { return m_private; }
00144 void set_priv(bool v) { m_private = v; }
00145
00146 void convert_file_names();
00147
00148 size_type piece_size(int index) const;
00149
00150 const sha1_hash& hash_for_piece(int index) const
00151 {
00152 assert(index >= 0);
00153 assert(index < (int)m_piece_hash.size());
00154 return m_piece_hash[index];
00155 }
00156
00157 boost::optional<boost::posix_time::ptime>
00158 creation_date() const;
00159
00160 const std::string& creator() const
00161 { return m_created_by; }
00162
00163 const std::string& comment() const
00164 { return m_comment; }
00165
00166
00167 typedef std::vector<std::pair<std::string, int> > nodes_t;
00168
00169 nodes_t const& nodes() const
00170 { return m_nodes; }
00171
00172 void add_node(std::pair<std::string, int> const& node);
00173
00174 void parse_info_section(entry const& e);
00175
00176 private:
00177
00178 void read_torrent_info(const entry& libtorrent);
00179
00180
00181 std::vector<announce_entry> m_urls;
00182
00183 std::vector<std::string> m_url_seeds;
00184
00185
00186
00187
00188 size_type m_piece_length;
00189
00190
00191 std::vector<sha1_hash> m_piece_hash;
00192
00193
00194 std::vector<file_entry> m_files;
00195
00196 nodes_t m_nodes;
00197
00198
00199 size_type m_total_size;
00200
00201
00202
00203
00204 mutable sha1_hash m_info_hash;
00205
00206 std::string m_name;
00207
00208
00209
00210
00211 boost::posix_time::ptime m_creation_date;
00212
00213
00214
00215 std::string m_comment;
00216
00217
00218
00219 std::string m_created_by;
00220
00221
00222
00223
00224
00225
00226 bool m_multifile;
00227
00228
00229
00230 bool m_private;
00231
00232
00233
00234
00235
00236 entry m_extra_info;
00237 };
00238
00239 }
00240
00241 #endif // TORRENT_TORRENT_INFO_HPP_INCLUDED
00242