merged get_torrent_info deprecation from libtorrent_aio, replaced by torrent_file()

This commit is contained in:
Arvid Norberg 2012-10-02 16:07:55 +00:00
parent bff648a89e
commit 5bc322c031
6 changed files with 73 additions and 51 deletions

View File

@ -2302,7 +2302,7 @@ Its declaration looks like this::
void file_progress(std::vector<size_type>& fp, int flags = 0); void file_progress(std::vector<size_type>& fp, int flags = 0);
void get_download_queue(std::vector<partial_piece_info>& queue) const; void get_download_queue(std::vector<partial_piece_info>& queue) const;
void get_peer_info(std::vector<peer_info>& v) const; void get_peer_info(std::vector<peer_info>& v) const;
torrent_info const& get_torrent_info() const; boost::intrusive_ptr<torrent_info> torrent_file() const;
bool is_valid() const; bool is_valid() const;
std::string name() const; std::string name() const;
@ -3204,7 +3204,7 @@ Example code to pause and save resume data for all torrents and wait for the ale
} }
torrent_handle h = rd->handle; torrent_handle h = rd->handle;
std::ofstream out((h.save_path() + "/" + h.get_torrent_info().name() + ".fastresume").c_str() std::ofstream out((h.save_path() + "/" + h.torrent_file()->name() + ".fastresume").c_str()
, std::ios_base::binary); , std::ios_base::binary);
out.unsetf(std::ios_base::skipws); out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), *rd->resume_data); bencode(std::ostream_iterator<char>(out), *rd->resume_data);
@ -3347,18 +3347,18 @@ torrent_handle_ is invalid, it will throw libtorrent_exception_ exception. Each
the vector contains information about that particular peer. See peer_info_. the vector contains information about that particular peer. See peer_info_.
get_torrent_info() torrent_file()
------------------ --------------
:: ::
torrent_info const& get_torrent_info() const; boost::intrusive_ptr<torrent_info> torrent_file() const;
Returns a const reference to the torrent_info_ object associated with this torrent. Returns a pointer to the torrent_info_ object associated with this torrent. The
This reference is valid as long as the torrent_handle_ is valid, no longer. If the ``torrent_info`` object is a copy of the internal object. If the torrent doesn't
torrent_handle_ is invalid or if it doesn't have any metadata, libtorrent_exception_ have metadata, the object being returned will not be fully filled in.
exception will be thrown. The torrent may be in a state without metadata only if The torrent may be in a state without metadata only if
it was started without a .torrent file, i.e. by using the libtorrent extension of it was started without a .torrent file, e.g. by using the libtorrent extension of
just supplying a tracker and info-hash. just supplying a tracker and info-hash.
@ -3616,8 +3616,7 @@ set to priority 0, i.e. are not downloaded.
``has_metadata`` is true if this torrent has metadata (either it was started from a ``has_metadata`` is true if this torrent has metadata (either it was started from a
.torrent file or the metadata has been downloaded). The only scenario where this can be .torrent file or the metadata has been downloaded). The only scenario where this can be
false is when the torrent was started torrent-less (i.e. with just an info-hash and tracker false is when the torrent was started torrent-less (i.e. with just an info-hash and tracker
ip, a magnet link for instance). Note that if the torrent doesn't have metadata, the member ip, a magnet link for instance).
`get_torrent_info()`_ will throw.
``error`` may be set to an error message describing why the torrent was paused, in ``error`` may be set to an error message describing why the torrent was paused, in
case it was paused by an error. If the torrent is not paused or if it's paused but case it was paused by an error. If the torrent is not paused or if it's paused but
@ -7171,12 +7170,12 @@ code to do that::
torrent_handle h = alert->handle(); torrent_handle h = alert->handle();
if (h.is_valid()) { if (h.is_valid()) {
torrent_info const& ti = h.get_torrent_info(); boost::intrusive_ptr<torrent_info const> ti = h.torrent_file();
create_torrent ct(ti); create_torrent ct(*ti);
entry te = ct.generate(); entry te = ct.generate();
std::vector<char> buffer; std::vector<char> buffer;
bencode(std::back_inserter(buffer), te); bencode(std::back_inserter(buffer), te);
FILE* f = fopen((to_hex(ti.info_hash().to_string()) + ".torrent").c_str(), "w+"); FILE* f = fopen((to_hex(ti->info_hash().to_string()) + ".torrent").c_str(), "w+");
if (f) { if (f) {
fwrite(&buffer[0], 1, buffer.size(), f); fwrite(&buffer[0], 1, buffer.size(), f);
fclose(f); fclose(f);
@ -7594,7 +7593,7 @@ Examples usage::
// write fast resume data // write fast resume data
// ... // ...
std::cout << a.handle.get_torrent_info().name() << "completed" std::cout << a.handle.torrent_file()->name() << "completed"
<< std::endl; << std::endl;
} }
}; };

View File

@ -884,6 +884,8 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a
} }
#endif #endif
boost::intrusive_ptr<torrent_info> ti;
if (metadata_received_alert* p = alert_cast<metadata_received_alert>(a)) if (metadata_received_alert* p = alert_cast<metadata_received_alert>(a))
{ {
// if we have a monitor dir, save the .torrent file we just received in it // if we have a monitor dir, save the .torrent file we just received in it
@ -891,12 +893,12 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a
// to keep the scan dir logic in sync so it's not removed, or added twice // to keep the scan dir logic in sync so it's not removed, or added twice
torrent_handle h = p->handle; torrent_handle h = p->handle;
if (h.is_valid()) { if (h.is_valid()) {
torrent_info const& ti = h.get_torrent_info(); if (!ti) ti = h.torrent_file();
create_torrent ct(ti); create_torrent ct(*ti);
entry te = ct.generate(); entry te = ct.generate();
std::vector<char> buffer; std::vector<char> buffer;
bencode(std::back_inserter(buffer), te); bencode(std::back_inserter(buffer), te);
std::string filename = ti.name() + "." + to_hex(ti.info_hash().to_string()) + ".torrent"; std::string filename = ti->name() + "." + to_hex(ti->info_hash().to_string()) + ".torrent";
filename = combine_path(monitor_dir, filename); filename = combine_path(monitor_dir, filename);
save_file(filename, buffer); save_file(filename, buffer);
@ -930,7 +932,9 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a
h.set_max_uploads(-1); h.set_max_uploads(-1);
h.set_upload_limit(torrent_upload_limit); h.set_upload_limit(torrent_upload_limit);
h.set_download_limit(torrent_download_limit); h.set_download_limit(torrent_download_limit);
#ifndef TORRENT_NO_DEPRECATE
h.use_interface(outgoing_interface.c_str()); h.use_interface(outgoing_interface.c_str());
#endif
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
h.resolve_countries(true); h.resolve_countries(true);
#endif #endif
@ -2273,15 +2277,15 @@ int main(int argc, char* argv[])
{ {
std::vector<size_type> file_progress; std::vector<size_type> file_progress;
h.file_progress(file_progress); h.file_progress(file_progress);
torrent_info const& info = h.get_torrent_info(); boost::intrusive_ptr<torrent_info> ti = h.torrent_file();
for (int i = 0; i < info.num_files(); ++i) for (int i = 0; i < ti->num_files(); ++i)
{ {
bool pad_file = info.file_at(i).pad_file; bool pad_file = ti->file_at(i).pad_file;
if (!show_pad_files && pad_file) continue; if (!show_pad_files && pad_file) continue;
int progress = info.file_at(i).size > 0 int progress = ti->file_at(i).size > 0
?file_progress[i] * 1000 / info.file_at(i).size:1000; ?file_progress[i] * 1000 / ti->file_at(i).size:1000;
char const* color = (file_progress[i] == info.file_at(i).size) char const* color = (file_progress[i] == ti->file_at(i).size)
?"32":"33"; ?"32":"33";
snprintf(str, sizeof(str), "%s %s %-5.2f%% %s %s%s\n", snprintf(str, sizeof(str), "%s %s %-5.2f%% %s %s%s\n",
@ -2289,7 +2293,7 @@ int main(int argc, char* argv[])
, pad_file?esc("34"):"" , pad_file?esc("34"):""
, progress / 10.f , progress / 10.f
, add_suffix(file_progress[i]).c_str() , add_suffix(file_progress[i]).c_str()
, filename(info.files().file_path(info.file_at(i))).c_str() , ti->files().file_name(i).c_str()
, pad_file?esc("0"):""); , pad_file?esc("0"):"");
out += str; out += str;
} }

View File

@ -721,6 +721,8 @@ namespace libtorrent
torrent_info const& torrent_file() const torrent_info const& torrent_file() const
{ return *m_torrent_file; } { return *m_torrent_file; }
boost::intrusive_ptr<torrent_info> get_torrent_copy();
std::string const& uuid() const { return m_uuid; } std::string const& uuid() const { return m_uuid; }
void set_uuid(std::string const& s) { m_uuid = s; } void set_uuid(std::string const& s) { m_uuid = s; }
std::string const& url() const { return m_url; } std::string const& url() const { return m_url; }

View File

@ -226,7 +226,7 @@ namespace libtorrent
#endif #endif
bool set_metadata(char const* metadata, int size) const; bool set_metadata(char const* metadata, int size) const;
const torrent_info& get_torrent_info() const;
bool is_valid() const; bool is_valid() const;
enum pause_flags_t { graceful_pause = 1 }; enum pause_flags_t { graceful_pause = 1 };
@ -264,12 +264,16 @@ namespace libtorrent
storage_interface* get_storage_impl() const; storage_interface* get_storage_impl() const;
// all these are deprecated, use piece boost::intrusive_ptr<torrent_info> torrent_file() const;
// priority functions instead
#ifndef TORRENT_NO_DEPRECATE
// ================ start deprecation ============ // ================ start deprecation ============
#ifndef TORRENT_NO_DEPRECATE // use torrent_file() instead
TORRENT_DEPRECATED_PREFIX
const torrent_info& get_torrent_info() const TORRENT_DEPRECATED;
// deprecated in 0.16, feature will be removed // deprecated in 0.16, feature will be removed
TORRENT_DEPRECATED_PREFIX TORRENT_DEPRECATED_PREFIX
int get_peer_upload_limit(tcp::endpoint ip) const TORRENT_DEPRECATED; int get_peer_upload_limit(tcp::endpoint ip) const TORRENT_DEPRECATED;
@ -301,6 +305,8 @@ namespace libtorrent
bool super_seeding() const TORRENT_DEPRECATED; bool super_seeding() const TORRENT_DEPRECATED;
// deprecated in 0.13 // deprecated in 0.13
// all these are deprecated, use piece
// priority functions instead
// marks the piece with the given index as filtered // marks the piece with the given index as filtered
// it will not be downloaded // it will not be downloaded
TORRENT_DEPRECATED_PREFIX TORRENT_DEPRECATED_PREFIX
@ -316,6 +322,14 @@ namespace libtorrent
TORRENT_DEPRECATED_PREFIX TORRENT_DEPRECATED_PREFIX
void filter_files(std::vector<bool> const& files) const TORRENT_DEPRECATED; void filter_files(std::vector<bool> const& files) const TORRENT_DEPRECATED;
TORRENT_DEPRECATED_PREFIX
void use_interface(const char* net_interface) const TORRENT_DEPRECATED;
// deprecated in 0.14
// use save_resume_data() instead. It is async. and
// will return the resume data in an alert
TORRENT_DEPRECATED_PREFIX
entry write_resume_data() const TORRENT_DEPRECATED;
// ================ end deprecation ============ // ================ end deprecation ============
#endif #endif
@ -335,18 +349,6 @@ namespace libtorrent
void prioritize_files(std::vector<int> const& files) const; void prioritize_files(std::vector<int> const& files) const;
std::vector<int> file_priorities() const; std::vector<int> file_priorities() const;
// set the interface to bind outgoing connections
// to.
void use_interface(const char* net_interface) const;
#ifndef TORRENT_NO_DEPRECATE
// deprecated in 0.14
// use save_resume_data() instead. It is async. and
// will return the resume data in an alert
TORRENT_DEPRECATED_PREFIX
entry write_resume_data() const TORRENT_DEPRECATED;
#endif
// forces this torrent to reannounce // forces this torrent to reannounce
// (make a rerequest from the tracker) // (make a rerequest from the tracker)
void force_reannounce() const; void force_reannounce() const;

View File

@ -5186,6 +5186,12 @@ namespace libtorrent
} }
} }
boost::intrusive_ptr<torrent_info> torrent::get_torrent_copy()
{
// copy the torrent_info object
return boost::intrusive_ptr<torrent_info>(new torrent_info(*m_torrent_file));
}
void torrent::write_resume_data(entry& ret) const void torrent::write_resume_data(entry& ret) const
{ {
using namespace libtorrent::detail; // for write_*_endpoint() using namespace libtorrent::detail; // for write_*_endpoint()
@ -8197,7 +8203,7 @@ namespace libtorrent
for (int i = 0; i < m_torrent_file->num_files(); ++i) for (int i = 0; i < m_torrent_file->num_files(); ++i)
{ {
peer_request ret = m_torrent_file->files().map_file(i, 0, 0); peer_request ret = m_torrent_file->files().map_file(i, 0, 0);
size_type size = m_torrent_file->files().at(i).size; size_type size = m_torrent_file->files().file_size(i);
// zero sized files are considered // zero sized files are considered
// 100% done all the time // 100% done all the time

View File

@ -825,6 +825,23 @@ namespace libtorrent
return r; return r;
} }
bool torrent_handle::is_valid() const
{
INVARIANT_CHECK;
return !m_torrent.expired();
}
boost::intrusive_ptr<torrent_info> torrent_handle::torrent_file() const
{
INVARIANT_CHECK;
TORRENT_SYNC_CALL_RET(boost::intrusive_ptr<torrent_info>, boost::intrusive_ptr<torrent_info>(), get_torrent_copy);
return r;
}
#ifndef TORRENT_NO_DEPRECATE
// this function should either be removed, or return
// reference counted handle to the torrent_info which
// forces the torrent to stay loaded while the client holds it
torrent_info const& torrent_handle::get_torrent_info() const torrent_info const& torrent_handle::get_torrent_info() const
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
@ -838,7 +855,6 @@ namespace libtorrent
#else #else
throw_invalid_handle(); throw_invalid_handle();
#endif #endif
// mutex::scoped_lock l(t->session().m_mutex);
if (!t->valid_metadata()) if (!t->valid_metadata())
#ifdef BOOST_NO_EXCEPTIONS #ifdef BOOST_NO_EXCEPTIONS
return empty; return empty;
@ -848,13 +864,6 @@ namespace libtorrent
return t->torrent_file(); return t->torrent_file();
} }
bool torrent_handle::is_valid() const
{
INVARIANT_CHECK;
return !m_torrent.expired();
}
#ifndef TORRENT_NO_DEPRECATE
entry torrent_handle::write_resume_data() const entry torrent_handle::write_resume_data() const
{ {
INVARIANT_CHECK; INVARIANT_CHECK;