fixes bug where priorities where lost when force-rechecking.
[libtorrent.git] / include / libtorrent / create_torrent.hpp
blob108de8fdaa940e828bf72c6b720d78120b317023
1 /*
3 Copyright (c) 2008, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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_CREATE_TORRENT_HPP_INCLUDED
34 #define TORRENT_CREATE_TORRENT_HPP_INCLUDED
36 #include "libtorrent/bencode.hpp"
37 #include "libtorrent/peer_id.hpp"
38 #include "libtorrent/file_storage.hpp"
39 #include "libtorrent/file_pool.hpp"
40 #include "libtorrent/config.hpp"
41 #include "libtorrent/storage.hpp"
42 #include "libtorrent/hasher.hpp"
44 #include <vector>
45 #include <string>
46 #include <utility>
48 #ifdef _MSC_VER
49 #pragma warning(push, 1)
50 #endif
52 #include <boost/filesystem/path.hpp>
53 #include <boost/filesystem/operations.hpp>
54 #include <boost/optional.hpp>
55 #include <boost/date_time/posix_time/posix_time.hpp>
56 #include <boost/scoped_ptr.hpp>
58 #ifdef _MSC_VER
59 #pragma warning(pop)
60 #endif
62 namespace libtorrent
64 namespace fs = boost::filesystem;
65 namespace pt = boost::posix_time;
67 struct TORRENT_EXPORT create_torrent
69 create_torrent(file_storage& fs, int piece_size);
70 create_torrent(file_storage& fs);
71 entry generate() const;
73 file_storage const& files() const { return m_files; }
75 void set_comment(char const* str);
76 void set_creator(char const* str);
77 void set_hash(int index, sha1_hash const& h);
78 void add_url_seed(std::string const& url);
79 void add_node(std::pair<std::string, int> const& node);
80 void add_tracker(std::string const& url, int tier = 0);
81 void set_priv(bool p) { m_private = p; }
83 int num_pieces() const { return m_files.num_pieces(); }
84 int piece_length() const { return m_files.piece_length(); }
85 int piece_size(int i) const { return m_files.piece_size(i); }
86 bool priv() const { return m_private; }
88 private:
90 file_storage& m_files;
92 // the urls to the trackers
93 typedef std::pair<std::string, int> announce_entry;
94 std::vector<announce_entry> m_urls;
96 std::vector<std::string> m_url_seeds;
98 std::vector<sha1_hash> m_piece_hash;
100 // dht nodes to add to the routing table/bootstrap from
101 typedef std::vector<std::pair<std::string, int> > nodes_t;
102 nodes_t m_nodes;
104 // the hash that identifies this torrent
105 // is mutable because it's calculated
106 // lazily
107 mutable sha1_hash m_info_hash;
109 // if a creation date is found in the torrent file
110 // this will be set to that, otherwise it'll be
111 // 1970, Jan 1
112 pt::ptime m_creation_date;
114 // if a comment is found in the torrent file
115 // this will be set to that comment
116 std::string m_comment;
118 // an optional string naming the software used
119 // to create the torrent file
120 std::string m_created_by;
122 // this is used when creating a torrent. If there's
123 // only one file there are cases where it's impossible
124 // to know if it should be written as a multifile torrent
125 // or not. e.g. test/test there's one file and one directory
126 // and they have the same name.
127 bool m_multifile;
129 // this is true if the torrent is private. i.e., is should not
130 // be announced on the dht
131 bool m_private;
134 namespace detail
136 inline bool default_pred(boost::filesystem::path const&) { return true; }
138 inline void nop(int i) {}
140 template <class Pred>
141 void add_files_impl(file_storage& fs, boost::filesystem::path const& p
142 , boost::filesystem::path const& l, Pred pred)
144 using boost::filesystem::path;
145 using boost::filesystem::directory_iterator;
146 #if BOOST_VERSION < 103600
147 std::string const& leaf = l.leaf();
148 #else
149 std::string const& leaf = l.filename();
150 #endif
151 if (leaf == ".." || leaf == ".") return;
152 if (!pred(l)) return;
153 path f(p / l);
154 if (is_directory(f))
156 for (directory_iterator i(f), end; i != end; ++i)
157 #if BOOST_VERSION < 103600
158 add_files_impl(fs, p, l / i->leaf(), pred);
159 #else
160 add_files_impl(fs, p, l / i->filename(), pred);
161 #endif
163 else
165 fs.add_file(l, file_size(f));
170 template <class Pred>
171 void add_files(file_storage& fs, boost::filesystem::path const& file, Pred p)
173 #if BOOST_VERSION < 103600
174 detail::add_files_impl(fs, complete(file).branch_path(), file.leaf(), p);
175 #else
176 detail::add_files_impl(fs, complete(file).parent_path(), file.filename(), p);
177 #endif
180 inline void add_files(file_storage& fs, boost::filesystem::path const& file)
182 detail::add_files_impl(fs, complete(file).branch_path(), file.leaf(), detail::default_pred);
185 template <class Fun>
186 void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p, Fun f)
188 file_pool fp;
189 boost::scoped_ptr<storage_interface> st(
190 default_storage_constructor(const_cast<file_storage&>(t.files()), p, fp));
192 // calculate the hash for all pieces
193 int num = t.num_pieces();
194 std::vector<char> buf(t.piece_length());
195 for (int i = 0; i < num; ++i)
197 // read hits the disk and will block. Progress should
198 // be updated in between reads
199 st->read(&buf[0], i, 0, t.piece_size(i));
200 hasher h(&buf[0], t.piece_size(i));
201 t.set_hash(i, h.final());
202 f(i);
206 inline void set_piece_hashes(create_torrent& t, boost::filesystem::path const& p)
208 set_piece_hashes(t, p, detail::nop);
213 #endif