aboutsummaryrefslogtreecommitdiff
path: root/src/fs/fs_unindex.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2009-08-30 21:37:12 +0000
committerChristian Grothoff <christian@grothoff.org>2009-08-30 21:37:12 +0000
commit0634957156088bc3c53d787c13ba3006307d8ca5 (patch)
tree5610330dc34ec07c8cc72e2a12d011006c00275c /src/fs/fs_unindex.c
parentc3d7c40c3cd0ec03c7f6b27e6b5f7eac1aa80ed5 (diff)
downloadgnunet-0634957156088bc3c53d787c13ba3006307d8ca5.tar.gz
gnunet-0634957156088bc3c53d787c13ba3006307d8ca5.zip
adding listing of indexed files
Diffstat (limited to 'src/fs/fs_unindex.c')
-rw-r--r--src/fs/fs_unindex.c211
1 files changed, 209 insertions, 2 deletions
diff --git a/src/fs/fs_unindex.c b/src/fs/fs_unindex.c
index 8fb13a644..c32a10cbf 100644
--- a/src/fs/fs_unindex.c
+++ b/src/fs/fs_unindex.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2003, 2004, 2006 Christian Grothoff (and other contributing authors) 3 (C) 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -29,21 +29,228 @@
29 */ 29 */
30 30
31#include "platform.h" 31#include "platform.h"
32#include "gnunet_constants.h"
32#include "gnunet_fs_service.h" 33#include "gnunet_fs_service.h"
34#include "gnunet_protocols.h"
33#include "fs.h" 35#include "fs.h"
34 36
37
38/**
39 * Context for "GNUNET_FS_get_indexed_files".
40 */
41struct GetIndexedContext
42{
43 /**
44 * Handle to global FS context.
45 */
46 struct GNUNET_FS_Handle *h;
47
48 /**
49 * Connection to the FS service.
50 */
51 struct GNUNET_CLIENT_Connection *client;
52
53 /**
54 * Function to call for each indexed file.
55 */
56 GNUNET_FS_IndexedFileProcessor iterator;
57
58 /**
59 * Closure for iterator.
60 */
61 void *iterator_cls;
62
63 /**
64 * Continuation to trigger at the end.
65 */
66 GNUNET_SCHEDULER_Task cont;
67
68 /**
69 * Closure for cont.
70 */
71 void *cont_cls;
72};
73
74
75/**
76 * Function called on each response from the FS
77 * service with information about indexed files.
78 *
79 * @param cls closure (of type "struct GetIndexedContext*")
80 * @param msg message with indexing information
81 */
82static void
83handle_index_info (void *cls,
84 const struct GNUNET_MessageHeader *msg)
85{
86 struct GetIndexedContext *gic = cls;
87 const struct IndexInfoMessage *iim;
88 uint16_t msize;
89 const char *filename;
90
91 if (NULL == msg)
92 {
93 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
94 _("Failed to receive response for `%s' request from `%s' service.\n"),
95 "GET_INDEXED",
96 "fs");
97 GNUNET_SCHEDULER_add_continuation (gic->h->sched,
98 GNUNET_NO,
99 gic->cont,
100 gic->cont_cls,
101 GNUNET_SCHEDULER_REASON_TIMEOUT);
102 GNUNET_CLIENT_disconnect (gic->client);
103 GNUNET_free (gic);
104 return;
105 }
106 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END)
107 {
108 /* normal end-of-list */
109 GNUNET_SCHEDULER_add_continuation (gic->h->sched,
110 GNUNET_NO,
111 gic->cont,
112 gic->cont_cls,
113 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
114 GNUNET_CLIENT_disconnect (gic->client);
115 GNUNET_free (gic);
116 return;
117 }
118 msize = ntohs (msg->size);
119 iim = (const struct IndexInfoMessage*) msg;
120 filename = (const char*) &iim[1];
121 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY) ||
122 (msize <= sizeof (struct IndexInfoMessage)) ||
123 (filename[msize-sizeof (struct IndexInfoMessage) -1] != '\0') )
124 {
125 /* bogus reply */
126 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
127 _("Failed to receive valid response for `%s' request from `%s' service.\n"),
128 "GET_INDEXED",
129 "fs");
130 GNUNET_SCHEDULER_add_continuation (gic->h->sched,
131 GNUNET_NO,
132 gic->cont,
133 gic->cont_cls,
134 GNUNET_SCHEDULER_REASON_TIMEOUT);
135 GNUNET_CLIENT_disconnect (gic->client);
136 GNUNET_free (gic);
137 return;
138 }
139 if (GNUNET_OK !=
140 gic->iterator (gic->iterator_cls,
141 filename,
142 &iim->file_id))
143 {
144 GNUNET_SCHEDULER_add_continuation (gic->h->sched,
145 GNUNET_NO,
146 gic->cont,
147 gic->cont_cls,
148 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
149 GNUNET_CLIENT_disconnect (gic->client);
150 GNUNET_free (gic);
151 return;
152 }
153 /* get more */
154 GNUNET_CLIENT_receive (gic->client,
155 &handle_index_info,
156 gic,
157 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
158}
159
160
161/**
162 * Transmit the request to get a list of all
163 * indexed files to the "FS" service.
164 *
165 * @param cls closure (of type "struct GetIndexedContext*")
166 * @param size number of bytes availabe in buf
167 * @param buf where to write the message, NULL on error
168 * @return number of bytes written to buf
169 */
170static size_t
171transmit_get_indexed (void *cls,
172 size_t size,
173 void *buf)
174{
175 struct GetIndexedContext *gic = cls;
176 struct GNUNET_MessageHeader *hdr;
177
178 if (NULL == buf)
179 {
180 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
181 _("Failed to transmit `%s' request to `%s' service.\n"),
182 "GET_INDEXED",
183 "fs");
184 GNUNET_SCHEDULER_add_continuation (gic->h->sched,
185 GNUNET_NO,
186 gic->cont,
187 gic->cont_cls,
188 GNUNET_SCHEDULER_REASON_TIMEOUT);
189 GNUNET_CLIENT_disconnect (gic->client);
190 GNUNET_free (gic);
191 return 0;
192 }
193 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
194 hdr = buf;
195 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
196 hdr->type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_GET);
197 GNUNET_CLIENT_receive (gic->client,
198 &handle_index_info,
199 gic,
200 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
201 return sizeof (struct GNUNET_MessageHeader);
202}
203
204
35/** 205/**
36 * Iterate over all indexed files. 206 * Iterate over all indexed files.
37 * 207 *
38 * @param h handle to the file sharing subsystem 208 * @param h handle to the file sharing subsystem
39 * @param iterator function to call on each indexed file 209 * @param iterator function to call on each indexed file
40 * @param iterator_cls closure for iterator 210 * @param iterator_cls closure for iterator
211 * @param cont continuation to call when done;
212 * reason should be "TIMEOUT" (on
213 * error) or "PREREQ_DONE" (on success)
214 * @param cont_cls closure for cont
41 */ 215 */
42void 216void
43GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h, 217GNUNET_FS_get_indexed_files (struct GNUNET_FS_Handle *h,
44 GNUNET_FS_IndexedFileProcessor iterator, 218 GNUNET_FS_IndexedFileProcessor iterator,
45 void *iterator_cls) 219 void *iterator_cls,
220 GNUNET_SCHEDULER_Task cont,
221 void *cont_cls)
46{ 222{
223 struct GNUNET_CLIENT_Connection *client;
224 struct GetIndexedContext *gic;
225
226 client = GNUNET_CLIENT_connect (h->sched,
227 "fs",
228 h->cfg);
229 if (NULL == client)
230 {
231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
232 _("Failed to not connect to `%s' service.\n"),
233 "fs");
234 GNUNET_SCHEDULER_add_continuation (h->sched,
235 GNUNET_NO,
236 cont,
237 cont_cls,
238 GNUNET_SCHEDULER_REASON_TIMEOUT);
239 return;
240 }
241
242 gic = GNUNET_malloc (sizeof (struct GetIndexedContext));
243 gic->h = h;
244 gic->client = client;
245 gic->iterator = iterator;
246 gic->iterator_cls = iterator_cls;
247 gic->cont = cont;
248 gic->cont_cls = cont_cls;
249 GNUNET_CLIENT_notify_transmit_ready (client,
250 sizeof (struct GNUNET_MessageHeader),
251 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
252 &transmit_get_indexed,
253 gic);
47} 254}
48 255
49 256