shutdown callback
[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 2, 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->h->sched,
94                                          gic->cont,
95                                          gic->cont_cls,
96                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
97       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
98       GNUNET_free (gic);
99       return;
100     }
101   if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END)
102     {
103       /* normal end-of-list */
104       GNUNET_SCHEDULER_add_continuation (gic->h->sched,
105                                          gic->cont,
106                                          gic->cont_cls,
107                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
108       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
109       GNUNET_free (gic);
110       return;
111     }
112   msize = ntohs (msg->size);
113   iim = (const struct IndexInfoMessage*) msg;
114   filename = (const char*) &iim[1];
115   if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) ||
116        (msize <= sizeof (struct IndexInfoMessage)) ||
117        (filename[msize-sizeof (struct IndexInfoMessage) -1] != '\0') )
118     {
119       /* bogus reply */
120       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
121                   _("Failed to receive valid response for `%s' request from `%s' service.\n"),
122                   "GET_INDEXED",
123                   "fs");
124       GNUNET_SCHEDULER_add_continuation (gic->h->sched,
125                                          gic->cont,
126                                          gic->cont_cls,
127                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
128       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
129       GNUNET_free (gic);
130       return;
131     }
132   if (GNUNET_OK !=
133       gic->iterator (gic->iterator_cls,
134                      filename,
135                      &iim->file_id))
136     {
137       GNUNET_SCHEDULER_add_continuation (gic->h->sched,
138                                          gic->cont,
139                                          gic->cont_cls,
140                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
141       GNUNET_CLIENT_disconnect (gic->client, GNUNET_NO);
142       GNUNET_free (gic);
143       return;
144     }
145   /* get more */
146   GNUNET_CLIENT_receive (gic->client,
147                          &handle_index_info,
148                          gic,
149                          GNUNET_CONSTANTS_SERVICE_TIMEOUT);  
150 }
151
152
153 /**
154  * Iterate over all indexed files.
155  *
156  * @param h handle to the file sharing subsystem
157  * @param iterator function to call on each indexed file
158  * @param iterator_cls closure for iterator
159  * @param cont continuation to call when done;
160  *             reason should be "TIMEOUT" (on
161  *             error) or  "PREREQ_DONE" (on success)
162  * @param cont_cls closure for cont
163  */
164 void 
165 GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
166                              GNUNET_FS_IndexedFileProcessor iterator,
167                              void *iterator_cls,
168                              GNUNET_SCHEDULER_Task cont,
169                              void *cont_cls)
170 {
171   struct GNUNET_CLIENT_Connection *client;
172   struct GetIndexedContext *gic;
173   struct GNUNET_MessageHeader msg;
174
175   client = GNUNET_CLIENT_connect (h->sched,
176                                   "fs",
177                                   h->cfg);
178   if (NULL == client)
179     {
180       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
181                   _("Failed to not connect to `%s' service.\n"),
182                   "fs");
183       GNUNET_SCHEDULER_add_continuation (h->sched,
184                                          cont,
185                                          cont_cls,
186                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
187       return;
188     }
189
190   gic = GNUNET_malloc (sizeof (struct GetIndexedContext));
191   gic->h = h;
192   gic->client = client;
193   gic->iterator = iterator;
194   gic->iterator_cls = iterator_cls;
195   gic->cont = cont;
196   gic->cont_cls = cont_cls;
197   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
198   msg.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
199   GNUNET_assert (GNUNET_OK ==
200                  GNUNET_CLIENT_transmit_and_get_response (client,
201                                                           &msg,
202                                                           GNUNET_CONSTANTS_SERVICE_TIMEOUT,
203                                                           GNUNET_YES,
204                                                           &handle_index_info,
205                                                           gic));
206 }
207
208 /* end of fs_list_indexed.c */