A clone of btpd with my configuration changes.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

97 lines
1.9 KiB

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