int torrent_spec(char *arg, struct ipc_torrent *tp) { char *p; tp->u.num = strtoul(arg, &p, 10); if (*p == '\0') { tp->by_hash = 0; return 1; } if ((p = mi_load(arg, NULL)) == NULL) { printf("btcli: bad torrent '%s' (%s).\n", arg, strerror(errno)); return 0; } tp->by_hash = 1; mi_info_hash(p, tp->u.hash); free(p); return 1; }
static int cmd_add(struct cli *cli, int argc, const char *args) { if (argc != 1 || !benc_isdct(args)) return IPC_COMMERR; struct tlib *tl; size_t mi_size = 0, csize = 0; const char *mi, *cp; char content[PATH_MAX]; uint8_t hash[20]; if ((mi = benc_dget_mem(args, "torrent", &mi_size)) == NULL) return IPC_COMMERR; if (!mi_test(mi, mi_size)) return write_code_buffer(cli, IPC_EBADT); if ((cp = benc_dget_mem(args, "content", &csize)) == NULL || csize >= PATH_MAX || csize == 0) return write_code_buffer(cli, IPC_EBADCDIR); if (cp[0] != '/') return write_code_buffer(cli, IPC_EBADCDIR); bcopy(cp, content, csize); content[csize] = '\0'; tl = tlib_by_hash(mi_info_hash(mi, hash)); if (tl != NULL && !torrent_haunting(tl)) 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, "label", NULL)); } else { tl = tlib_add(hash, mi, mi_size, content, benc_dget_str(args, "name", NULL), benc_dget_str(args, "label", NULL)); } return write_add_buffer(cli, tl->num); }
void cmd_add(int argc, char **argv) { int ch, topdir = 0, start = 1, nfile, nloaded = 0; size_t dirlen = 0, labellen = 0; char *dir = NULL, *name = NULL, *glabel = NULL, *label; while ((ch = getopt_long(argc, argv, "NTd:l:n:", add_opts, NULL)) != -1) { switch (ch) { case 'N': start = 0; break; case 'T': topdir = 1; break; case 'd': dir = optarg; 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; default: usage_add(); } } argc -= optind; argv += optind; if (argc < 1 || dir == NULL) usage_add(); btpd_connect(); char *mi; size_t mi_size; enum ipc_err code; char dpath[PATH_MAX]; struct iobuf iob; for (nfile = 0; nfile < argc; nfile++) { if ((mi = mi_load(argv[nfile], &mi_size)) == NULL) { fprintf(stderr, "error loading '%s' (%s).\n", argv[nfile], strerror(errno)); continue; } iob = iobuf_init(PATH_MAX); iobuf_write(&iob, dir, dirlen); if (topdir && !mi_simple(mi)) { size_t tdlen; const char *td = benc_dget_mem(benc_dget_dct(mi, "info"), "name", &tdlen); iobuf_swrite(&iob, "/"); iobuf_write(&iob, td, tdlen); } iobuf_swrite(&iob, "\0"); if ((errno = make_abs_path(iob.buf, dpath)) != 0) { fprintf(stderr, "make_abs_path '%s' failed (%s).\n", dpath, strerror(errno)); iobuf_free(&iob); continue; } 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; mi_info_hash(mi, tspec.u.hash); code = btpd_start(ipc, &tspec); } if (code != IPC_OK) { fprintf(stderr, "command failed for '%s' (%s).\n", argv[nfile], ipc_strerror(code)); } else { nloaded++; } iobuf_free(&iob); } if (nloaded != nfile) { diemsg("error loaded %d of %d files.\n", nloaded, nfile); } }