3d39cb93230437650946174802e6785106be5d73
[oweals/gnunet.git] / src / fs / fs_unindex.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_unindex.c
23  * @author Krista Grothoff
24  * @author Christian Grothoff
25  * @brief Unindex file.
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 #include "fs_tree.h"
33
34
35 /**
36  * Function called by the tree encoder to obtain
37  * a block of plaintext data (for the lowest level
38  * of the tree).
39  *
40  * @param cls our publishing context
41  * @param offset identifies which block to get
42  * @param max (maximum) number of bytes to get; returning
43  *        fewer will also cause errors
44  * @param buf where to copy the plaintext buffer
45  * @param emsg location to store an error message (on error)
46  * @return number of bytes copied to buf, 0 on error
47  */
48 static size_t
49 unindex_reader (void *cls,
50                 uint64_t offset,
51                 size_t max, 
52                 void *buf,
53                 char **emsg)
54 {
55   struct GNUNET_FS_UnindexContext *uc = cls;
56   size_t pt_size;
57
58   pt_size = GNUNET_MIN(max,
59                        uc->file_size - offset);
60   if (offset != 
61       GNUNET_DISK_file_seek (uc->fh, offset, GNUNET_DISK_SEEK_SET))
62     {
63       *emsg = GNUNET_strdup (_("Failed to find given position in file"));
64       return 0;
65     }
66   if (pt_size !=
67       GNUNET_DISK_file_read (uc->fh,
68                              buf,
69                              pt_size))
70     {
71       *emsg = GNUNET_strdup (_("Failed to read file"));
72       return 0;
73     }
74   return pt_size;
75 }
76
77
78 /**
79  * Fill in all of the generic fields for 
80  * an unindex event.
81  *
82  * @param pi structure to fill in
83  * @param uc overall unindex context
84  * @param offset where we are in the file (for progress)
85  */
86 static void
87 make_unindex_status (struct GNUNET_FS_ProgressInfo *pi,
88                      struct GNUNET_FS_UnindexContext *uc,
89                      uint64_t offset)
90 {
91   pi->value.unindex.uc = uc;
92   pi->value.unindex.cctx = uc->client_info;
93   pi->value.unindex.filename = uc->filename;
94   pi->value.unindex.size = uc->file_size;
95   pi->value.unindex.eta 
96     = GNUNET_TIME_calculate_eta (uc->start_time,
97                                  offset,
98                                  uc->file_size);
99   pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
100   pi->value.publish.completed = offset;
101 }
102
103
104 /**
105  * Function called with information about our
106  * progress in computing the tree encoding.
107  *
108  * @param cls closure
109  * @param offset where are we in the file
110  * @param pt_block plaintext of the currently processed block
111  * @param pt_size size of pt_block
112  * @param depth depth of the block in the tree
113  */
114 static void
115 unindex_progress (void *cls,
116                   uint64_t offset,
117                   const void *pt_block,
118                   size_t pt_size,
119                   unsigned int depth)
120 {
121   struct GNUNET_FS_UnindexContext *uc = cls;
122   struct GNUNET_FS_ProgressInfo pi;
123
124   pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
125   make_unindex_status (&pi, uc, offset);
126   pi.value.unindex.specifics.progress.data = pt_block;
127   pi.value.unindex.specifics.progress.offset = offset;
128   pi.value.unindex.specifics.progress.data_len = pt_size;
129   pi.value.unindex.specifics.progress.depth = depth;
130   uc->client_info 
131     = uc->h->upcb (uc->h->upcb_cls,
132                    &pi);
133 }
134                                                
135
136 /**
137  * We've encountered an error during
138  * unindexing.  Signal the client.
139  *
140  * @param uc context for the failed unindexing operation
141  * @param emsg the error message
142  */
143 static void
144 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc,
145                       const char *emsg)
146 {
147   struct GNUNET_FS_ProgressInfo pi;
148   
149   pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
150   make_unindex_status (&pi, uc, 0);
151   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
152   pi.value.unindex.specifics.error.message = emsg;
153   uc->client_info
154     = uc->h->upcb (uc->h->upcb_cls,
155                    &pi);
156 }
157
158
159 /**
160  * Continuation called to notify client about result of the
161  * datastore removal operation.
162  *
163  * @param cls closure
164  * @param success GNUNET_SYSERR on failure
165  * @param msg NULL on success, otherwise an error message
166  */
167 static void
168 process_cont (void *cls,
169               int success,
170               const char *msg)
171 {
172   struct GNUNET_FS_UnindexContext *uc = cls;
173   if (success == GNUNET_SYSERR)
174     {
175       signal_unindex_error (uc,
176                             msg);
177       return;
178     }
179   
180   GNUNET_FS_tree_encoder_next (uc->tc);
181 }
182
183
184 /**
185  * Function called asking for the current (encoded)
186  * block to be processed.  After processing the
187  * client should either call "GNUNET_FS_tree_encode_next"
188  * or (on error) "GNUNET_FS_tree_encode_finish".
189  *
190  * @param cls closure
191  * @param query the query for the block (key for lookup in the datastore)
192  * @param offset offset of the block
193  * @param type type of the block (IBLOCK or DBLOCK)
194  * @param block the (encrypted) block
195  * @param block_size size of block (in bytes)
196  */
197 static void 
198 unindex_process (void *cls,
199                  const GNUNET_HashCode *query,
200                  uint64_t offset,
201                  uint32_t type,
202                  const void *block,
203                  uint16_t block_size)
204 {
205   struct GNUNET_FS_UnindexContext *uc = cls;
206   uint32_t size;
207   const void *data;
208   struct OnDemandBlock odb;
209
210   if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
211     {
212       size = block_size;
213       data = block;
214     }
215   else /* on-demand encoded DBLOCK */
216     {
217       size = sizeof(struct OnDemandBlock);
218       odb.offset = offset;
219       odb.file_id = uc->file_id;
220       data = &odb;
221     }
222   GNUNET_DATASTORE_remove (uc->dsh,
223                            query,
224                            size,
225                            data,
226                            &process_cont,
227                            uc,
228                            GNUNET_CONSTANTS_SERVICE_TIMEOUT);
229 }
230
231
232 /**
233  * Function called when the tree encoder has
234  * processed all blocks.  Clean up.
235  *
236  * @param cls our unindexing context
237  * @param tc not used
238  */
239 static void
240 unindex_finish (void *cls,
241                 const struct GNUNET_SCHEDULER_TaskContext *tc)
242 {
243   struct GNUNET_FS_UnindexContext *uc = cls;
244   char *emsg;
245   struct GNUNET_FS_Uri *uri;
246   struct GNUNET_FS_ProgressInfo pi;
247
248   GNUNET_FS_tree_encoder_finish (uc->tc,
249                                  &uri,
250                                  &emsg);
251   if (uri != NULL)
252     GNUNET_FS_uri_destroy (uri);
253   GNUNET_DISK_file_close (uc->fh);
254   uc->fh = NULL;
255   GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
256   uc->dsh = NULL;
257   if (emsg != NULL)
258     {
259       signal_unindex_error (uc, emsg);
260       GNUNET_free (emsg);
261     }
262   else
263     {   
264       pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
265       make_unindex_status (&pi, uc, uc->file_size);
266       pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
267       uc->client_info
268         = uc->h->upcb (uc->h->upcb_cls,
269                        &pi);
270     }
271 }
272
273
274 /**
275  * Function called with the response from the
276  * FS service to our unindexing request.
277  *
278  * @param cls closure, unindex context
279  * @param msg NULL on timeout, otherwise the response
280  */
281 static void
282 process_fs_response (void *cls,
283                      const struct GNUNET_MessageHeader *msg)
284 {
285   struct GNUNET_FS_UnindexContext *uc = cls;
286
287   GNUNET_CLIENT_disconnect (uc->client);
288   uc->client = NULL;
289   if (uc->state != UNINDEX_STATE_FS_NOTIFY) 
290     {
291       GNUNET_FS_unindex_stop (uc);
292       return;
293     }
294   if (NULL == msg)
295     {
296       uc->state = UNINDEX_STATE_ERROR;
297       signal_unindex_error (uc, 
298                             _("Timeout waiting for `fs' service."));
299       return;
300     }
301   if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
302     {
303       uc->state = UNINDEX_STATE_ERROR;
304       signal_unindex_error (uc, 
305                             _("Invalid response from `fs' service."));
306       return;      
307     }
308   uc->state = UNINDEX_STATE_DS_REMOVE;
309   uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
310                                       uc->h->sched);
311   if (NULL == uc->dsh)
312     {
313       uc->state = UNINDEX_STATE_ERROR;
314       signal_unindex_error (uc, 
315                             _("Failed to connect to `datastore' service."));
316       return;
317     }
318   uc->fh = GNUNET_DISK_file_open (uc->filename,
319                                   GNUNET_DISK_OPEN_READ,
320                                   GNUNET_DISK_PERM_NONE);
321   if (NULL == uc->fh)
322     {
323       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
324       uc->dsh = NULL;
325       uc->state = UNINDEX_STATE_ERROR;
326       signal_unindex_error (uc, 
327                             _("Failed to open file for unindexing."));
328       return;
329     }
330   uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
331                                           uc->file_size,
332                                           uc,
333                                           &unindex_reader,
334                                           &unindex_process,
335                                           &unindex_progress,
336                                           &unindex_finish);
337   GNUNET_FS_tree_encoder_next (uc->tc);
338 }
339
340
341 /**
342  * Function called once the hash of the file
343  * that is being unindexed has been computed.
344  *
345  * @param cls closure, unindex context
346  * @param file_id computed hash, NULL on error
347  */
348 static void 
349 process_hash (void *cls,
350               const GNUNET_HashCode *file_id)
351 {
352   struct GNUNET_FS_UnindexContext *uc = cls;
353   struct UnindexMessage req;
354
355   if (uc->state != UNINDEX_STATE_HASHING) 
356     {
357       GNUNET_FS_unindex_stop (uc);
358       return;
359     }
360   if (file_id == NULL)
361     {
362       uc->state = UNINDEX_STATE_ERROR;
363       signal_unindex_error (uc, 
364                             _("Failed to compute hash of file."));
365       return;
366     }
367   uc->file_id = *file_id;
368   uc->state = UNINDEX_STATE_FS_NOTIFY;
369   uc->client = GNUNET_CLIENT_connect (uc->h->sched,
370                                       "fs",
371                                       uc->h->cfg);
372   req.header.size = htons (sizeof (struct UnindexMessage));
373   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
374   req.reserved = 0;
375   req.file_id = *file_id;
376   GNUNET_CLIENT_transmit_and_get_response (uc->client,
377                                            &req.header,
378                                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
379                                            GNUNET_YES,
380                                            &process_fs_response,
381                                            uc);
382 }
383
384
385 /**
386  * Unindex a file.
387  *
388  * @param h handle to the file sharing subsystem
389  * @param filename file to unindex
390  * @param cctx initial value for the client context
391  * @return NULL on error, otherwise handle 
392  */
393 struct GNUNET_FS_UnindexContext *
394 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
395                          const char *filename,
396                          void *cctx)
397 {
398   struct GNUNET_FS_UnindexContext *ret;
399   struct GNUNET_FS_ProgressInfo pi;
400   uint64_t size;
401
402   if (GNUNET_OK !=
403       GNUNET_DISK_file_size (filename,
404                              &size,
405                              GNUNET_YES))
406     return NULL;
407   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
408   ret->h = h;
409   ret->filename = GNUNET_strdup (filename);
410   ret->start_time = GNUNET_TIME_absolute_get ();
411   ret->file_size = size;
412   ret->client_info = cctx;
413
414   // FIXME: make persistent!
415   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
416   make_unindex_status (&pi, ret, 0);
417   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
418   ret->client_info
419     = h->upcb (h->upcb_cls,
420                &pi);
421   GNUNET_CRYPTO_hash_file (h->sched,
422                            GNUNET_SCHEDULER_PRIORITY_IDLE,
423                            GNUNET_NO,
424                            filename,
425                            HASHING_BLOCKSIZE,
426                            &process_hash,
427                            ret);
428   return ret;
429 }
430
431
432 /**
433  * Clean up after completion of an unindex operation.
434  *
435  * @param uc handle
436  */
437 void
438 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
439 {  
440   struct GNUNET_FS_ProgressInfo pi;
441
442   if ( (uc->state != UNINDEX_STATE_COMPLETE) &&
443        (uc->state != UNINDEX_STATE_ERROR) )
444     {
445       uc->state = UNINDEX_STATE_ABORTED;
446       return;
447     }
448   // FIXME: make unpersistent!
449   make_unindex_status (&pi, uc, 
450                        (uc->state == UNINDEX_STATE_COMPLETE)
451                        ? uc->file_size : 0);
452   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
453   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
454   uc->client_info
455     = uc->h->upcb (uc->h->upcb_cls,
456                    &pi);
457   GNUNET_break (NULL == uc->client_info);
458   GNUNET_free (uc->filename);
459   GNUNET_free (uc);
460 }
461
462 /* end of fs_unindex.c */