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