A clone of btpd with my configuration changes.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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