cleaning up core API with bugfix
[oweals/gnunet.git] / src / fs / fs_list_indexed.c
1 /*
2      This file is part of GNUnet.
3      (C) 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file fs/fs_list_indexed.c
23  * @author Christian Grothoff
24  * @brief provide a list of all indexed files
25  */
26
27 #include "platform.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_protocols.h"
31 #include "fs.h"
32
33
34 /**
35  * Context for "GNUNET_FS_get_indexed_files".
36  */
37 struct GetIndexedContext
38 {
39   /**
40    * Handle to global FS context.
41    */
42   struct GNUNET_FS_Handle *h;
43
44   /**
45    * Connection to the FS service.
46    */
47   struct GNUNET_CLIENT_Connection *client;
48
49   /**
50    * Function to call for each indexed file.
51    */
52   GNUNET_FS_IndexedFileProcessor iterator;
53
54   /**
55    * Closure for iterator.
56    */
57   void *iterator_cls;
58
59   /**
60    * Continuation to trigger at the end.
61    */
62   GNUNET_SCHEDULER_Task cont;
63
64   /**
65    * Closure for cont.
66    */
67   void *cont_cls;
68 };
69
70
71 /**
72  * Function called on each response from the FS
73  * service with information about indexed files.
74  *
75  * @param cls closure (of type "struct GetIndexedContext*")
76  * @param msg message with indexing information
77  */
78 static void
79 handle_index_info (void *cls,
80                    const struct GNUNET_MessageHeader *msg)
81 {
82   struct GetIndexedContext *gic = cls;
83   const struct IndexInfoMessage *iim;
84   uint16_t msize;
85   const char *filename;
86
87   if (NULL == msg)
88     {
89       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
90                   _("Failed to receive response for `%s' request from `%s' service.\n"),
91                   "GET_INDEXED",
92                   "fs");
93       GNUNET_SCHEDULER_add_continuation (gic->cont,
94                                          gic->cont_cls,
95                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
96       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
97       GNUNET_free (gic);
98       return;
99     }
100   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END)
101     {
102       /* normal end-of-list */
103       GNUNET_SCHEDULER_add_continuation (gic->cont,
104                                          gic->cont_cls,
105                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
106       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
107       GNUNET_free (gic);
108       return;
109     }
110   msize = ntohs (msg->size);
111   iim = (const struct IndexInfoMessage*) msg;
112   filename = (const char*) &iim[1];
113   if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) ||
114        (msize <= sizeof (struct IndexInfoMessage)) ||
115        (filename[msize-sizeof (struct IndexInfoMessage) -1] != '\0') )
116     {
117       /* bogus reply */
118       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
119                   _("Failed to receive valid response for `%s' request from `%s' service.\n"),
120                   "GET_INDEXED",
121                   "fs");
122       GNUNET_SCHEDULER_add_continuation (gic->cont,
123                                          gic->cont_cls,
124                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
125       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
126       GNUNET_free (gic);
127       return;
128     }
129   if (GNUNET_OK !=
130       gic->iterator (gic->iterator_cls,
131                      filename,
132                      &iim->file_id))
133     {
134       GNUNET_SCHEDULER_add_continuation (gic->cont,
135                                          gic->cont_cls,
136                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
137       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
138       GNUNET_free (gic);
139       return;
140     }
141   /* get more */
142   GNUNET_CLIENT_receive (gic->client,
143                          &handle_index_info,
144                          gic,
145                          GNUNET_CONSTANTS_SERVICE_TIMEOUT);  
146 }
147
148
149 /**
150  * Iterate over all indexed files.
151  *
152  * @param h handle to the file sharing subsystem
153  * @param iterator function to call on each indexed file
154  * @param iterator_cls closure for iterator
155  * @param cont continuation to call when done;
156  *             reason should be "TIMEOUT" (on
157  *             error) or  "PREREQ_DONE" (on success)
158  * @param cont_cls closure for cont
159  */
160 void 
161 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
162                              GNUNET_FS_IndexedFileProcessor iterator,
163                              void *iterator_cls,
164                              GNUNET_SCHEDULER_Task cont,
165                              void *cont_cls)
166 {
167   struct GNUNET_CLIENT_Connection *client;
168   struct GetIndexedContext *gic;
169   struct GNUNET_MessageHeader msg;
170
171   client = GNUNET_CLIENT_connect ("fs",
172                                   h->cfg);
173   if (NULL == client)
174     {
175       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
176                   _("Failed to not connect to `%s' service.\n"),
177                   "fs");
178       GNUNET_SCHEDULER_add_continuation (cont,
179                                          cont_cls,
180                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
181       return;
182     }
183
184   gic = GNUNET_malloc (sizeof (struct GetIndexedContext));
185   gic->h = h;
186   gic->client = client;
187   gic->iterator = iterator;
188   gic->iterator_cls = iterator_cls;
189   gic->cont = cont;
190   gic->cont_cls = cont_cls;
191   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
192   msg.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
193   GNUNET_assert (GNUNET_OK ==
194                  GNUNET_CLIENT_transmit_and_get_response (client,
195                                                           &msg,
196                                                           GNUNET_CONSTANTS_SERVICE_TIMEOUT,
197                                                           GNUNET_YES,
198                                                           &handle_index_info,
199                                                           gic));
200 }
201
202 /* end of fs_list_indexed.c */