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
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_TaskCallback cont;
72 * Function called on each response from the FS
73 * service with information about indexed files.
75 * @param cls closure (of type "struct GNUNET_FS_GetIndexedContext*")
76 * @param msg message with indexing information
79 handle_index_info (void *cls, const struct GNUNET_MessageHeader *msg)
81 struct GNUNET_FS_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 (void) gic->iterator (gic->iterator_cls, NULL, NULL);
93 GNUNET_FS_get_indexed_files_cancel (gic);
96 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END)
98 /* normal end-of-list */
99 (void) gic->iterator (gic->iterator_cls, NULL, NULL);
100 GNUNET_FS_get_indexed_files_cancel (gic);
103 msize = ntohs (msg->size);
104 iim = (const struct IndexInfoMessage *) msg;
105 filename = (const char *) &iim[1];
106 if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) ||
107 (msize <= sizeof (struct IndexInfoMessage)) ||
108 (filename[msize - sizeof (struct IndexInfoMessage) - 1] != '\0'))
111 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
113 ("Failed to receive valid response for `%s' request from `%s' service.\n"),
114 "GET_INDEXED", "fs");
115 (void) gic->iterator (gic->iterator_cls, NULL, NULL);
116 GNUNET_FS_get_indexed_files_cancel (gic);
119 if (GNUNET_OK != gic->iterator (gic->iterator_cls, filename, &iim->file_id))
121 GNUNET_FS_get_indexed_files_cancel (gic);
125 GNUNET_CLIENT_receive (gic->client, &handle_index_info, gic,
126 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
131 * Iterate over all indexed files.
133 * @param h handle to the file sharing subsystem
134 * @param iterator function to call on each indexed file
135 * @param iterator_cls closure for iterator
136 * @return NULL on error ('iter' is not called)
138 struct GNUNET_FS_GetIndexedContext *
139 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
140 GNUNET_FS_IndexedFileProcessor iterator,
143 struct GNUNET_CLIENT_Connection *client;
144 struct GNUNET_FS_GetIndexedContext *gic;
145 struct GNUNET_MessageHeader msg;
147 client = GNUNET_CLIENT_connect ("fs", h->cfg);
150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
151 _("Failed to not connect to `%s' service.\n"), "fs");
154 gic = GNUNET_new (struct GNUNET_FS_GetIndexedContext);
156 gic->client = client;
157 gic->iterator = iterator;
158 gic->iterator_cls = iterator_cls;
159 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
160 msg.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
161 GNUNET_assert (GNUNET_OK ==
162 GNUNET_CLIENT_transmit_and_get_response (client, &msg,
163 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
172 * Cancel iteration over all indexed files.
174 * @param gic operation to cancel
177 GNUNET_FS_get_indexed_files_cancel (struct GNUNET_FS_GetIndexedContext *gic)
179 GNUNET_CLIENT_disconnect (gic->client);
184 /* end of fs_list_indexed.c */