Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add optional label to be associated with torrent.

If none given, use announce URL as label.
Torrents added with previous versions of btpd will show up as "bad torrent entry"

Closes GH-26
  • Loading branch information...
commit 2fd1f7c34e9ee85d5b995dc165fe8531fcb0ad31 1 parent b0b8a2f
@queueRAM queueRAM authored
View
12 btpd/cli_if.c
@@ -158,6 +158,12 @@ write_ans(struct iobuf *iob, struct tlib *tl, enum ipc_tval val)
iobuf_print(iob, "i%dei%de", IPC_TYPE_NUM,
tl->tp == NULL ? 0 : tr_good_count(tl->tp));
return;
+ case IPC_TVAL_LABEL:
+ if (tl->label != NULL)
+ iobuf_print(iob, "i%de%d:%s", IPC_TYPE_STR, (int)strlen(tl->label), tl->label);
+ else
+ iobuf_print(iob, "i%dei%de", IPC_TYPE_ERR, IPC_EBADTENT);
+ return;
case IPC_TVALCOUNT:
break;
}
@@ -261,10 +267,12 @@ cmd_add(struct cli *cli, int argc, const char *args)
return write_code_buffer(cli, IPC_ETENTEXIST);
if (tl != NULL) {
tl = tlib_readd(tl, hash, mi, mi_size, content,
- benc_dget_str(args, "name", NULL));
+ benc_dget_str(args, "name", NULL),
+ benc_dget_str(args, "label", NULL));
} else {
tl = tlib_add(hash, mi, mi_size, content,
- benc_dget_str(args, "name", NULL));
+ benc_dget_str(args, "name", NULL),
+ benc_dget_str(args, "label", NULL));
}
return write_add_buffer(cli, tl->num);
}
View
12 btpd/tlib.c
@@ -51,6 +51,8 @@ tlib_kill(struct tlib *tl)
free(tl->name);
if (tl->dir != NULL)
free(tl->dir);
+ if (tl->label != NULL)
+ free(tl->label);
free(tl);
m_ntlibs--;
}
@@ -166,6 +168,7 @@ load_info(struct tlib *tl, const char *path)
info = benc_dget_dct(buf, "info");
tl->name = benc_dget_str(info, "name", NULL);
+ tl->label = benc_dget_str(info, "label", NULL);
tl->dir = benc_dget_str(info, "dir", NULL);
tl->tot_up = benc_dget_int(info, "total upload");
tl->tot_down = benc_dget_int(info, "total download");
@@ -186,10 +189,12 @@ save_info(struct tlib *tl)
"d4:infod"
"12:content havei%llde12:content sizei%llde"
"3:dir%d:%s4:name%d:%s"
+ "5:label%d:%s"
"14:total downloadi%llde12:total uploadi%llde"
"ee",
(long long)tl->content_have, (long long)tl->content_size,
(int)strlen(tl->dir), tl->dir, (int)strlen(tl->name), tl->name,
+ (int)strlen(tl->label), tl->label,
tl->tot_down, tl->tot_up);
if (iob.error)
btpd_err("Out of memory.\n");
@@ -245,7 +250,7 @@ write_torrent(const char *mi, size_t mi_size, const char *path)
struct tlib *
tlib_add(const uint8_t *hash, const char *mi, size_t mi_size,
- const char *content, char *name)
+ const char *content, char *name, char *label)
{
struct tlib *tl = tlib_create(hash);
char relpath[RELPATH_SIZE], file[PATH_MAX];
@@ -257,6 +262,7 @@ tlib_add(const uint8_t *hash, const char *mi, size_t mi_size,
tl->content_size = mi_total_length(mi);
tl->name = name;
+ tl->label = label;
tl->dir = strdup(content);
if (tl->name == NULL || tl->dir == NULL)
btpd_err("out of memory.\n");
@@ -272,13 +278,13 @@ tlib_add(const uint8_t *hash, const char *mi, size_t mi_size,
struct tlib *
tlib_readd(struct tlib *tl, const uint8_t *hash, const char *mi,
- size_t mi_size, const char *content, char *name)
+ size_t mi_size, const char *content, char *name, char *label)
{
struct tlib *tln;
struct torrent *tp = tl->tp;
tp->delete = 0;
tlib_kill(tl);
- tln = tlib_add(hash, mi, mi_size, content, name);
+ tln = tlib_add(hash, mi, mi_size, content, name, label);
tln->tp = tp;
return tln;
}
View
5 btpd/tlib.h
@@ -8,6 +8,7 @@ struct tlib {
char *name;
char *dir;
+ char *label;
unsigned long long tot_up, tot_down;
off_t content_size, content_have;
@@ -27,9 +28,9 @@ struct tlib *tlib_iter_first(struct htbl_iter *it);
struct tlib *tlib_iter_next(struct htbl_iter *it);
struct tlib *tlib_add(const uint8_t *hash, const char *mi, size_t mi_size,
- const char *content, char *name);
+ const char *content, char *name, char *label);
struct tlib *tlib_readd(struct tlib *tl, const uint8_t *hash, const char *mi,
- size_t mi_size, const char *content, char *name);
+ size_t mi_size, const char *content, char *name, char *label);
int tlib_del(struct tlib *tl);
void tlib_kill(struct tlib *tl);
View
17 cli/add.c
@@ -40,10 +40,10 @@ void
cmd_add(int argc, char **argv)
{
int ch, topdir = 0, start = 1, nfile, nloaded = 0;
- size_t dirlen = 0;
- char *dir = NULL, *name = NULL;
+ size_t dirlen = 0, labellen = 0;
+ char *dir = NULL, *name = NULL, *glabel = NULL, *label;
- while ((ch = getopt_long(argc, argv, "NTd:n:", add_opts, NULL)) != -1) {
+ while ((ch = getopt_long(argc, argv, "NTd:l:n:", add_opts, NULL)) != -1) {
switch (ch) {
case 'N':
start = 0;
@@ -56,6 +56,11 @@ cmd_add(int argc, char **argv)
if ((dirlen = strlen(dir)) == 0)
diemsg("bad option value for -d.\n");
break;
+ case 'l':
+ glabel = optarg;
+ if ((labellen = strlen(dir)) == 0)
+ diemsg("bad option value for -l.\n");
+ break;
case 'n':
name = optarg;
break;
@@ -96,7 +101,11 @@ cmd_add(int argc, char **argv)
iobuf_free(&iob);
continue;
}
- code = btpd_add(ipc, mi, mi_size, dpath, name);
+ if(NULL == glabel)
+ label = benc_dget_str(mi, "announce", NULL);
+ else
+ label = glabel;
+ code = btpd_add(ipc, mi, mi_size, dpath, name, label);
if ((code == IPC_OK) && start) {
struct ipc_torrent tspec;
tspec.by_hash = 1;
View
10 cli/list.c
@@ -28,7 +28,7 @@ usage_list(void)
struct item {
unsigned num, peers;
- char *name, *dir;
+ char *name, *dir, *label;
char hash[SHAHEXSIZE];
char st;
long long cgot, csize, totup, downloaded, uploaded, rate_up, rate_down;
@@ -79,6 +79,11 @@ list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
else
asprintf(&itm->dir, "%.*s", (int)res[IPC_TVAL_DIR].v.str.l,
res[IPC_TVAL_DIR].v.str.p);
+ if (res[IPC_TVAL_LABEL].type == IPC_TYPE_ERR)
+ asprintf(&itm->label, "%s", ipc_strerror(res[IPC_TVAL_LABEL].v.num));
+ else
+ asprintf(&itm->label, "%.*s", (int)res[IPC_TVAL_LABEL].v.str.l,
+ res[IPC_TVAL_LABEL].v.str.p);
bin2hex(res[IPC_TVAL_IHASH].v.str.p, itm->hash, 20);
itm->cgot = res[IPC_TVAL_CGOT].v.num;
itm->csize = res[IPC_TVAL_CSIZE].v.num;
@@ -121,6 +126,7 @@ print_items(struct items* itms, char *format)
case 'd': printf("%s", p->dir); break;
case 'g': printf("%lld", p->cgot); break;
case 'h': printf("%s", p->hash); break;
+ case 'l': printf("%s", p->label); break;
case 'n': printf("%s", p->name); break;
case 'p': print_percent(p->cgot, p->csize); break;
case 'r': print_ratio(p->totup, p->csize); break;
@@ -170,7 +176,7 @@ cmd_list(int argc, char **argv)
IPC_TVAL_TOTUP, IPC_TVAL_CSIZE, IPC_TVAL_CGOT, IPC_TVAL_PCOUNT,
IPC_TVAL_PCCOUNT, IPC_TVAL_PCSEEN, IPC_TVAL_PCGOT, IPC_TVAL_SESSUP,
IPC_TVAL_SESSDWN, IPC_TVAL_RATEUP, IPC_TVAL_RATEDWN, IPC_TVAL_IHASH,
- IPC_TVAL_DIR };
+ IPC_TVAL_DIR, IPC_TVAL_LABEL };
size_t nkeys = ARRAY_COUNT(keys);
struct items itms;
while ((ch = getopt_long(argc, argv, "aif:", list_opts, NULL)) != -1) {
View
4 misc/btpd_if.c
@@ -264,13 +264,15 @@ btpd_tget_wc(struct ipc *ipc, enum ipc_twc twc, enum ipc_tval *keys,
enum ipc_err
btpd_add(struct ipc *ipc, const char *mi, size_t mi_size, const char *content,
- const char *name)
+ const char *name, const char *label)
{
struct iobuf iob = iobuf_init(1 << 10);
iobuf_print(&iob, "l3:addd7:content%d:%s", (int)strlen(content),
content);
if (name != NULL)
iobuf_print(&iob, "4:name%d:%s", (int)strlen(name), name);
+ if (label != NULL)
+ iobuf_print(&iob, "5:label%d:%s", (int)strlen(label), label);
iobuf_print(&iob, "7:torrent%lu:", (unsigned long)mi_size);
iobuf_write(&iob, mi, mi_size);
iobuf_swrite(&iob, "ee");
View
2  misc/btpd_if.h
@@ -75,7 +75,7 @@ void ipc_close(struct ipc *ipc);
const char *ipc_strerror(enum ipc_err err);
enum ipc_err btpd_add(struct ipc *ipc, const char *mi, size_t mi_size,
- const char *content, const char *name);
+ const char *content, const char *name, const char *label);
enum ipc_err btpd_del(struct ipc *ipc, struct ipc_torrent *tp);
enum ipc_err btpd_rate(struct ipc *ipc, unsigned up, unsigned down);
enum ipc_err btpd_start(struct ipc *ipc, struct ipc_torrent *tp);
View
3  misc/ipcdefs.h
@@ -41,7 +41,8 @@ TVDEF(STATE, TSTATE, "state")
TVDEF(TOTDWN, NUM, "total_down")
TVDEF(TOTUP, NUM, "total_up")
TVDEF(TRERR, NUM, "tr_errors")
-TVDEF(TRGOOD, NUM, "tr_good")
+TVDEF(TRGOOD, NUM, "tr_good")
+TVDEF(LABEL, STR, "label")
#ifdef __IPCTV
#undef __IPCTV
#undef TVDEF
Please sign in to comment.
Something went wrong with that request. Please try again.