fs_list_indexed.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_api.h"
  27. /**
  28. * Context for "GNUNET_FS_get_indexed_files".
  29. */
  30. struct GNUNET_FS_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 GNUNET_FS_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 GNUNET_FS_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. (void) gic->iterator (gic->iterator_cls, NULL, NULL);
  78. GNUNET_FS_get_indexed_files_cancel (gic);
  79. return;
  80. }
  81. if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END)
  82. {
  83. /* normal end-of-list */
  84. (void) gic->iterator (gic->iterator_cls, NULL, NULL);
  85. GNUNET_FS_get_indexed_files_cancel (gic);
  86. return;
  87. }
  88. msize = ntohs (msg->size);
  89. iim = (const struct IndexInfoMessage *) msg;
  90. filename = (const char *) &iim[1];
  91. if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) ||
  92. (msize <= sizeof (struct IndexInfoMessage)) ||
  93. (filename[msize - sizeof (struct IndexInfoMessage) - 1] != '\0'))
  94. {
  95. /* bogus reply */
  96. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  97. _
  98. ("Failed to receive valid response for `%s' request from `%s' service.\n"),
  99. "GET_INDEXED", "fs");
  100. (void) gic->iterator (gic->iterator_cls, NULL, NULL);
  101. GNUNET_FS_get_indexed_files_cancel (gic);
  102. return;
  103. }
  104. if (GNUNET_OK != gic->iterator (gic->iterator_cls, filename, &iim->file_id))
  105. {
  106. GNUNET_FS_get_indexed_files_cancel (gic);
  107. return;
  108. }
  109. /* get more */
  110. GNUNET_CLIENT_receive (gic->client, &handle_index_info, gic,
  111. GNUNET_CONSTANTS_SERVICE_TIMEOUT);
  112. }
  113. /**
  114. * Iterate over all indexed files.
  115. *
  116. * @param h handle to the file sharing subsystem
  117. * @param iterator function to call on each indexed file
  118. * @param iterator_cls closure for iterator
  119. * @return NULL on error ('iter' is not called)
  120. */
  121. struct GNUNET_FS_GetIndexedContext *
  122. GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
  123. GNUNET_FS_IndexedFileProcessor iterator,
  124. void *iterator_cls)
  125. {
  126. struct GNUNET_CLIENT_Connection *client;
  127. struct GNUNET_FS_GetIndexedContext *gic;
  128. struct GNUNET_MessageHeader msg;
  129. client = GNUNET_CLIENT_connect ("fs", h->cfg);
  130. if (NULL == client)
  131. {
  132. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  133. _("Failed to not connect to `%s' service.\n"), "fs");
  134. return NULL;
  135. }
  136. gic = GNUNET_new (struct GNUNET_FS_GetIndexedContext);
  137. gic->h = h;
  138. gic->client = client;
  139. gic->iterator = iterator;
  140. gic->iterator_cls = iterator_cls;
  141. msg.size = htons (sizeof (struct GNUNET_MessageHeader));
  142. msg.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
  143. GNUNET_assert (GNUNET_OK ==
  144. GNUNET_CLIENT_transmit_and_get_response (client, &msg,
  145. GNUNET_CONSTANTS_SERVICE_TIMEOUT,
  146. GNUNET_YES,
  147. &handle_index_info,
  148. gic));
  149. return gic;
  150. }
  151. /**
  152. * Cancel iteration over all indexed files.
  153. *
  154. * @param gic operation to cancel
  155. */
  156. void
  157. GNUNET_FS_get_indexed_files_cancel (struct GNUNET_FS_GetIndexedContext *gic)
  158. {
  159. GNUNET_CLIENT_disconnect (gic->client);
  160. GNUNET_free (gic);
  161. }
  162. /* end of fs_list_indexed.c */