2 This file is part of GNUnet.
3 Copyright (C) 2003, 2004, 2006, 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file fs/fs_list_indexed.c
18 * @author Christian Grothoff
19 * @brief provide a list of all indexed files
23 #include "gnunet_constants.h"
24 #include "gnunet_fs_service.h"
25 #include "gnunet_protocols.h"
30 * Context for #GNUNET_FS_get_indexed_files().
32 struct GNUNET_FS_GetIndexedContext
36 * Connection to the FS service.
38 struct GNUNET_MQ_Handle *mq;
41 * Function to call for each indexed file.
43 GNUNET_FS_IndexedFileProcessor iterator;
46 * Closure for @e iterator.
51 * Continuation to trigger at the end.
53 GNUNET_SCHEDULER_TaskCallback cont;
56 * Closure for @e cont.
63 * Function called on each response from the FS
64 * service with information about indexed files.
66 * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`)
67 * @param msg message with indexing information
70 handle_index_info_end (void *cls,
71 const struct GNUNET_MessageHeader *msg)
73 struct GNUNET_FS_GetIndexedContext *gic = cls;
75 (void) gic->iterator (gic->iterator_cls,
78 GNUNET_FS_get_indexed_files_cancel (gic);
83 * Check validity of response from the FS
84 * service with information about indexed files.
86 * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`)
87 * @param iim message with indexing information
90 check_index_info (void *cls,
91 const struct IndexInfoMessage *iim)
93 uint16_t msize = ntohs (iim->header.size) - sizeof (*iim);
96 filename = (const char *) &iim[1];
97 if (filename[msize - 1] != '\0')
100 return GNUNET_SYSERR;
107 * Function called on each response from the FS
108 * service with information about indexed files.
110 * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`)
111 * @param iim message with indexing information
114 handle_index_info (void *cls,
115 const struct IndexInfoMessage *iim)
117 struct GNUNET_FS_GetIndexedContext *gic = cls;
118 const char *filename;
120 filename = (const char *) &iim[1];
122 gic->iterator (gic->iterator_cls,
126 GNUNET_FS_get_indexed_files_cancel (gic);
133 * Generic error handler, called with the appropriate error code and
134 * the same closure specified at the creation of the message queue.
135 * Not every message queue implementation supports an error handler.
137 * @param cls closure with the `struct GNUNET_FS_GetIndexedContent *`
138 * @param error error code
141 mq_error_handler (void *cls,
142 enum GNUNET_MQ_Error error)
144 struct GNUNET_FS_GetIndexedContext *gic = cls;
146 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
147 _("Failed to receive response from `%s' service.\n"),
149 (void) gic->iterator (gic->iterator_cls, NULL, NULL);
150 GNUNET_FS_get_indexed_files_cancel (gic);
155 * Iterate over all indexed files.
157 * @param h handle to the file sharing subsystem
158 * @param iterator function to call on each indexed file
159 * @param iterator_cls closure for iterator
160 * @return NULL on error ('iter' is not called)
162 struct GNUNET_FS_GetIndexedContext *
163 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
164 GNUNET_FS_IndexedFileProcessor iterator,
167 struct GNUNET_FS_GetIndexedContext *gic
168 = GNUNET_new (struct GNUNET_FS_GetIndexedContext);
169 struct GNUNET_MQ_MessageHandler handlers[] = {
170 GNUNET_MQ_hd_fixed_size (index_info_end,
171 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END,
172 struct GNUNET_MessageHeader,
174 GNUNET_MQ_hd_var_size (index_info,
175 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY,
176 struct IndexInfoMessage,
178 GNUNET_MQ_handler_end ()
180 struct GNUNET_MQ_Envelope *env;
181 struct GNUNET_MessageHeader *msg;
183 gic->mq = GNUNET_CLIENT_connect (h->cfg,
190 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
191 _("Failed to not connect to `%s' service.\n"),
196 gic->iterator = iterator;
197 gic->iterator_cls = iterator_cls;
198 env = GNUNET_MQ_msg (msg,
199 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
200 GNUNET_MQ_send (gic->mq,
207 * Cancel iteration over all indexed files.
209 * @param gic operation to cancel
212 GNUNET_FS_get_indexed_files_cancel (struct GNUNET_FS_GetIndexedContext *gic)
214 GNUNET_MQ_destroy (gic->mq);
219 /* end of fs_list_indexed.c */