fs_list_indexed.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/fs_list_indexed.c
  19. * @author Christian Grothoff
  20. * @brief provide a list of all indexed files
  21. */
  22. #include "platform.h"
  23. #include "gnunet_constants.h"
  24. #include "gnunet_fs_service.h"
  25. #include "gnunet_protocols.h"
  26. #include "fs.h"
  27. /**
  28. * Context for "GNUNET_FS_get_indexed_files".
  29. */
  30. struct GetIndexedContext
  31. {
  32. /**
  33. * Handle to global FS context.
  34. */
  35. struct GNUNET_FS_Handle *h;
  36. /**
  37. * Connection to the FS service.
  38. */
  39. struct GNUNET_CLIENT_Connection *client;
  40. /**
  41. * Function to call for each indexed file.
  42. */
  43. GNUNET_FS_IndexedFileProcessor iterator;
  44. /**
  45. * Closure for iterator.
  46. */
  47. void *iterator_cls;
  48. /**
  49. * Continuation to trigger at the end.
  50. */
  51. GNUNET_SCHEDULER_Task cont;
  52. /**
  53. * Closure for cont.
  54. */
  55. void *cont_cls;
  56. };
  57. /**
  58. * Function called on each response from the FS
  59. * service with information about indexed files.
  60. *
  61. * @param cls closure (of type "struct GetIndexedContext*")
  62. * @param msg message with indexing information
  63. */
  64. static void
  65. handle_index_info (void *cls, const struct GNUNET_MessageHeader *msg)
  66. {
  67. struct GetIndexedContext *gic = cls;
  68. const struct IndexInfoMessage *iim;
  69. uint16_t msize;
  70. const char *filename;
  71. if (NULL == msg)
  72. {
  73. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  74. _
  75. ("Failed to receive response for `%s' request from `%s' service.\n"),
  76. "GET_INDEXED", "fs");
  77. GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
  78. GNUNET_SCHEDULER_REASON_TIMEOUT);
  79. GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
  80. GNUNET_free (gic);
  81. return;
  82. }
  83. if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END)
  84. {
  85. /* normal end-of-list */
  86. GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
  87. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  88. GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
  89. GNUNET_free (gic);
  90. return;
  91. }
  92. msize = ntohs (msg->size);
  93. iim = (const struct IndexInfoMessage *) msg;
  94. filename = (const char *) &iim[1];
  95. if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) ||
  96. (msize <= sizeof (struct IndexInfoMessage)) ||
  97. (filename[msize - sizeof (struct IndexInfoMessage) - 1] != '\0'))
  98. {
  99. /* bogus reply */
  100. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  101. _
  102. ("Failed to receive valid response for `%s' request from `%s' service.\n"),
  103. "GET_INDEXED", "fs");
  104. GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
  105. GNUNET_SCHEDULER_REASON_TIMEOUT);
  106. GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
  107. GNUNET_free (gic);
  108. return;
  109. }
  110. if (GNUNET_OK != gic->iterator (gic->iterator_cls, filename, &iim->file_id))
  111. {
  112. GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
  113. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  114. GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
  115. GNUNET_free (gic);
  116. return;
  117. }
  118. /* get more */
  119. GNUNET_CLIENT_receive (gic->client, &handle_index_info, gic,
  120. GNUNET_CONSTANTS_SERVICE_TIMEOUT);
  121. }
  122. /**
  123. * Iterate over all indexed files.
  124. *
  125. * @param h handle to the file sharing subsystem
  126. * @param iterator function to call on each indexed file
  127. * @param iterator_cls closure for iterator
  128. * @param cont continuation to call when done;
  129. * reason should be "TIMEOUT" (on
  130. * error) or "PREREQ_DONE" (on success)
  131. * @param cont_cls closure for cont
  132. */
  133. void
  134. GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
  135. GNUNET_FS_IndexedFileProcessor iterator,
  136. void *iterator_cls, GNUNET_SCHEDULER_Task cont,
  137. void *cont_cls)
  138. {
  139. struct GNUNET_CLIENT_Connection *client;
  140. struct GetIndexedContext *gic;
  141. struct GNUNET_MessageHeader msg;
  142. client = GNUNET_CLIENT_connect ("fs", h->cfg);
  143. if (NULL == client)
  144. {
  145. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  146. _("Failed to not connect to `%s' service.\n"), "fs");
  147. GNUNET_SCHEDULER_add_continuation (cont, cont_cls,
  148. GNUNET_SCHEDULER_REASON_TIMEOUT);
  149. return;
  150. }
  151. gic = GNUNET_malloc (sizeof (struct GetIndexedContext));
  152. gic->h = h;
  153. gic->client = client;
  154. gic->iterator = iterator;
  155. gic->iterator_cls = iterator_cls;
  156. gic->cont = cont;
  157. gic->cont_cls = cont_cls;
  158. msg.size = htons (sizeof (struct GNUNET_MessageHeader));
  159. msg.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
  160. GNUNET_assert (GNUNET_OK ==
  161. GNUNET_CLIENT_transmit_and_get_response (client, &msg,
  162. GNUNET_CONSTANTS_SERVICE_TIMEOUT,
  163. GNUNET_YES,
  164. &handle_index_info,
  165. gic));
  166. }
  167. /* end of fs_list_indexed.c */