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

103 рядки
2.0 KiB

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "btpd.h"
  6. void
  7. active_add(const uint8_t *hash)
  8. {
  9. FILE *fp;
  10. if ((fp = fopen("active", "a")) == NULL) {
  11. btpd_log(BTPD_L_ERROR, "couldn't open file 'active' (%s).\n",
  12. strerror(errno));
  13. return;
  14. }
  15. fwrite(hash, 20, 1, fp);
  16. fclose(fp);
  17. }
  18. static void
  19. active_del_pos(FILE *fp, long pos, off_t *size)
  20. {
  21. uint8_t ehash[20];
  22. fseek(fp, -20, SEEK_END);
  23. fread(ehash, 20, 1, fp);
  24. fseek(fp, pos, SEEK_SET);
  25. fwrite(ehash, 20, 1, fp);
  26. fflush(fp);
  27. *size -= 20;
  28. ftruncate(fileno(fp), *size);
  29. }
  30. void
  31. active_del(const uint8_t *hash)
  32. {
  33. FILE *fp;
  34. long pos;
  35. struct stat sb;
  36. uint8_t buf[20];
  37. if ((fp = fopen("active", "r+")) == NULL) {
  38. btpd_log(BTPD_L_ERROR, "couldn't open file 'active' (%s).\n",
  39. strerror(errno));
  40. return;
  41. }
  42. if (fstat(fileno(fp), &sb) != 0) {
  43. btpd_log(BTPD_L_ERROR, "couldn't stat file 'active' (%s).\n",
  44. strerror(errno));
  45. goto close;
  46. }
  47. pos = 0;
  48. while (fread(buf, 20, 1, fp) == 1) {
  49. if (bcmp(buf, hash, 20) == 0) {
  50. active_del_pos(fp, pos, &sb.st_size);
  51. break;
  52. }
  53. pos += 20;
  54. }
  55. close:
  56. fclose(fp);
  57. }
  58. void
  59. active_start(void)
  60. {
  61. FILE *fp;
  62. long pos;
  63. struct stat sb;
  64. uint8_t hash[20];
  65. if ((fp = fopen("active", "r+")) == NULL)
  66. return;
  67. if (fstat(fileno(fp), &sb) != 0) {
  68. btpd_log(BTPD_L_ERROR, "Couldn't stat file 'active' (%s).\n",
  69. strerror(errno));
  70. goto close;
  71. }
  72. pos = 0;
  73. while (fread(hash, sizeof(hash), 1, fp) == 1) {
  74. struct tlib *tl = tlib_by_hash(hash);
  75. if (tl != NULL && tl->tp == NULL)
  76. if (torrent_start(tl) != 0) {
  77. active_del_pos(fp, pos, &sb.st_size);
  78. fseek(fp, pos, SEEK_SET);
  79. }
  80. pos += 20;
  81. }
  82. close:
  83. fclose(fp);
  84. }
  85. void
  86. active_clear(void)
  87. {
  88. unlink("active");
  89. }