A clone of btpd with my configuration changes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.0 KiB

  1. #include "btcli.h"
  2. void
  3. usage_add(void)
  4. {
  5. printf(
  6. "Add torrents to btpd.\n"
  7. "\n"
  8. "Usage: add [--topdir] -d dir file\n"
  9. " add file ...\n"
  10. "\n"
  11. "Arguments:\n"
  12. "file ...\n"
  13. "\tOne or more torrents to add.\n"
  14. "\n"
  15. "Options:\n"
  16. "-d dir\n"
  17. "\tUse the dir for content.\n"
  18. "\n"
  19. "--topdir\n"
  20. "\tAppend the torrent top directory (if any) to the content path.\n"
  21. "\tThis option cannot be used without the '-d' option.\n"
  22. "\n"
  23. );
  24. exit(1);
  25. }
  26. static struct option add_opts [] = {
  27. { "help", no_argument, NULL, 'H' },
  28. { "topdir", no_argument, NULL, 'T'},
  29. {NULL, 0, NULL, 0}
  30. };
  31. void
  32. cmd_add(int argc, char **argv)
  33. {
  34. int ch, topdir = 0;
  35. size_t dirlen = 0;
  36. char *dir = NULL, *name = NULL;
  37. while ((ch = getopt_long(argc, argv, "d:n:", add_opts, NULL)) != -1) {
  38. switch (ch) {
  39. case 'T':
  40. topdir = 1;
  41. break;
  42. case 'd':
  43. dir = optarg;
  44. if ((dirlen = strlen(dir)) == 0)
  45. errx(1, "bad option value for -d");
  46. break;
  47. case 'n':
  48. name = optarg;
  49. break;
  50. default:
  51. usage_add();
  52. }
  53. }
  54. argc -= optind;
  55. argv += optind;
  56. if (argc != 1 || dir == NULL)
  57. usage_add();
  58. btpd_connect();
  59. char *mi;
  60. size_t mi_size;
  61. char dpath[PATH_MAX];
  62. struct io_buffer iob;
  63. if ((mi = mi_load(argv[0], &mi_size)) == NULL)
  64. err(1, "error loading '%s'", argv[0]);
  65. buf_init(&iob, PATH_MAX);
  66. buf_write(&iob, dir, dirlen);
  67. if (topdir) {
  68. size_t tdlen;
  69. const char *td =
  70. benc_dget_mem(benc_dget_dct(mi, "info"), "name", &tdlen);
  71. buf_swrite(&iob, "/");
  72. buf_write(&iob, td, tdlen);
  73. }
  74. buf_swrite(&iob, "");
  75. if (realpath(iob.buf, dpath) == NULL)
  76. err(1, "realpath '%s'", dpath);
  77. handle_ipc_res(btpd_add(ipc, mi, mi_size, dpath, name), argv[0]);
  78. return;
  79. }