A clone of btpd with my configuration changes.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

127 satır
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. "--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, labellen = 0;
  41. char *dir = NULL, *name = NULL, *glabel = NULL, *label;
  42. while ((ch = getopt_long(argc, argv, "NTd:l: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 'l':
  56. glabel = optarg;
  57. if ((labellen = strlen(dir)) == 0)
  58. diemsg("bad option value for -l.\n");
  59. break;
  60. case 'n':
  61. name = optarg;
  62. break;
  63. default:
  64. usage_add();
  65. }
  66. }
  67. argc -= optind;
  68. argv += optind;
  69. if (argc < 1 || dir == NULL)
  70. usage_add();
  71. btpd_connect();
  72. char *mi;
  73. size_t mi_size;
  74. enum ipc_err code;
  75. char dpath[PATH_MAX];
  76. struct iobuf iob;
  77. for (nfile = 0; nfile < argc; nfile++) {
  78. if ((mi = mi_load(argv[nfile], &mi_size)) == NULL) {
  79. fprintf(stderr, "error loading '%s' (%s).\n", argv[nfile], strerror(errno));
  80. continue;
  81. }
  82. iob = iobuf_init(PATH_MAX);
  83. iobuf_write(&iob, dir, dirlen);
  84. if (topdir && !mi_simple(mi)) {
  85. size_t tdlen;
  86. const char *td =
  87. benc_dget_mem(benc_dget_dct(mi, "info"), "name", &tdlen);
  88. iobuf_swrite(&iob, "/");
  89. iobuf_write(&iob, td, tdlen);
  90. }
  91. iobuf_swrite(&iob, "\0");
  92. if ((errno = make_abs_path(iob.buf, dpath)) != 0) {
  93. fprintf(stderr, "make_abs_path '%s' failed (%s).\n", dpath, strerror(errno));
  94. iobuf_free(&iob);
  95. continue;
  96. }
  97. if(NULL == glabel)
  98. label = benc_dget_str(mi, "announce", NULL);
  99. else
  100. label = glabel;
  101. code = btpd_add(ipc, mi, mi_size, dpath, name, label);
  102. if ((code == IPC_OK) && start) {
  103. struct ipc_torrent tspec;
  104. tspec.by_hash = 1;
  105. mi_info_hash(mi, tspec.u.hash);
  106. code = btpd_start(ipc, &tspec);
  107. }
  108. if (code != IPC_OK) {
  109. fprintf(stderr, "command failed for '%s' (%s).\n", argv[nfile], ipc_strerror(code));
  110. } else {
  111. nloaded++;
  112. }
  113. iobuf_free(&iob);
  114. }
  115. if (nloaded != nfile) {
  116. diemsg("error loaded %d of %d files.\n", nloaded, nfile);
  117. }
  118. }