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.

add.c 2.4 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. { "nostart", no_argument, NULL, 'N'},
  29. { "topdir", no_argument, NULL, 'T'},
  30. {NULL, 0, NULL, 0}
  31. };
  32. void
  33. cmd_add(int argc, char **argv)
  34. {
  35. int ch, topdir = 0, start = 1;
  36. size_t dirlen = 0;
  37. char *dir = NULL, *name = NULL;
  38. while ((ch = getopt_long(argc, argv, "Nd:n:", add_opts, NULL)) != -1) {
  39. switch (ch) {
  40. case 'N':
  41. start = 0;
  42. break;
  43. case 'T':
  44. topdir = 1;
  45. break;
  46. case 'd':
  47. dir = optarg;
  48. if ((dirlen = strlen(dir)) == 0)
  49. errx(1, "bad option value for -d");
  50. break;
  51. case 'n':
  52. name = optarg;
  53. break;
  54. default:
  55. usage_add();
  56. }
  57. }
  58. argc -= optind;
  59. argv += optind;
  60. if (argc != 1 || dir == NULL)
  61. usage_add();
  62. btpd_connect();
  63. char *mi;
  64. size_t mi_size;
  65. enum ipc_err code;
  66. char dpath[PATH_MAX];
  67. struct io_buffer iob;
  68. if ((mi = mi_load(argv[0], &mi_size)) == NULL)
  69. err(1, "error loading '%s'", argv[0]);
  70. iob = buf_init(PATH_MAX);
  71. buf_write(&iob, dir, dirlen);
  72. if (topdir) {
  73. size_t tdlen;
  74. const char *td =
  75. benc_dget_mem(benc_dget_dct(mi, "info"), "name", &tdlen);
  76. buf_swrite(&iob, "/");
  77. buf_write(&iob, td, tdlen);
  78. }
  79. buf_swrite(&iob, "");
  80. if (realpath(iob.buf, dpath) == NULL)
  81. err(1, "realpath '%s'", dpath);
  82. code = btpd_add(ipc, mi, mi_size, dpath, name);
  83. if (code == 0 && start) {
  84. struct ipc_torrent tspec;
  85. tspec.by_hash = 1;
  86. mi_info_hash(mi, tspec.u.hash);
  87. code = btpd_start(ipc, &tspec);
  88. }
  89. if (code != IPC_OK)
  90. errx(1, "%s", ipc_strerror(code));
  91. return;
  92. }