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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file fs/fs_list_indexed.c
23 * @author Christian Grothoff
24 * @brief provide a list of all indexed files
28 #include "gnunet_constants.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_protocols.h"
35 * Context for #GNUNET_FS_get_indexed_files().
37 struct GNUNET_FS_GetIndexedContext
41 * Connection to the FS service.
43 struct GNUNET_MQ_Handle *mq;
46 * Function to call for each indexed file.
48 GNUNET_FS_IndexedFileProcessor iterator;
51 * Closure for @e iterator.
56 * Continuation to trigger at the end.
58 GNUNET_SCHEDULER_TaskCallback cont;
61 * Closure for @e cont.
68 * Function called on each response from the FS
69 * service with information about indexed files.
71 * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`)
72 * @param msg message with indexing information
75 handle_index_info_end (void *cls,
76 const struct GNUNET_MessageHeader *msg)
78 struct GNUNET_FS_GetIndexedContext *gic = cls;
80 (void) gic->iterator (gic->iterator_cls,
83 GNUNET_FS_get_indexed_files_cancel (gic);
88 * Check validity of response from the FS
89 * service with information about indexed files.
91 * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`)
92 * @param iim message with indexing information
95 check_index_info (void *cls,
96 const struct IndexInfoMessage *iim)
98 uint16_t msize = ntohs (iim->header.size) - sizeof (*iim);
101 filename = (const char *) &iim[1];
102 if (filename[msize - 1] != '\0')
105 return GNUNET_SYSERR;
112 * Function called on each response from the FS
113 * service with information about indexed files.
115 * @param cls closure (of type `struct GNUNET_FS_GetIndexedContext *`)
116 * @param iim message with indexing information
119 handle_index_info (void *cls,
120 const struct IndexInfoMessage *iim)
122 struct GNUNET_FS_GetIndexedContext *gic = cls;
123 const char *filename;
125 filename = (const char *) &iim[1];
127 gic->iterator (gic->iterator_cls,
131 GNUNET_FS_get_indexed_files_cancel (gic);
138 * Generic error handler, called with the appropriate error code and
139 * the same closure specified at the creation of the message queue.
140 * Not every message queue implementation supports an error handler.
142 * @param cls closure with the `struct GNUNET_FS_GetIndexedContent *`
143 * @param error error code
146 mq_error_handler (void *cls,
147 enum GNUNET_MQ_Error error)
149 struct GNUNET_FS_GetIndexedContext *gic = cls;
151 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
152 _("Failed to receive response from `%s' service.\n"),
154 (void) gic->iterator (gic->iterator_cls, NULL, NULL);
155 GNUNET_FS_get_indexed_files_cancel (gic);
160 * Iterate over all indexed files.
162 * @param h handle to the file sharing subsystem
163 * @param iterator function to call on each indexed file
164 * @param iterator_cls closure for iterator
165 * @return NULL on error ('iter' is not called)
167 struct GNUNET_FS_GetIndexedContext *
168 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
169 GNUNET_FS_IndexedFileProcessor iterator,
172 GNUNET_MQ_hd_fixed_size (index_info_end,
173 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END,
174 struct GNUNET_MessageHeader);
175 GNUNET_MQ_hd_var_size (index_info,
176 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY,
177 struct IndexInfoMessage);
178 struct GNUNET_FS_GetIndexedContext *gic
179 = GNUNET_new (struct GNUNET_FS_GetIndexedContext);
180 struct GNUNET_MQ_MessageHandler handlers[] = {
181 make_index_info_end_handler (gic),
182 make_index_info_handler (gic),
183 GNUNET_MQ_handler_end ()
185 struct GNUNET_MQ_Envelope *env;
186 struct GNUNET_MessageHeader *msg;
188 gic->mq = GNUNET_CLIENT_connecT (h->cfg,
195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
196 _("Failed to not connect to `%s' service.\n"),
201 gic->iterator = iterator;
202 gic->iterator_cls = iterator_cls;
203 env = GNUNET_MQ_msg (msg,
204 GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
205 GNUNET_MQ_send (gic->mq,
212 * Cancel iteration over all indexed files.
214 * @param gic operation to cancel
217 GNUNET_FS_get_indexed_files_cancel (struct GNUNET_FS_GetIndexedContext *gic)
219 GNUNET_MQ_destroy (gic->mq);
224 /* end of fs_list_indexed.c */