summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarq Schneider <queueRAM@gmail.com>2010-07-22 22:57:40 -0500
committerMarq Schneider <queueRAM@gmail.com>2010-07-22 22:57:40 -0500
commitfd18baa9e62f6defd4a285425bd087a4b27c82d3 (patch)
tree8d60638aa637f0ac5412a011fe13f7e9f6c343ac
parentb1d891c7b134755e60175a6c1cdf25f0e618d159 (diff)
downloadbtpd-fd18baa9e62f6defd4a285425bd087a4b27c82d3.tar.gz
btpd-fd18baa9e62f6defd4a285425bd087a4b27c82d3.zip
Implement start all (btcli start -a) functionality.
Closes GH-7
-rw-r--r--btpd/cli_if.c26
-rw-r--r--cli/start.c31
-rw-r--r--misc/btpd_if.c8
-rw-r--r--misc/btpd_if.h1
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
@@ -324,6 +324,30 @@ cmd_start(struct cli *cli, int argc, const char *args)
 }
 
 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)
 {
     if (argc != 1)
@@ -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
@@ -303,6 +303,14 @@ btpd_start(struct ipc *ipc, struct ipc_torrent *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)
 {
     return simple_treq(ipc, "stop", 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);