浏览代码

btcli add now starts the torrent if not told otherwise. Soem cleanup and

improved error output.
master
Richard Nyberg 18 年前
父节点
当前提交
311a05f613
共有 9 个文件被更改,包括 36 次插入33 次删除
  1. +16
    -3
      cli/add.c
  2. +6
    -6
      cli/btcli.c
  3. +2
    -2
      cli/btcli.h
  4. +1
    -1
      cli/del.c
  5. +3
    -1
      cli/kill.c
  6. +4
    -16
      cli/list.c
  7. +1
    -1
      cli/start.c
  8. +2
    -2
      cli/stat.c
  9. +1
    -1
      cli/stop.c

+ 16
- 3
cli/add.c 查看文件

@@ -27,6 +27,7 @@ usage_add(void)


static struct option add_opts [] = { static struct option add_opts [] = {
{ "help", no_argument, NULL, 'H' }, { "help", no_argument, NULL, 'H' },
{ "nostart", no_argument, NULL, 'N'},
{ "topdir", no_argument, NULL, 'T'}, { "topdir", no_argument, NULL, 'T'},
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };
@@ -34,12 +35,15 @@ static struct option add_opts [] = {
void void
cmd_add(int argc, char **argv) cmd_add(int argc, char **argv)
{ {
int ch, topdir = 0; int ch, topdir = 0, start = 1;
size_t dirlen = 0; size_t dirlen = 0;
char *dir = NULL, *name = NULL; char *dir = NULL, *name = NULL;


while ((ch = getopt_long(argc, argv, "d:n:", add_opts, NULL)) != -1) { while ((ch = getopt_long(argc, argv, "Nd:n:", add_opts, NULL)) != -1) {
switch (ch) { switch (ch) {
case 'N':
start = 0;
break;
case 'T': case 'T':
topdir = 1; topdir = 1;
break; break;
@@ -64,6 +68,7 @@ cmd_add(int argc, char **argv)
btpd_connect(); btpd_connect();
char *mi; char *mi;
size_t mi_size; size_t mi_size;
enum ipc_err code;
char dpath[PATH_MAX]; char dpath[PATH_MAX];
struct io_buffer iob; struct io_buffer iob;


@@ -82,6 +87,14 @@ cmd_add(int argc, char **argv)
buf_swrite(&iob, ""); buf_swrite(&iob, "");
if (realpath(iob.buf, dpath) == NULL) if (realpath(iob.buf, dpath) == NULL)
err(1, "realpath '%s'", dpath); err(1, "realpath '%s'", dpath);
handle_ipc_res(btpd_add(ipc, mi, mi_size, dpath, name), argv[0]); code = btpd_add(ipc, mi, mi_size, dpath, name);
if (code == 0 && start) {
struct ipc_torrent tspec;
tspec.by_hash = 1;
mi_info_hash(mi, tspec.u.hash);
code = btpd_start(ipc, &tspec);
}
if (code != IPC_OK)
errx(1, "%s", ipc_strerror(code));
return; return;
} }

+ 6
- 6
cli/btcli.c 查看文件

@@ -11,15 +11,15 @@ btpd_connect(void)
} }


enum ipc_err enum ipc_err
handle_ipc_res(enum ipc_err code, const char *target) handle_ipc_res(enum ipc_err code, const char *cmd, const char *target)
{ {
switch (code) { switch (code) {
case IPC_OK: case IPC_OK:
break; break;
case IPC_COMMERR: case IPC_COMMERR:
errx(1, "fatal error in communication with btpd"); errx(1, "%s", ipc_strerror(code));
default: default:
warnx("btpd response for '%s': %s", target, ipc_strerror(code)); warnx("%s '%s': %s", cmd, target, ipc_strerror(code));
} }
return code; return code;
} }
@@ -61,7 +61,7 @@ torrent_spec(char *arg, struct ipc_torrent *tp)
return 1; return 1;
} }


struct { static struct {
const char *name; const char *name;
void (*fun)(int, char **); void (*fun)(int, char **);
void (*help)(void); void (*help)(void);
@@ -77,7 +77,7 @@ struct {


int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]); int ncmds = sizeof(cmd_table) / sizeof(cmd_table[0]);


void static void
usage(void) usage(void)
{ {
printf( printf(
@@ -104,7 +104,7 @@ usage(void)
exit(1); exit(1);
} }


struct option base_opts [] = { static struct option base_opts [] = {
{ "help", no_argument, NULL, 'H' }, { "help", no_argument, NULL, 'H' },
{NULL, 0, NULL, 0} {NULL, 0, NULL, 0}
}; };


+ 2
- 2
cli/btcli.h 查看文件

@@ -21,8 +21,8 @@ extern const char *btpd_dir;
extern struct ipc *ipc; extern struct ipc *ipc;


void btpd_connect(void); void btpd_connect(void);
enum ipc_err handle_ipc_res(enum ipc_err err, const char *target); enum ipc_err handle_ipc_res(enum ipc_err err, const char *cmd,
const char *target);
char tstate_char(enum ipc_tstate ts); char tstate_char(enum ipc_tstate ts);
int torrent_spec(char *arg, struct ipc_torrent *tp); int torrent_spec(char *arg, struct ipc_torrent *tp);




+ 1
- 1
cli/del.c 查看文件

@@ -26,5 +26,5 @@ cmd_del(int argc, char **argv)
btpd_connect(); btpd_connect();
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
if (torrent_spec(argv[i], &t)) if (torrent_spec(argv[i], &t))
handle_ipc_res(btpd_del(ipc, &t), argv[i]); handle_ipc_res(btpd_del(ipc, &t), "del", argv[i]);
} }

+ 3
- 1
cli/kill.c 查看文件

@@ -21,6 +21,7 @@ void
cmd_kill(int argc, char **argv) cmd_kill(int argc, char **argv)
{ {
int seconds = -1; int seconds = -1;
enum ipc_err code;
char *endptr; char *endptr;


if (argc == 2) { if (argc == 2) {
@@ -31,5 +32,6 @@ cmd_kill(int argc, char **argv)
usage_kill(); usage_kill();


btpd_connect(); btpd_connect();
handle_ipc_res(btpd_die(ipc, seconds), "kill"); if ((code = btpd_die(ipc, seconds)) != 0)
errx(1, "%s", ipc_strerror(code));
} }

+ 4
- 16
cli/list.c 查看文件

@@ -30,9 +30,6 @@ itm_insert(struct items *itms, struct item *itm)
struct item *p; struct item *p;
BTPDQ_FOREACH(p, &itms->hd, entry) BTPDQ_FOREACH(p, &itms->hd, entry)
if (itm->num < p->num) if (itm->num < p->num)
#if 0
if (strcmp(itm->name, p->name) < 0)
#endif
break; break;
if (p != NULL) if (p != NULL)
BTPDQ_INSERT_BEFORE(p, itm, entry); BTPDQ_INSERT_BEFORE(p, itm, entry);
@@ -54,17 +51,6 @@ list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
asprintf(&itm->name, "%.*s", (int)res[IPC_TVAL_NAME].v.str.l, asprintf(&itm->name, "%.*s", (int)res[IPC_TVAL_NAME].v.str.l,
res[IPC_TVAL_NAME].v.str.p); res[IPC_TVAL_NAME].v.str.p);
itm_insert(itms, itm); itm_insert(itms, itm);
#if 0
int *count = arg;
(*count)++;
printf("%4u %c.", (unsigned)res[IPC_TVAL_NUM].v.num,
tstate_char(res[IPC_TVAL_STATE].v.num));
if (res[IPC_TVAL_NAME].type == IPC_TYPE_ERR)
printf(" %s\n", ipc_strerror(res[IPC_TVAL_NAME].v.num));
else
printf(" %.*s\n", (int)res[IPC_TVAL_NAME].v.str.l,
res[IPC_TVAL_NAME].v.str.p);
#endif
} }


void void
@@ -90,7 +76,8 @@ static struct option list_opts [] = {
void void
cmd_list(int argc, char **argv) cmd_list(int argc, char **argv)
{ {
int ch, /*count = 0,*/ inactive = 0, active = 0; int ch, inactive = 0, active = 0;
enum ipc_err code;
enum ipc_twc twc; enum ipc_twc twc;
enum ipc_tval keys[] = { IPC_TVAL_NUM, IPC_TVAL_STATE, IPC_TVAL_NAME }; enum ipc_tval keys[] = { IPC_TVAL_NUM, IPC_TVAL_STATE, IPC_TVAL_NAME };
struct items itms; struct items itms;
@@ -118,7 +105,8 @@ cmd_list(int argc, char **argv)
printf("NUM ST NAME\n"); printf("NUM ST NAME\n");
itms.count = 0; itms.count = 0;
BTPDQ_INIT(&itms.hd); BTPDQ_INIT(&itms.hd);
handle_ipc_res(btpd_tget_wc(ipc, twc, keys, 3, list_cb, &itms), "tget"); if ((code = btpd_tget_wc(ipc, twc, keys, 3, list_cb, &itms)) != IPC_OK)
errx(1, "%s", ipc_strerror(code));
print_items(&itms); print_items(&itms);
printf("Listed %d torrent%s.\n", itms.count, itms.count == 1 ? "" : "s"); printf("Listed %d torrent%s.\n", itms.count, itms.count == 1 ? "" : "s");
} }

+ 1
- 1
cli/start.c 查看文件

@@ -23,5 +23,5 @@ cmd_start(int argc, char **argv)
btpd_connect(); btpd_connect();
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
if (torrent_spec(argv[i], &t)) if (torrent_spec(argv[i], &t))
handle_ipc_res(btpd_start(ipc, &t), argv[i]); handle_ipc_res(btpd_start(ipc, &t), "start", argv[i]);
} }

+ 2
- 2
cli/stat.c 查看文件

@@ -162,8 +162,8 @@ again:
stat_cb, &cba); stat_cb, &cba);
else else
err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba); err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba);
if (handle_ipc_res(err, "stat") != IPC_OK) if (err != IPC_OK)
exit(1); errx(1, ipc_strerror(err));
if (names) if (names)
printf("-----\n"); printf("-----\n");
if (individual) if (individual)


+ 1
- 1
cli/stop.c 查看文件

@@ -23,5 +23,5 @@ cmd_stop(int argc, char **argv)
btpd_connect(); btpd_connect();
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
if (torrent_spec(argv[i], &t)) if (torrent_spec(argv[i], &t))
handle_ipc_res(btpd_stop(ipc, &t), argv[i]); handle_ipc_res(btpd_stop(ipc, &t), "stop", argv[i]);
} }

||||||
x
 
000:0
正在加载...
取消
保存