A clone of btpd with my configuration changes.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

105 lignes
2.5 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. "\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\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;
  40. size_t dirlen = 0;
  41. char *dir = NULL, *name = NULL;
  42. while ((ch = getopt_long(argc, argv, "Nd: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. errx(1, "bad option value for -d");
  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 io_buffer iob;
  72. if ((mi = mi_load(argv[0], &mi_size)) == NULL)
  73. err(1, "error loading '%s'", argv[0]);
  74. iob = buf_init(PATH_MAX);
  75. buf_write(&iob, dir, dirlen);
  76. if (topdir && !mi_simple(mi)) {
  77. size_t tdlen;
  78. const char *td =
  79. benc_dget_mem(benc_dget_dct(mi, "info"), "name", &tdlen);
  80. buf_swrite(&iob, "/");
  81. buf_write(&iob, td, tdlen);
  82. }
  83. buf_swrite(&iob, "\0");
  84. if ((errno = make_abs_path(iob.buf, dpath)) != 0)
  85. err(1, "make_abs_path '%s'", dpath);
  86. code = btpd_add(ipc, mi, mi_size, dpath, name);
  87. if (code == 0 && start) {
  88. struct ipc_torrent tspec;
  89. tspec.by_hash = 1;
  90. mi_info_hash(mi, tspec.u.hash);
  91. code = btpd_start(ipc, &tspec);
  92. }
  93. if (code != IPC_OK)
  94. errx(1, "%s", ipc_strerror(code));
  95. return;
  96. }