Compare commits

...

5 Commits

Author SHA1 Message Date
rakshasa 88782d005d Stuff. 2026-09-01 17:46:15 +02:00
rakshasa 5fcd779b15 Stuff. 2026-09-01 17:11:44 +02:00
rakshasa 7104512ac1 Stuff. 2026-09-01 17:10:29 +02:00
rakshasa bd82f6152e Stuff. 2026-09-01 16:40:37 +02:00
rakshasa 83eb4e016a Stuff. 2026-09-01 16:16:30 +02:00
7 changed files with 118 additions and 64 deletions
+1
View File
@@ -187,6 +187,7 @@ libsub_root_a_SOURCES = \
command_helpers.cc \
command_helpers.h \
command_groups.cc \
command_load.cc \
command_local.cc \
command_logging.cc \
command_network.cc \
+1 -36
View File
@@ -1,25 +1,17 @@
#include "config.h"
#include <functional>
#include <cstdio>
#include <string>
#include <vector>
#include <torrent/rate.h>
#include <torrent/hash_string.h>
#include <torrent/exceptions.h>
#include <torrent/utils/log.h>
#include <torrent/utils/directory_events.h>
#include <torrent/utils/file_stat.h>
#include <torrent/utils/string_manip.h>
#include "globals.h"
#include "control.h"
#include "command_helpers.h"
#include "core/download.h"
#include "core/download_list.h"
#include "core/manager.h"
#include "core/view_manager.h"
#include "rpc/command_scheduler.h"
#include "rpc/parse.h"
#include "rpc/parse_commands.h"
#include "utils/watch_ready_queue.h"
@@ -146,24 +138,6 @@ apply_schedule(const torrent::Object::list_type& args, bool if_absent) {
return torrent::Object();
}
torrent::Object
apply_load(const torrent::Object::list_type& args, int flags) {
torrent::Object::list_const_iterator argsItr = args.begin();
if (argsItr == args.end())
throw torrent::input_error("Too few arguments.");
auto& filename = argsItr->as_string();
core::Manager::command_list_type commands;
while (++argsItr != args.end())
commands.push_back(argsItr->as_string());
control->core()->try_create_download_expand(filename, flags, commands);
return torrent::Object();
}
void apply_import(const std::string& path) { if (!rpc::parse_command_file(path)) throw torrent::input_error("Could not open option file: " + path); }
void apply_try_import(const std::string& path) { if (!rpc::parse_command_file(path)) control->core()->push_log_std("Could not read resource file: " + path); }
@@ -368,15 +342,6 @@ initialize_command_events() {
CMD2_ANY_STRING_V("import", [](auto, auto& str) { return apply_import(str); });
CMD2_ANY_STRING_V("try_import", [](auto, auto& str) { return apply_try_import(str); });
CMD2_ANY_LIST ("load.normal", [](auto, auto& args) { return apply_load(args, core::Manager::create_quiet | core::Manager::create_tied); });
CMD2_ANY_LIST ("load.verbose", [](auto, auto& args) { return apply_load(args, core::Manager::create_tied); });
CMD2_ANY_LIST ("load.start", [](auto, auto& args) { return apply_load(args, core::Manager::create_quiet | core::Manager::create_tied | core::Manager::create_start); });
CMD2_ANY_LIST ("load.start_verbose", [](auto, auto& args) { return apply_load(args, core::Manager::create_tied | core::Manager::create_start); });
CMD2_ANY_LIST ("load.raw", [](auto, auto& args) { return apply_load(args, core::Manager::create_quiet | core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.raw_verbose", [](auto, auto& args) { return apply_load(args, core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.raw_start", [](auto, auto& args) { return apply_load(args, core::Manager::create_quiet | core::Manager::create_start | core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.raw_start_verbose", [](auto, auto& args) { return apply_load(args, core::Manager::create_start | core::Manager::create_raw_data); });
CMD2_ANY_VALUE ("close_low_diskspace", [](auto, auto& arg) { return apply_close_low_diskspace(arg, 99); });
CMD2_ANY_VALUE ("close_low_diskspace.normal", [](auto, auto& arg) { return apply_close_low_diskspace(arg, 3); });
+81
View File
@@ -0,0 +1,81 @@
#include "config.h"
#include "command_helpers.h"
#include "control.h"
#include "core/manager.h"
torrent::Object
apply_load_trusted(const torrent::Object::list_type& args, int flags) {
auto argsItr = args.begin();
if (argsItr == args.end())
throw torrent::input_error("Too few arguments.");
auto& filename = argsItr->as_string();
core::Manager::command_list_type commands;
while (++argsItr != args.end())
commands.push_back(argsItr->as_string());
control->core()->try_create_download_expand(filename, flags, commands);
return torrent::Object();
}
torrent::Object
apply_load_untrusted(const torrent::Object::list_type& args, int flags) {
auto argsItr = args.begin();
if (argsItr == args.end())
throw torrent::input_error("Too few arguments.");
auto& filename = argsItr->as_string();
core::Manager::command_list_type commands;
while (++argsItr != args.end())
commands.push_back(argsItr->as_string());
control->core()->try_create_download_untrusted(filename, flags, commands);
return torrent::Object();
}
void
initialize_command_load() {
CMD2_ANY_LIST ("load.normal", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_quiet | core::Manager::create_tied); });
CMD2_ANY_LIST ("load.verbose", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_tied); });
CMD2_ANY_LIST ("load.start", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_quiet | core::Manager::create_tied | core::Manager::create_start); });
CMD2_ANY_LIST ("load.start_verbose", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_tied | core::Manager::create_start); });
CMD2_ANY_LIST ("load.raw", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_quiet | core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.raw_verbose", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.raw_start", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_quiet | core::Manager::create_start | core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.raw_start_verbose", [](auto, auto& args) { return apply_load_trusted(args, core::Manager::create_start | core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.untrusted.normal", [](auto, auto& args) { return apply_load_untrusted(args, core::Manager::create_quiet); });
CMD2_ANY_LIST ("load.untrusted.verbose", [](auto, auto& args) { return apply_load_untrusted(args, 0); });
CMD2_ANY_LIST ("load.untrusted.start", [](auto, auto& args) { return apply_load_untrusted(args, core::Manager::create_quiet | core::Manager::create_start); });
CMD2_ANY_LIST ("load.untrusted.start_verbose", [](auto, auto& args) { return apply_load_untrusted(args, core::Manager::create_start); });
CMD2_ANY_LIST ("load.untrusted.raw", [](auto, auto& args) { return apply_load_untrusted(args, core::Manager::create_quiet | core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.untrusted.raw_verbose", [](auto, auto& args) { return apply_load_untrusted(args, core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.untrusted.raw_start", [](auto, auto& args) { return apply_load_untrusted(args, core::Manager::create_quiet | core::Manager::create_start | core::Manager::create_raw_data); });
CMD2_ANY_LIST ("load.untrusted.raw_start_verbose", [](auto, auto& args) { return apply_load_untrusted(args, core::Manager::create_start | core::Manager::create_raw_data); });
CMD2_ANY_VALUE_V("load.untrusted.mark_safe", [](auto, auto& arg) {
if (arg == 0)
return;
// TODO: Add a bool to core::Manager.
rpc::rpc.mark_safe("load.untrusted.normal");
rpc::rpc.mark_safe("load.untrusted.verbose");
rpc::rpc.mark_safe("load.untrusted.start");
rpc::rpc.mark_safe("load.untrusted.start_verbose");
rpc::rpc.mark_safe("load.untrusted.raw");
rpc::rpc.mark_safe("load.untrusted.raw_verbose");
rpc::rpc.mark_safe("load.untrusted.raw_start");
rpc::rpc.mark_safe("load.untrusted.raw_start_verbose");
});
}
+16 -10
View File
@@ -128,27 +128,33 @@ DownloadFactory::load_untrusted(const std::string& uri) {
}
void
DownloadFactory::load_raw_data(const std::string& input) {
DownloadFactory::load_raw_data_trusted(const std::string& input) {
if (!m_trusted)
throw torrent::internal_error("DownloadFactory::load_raw_data_trusted() called on an untrusted object");
if (m_stream)
throw torrent::internal_error("DownloadFactory::load_raw_data() called on an object with m_stream != NULL");
throw torrent::internal_error("DownloadFactory::load_raw_data_trusted() called on an object with m_stream != NULL");
m_stream.reset(new std::stringstream(input));
m_loaded = true;
}
void
DownloadFactory::commit_trusted() {
if (!m_trusted)
throw torrent::internal_error("DownloadFactory::commit_trusted() called on an untrusted object");
DownloadFactory::load_raw_data_untrusted(const std::string& input) {
if (m_trusted)
throw torrent::internal_error("DownloadFactory::load_raw_data_untrusted() called on an trusted object");
m_task_commit.slot() = [this]() { receive_commit(); };
torrent::this_thread::scheduler()->wait_for(&m_task_commit, 0ms);
if (m_stream)
throw torrent::internal_error("DownloadFactory::load_raw_data_untrusted() called on an object with m_stream != NULL");
m_stream.reset(new std::stringstream(input));
m_loaded = true;
}
void
DownloadFactory::commit_untrusted() {
if (m_trusted)
throw torrent::internal_error("DownloadFactory::commit_untrusted() called on a trusted object");
DownloadFactory::commit() {
if (!m_trusted)
throw torrent::internal_error("DownloadFactory::commit() called on an untrusted object");
m_task_commit.slot() = [this]() { receive_commit(); };
torrent::this_thread::scheduler()->wait_for(&m_task_commit, 0ms);
+3 -3
View File
@@ -33,10 +33,10 @@ public:
void load_trusted(const std::string& uri);
void load_untrusted(const std::string& uri);
void load_raw_data(const std::string& input);
void load_raw_data_trusted(const std::string& input);
void load_raw_data_untrusted(const std::string& input);
void commit_trusted();
void commit_untrusted();
void commit();
command_list_type& commands() { return m_commands; }
torrent::Object::map_type& variables() { return m_variables; }
+12 -11
View File
@@ -197,7 +197,7 @@ Manager::try_create_download(const std::string& uri, int flags, const command_li
return;
// Adding download.
DownloadFactory* f = new DownloadFactory(this);
DownloadFactory* f = new DownloadFactory(this, true);
f->variables()["tied_to_file"] = (int64_t)(bool)(flags & create_tied);
f->commands().insert(f->commands().end(), commands.begin(), commands.end());
@@ -207,16 +207,16 @@ Manager::try_create_download(const std::string& uri, int flags, const command_li
f->slot_finished([f]() { delete f; });
if (flags & create_raw_data) {
f->load_raw_data(uri);
f->load_raw_data_trusted(uri);
} else if (is_data_uri(uri)) {
// Allow the use of data URIs, primarily for JSON-RPC which
// doesn't have a defined mechanism for binary data
f->load_raw_data(decode_data_uri(uri));
f->load_raw_data_trusted(decode_data_uri(uri));
f->variables()["tied_to_file"] = (int64_t)false;
} else {
f->load(uri);
f->load_trusted(uri);
}
f->commit();
@@ -244,13 +244,14 @@ Manager::try_create_download_expand(const std::string& uri, int flags, command_l
void
Manager::try_create_download_from_meta_download(torrent::Object* bencode, const std::string& metafile) {
DownloadFactory* f = new DownloadFactory(this);
DownloadFactory* f = new DownloadFactory(this, true);
f->variables()["tied_to_file"] = (int64_t)true;
f->variables()["tied_file"] = metafile;
f->variables()["tied_file"] = metafile;
torrent::Object& meta = bencode->get_key("rtorrent_meta_download");
torrent::Object::list_type& commands = meta.get_key_list("commands");
for (const auto& command : commands)
f->commands().insert(f->commands().end(), command.as_string());
@@ -263,7 +264,8 @@ Manager::try_create_download_from_meta_download(torrent::Object* bencode, const
std::stringstream s;
s.imbue(std::locale::classic());
s << *bencode;
f->load_raw_data(s.str());
f->load_raw_data_trusted(s.str());
f->commit();
}
@@ -279,8 +281,7 @@ Manager::try_create_download_untrusted(const std::string& uri, int flags, const
return;
}
// Adding download.
DownloadFactory* f = new DownloadFactory(this);
DownloadFactory* f = new DownloadFactory(this, false);
f->commands().insert(f->commands().end(), commands.begin(), commands.end());
@@ -299,10 +300,10 @@ Manager::try_create_download_untrusted(const std::string& uri, int flags, const
} else {
// f->load_untrusted(uri);
throw internal_error("Manager::try_create_download_untrusted() unimplemented.");
throw torrent::internal_error("Manager::try_create_download_untrusted() unimplemented.");
}
f->commit_untrusted();
f->commit();
}
namespace {
+4 -4
View File
@@ -157,12 +157,12 @@ load_session_torrents(const std::string& path) {
if (!entry.is_file())
continue;
auto* f = new core::DownloadFactory(control->core());
auto* f = new core::DownloadFactory(control->core(), true);
f->set_session(true);
f->set_init_load(true);
f->slot_finished([f](){ delete f; });
f->load(entries.path() + entry.s_name);
f->load_trusted(entries.path() + entry.s_name);
f->commit();
}
}
@@ -170,12 +170,12 @@ load_session_torrents(const std::string& path) {
void
load_arg_torrents(char** first, char** last) {
for (; first != last; ++first) {
auto* f = new core::DownloadFactory(control->core());
auto* f = new core::DownloadFactory(control->core(), true);
f->set_start(true);
f->set_init_load(true);
f->slot_finished([f](){ delete f; });
f->load(*first);
f->load_trusted(*first);
f->commit();
}
}