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.

130 lines
3.4 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 [-n name] [-T] [-N] -d dir file(s)\n"
  9. "\n"
  10. "Arguments:\n"
  11. "file\n"
  12. "\tThe torrent file to add.\n"
  13. "\n"
  14. "Options:\n"
  15. "-d dir\n"
  16. "\tUse the dir for content.\n"
  17. "\n"
  18. "-n name\n"
  19. "\tSet the name displayed for this torrent.\n"
  20. "\n"
  21. "-l label\n"
  22. "\tSet the label to associate with torrent.\n"
  23. "\n"
  24. "--nostart, -N\n"
  25. "\tDon't activate the torrent after adding it.\n"
  26. "\n"
  27. "--topdir, -T\n"
  28. "\tAppend the torrent top directory (if any) to the content path.\n"
  29. "\n"
  30. );
  31. exit(1);
  32. }
  33. static struct option add_opts [] = {
  34. { "help", no_argument, NULL, 'H' },
  35. { "nostart", no_argument, NULL, 'N'},
  36. { "topdir", no_argument, NULL, 'T'},
  37. {NULL, 0, NULL, 0}
  38. };
  39. void
  40. cmd_add(int argc, char **argv)
  41. {
  42. int ch, topdir = 0, start = 1, nfile, nloaded = 0;
  43. size_t dirlen = 0, labellen = 0;
  44. char *dir = NULL, *name = NULL, *glabel = NULL, *label;
  45. while ((ch = getopt_long(argc, argv, "NTd:l:n:", add_opts, NULL)) != -1) {
  46. switch (ch) {
  47. case 'N':
  48. start = 0;
  49. break;
  50. case 'T':
  51. topdir = 1;
  52. break;
  53. case 'd':
  54. dir = optarg;
  55. if ((dirlen = strlen(dir)) == 0)
  56. diemsg("bad option value for -d.\n");
  57. break;
  58. case 'l':
  59. glabel = optarg;
  60. if ((labellen = strlen(dir)) == 0)
  61. diemsg("bad option value for -l.\n");
  62. break;
  63. case 'n':
  64. name = optarg;
  65. break;
  66. default:
  67. usage_add();
  68. }
  69. }
  70. argc -= optind;
  71. argv += optind;
  72. if (argc < 1 || dir == NULL)
  73. usage_add();
  74. btpd_connect();
  75. char *mi;
  76. size_t mi_size;
  77. enum ipc_err code;
  78. char dpath[PATH_MAX];
  79. struct iobuf iob;
  80. for (nfile = 0; nfile < argc; nfile++) {
  81. if ((mi = mi_load(argv[nfile], &mi_size)) == NULL) {
  82. fprintf(stderr, "error loading '%s' (%s).\n", argv[nfile], strerror(errno));
  83. continue;
  84. }
  85. iob = iobuf_init(PATH_MAX);
  86. iobuf_write(&iob, dir, dirlen);
  87. if (topdir && !mi_simple(mi)) {
  88. size_t tdlen;
  89. const char *td =
  90. benc_dget_mem(benc_dget_dct(mi, "info"), "name", &tdlen);
  91. iobuf_swrite(&iob, "/");
  92. iobuf_write(&iob, td, tdlen);
  93. }
  94. iobuf_swrite(&iob, "\0");
  95. if ((errno = make_abs_path(iob.buf, dpath)) != 0) {
  96. fprintf(stderr, "make_abs_path '%s' failed (%s).\n", dpath, strerror(errno));
  97. iobuf_free(&iob);
  98. continue;
  99. }
  100. if(NULL == glabel)
  101. label = benc_dget_str(mi, "announce", NULL);
  102. else
  103. label = glabel;
  104. code = btpd_add(ipc, mi, mi_size, dpath, name, label);
  105. if ((code == IPC_OK) && start) {
  106. struct ipc_torrent tspec;
  107. tspec.by_hash = 1;
  108. mi_info_hash(mi, tspec.u.hash);
  109. code = btpd_start(ipc, &tspec);
  110. }
  111. if (code != IPC_OK) {
  112. fprintf(stderr, "command failed for '%s' (%s).\n", argv[nfile], ipc_strerror(code));
  113. } else {
  114. nloaded++;
  115. }
  116. iobuf_free(&iob);
  117. }
  118. if (nloaded != nfile) {
  119. diemsg("error loaded %d of %d files.\n", nloaded, nfile);
  120. }
  121. }