3 Copyright (c) 2008, 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_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"
49 #pragma warning(push, 1)
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>
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
; }
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
;
104 // the hash that identifies this torrent
105 // is mutable because it's calculated
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
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.
129 // this is true if the torrent is private. i.e., is should not
130 // be announced on the dht
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 std::string
const& leaf
= l
.leaf();
147 if (leaf
== ".." || leaf
== ".") return;
148 if (!pred(l
)) return;
152 for (directory_iterator
i(f
), end
; i
!= end
; ++i
)
153 add_files_impl(fs
, p
, l
/ i
->leaf(), pred
);
157 fs
.add_file(l
, file_size(f
));
162 template <class Pred
>
163 void add_files(file_storage
& fs
, boost::filesystem::path
const& file
, Pred p
)
165 detail::add_files_impl(fs
, complete(file
).branch_path(), file
.leaf(), p
);
168 inline void add_files(file_storage
& fs
, boost::filesystem::path
const& file
)
170 detail::add_files_impl(fs
, complete(file
).branch_path(), file
.leaf(), detail::default_pred
);
174 void set_piece_hashes(create_torrent
& t
, boost::filesystem::path
const& p
, Fun f
)
177 boost::scoped_ptr
<storage_interface
> st(
178 default_storage_constructor(const_cast<file_storage
&>(t
.files()), p
, fp
));
180 // calculate the hash for all pieces
181 int num
= t
.num_pieces();
182 std::vector
<char> buf(t
.piece_length());
183 for (int i
= 0; i
< num
; ++i
)
185 // read hits the disk and will block. Progress should
186 // be updated in between reads
187 st
->read(&buf
[0], i
, 0, t
.piece_size(i
));
188 hasher
h(&buf
[0], t
.piece_size(i
));
189 t
.set_hash(i
, h
.final());
194 inline void set_piece_hashes(create_torrent
& t
, boost::filesystem::path
const& p
)
196 set_piece_hashes(t
, p
, detail::nop
);