2008-05-14 07:29:42 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2008, Arvid Norberg
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef TORRENT_CREATE_TORRENT_HPP_INCLUDED
|
|
|
|
#define TORRENT_CREATE_TORRENT_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "libtorrent/bencode.hpp"
|
|
|
|
#include "libtorrent/peer_id.hpp"
|
2008-05-28 10:44:40 +02:00
|
|
|
#include "libtorrent/file_storage.hpp"
|
|
|
|
#include "libtorrent/file_pool.hpp"
|
|
|
|
#include "libtorrent/config.hpp"
|
|
|
|
#include "libtorrent/storage.hpp"
|
|
|
|
#include "libtorrent/hasher.hpp"
|
2008-11-30 09:12:26 +01:00
|
|
|
#include "libtorrent/utf8.hpp"
|
2009-01-15 18:09:36 +01:00
|
|
|
#include "libtorrent/allocator.hpp"
|
2009-10-26 02:29:39 +01:00
|
|
|
#include "libtorrent/file.hpp" // for combine_path etc.
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2009-04-07 17:06:07 +02:00
|
|
|
#include <boost/config.hpp>
|
2008-05-14 07:29:42 +02:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
2008-09-24 19:05:12 +02:00
|
|
|
class torrent_info;
|
2008-05-14 07:29:42 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
struct TORRENT_EXPORT create_torrent
|
2008-05-14 07:29:42 +02:00
|
|
|
{
|
2011-04-10 10:41:07 +02:00
|
|
|
enum flags_t
|
|
|
|
{
|
2010-03-20 08:30:34 +01:00
|
|
|
optimize = 1
|
|
|
|
, merkle = 2
|
|
|
|
, modification_time = 4
|
|
|
|
, symlinks = 8
|
2010-03-27 16:51:30 +01:00
|
|
|
, calculate_file_hashes = 16
|
2010-03-20 08:30:34 +01:00
|
|
|
};
|
2009-01-19 09:46:45 +01:00
|
|
|
|
|
|
|
create_torrent(file_storage& fs, int piece_size = 0
|
|
|
|
, int pad_file_limit = -1, int flags = optimize);
|
2008-09-24 19:05:12 +02:00
|
|
|
create_torrent(torrent_info const& ti);
|
2008-05-14 07:29:42 +02:00
|
|
|
entry generate() const;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage const& files() const { return m_files; }
|
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
void set_comment(char const* str);
|
|
|
|
void set_creator(char const* str);
|
|
|
|
void set_hash(int index, sha1_hash const& h);
|
2010-03-27 16:51:30 +01:00
|
|
|
void set_file_hash(int index, sha1_hash const& h);
|
2008-05-14 07:29:42 +02:00
|
|
|
void add_url_seed(std::string const& url);
|
2010-10-10 23:06:35 +02:00
|
|
|
void add_http_seed(std::string const& url);
|
2008-05-14 07:29:42 +02:00
|
|
|
void add_node(std::pair<std::string, int> const& node);
|
|
|
|
void add_tracker(std::string const& url, int tier = 0);
|
2011-08-28 23:06:15 +02:00
|
|
|
void set_root_cert(std::string const& pem);
|
2008-07-22 15:01:22 +02:00
|
|
|
void set_priv(bool p) { m_private = p; }
|
2008-05-14 07:29:42 +02:00
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
int num_pieces() const { return m_files.num_pieces(); }
|
|
|
|
int piece_length() const { return m_files.piece_length(); }
|
|
|
|
int piece_size(int i) const { return m_files.piece_size(i); }
|
2008-07-21 19:04:31 +02:00
|
|
|
bool priv() const { return m_private; }
|
2008-05-14 07:29:42 +02:00
|
|
|
|
2010-03-27 16:51:30 +01:00
|
|
|
bool should_add_file_hashes() const { return m_calculate_file_hashes; }
|
2011-07-30 19:35:22 +02:00
|
|
|
std::vector<sha1_hash> const& merkle_tree() const { return m_merkle_tree; }
|
2010-03-27 16:51:30 +01:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
private:
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
file_storage& m_files;
|
2008-09-24 19:05:12 +02:00
|
|
|
// if m_info_dict is initialized, it is
|
|
|
|
// used instead of m_files to generate
|
|
|
|
// the info dictionary
|
|
|
|
entry m_info_dict;
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
// the urls to the trackers
|
|
|
|
typedef std::pair<std::string, int> announce_entry;
|
|
|
|
std::vector<announce_entry> m_urls;
|
|
|
|
|
|
|
|
std::vector<std::string> m_url_seeds;
|
2010-10-10 23:06:35 +02:00
|
|
|
std::vector<std::string> m_http_seeds;
|
2008-05-14 07:29:42 +02:00
|
|
|
|
|
|
|
std::vector<sha1_hash> m_piece_hash;
|
|
|
|
|
2010-03-27 16:51:30 +01:00
|
|
|
std::vector<sha1_hash> m_filehashes;
|
|
|
|
|
2011-07-30 19:35:22 +02:00
|
|
|
// if we're generating a merkle torrent, this is the
|
|
|
|
// merkle tree we got. This should be saved in fast-resume
|
|
|
|
// in order to start seeding the torrent
|
|
|
|
mutable std::vector<sha1_hash> m_merkle_tree;
|
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
// dht nodes to add to the routing table/bootstrap from
|
|
|
|
typedef std::vector<std::pair<std::string, int> > nodes_t;
|
|
|
|
nodes_t m_nodes;
|
|
|
|
|
|
|
|
// the hash that identifies this torrent
|
|
|
|
// is mutable because it's calculated
|
|
|
|
// lazily
|
|
|
|
mutable sha1_hash m_info_hash;
|
|
|
|
|
|
|
|
// if a creation date is found in the torrent file
|
|
|
|
// this will be set to that, otherwise it'll be
|
|
|
|
// 1970, Jan 1
|
2010-08-22 00:10:16 +02:00
|
|
|
time_t m_creation_date;
|
2008-05-14 07:29:42 +02:00
|
|
|
|
|
|
|
// if a comment is found in the torrent file
|
|
|
|
// this will be set to that comment
|
|
|
|
std::string m_comment;
|
|
|
|
|
|
|
|
// an optional string naming the software used
|
|
|
|
// to create the torrent file
|
|
|
|
std::string m_created_by;
|
|
|
|
|
2011-08-28 23:06:15 +02:00
|
|
|
// this is the root cert for SSL torrents
|
|
|
|
std::string m_root_cert;
|
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
// this is used when creating a torrent. If there's
|
|
|
|
// only one file there are cases where it's impossible
|
|
|
|
// to know if it should be written as a multifile torrent
|
|
|
|
// or not. e.g. test/test there's one file and one directory
|
|
|
|
// and they have the same name.
|
2009-03-13 07:09:39 +01:00
|
|
|
bool m_multifile:1;
|
2008-05-14 07:29:42 +02:00
|
|
|
|
|
|
|
// this is true if the torrent is private. i.e., is should not
|
|
|
|
// be announced on the dht
|
2009-03-13 07:09:39 +01:00
|
|
|
bool m_private:1;
|
|
|
|
|
|
|
|
// if set to one, a merkle torrent will be generated
|
|
|
|
bool m_merkle_torrent:1;
|
2010-03-20 03:41:36 +01:00
|
|
|
|
|
|
|
// if set, include the 'mtime' modification time in the
|
|
|
|
// torrent file
|
|
|
|
bool m_include_mtime:1;
|
2010-03-20 08:30:34 +01:00
|
|
|
|
|
|
|
// if set, symbolic links are declared as such in
|
|
|
|
// the torrent file. The full data of the pointed-to
|
|
|
|
// file is still included
|
|
|
|
bool m_include_symlinks:1;
|
2010-03-27 16:51:30 +01:00
|
|
|
|
|
|
|
// this is only used by set_piece_hashes(). It will
|
|
|
|
// calculate sha1 hashes for each file and add it
|
|
|
|
// to the file list
|
|
|
|
bool m_calculate_file_hashes:1;
|
2008-05-14 07:29:42 +02:00
|
|
|
};
|
2008-05-28 10:44:40 +02:00
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
inline bool default_pred(std::string const&) { return true; }
|
2008-11-30 09:12:26 +01:00
|
|
|
|
|
|
|
inline bool ignore_subdir(std::string const& leaf)
|
|
|
|
{ return leaf == ".." || leaf == "."; }
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
inline void nop(int i) {}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
int TORRENT_EXPORT get_file_attributes(std::string const& p);
|
|
|
|
std::string TORRENT_EXPORT get_symlink_path(std::string const& p);
|
2009-06-23 04:50:41 +02:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
template <class Pred>
|
|
|
|
void add_files_impl(file_storage& fs, std::string const& p
|
2010-04-01 02:44:29 +02:00
|
|
|
, std::string const& l, Pred pred, boost::uint32_t flags)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string f = combine_path(p, l);
|
2010-01-19 04:05:43 +01:00
|
|
|
if (!pred(f)) return;
|
2009-10-26 02:29:39 +01:00
|
|
|
error_code ec;
|
|
|
|
file_status s;
|
2010-04-01 02:44:29 +02:00
|
|
|
stat_file(f, &s, ec, (flags & create_torrent::symlinks) ? dont_follow_links : 0);
|
2009-10-26 02:29:39 +01:00
|
|
|
if (ec) return;
|
|
|
|
|
2010-04-01 02:44:29 +02:00
|
|
|
// recurse into directories
|
2010-10-16 17:24:45 +02:00
|
|
|
bool recurse = (s.mode & file_status::directory) != 0;
|
2010-04-01 02:44:29 +02:00
|
|
|
|
|
|
|
// if the file is not a link or we're following links, and it's a directory
|
|
|
|
// only then should we recurse
|
|
|
|
#ifndef TORRENT_WINDOWS
|
|
|
|
if ((s.mode & file_status::link) && (flags & create_torrent::symlinks))
|
|
|
|
recurse = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (recurse)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
for (directory i(f, ec); !i.done(); i.next(ec))
|
2009-10-25 22:07:14 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string leaf = i.file();
|
2009-10-25 22:07:14 +01:00
|
|
|
if (ignore_subdir(leaf)) continue;
|
2010-04-01 02:44:29 +02:00
|
|
|
add_files_impl(fs, p, combine_path(l, leaf), pred, flags);
|
2009-10-25 22:07:14 +01:00
|
|
|
}
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
// #error use the fields from s
|
2009-01-11 23:27:43 +01:00
|
|
|
int file_flags = get_file_attributes(f);
|
2009-10-26 02:29:39 +01:00
|
|
|
|
|
|
|
// mask all bits to check if the file is a symlink
|
2010-04-01 02:44:29 +02:00
|
|
|
if ((file_flags & file_storage::attribute_symlink)
|
|
|
|
&& (flags & create_torrent::symlinks))
|
2009-07-01 10:35:45 +02:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string sym_path = get_symlink_path(f);
|
|
|
|
fs.add_file(l, 0, file_flags, s.mtime, sym_path);
|
2009-07-01 10:35:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
fs.add_file(l, s.file_size, file_flags, s.mtime);
|
2009-07-01 10:35:45 +02:00
|
|
|
}
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Pred>
|
2010-04-01 02:44:29 +02:00
|
|
|
void add_files(file_storage& fs, std::string const& file, Pred p, boost::uint32_t flags = 0)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2010-04-01 02:44:29 +02:00
|
|
|
detail::add_files_impl(fs, parent_path(complete(file)), filename(file), p, flags);
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
|
2010-04-01 02:44:29 +02:00
|
|
|
inline void add_files(file_storage& fs, std::string const& file, boost::uint32_t flags = 0)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2010-04-01 02:44:29 +02:00
|
|
|
detail::add_files_impl(fs, parent_path(complete(file)), filename(file)
|
|
|
|
, detail::default_pred, flags);
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
|
2009-01-15 18:09:36 +01:00
|
|
|
struct piece_holder
|
|
|
|
{
|
|
|
|
piece_holder(int bytes): m_piece(page_aligned_allocator::malloc(bytes)) {}
|
|
|
|
~piece_holder() { page_aligned_allocator::free(m_piece); }
|
|
|
|
char* bytes() { return m_piece; }
|
|
|
|
private:
|
|
|
|
char* m_piece;
|
|
|
|
};
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
template <class Fun>
|
2009-10-26 02:29:39 +01:00
|
|
|
void set_piece_hashes(create_torrent& t, std::string const& p, Fun f
|
2009-03-19 18:32:40 +01:00
|
|
|
, error_code& ec)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
|
|
|
file_pool fp;
|
|
|
|
boost::scoped_ptr<storage_interface> st(
|
2010-07-15 03:14:36 +02:00
|
|
|
default_storage_constructor(const_cast<file_storage&>(t.files()), 0, p, fp
|
|
|
|
, std::vector<boost::uint8_t>()));
|
2008-05-28 10:44:40 +02:00
|
|
|
|
2010-03-27 16:51:30 +01:00
|
|
|
// if we're calculating file hashes as well, use this hasher
|
|
|
|
hasher filehash;
|
|
|
|
int file_idx = 0;
|
|
|
|
size_type left_in_file = t.files().at(0).size;
|
|
|
|
|
2008-05-28 10:44:40 +02:00
|
|
|
// calculate the hash for all pieces
|
|
|
|
int num = t.num_pieces();
|
2009-01-15 18:09:36 +01:00
|
|
|
piece_holder buf(t.piece_length());
|
2008-05-28 10:44:40 +02:00
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
|
|
|
// read hits the disk and will block. Progress should
|
|
|
|
// be updated in between reads
|
2009-01-15 18:09:36 +01:00
|
|
|
st->read(buf.bytes(), i, 0, t.piece_size(i));
|
2009-03-19 18:32:40 +01:00
|
|
|
if (st->error())
|
|
|
|
{
|
|
|
|
ec = st->error();
|
|
|
|
return;
|
|
|
|
}
|
2010-03-27 16:51:30 +01:00
|
|
|
|
|
|
|
if (t.should_add_file_hashes())
|
|
|
|
{
|
|
|
|
int left_in_piece = t.piece_size(i);
|
2010-10-18 02:46:15 +02:00
|
|
|
int this_piece_size = left_in_piece;
|
2010-03-27 16:51:30 +01:00
|
|
|
// the number of bytes from this file we just read
|
|
|
|
while (left_in_piece > 0)
|
|
|
|
{
|
2010-10-16 17:24:45 +02:00
|
|
|
int to_hash_for_file = int((std::min)(size_type(left_in_piece), left_in_file));
|
2010-10-18 02:46:15 +02:00
|
|
|
if (to_hash_for_file > 0)
|
|
|
|
{
|
|
|
|
int offset = this_piece_size - left_in_piece;
|
|
|
|
filehash.update(buf.bytes() + offset, to_hash_for_file);
|
|
|
|
}
|
2010-03-27 16:51:30 +01:00
|
|
|
left_in_file -= to_hash_for_file;
|
|
|
|
left_in_piece -= to_hash_for_file;
|
|
|
|
if (left_in_file == 0)
|
|
|
|
{
|
|
|
|
if (!t.files().at(file_idx).pad_file)
|
|
|
|
t.set_file_hash(file_idx, filehash.final());
|
|
|
|
filehash.reset();
|
|
|
|
file_idx++;
|
|
|
|
if (file_idx >= t.files().num_files()) break;
|
|
|
|
left_in_file = t.files().at(file_idx).size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-15 18:09:36 +01:00
|
|
|
hasher h(buf.bytes(), t.piece_size(i));
|
2008-05-28 10:44:40 +02:00
|
|
|
t.set_hash(i, h.final());
|
|
|
|
f(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 17:06:07 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-03-19 18:32:40 +01:00
|
|
|
template <class Fun>
|
2009-10-26 02:29:39 +01:00
|
|
|
void set_piece_hashes(create_torrent& t, std::string const& p, Fun f)
|
2009-03-19 18:32:40 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
set_piece_hashes(t, p, f, ec);
|
|
|
|
if (ec) throw libtorrent_exception(ec);
|
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
inline void set_piece_hashes(create_torrent& t, std::string const& p)
|
2008-05-28 10:44:40 +02:00
|
|
|
{
|
2009-03-19 18:32:40 +01:00
|
|
|
error_code ec;
|
|
|
|
set_piece_hashes(t, p, detail::nop, ec);
|
|
|
|
if (ec) throw libtorrent_exception(ec);
|
|
|
|
}
|
2009-04-07 17:06:07 +02:00
|
|
|
#endif
|
2009-03-19 18:32:40 +01:00
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
inline void set_piece_hashes(create_torrent& t, std::string const& p, error_code& ec)
|
2009-03-19 18:32:40 +01:00
|
|
|
{
|
|
|
|
set_piece_hashes(t, p, detail::nop, ec);
|
2008-05-28 10:44:40 +02:00
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
#if TORRENT_USE_WSTRING
|
|
|
|
// wstring versions
|
2008-11-30 09:12:26 +01:00
|
|
|
|
|
|
|
template <class Pred>
|
2010-04-01 02:44:29 +02:00
|
|
|
void add_files(file_storage& fs, std::wstring const& wfile, Pred p, boost::uint32_t flags = 0)
|
2008-11-30 09:12:26 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string utf8;
|
|
|
|
wchar_utf8(wfile, utf8);
|
2010-04-01 02:44:29 +02:00
|
|
|
detail::add_files_impl(fs, parent_path(complete(utf8))
|
|
|
|
, filename(utf8), p, flags);
|
2008-11-30 09:12:26 +01:00
|
|
|
}
|
|
|
|
|
2010-04-01 02:44:29 +02:00
|
|
|
inline void add_files(file_storage& fs, std::wstring const& wfile, boost::uint32_t flags = 0)
|
2008-11-30 09:12:26 +01:00
|
|
|
{
|
2009-10-26 02:29:39 +01:00
|
|
|
std::string utf8;
|
|
|
|
wchar_utf8(wfile, utf8);
|
2010-04-01 02:44:29 +02:00
|
|
|
detail::add_files_impl(fs, parent_path(complete(utf8))
|
|
|
|
, filename(utf8), detail::default_pred, flags);
|
2008-11-30 09:12:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Fun>
|
2009-10-26 02:29:39 +01:00
|
|
|
void set_piece_hashes(create_torrent& t, std::wstring const& p, Fun f
|
2009-03-19 18:32:40 +01:00
|
|
|
, error_code& ec)
|
2008-11-30 09:12:26 +01:00
|
|
|
{
|
|
|
|
file_pool fp;
|
|
|
|
std::string utf8;
|
2009-10-26 02:29:39 +01:00
|
|
|
wchar_utf8(p, utf8);
|
2008-11-30 09:12:26 +01:00
|
|
|
boost::scoped_ptr<storage_interface> st(
|
2010-07-15 03:14:36 +02:00
|
|
|
default_storage_constructor(const_cast<file_storage&>(t.files()), 0, utf8, fp
|
|
|
|
, std::vector<boost::uint8_t>()));
|
2008-11-30 09:12:26 +01:00
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
|
|
|
int num = t.num_pieces();
|
|
|
|
std::vector<char> buf(t.piece_length());
|
|
|
|
for (int i = 0; i < num; ++i)
|
|
|
|
{
|
|
|
|
// read hits the disk and will block. Progress should
|
|
|
|
// be updated in between reads
|
|
|
|
st->read(&buf[0], i, 0, t.piece_size(i));
|
2009-03-19 18:32:40 +01:00
|
|
|
if (st->error())
|
|
|
|
{
|
|
|
|
ec = st->error();
|
|
|
|
return;
|
|
|
|
}
|
2008-11-30 09:12:26 +01:00
|
|
|
hasher h(&buf[0], t.piece_size(i));
|
|
|
|
t.set_hash(i, h.final());
|
|
|
|
f(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-07 17:06:07 +02:00
|
|
|
#ifndef BOOST_NO_EXCEPTIONS
|
2009-03-19 18:32:40 +01:00
|
|
|
template <class Fun>
|
2009-10-26 02:29:39 +01:00
|
|
|
void set_piece_hashes(create_torrent& t, std::wstring const& p, Fun f)
|
2009-03-19 18:32:40 +01:00
|
|
|
{
|
|
|
|
error_code ec;
|
|
|
|
set_piece_hashes(t, p, f, ec);
|
|
|
|
if (ec) throw libtorrent_exception(ec);
|
|
|
|
}
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
inline void set_piece_hashes(create_torrent& t, std::wstring const& p)
|
2008-11-30 09:12:26 +01:00
|
|
|
{
|
2009-03-19 18:32:40 +01:00
|
|
|
error_code ec;
|
|
|
|
set_piece_hashes(t, p, detail::nop, ec);
|
|
|
|
if (ec) throw libtorrent_exception(ec);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-10-26 02:29:39 +01:00
|
|
|
inline void set_piece_hashes(create_torrent& t, std::wstring const& p, error_code& ec)
|
2009-03-19 18:32:40 +01:00
|
|
|
{
|
|
|
|
set_piece_hashes(t, p, detail::nop, ec);
|
2008-11-30 09:12:26 +01:00
|
|
|
}
|
2009-10-20 18:44:11 +02:00
|
|
|
|
|
|
|
#endif // TORRENT_USE_WPATH
|
|
|
|
|
2008-05-14 07:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2009-10-26 02:29:39 +01:00
|
|
|
|