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 #include "libtorrent/create_torrent.hpp"
34 #include "libtorrent/file_pool.hpp"
35 #include "libtorrent/storage.hpp"
37 #include <boost/date_time/posix_time/posix_time.hpp>
38 #include <boost/date_time/gregorian/gregorian.hpp>
39 #include <boost/bind.hpp>
41 namespace gr
= boost::gregorian
;
45 create_torrent::create_torrent(file_storage
& fs
, int size
)
47 , m_creation_date(pt::second_clock::universal_time())
48 , m_multifile(fs
.num_files() > 1)
51 TORRENT_ASSERT(fs
.num_files() > 0);
52 if (!m_multifile
&& m_files
.at(0).path
.has_branch_path()) m_multifile
= true;
54 // make sure the size is an even power of 2
56 for (int i
= 0; i
< 32; ++i
)
60 TORRENT_ASSERT((size
& ~(1 << i
)) == 0);
65 m_files
.set_piece_length(size
);
66 m_files
.set_num_pieces(static_cast<int>(
67 (m_files
.total_size() + m_files
.piece_length() - 1) / m_files
.piece_length()));
68 m_piece_hash
.resize(m_files
.num_pieces());
71 create_torrent::create_torrent(file_storage
& fs
)
73 , m_creation_date(pt::second_clock::universal_time())
74 , m_multifile(fs
.num_files() > 1)
77 TORRENT_ASSERT(fs
.num_files() > 0);
78 if (!m_multifile
&& m_files
.at(0).path
.has_branch_path()) m_multifile
= true;
80 const int target_size
= 40 * 1024;
81 int size
= fs
.total_size() / (target_size
/ 20);
83 for (int i
= 4*1024*1024; i
> 16*1024; i
/= 2)
85 if (size
< i
) continue;
90 m_files
.set_piece_length(size
);
91 m_files
.set_num_pieces(static_cast<int>(
92 (m_files
.total_size() + m_files
.piece_length() - 1) / m_files
.piece_length()));
93 m_piece_hash
.resize(m_files
.num_pieces());
95 entry
create_torrent::generate() const
97 TORRENT_ASSERT(m_files
.piece_length() > 0);
99 if (m_files
.num_files() == 0)
101 // TODO: throw something here
108 if (!m_urls
.empty()) dict
["announce"] = m_urls
.front().first
;
110 if (!m_nodes
.empty())
112 entry
& nodes
= dict
["nodes"];
113 entry::list_type
& nodes_list
= nodes
.list();
114 for (nodes_t::const_iterator i
= m_nodes
.begin()
115 , end(m_nodes
.end()); i
!= end
; ++i
)
117 entry::list_type node
;
118 node
.push_back(entry(i
->first
));
119 node
.push_back(entry(i
->second
));
120 nodes_list
.push_back(entry(node
));
124 if (m_urls
.size() > 1)
126 entry
trackers(entry::list_t
);
127 entry
tier(entry::list_t
);
128 int current_tier
= m_urls
.front().second
;
129 for (std::vector
<announce_entry
>::const_iterator i
= m_urls
.begin();
130 i
!= m_urls
.end(); ++i
)
132 if (i
->second
!= current_tier
)
134 current_tier
= i
->second
;
135 trackers
.list().push_back(tier
);
138 tier
.list().push_back(entry(i
->first
));
140 trackers
.list().push_back(tier
);
141 dict
["announce-list"] = trackers
;
144 if (!m_comment
.empty())
145 dict
["comment"] = m_comment
;
147 dict
["creation date"] =
148 (m_creation_date
- pt::ptime(gr::date(1970, gr::Jan
, 1))).total_seconds();
150 if (!m_created_by
.empty())
151 dict
["created by"] = m_created_by
;
153 if (!m_url_seeds
.empty())
155 if (m_url_seeds
.size() == 1)
157 dict
["url-list"] = m_url_seeds
.front();
161 entry
& list
= dict
["url-list"];
162 for (std::vector
<std::string
>::const_iterator i
163 = m_url_seeds
.begin(); i
!= m_url_seeds
.end(); ++i
)
165 list
.list().push_back(entry(*i
));
170 entry
& info
= dict
["info"];
171 info
["name"] = m_files
.name();
173 if (m_private
) info
["private"] = 1;
177 info
["length"] = m_files
.at(0).size
;
181 if (!info
.find_key("files"))
183 entry
& files
= info
["files"];
185 for (file_storage::iterator i
= m_files
.begin();
186 i
!= m_files
.end(); ++i
)
188 files
.list().push_back(entry());
189 entry
& file_e
= files
.list().back();
190 file_e
["length"] = i
->size
;
191 entry
& path_e
= file_e
["path"];
193 TORRENT_ASSERT(i
->path
.has_branch_path());
194 TORRENT_ASSERT(*i
->path
.begin() == m_files
.name());
196 for (fs::path::iterator j
= boost::next(i
->path
.begin());
197 j
!= i
->path
.end(); ++j
)
199 path_e
.list().push_back(entry(*j
));
205 info
["piece length"] = m_files
.piece_length();
206 entry
& pieces
= info
["pieces"];
208 std::string
& p
= pieces
.string();
210 for (std::vector
<sha1_hash
>::const_iterator i
= m_piece_hash
.begin();
211 i
!= m_piece_hash
.end(); ++i
)
213 p
.append((char*)i
->begin(), (char*)i
->end());
216 std::vector
<char> buf
;
217 bencode(std::back_inserter(buf
), info
);
218 m_info_hash
= hasher(&buf
[0], buf
.size()).final();
224 void create_torrent::add_tracker(std::string
const& url
, int tier
)
226 m_urls
.push_back(announce_entry(url
, tier
));
229 std::sort(m_urls
.begin(), m_urls
.end()
230 , bind(&announce_entry::second
, _1
) < bind(&announce_entry::second
, _2
));
233 void create_torrent::set_hash(int index
, sha1_hash
const& h
)
235 TORRENT_ASSERT(index
>= 0);
236 TORRENT_ASSERT(index
< (int)m_piece_hash
.size());
237 m_piece_hash
[index
] = h
;
240 void create_torrent::add_node(std::pair
<std::string
, int> const& node
)
242 m_nodes
.push_back(node
);
245 void create_torrent::add_url_seed(std::string
const& url
)
247 m_url_seeds
.push_back(url
);
250 void create_torrent::set_comment(char const* str
)
255 void create_torrent::set_creator(char const* str
)