* Make DownloadStore::save(...) validate the new file by loading the

bencoded data.

* Make it safe to remove torrents that are selected.

* Delete the rtorrent and libtorrent sections when loading non-session
torrents.

* Added ^O to change a torrent's root dir, ^P to set a variable.


git-svn-id: svn://rakshasa.no/libtorrent/trunk/rtorrent@628 e378c898-3ddf-0310-93e7-cc216c733640
This commit is contained in:
rakshasa
2006-01-27 20:16:52 +00:00
parent c89ae513ef
commit 6f44f8f920
15 changed files with 203 additions and 150 deletions
+7 -3
View File
@@ -64,9 +64,11 @@ Download::Download(torrent::Download d) :
rak::mem_fn(this, &Download::set_connection_current)));
m_variables.insert("connection_leech", new utils::VariableAny(connection_type_to_string(torrent::Download::CONNECTION_LEECH)));
m_variables.insert("connection_seed", new utils::VariableAny(connection_type_to_string(torrent::Download::CONNECTION_SEED)));
m_variables.insert("directory", new utils::VariableSlotString<std::string, const std::string&>(NULL,
rak::mem_fn(this, &Download::set_root_directory)));
m_variables.insert("tied_to_file", new utils::VariableBencode(&m_download.bencode(), "tied_to_file", torrent::Bencode::TYPE_STRING));
m_variables.insert("state", new utils::VariableBencode(&m_download.bencode(), "rtorrent", "state", torrent::Bencode::TYPE_STRING));
m_variables.insert("tied_to_file", new utils::VariableBencode(&m_download.bencode(), "rtorrent", "tied_to_file", torrent::Bencode::TYPE_STRING));
m_variables.insert("directory", new utils::VariableSlotString<>(rak::mem_fn(&m_download, &torrent::Download::root_dir),
rak::mem_fn(this, &Download::set_root_directory)));
m_variables.insert("min_peers", new utils::VariableSlotValue<uint32_t, uint32_t>(rak::mem_fn(&m_download, &torrent::Download::peers_min),
rak::mem_fn(&m_download, &torrent::Download::set_peers_min),
@@ -217,6 +219,8 @@ Download::set_root_directory(const std::string& d) {
m_download.set_root_dir(d +
(*d.rbegin() != '/' ? "/" : "") +
(m_download.size_file_entries() > 1 ? m_download.name() : ""));
m_download.bencode().get_key("rtorrent").insert_key("directory", d);
}
}
+41 -17
View File
@@ -146,38 +146,62 @@ DownloadFactory::receive_success() {
return;
}
torrent::Bencode& root = (*itr)->get_bencode();
if (!m_session) {
// We only allow session torrents to keep their
// 'rtorrent/libtorrent' sections.
root.erase_key("rtorrent");
root.erase_key("libtorrent");
}
if (!root.has_key("rtorrent") ||
!root.get_key("rtorrent").is_map())
root.insert_key("rtorrent", torrent::Bencode(torrent::Bencode::TYPE_MAP));
torrent::Bencode& rtorrent = root.get_key("rtorrent");
if (!rtorrent.has_key("state") ||
!rtorrent.get_key("state").is_string() ||
(rtorrent.get_key("state").as_string() != "stopped" &&
rtorrent.get_key("state").as_string() != "started"))
rtorrent.insert_key("state", "stopped");
if (!rtorrent.has_key("tied_to_file") ||
!rtorrent.get_key("tied_to_file").is_string())
rtorrent.insert_key("tied_to_file", std::string());
if (rtorrent.has_key("priority") &&
rtorrent.get_key("priority").is_value())
(*itr)->variables()->set("priority", rtorrent.get_key("priority").as_value() % 4);
else
(*itr)->variables()->set("priority", (int64_t)2);
// Move to 'rtorrent'.
(*itr)->variables()->set("connection_leech", m_variables.get("connection_leech"));
(*itr)->variables()->set("connection_seed", m_variables.get("connection_seed"));
(*itr)->variables()->set("directory", m_variables.get("directory"));
(*itr)->variables()->set("min_peers", control->variables()->get("min_peers"));
(*itr)->variables()->set("max_peers", control->variables()->get("max_peers"));
(*itr)->variables()->set("max_uploads", control->variables()->get("max_uploads"));
if ((*itr)->get_bencode().get_key("rtorrent").has_key("priority") &&
(*itr)->get_bencode().get_key("rtorrent").get_key("priority").is_value())
(*itr)->variables()->set("priority", (*itr)->get_bencode().get_key("rtorrent").get_key("priority").as_value() % 4);
else
(*itr)->variables()->set("priority", (int64_t)2);
torrent::Bencode& bencode = (*itr)->get_bencode();
if (control->variables()->get("use_udp_trackers").as_string() == "no")
(*itr)->enable_udp_trackers(false);
if (m_session) {
// Hmm... this safe?
if (bencode.get_key("rtorrent").get_key("state").as_string() == "started")
if (!rtorrent.has_key("directory") ||
!rtorrent.get_key("directory").is_string())
(*itr)->variables()->set("directory", m_variables.get("directory"));
else
(*itr)->variables()->set("directory", rtorrent.get_key("directory"));
if ((*itr)->variables()->get_string("state") == "started")
m_manager->start(*itr, m_printLog);
// Consider adding an empty 'tied_to_file' here if not present.
} else {
// Remove the settings if this isn't a session torrent.
//bencode.erase_key("rtorrent");
(*itr)->variables()->set("directory", m_variables.get("directory"));
if (m_variables.get("tied_to_file").as_value())
(*itr)->variables()->set("tied_to_file", m_uri);
else
(*itr)->variables()->set("tied_to_file", std::string());
if (m_start)
m_manager->start(*itr, m_printLog);
+12 -1
View File
@@ -71,7 +71,18 @@ DownloadStore::save(Download* d) {
f << d->get_bencode();
if (f.fail())
if (!f.good())
return;
f.close();
// Test the new file, to ensure it is a valid bencode string.
f.open((create_filename(d) + ".new").c_str(), std::ios::in);
torrent::Bencode tmp;
f >> tmp;
if (!f.good())
return;
f.close();
+4 -17
View File
@@ -116,7 +116,7 @@ Manager::initialize_second() {
// Register slots to be called when a download is inserted/erased,
// opened or closed.
m_downloadList.slot_map_insert()["0_initialize_bencode"] = sigc::mem_fun(*this, &Manager::initialize_bencode);
// m_downloadList.slot_map_insert()["0_initialize_bencode"] = sigc::mem_fun(*this, &Manager::initialize_bencode);
m_downloadList.slot_map_insert()["1_connect_network_log"] = sigc::bind(sigc::ptr_fun(&connect_signal_network_log), sigc::mem_fun(m_logComplete, &Log::push_front));
// m_downloadList.slot_map_insert()["1_connect_tracker_log"] = sigc::bind(sigc::ptr_fun(&connect_signal_tracker_log), sigc::mem_fun(m_logComplete, &Log::push_front));
m_downloadList.slot_map_insert()["1_connect_storage_log"] = sigc::bind(sigc::ptr_fun(&connect_signal_storage_log), sigc::mem_fun(m_logComplete, &Log::push_front));
@@ -193,7 +193,7 @@ Manager::erase(DListItr itr) {
void
Manager::start(Download* d, bool printLog) {
try {
d->get_bencode().get_key("rtorrent").insert_key("state", "started");
d->variables()->set("state", "started");
if (d->get_download().is_active())
return;
@@ -218,7 +218,7 @@ Manager::start(Download* d, bool printLog) {
void
Manager::stop(Download* d) {
try {
d->get_bencode().get_key("rtorrent").insert_key("state", "stopped");
d->variables()->set("state", "stopped");
m_downloadList.stop(d);
@@ -294,19 +294,6 @@ Manager::bind(const std::string& addr) {
m_pollManager->get_http_stack()->set_bind_address(torrent::bind_address());
}
void
Manager::initialize_bencode(Download* d) {
torrent::Bencode& bencode = d->get_bencode();
if (!bencode.has_key("rtorrent") ||
!bencode.get_key("rtorrent").is_map())
bencode.insert_key("rtorrent", torrent::Bencode(torrent::Bencode::TYPE_MAP));
if (!bencode.get_key("rtorrent").has_key("state") ||
!bencode.get_key("rtorrent").get_key("state").is_string())
bencode.get_key("rtorrent").insert_key("state", "stopped");
}
void
Manager::prepare_hash_check(Download* d) {
m_downloadList.close(d);
@@ -334,7 +321,7 @@ Manager::receive_download_done_hash_checked(Download* d) {
// Don't send if we did a hash check and found incompelete chunks.
//if (d->is_done())
d->get_download().tracker_send_completed();
d->get_download().tracker_send_completed();
}
void