From fd18baa9e62f6defd4a285425bd087a4b27c82d3 Mon Sep 17 00:00:00 2001 From: Marq Schneider Date: Thu, 22 Jul 2010 22:57:40 -0500 Subject: Implement start all (btcli start -a) functionality. Closes GH-7 --- btpd/cli_if.c | 26 ++++++++++++++++++++++++++ cli/start.c | 31 ++++++++++++++++++++++++------- misc/btpd_if.c | 8 ++++++++ misc/btpd_if.h | 1 + 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/btpd/cli_if.c b/btpd/cli_if.c index dd5ddcb..7479035 100644 --- a/btpd/cli_if.c +++ b/btpd/cli_if.c @@ -323,6 +323,30 @@ cmd_start(struct cli *cli, int argc, const char *args) return write_code_buffer(cli, code); } +static int +cmd_start_all(struct cli *cli, int argc, const char *args) +{ + struct htbl_iter it; + struct tlib *tl; + enum ipc_err last_code, ret_code= IPC_OK; + + if (btpd_is_stopping()) + return write_code_buffer(cli, IPC_ESHUTDOWN); + + for (tl = tlib_iter_first(&it); tl != NULL; tl = tlib_iter_next(&it)) { + if (torrent_startable(tl)) { + if ((last_code = torrent_start(tl)) == IPC_OK) { + active_add(tl->hash); + } else { + btpd_err("torrent_start(%d) failed.\n", tl->num); + ret_code = last_code; + } + } + } + + return write_code_buffer(cli, ret_code); +} + static int cmd_stop(struct cli *cli, int argc, const char *args) { @@ -355,6 +379,7 @@ cmd_stop_all(struct cli *cli, int argc, const char *args) { struct torrent *tp, *next; int ret = write_code_buffer(cli, IPC_OK); + active_clear(); BTPDQ_FOREACH_MUTABLE(tp, torrent_get_all(), entry, next) torrent_stop(tp, 0); @@ -381,6 +406,7 @@ static struct { { "del", 3, cmd_del }, { "die", 3, cmd_die }, { "start", 5, cmd_start }, + { "start-all", 9, cmd_start_all}, { "stop", 4, cmd_stop }, { "stop-all", 8, cmd_stop_all}, { "tget", 4, cmd_tget } diff --git a/cli/start.c b/cli/start.c index e3a3684..2bd55ad 100644 --- a/cli/start.c +++ b/cli/start.c @@ -12,6 +12,10 @@ usage_start(void) "torrent ...\n" "\tThe torrents to activate.\n" "\n" + "Options:\n" + "-a\n" + "\tActivate all inactive torrents.\n" + "\n" ); exit(1); } @@ -24,19 +28,32 @@ static struct option start_opts [] = { void cmd_start(int argc, char **argv) { - int ch; + int ch, all = 0; struct ipc_torrent t; - while ((ch = getopt_long(argc, argv, "", start_opts, NULL)) != -1) - usage_start(); + while ((ch = getopt_long(argc, argv, "a", start_opts, NULL)) != -1) { + switch (ch) { + case 'a': + all = 1; + break; + default: + usage_start(); + } + } argc -= optind; argv += optind; - if (argc < 1) + if ((argc == 0 && !all) || (all && argc != 0)) usage_start(); btpd_connect(); - for (int i = 0; i < argc; i++) - if (torrent_spec(argv[i], &t)) - handle_ipc_res(btpd_start(ipc, &t), "start", argv[i]); + if (all) { + enum ipc_err code = btpd_start_all(ipc); + if (code != IPC_OK) + diemsg("command failed (%s).\n", ipc_strerror(code)); + } else { + for (int i = 0; i < argc; i++) + if (torrent_spec(argv[i], &t)) + handle_ipc_res(btpd_start(ipc, &t), "start", argv[i]); + } } diff --git a/misc/btpd_if.c b/misc/btpd_if.c index a62daba..b661b21 100644 --- a/misc/btpd_if.c +++ b/misc/btpd_if.c @@ -302,6 +302,14 @@ btpd_start(struct ipc *ipc, struct ipc_torrent *tp) return simple_treq(ipc, "start", tp); } +enum ipc_err +btpd_start_all(struct ipc *ipc) +{ + struct iobuf iob = iobuf_init(16); + iobuf_swrite(&iob, "l9:start-alle"); + return ipc_buf_req_code(ipc, &iob); +} + enum ipc_err btpd_stop(struct ipc *ipc, struct ipc_torrent *tp) { diff --git a/misc/btpd_if.h b/misc/btpd_if.h index 526e5c3..3bf77d1 100644 --- a/misc/btpd_if.h +++ b/misc/btpd_if.h @@ -78,6 +78,7 @@ enum ipc_err btpd_add(struct ipc *ipc, const char *mi, size_t mi_size, const char *content, const char *name); enum ipc_err btpd_del(struct ipc *ipc, struct ipc_torrent *tp); enum ipc_err btpd_start(struct ipc *ipc, struct ipc_torrent *tp); +enum ipc_err btpd_start_all(struct ipc *ipc); enum ipc_err btpd_stop(struct ipc *ipc, struct ipc_torrent *tp); enum ipc_err btpd_stop_all(struct ipc *ipc); enum ipc_err btpd_die(struct ipc *ipc); -- cgit 1.4.1