2 This file is part of GNUnet.
3 (C) 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GetIndexedContext
40 * Handle to global FS context.
42 struct GNUNET_FS_Handle *h;
45 * Connection to the FS service.
47 struct GNUNET_CLIENT_Connection *client;
50 * Function to call for each indexed file.
52 GNUNET_FS_IndexedFileProcessor iterator;
55 * Closure for iterator.
60 * Continuation to trigger at the end.
62 GNUNET_SCHEDULER_Task cont;
72 * Function called on each response from the FS
73 * service with information about indexed files.
75 * @param cls closure (of type "struct GetIndexedContext*")
76 * @param msg message with indexing information
79 handle_index_info (void *cls, const struct GNUNET_MessageHeader *msg)
81 struct GetIndexedContext *gic = cls;
82 const struct IndexInfoMessage *iim;
88 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
90 ("Failed to receive response for `%s' request from `%s' service.\n"),
92 GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
93 GNUNET_SCHEDULER_REASON_TIMEOUT);
94 GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
98 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END)
100 /* normal end-of-list */
101 GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
102 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
103 GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
107 msize = ntohs (msg->size);
108 iim = (const struct IndexInfoMessage *) msg;
109 filename = (const char *) &iim[1];
110 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) ||
111 (msize <= sizeof (struct IndexInfoMessage)) ||
112 (filename[msize - sizeof (struct IndexInfoMessage) - 1] != '\0'))
115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
117 ("Failed to receive valid response for `%s' request from `%s' service.\n"),
118 "GET_INDEXED", "fs");
119 GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
120 GNUNET_SCHEDULER_REASON_TIMEOUT);
121 GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
125 if (GNUNET_OK != gic->iterator (gic->iterator_cls, filename, &iim->file_id))
127 GNUNET_SCHEDULER_add_continuation (gic->cont, gic->cont_cls,
128 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
129 GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
134 GNUNET_CLIENT_receive (gic->client, &handle_index_info, gic,
135 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
140 * Iterate over all indexed files.
142 * @param h handle to the file sharing subsystem
143 * @param iterator function to call on each indexed file
144 * @param iterator_cls closure for iterator
145 * @param cont continuation to call when done;
146 * reason should be "TIMEOUT" (on
147 * error) or "PREREQ_DONE" (on success)
148 * @param cont_cls closure for cont
151 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
152 GNUNET_FS_IndexedFileProcessor iterator,
153 void *iterator_cls, GNUNET_SCHEDULER_Task cont,
156 struct GNUNET_CLIENT_Connection *client;
157 struct GetIndexedContext *gic;
158 struct GNUNET_MessageHeader msg;
160 client = GNUNET_CLIENT_connect ("fs", h->cfg);
163 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
164 _("Failed to not connect to `%s' service.\n"), "fs");
165 GNUNET_SCHEDULER_add_continuation (cont, cont_cls,
166 GNUNET_SCHEDULER_REASON_TIMEOUT);
170 gic = GNUNET_malloc (sizeof (struct GetIndexedContext));
172 gic->client = client;
173 gic->iterator = iterator;
174 gic->iterator_cls = iterator_cls;
176 gic->cont_cls = cont_cls;
177 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
178 msg.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
179 GNUNET_assert (GNUNET_OK ==
180 GNUNET_CLIENT_transmit_and_get_response (client, &msg,
181 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
187 /* end of fs_list_indexed.c */