expose our hello to plugins
[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 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_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 #define DEBUG_UNINDEX GNUNET_NO
35
36 /**
37  * Function called by the tree encoder to obtain
38  * a block of plaintext data (for the lowest level
39  * of the tree).
40  *
41  * @param cls our publishing context
42  * @param offset identifies which block to get
43  * @param max (maximum) number of bytes to get; returning
44  *        fewer will also cause errors
45  * @param buf where to copy the plaintext buffer
46  * @param emsg location to store an error message (on error)
47  * @return number of bytes copied to buf, 0 on error
48  */
49 static size_t
50 unindex_reader (void *cls,
51                 uint64_t offset,
52                 size_t max, 
53                 void *buf,
54                 char **emsg)
55 {
56   struct GNUNET_FS_UnindexContext *uc = cls;
57   size_t pt_size;
58
59   pt_size = GNUNET_MIN(max,
60                        uc->file_size - offset);
61   if (offset != 
62       GNUNET_DISK_file_seek (uc->fh, offset, GNUNET_DISK_SEEK_SET))
63     {
64       *emsg = GNUNET_strdup (_("Failed to find given position in file"));
65       return 0;
66     }
67   if (pt_size !=
68       GNUNET_DISK_file_read (uc->fh,
69                              buf,
70                              pt_size))
71     {
72       *emsg = GNUNET_strdup (_("Failed to read file"));
73       return 0;
74     }
75   return pt_size;
76 }
77
78
79 /**
80  * Fill in all of the generic fields for 
81  * an unindex event and call the callback.
82  *
83  * @param pi structure to fill in
84  * @param uc overall unindex context
85  * @param offset where we are in the file (for progress)
86  */
87 void
88 GNUNET_FS_unindex_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
89                                 struct GNUNET_FS_UnindexContext *uc,
90                                 uint64_t offset)
91 {
92   pi->value.unindex.uc = uc;
93   pi->value.unindex.cctx = uc->client_info;
94   pi->value.unindex.filename = uc->filename;
95   pi->value.unindex.size = uc->file_size;
96   pi->value.unindex.eta 
97     = GNUNET_TIME_calculate_eta (uc->start_time,
98                                  offset,
99                                  uc->file_size);
100   pi->value.unindex.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
101   pi->value.unindex.completed = offset;
102   uc->client_info 
103     = uc->h->upcb (uc->h->upcb_cls,
104                    pi);
105
106 }
107
108
109 /**
110  * Function called with information about our
111  * progress in computing the tree encoding.
112  *
113  * @param cls closure
114  * @param offset where are we in the file
115  * @param pt_block plaintext of the currently processed block
116  * @param pt_size size of pt_block
117  * @param depth depth of the block in the tree
118  */
119 static void
120 unindex_progress (void *cls,
121                   uint64_t offset,
122                   const void *pt_block,
123                   size_t pt_size,
124                   unsigned int depth)
125 {
126   struct GNUNET_FS_UnindexContext *uc = cls;
127   struct GNUNET_FS_ProgressInfo pi;
128
129   pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
130   pi.value.unindex.specifics.progress.data = pt_block;
131   pi.value.unindex.specifics.progress.offset = offset;
132   pi.value.unindex.specifics.progress.data_len = pt_size;
133   pi.value.unindex.specifics.progress.depth = depth;
134   GNUNET_FS_unindex_make_status_ (&pi, uc, offset);
135 }
136                                                
137
138 /**
139  * We've encountered an error during
140  * unindexing.  Signal the client.
141  *
142  * @param uc context for the failed unindexing operation
143  */
144 static void
145 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc)
146 {
147   struct GNUNET_FS_ProgressInfo pi;
148   
149   pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
150   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
151   pi.value.unindex.specifics.error.message = uc->emsg;
152   GNUNET_FS_unindex_make_status_ (&pi, uc, 0);
153 }
154
155
156 /**
157  * Continuation called to notify client about result of the
158  * datastore removal operation.
159  *
160  * @param cls closure
161  * @param success GNUNET_SYSERR on failure
162  * @param msg NULL on success, otherwise an error message
163  */
164 static void
165 process_cont (void *cls,
166               int success,
167               const char *msg)
168 {
169   struct GNUNET_FS_UnindexContext *uc = cls;
170   if (success == GNUNET_SYSERR)
171     {
172       uc->emsg = GNUNET_strdup (msg);
173       signal_unindex_error (uc);
174       return;
175     }  
176 #if DEBUG_UNINDEX
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178               "Datastore REMOVE operation succeeded\n");
179 #endif
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                  enum GNUNET_BLOCK_Type 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_BLOCK_TYPE_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 = GNUNET_htonll (offset);
219       odb.file_id = uc->file_id;
220       data = &odb;
221     }
222 #if DEBUG_UNINDEX
223   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224               "Sending REMOVE request to DATASTORE service\n");
225 #endif
226   GNUNET_DATASTORE_remove (uc->dsh,
227                            query,
228                            size,
229                            data,
230                            -2, 1,
231                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
232                            &process_cont,
233                            uc);
234 }
235
236
237 /**
238  * Function called with the response from the
239  * FS service to our unindexing request.
240  *
241  * @param cls closure, unindex context
242  * @param msg NULL on timeout, otherwise the response
243  */
244 static void
245 process_fs_response (void *cls,
246                      const struct GNUNET_MessageHeader *msg)
247 {
248   struct GNUNET_FS_UnindexContext *uc = cls;
249   struct GNUNET_FS_ProgressInfo pi;
250
251   if (uc->client != NULL)
252     {
253       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
254       uc->client = NULL;
255     }
256   if (uc->state != UNINDEX_STATE_FS_NOTIFY) 
257     {
258       uc->state = UNINDEX_STATE_ERROR;
259       uc->emsg = GNUNET_strdup (_("Unexpected time for a response from `fs' service."));
260       GNUNET_FS_unindex_sync_ (uc);
261       signal_unindex_error (uc);
262       return;
263     }
264   if (NULL == msg)
265     {
266       uc->state = UNINDEX_STATE_ERROR;
267       uc->emsg = GNUNET_strdup (_("Timeout waiting for `fs' service."));
268       GNUNET_FS_unindex_sync_ (uc);
269       signal_unindex_error (uc);
270       return;
271     }
272   if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
273     {
274       uc->state = UNINDEX_STATE_ERROR;
275       uc->emsg = GNUNET_strdup (_("Invalid response from `fs' service."));
276       GNUNET_FS_unindex_sync_ (uc);
277       signal_unindex_error (uc);                            
278       return;      
279     }
280   uc->state = UNINDEX_STATE_COMPLETE;
281   pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
282   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
283   GNUNET_FS_unindex_sync_ (uc);
284   GNUNET_FS_unindex_make_status_ (&pi, uc, uc->file_size);
285 }
286
287
288 /**
289  * Function called when the tree encoder has
290  * processed all blocks.  Clean up.
291  *
292  * @param cls our unindexing context
293  * @param tc not used
294  */
295 static void
296 unindex_finish (void *cls,
297                 const struct GNUNET_SCHEDULER_TaskContext *tc)
298 {
299   struct GNUNET_FS_UnindexContext *uc = cls;
300   char *emsg;
301   struct GNUNET_FS_Uri *uri;
302   struct UnindexMessage req;
303
304   /* generate final progress message */
305   unindex_progress (uc, 
306                     uc->file_size,
307                     NULL,
308                     0, 0);
309   GNUNET_FS_tree_encoder_finish (uc->tc,
310                                  &uri,
311                                  &emsg);
312   uc->tc = NULL;
313   if (uri != NULL)
314     GNUNET_FS_uri_destroy (uri);
315   GNUNET_DISK_file_close (uc->fh);
316   uc->fh = NULL;
317   GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
318   uc->dsh = NULL;
319   uc->state = UNINDEX_STATE_FS_NOTIFY;
320   GNUNET_FS_unindex_sync_ (uc);
321   uc->client = GNUNET_CLIENT_connect (uc->h->sched,
322                                       "fs",
323                                       uc->h->cfg);
324   if (uc->client == NULL)
325     {
326       uc->state = UNINDEX_STATE_ERROR;
327       uc->emsg = GNUNET_strdup (_("Failed to connect to FS service for unindexing."));
328       GNUNET_FS_unindex_sync_ (uc);
329       signal_unindex_error (uc);
330       return;
331     }
332 #if DEBUG_UNINDEX
333   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334               "Sending UNINDEX message to FS service\n");
335 #endif
336   req.header.size = htons (sizeof (struct UnindexMessage));
337   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
338   req.reserved = 0;
339   req.file_id = uc->file_id;
340   GNUNET_break (GNUNET_OK == 
341                 GNUNET_CLIENT_transmit_and_get_response (uc->client,
342                                                          &req.header,
343                                                          GNUNET_CONSTANTS_SERVICE_TIMEOUT,
344                                                          GNUNET_YES,
345                                                          &process_fs_response,
346                                                          uc));
347 }
348
349
350 /**
351  * Connect to the datastore and remove the blocks.
352  *
353  * @param uc context for the unindex operation.
354  */
355 void 
356 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
357 {
358   uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
359                                       uc->h->sched);
360   if (NULL == uc->dsh)
361     {
362       uc->state = UNINDEX_STATE_ERROR;
363       uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
364       GNUNET_FS_unindex_sync_ (uc);
365       signal_unindex_error (uc);
366       return;
367     }
368   uc->fh = GNUNET_DISK_file_open (uc->filename,
369                                   GNUNET_DISK_OPEN_READ,
370                                   GNUNET_DISK_PERM_NONE);
371   if (NULL == uc->fh)
372     {
373       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
374       uc->dsh = NULL;
375       uc->state = UNINDEX_STATE_ERROR;
376       uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
377       GNUNET_FS_unindex_sync_ (uc);
378       signal_unindex_error (uc);
379       return;
380     }
381   uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
382                                           uc->file_size,
383                                           uc,
384                                           &unindex_reader,
385                                           &unindex_process,
386                                           &unindex_progress,
387                                           &unindex_finish);
388   GNUNET_FS_tree_encoder_next (uc->tc);
389 }
390
391
392 /**
393  * Function called once the hash of the file
394  * that is being unindexed has been computed.
395  *
396  * @param cls closure, unindex context
397  * @param file_id computed hash, NULL on error
398  */
399 void 
400 GNUNET_FS_unindex_process_hash_ (void *cls,
401                                  const GNUNET_HashCode *file_id)
402 {
403   struct GNUNET_FS_UnindexContext *uc = cls;
404
405   uc->fhc = NULL;
406   if (uc->state != UNINDEX_STATE_HASHING) 
407     {
408       GNUNET_FS_unindex_stop (uc);
409       return;
410     }
411   if (file_id == NULL)
412     {
413       uc->state = UNINDEX_STATE_ERROR;
414       uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
415       GNUNET_FS_unindex_sync_ (uc);
416       signal_unindex_error (uc);
417       return;
418     }
419   uc->file_id = *file_id;
420   uc->state = UNINDEX_STATE_DS_REMOVE;
421   GNUNET_FS_unindex_sync_ (uc);
422   GNUNET_FS_unindex_do_remove_ (uc);
423 }
424
425
426 /**
427  * Create SUSPEND event for the given unindex operation
428  * and then clean up our state (without stop signal).
429  *
430  * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
431  */
432 void
433 GNUNET_FS_unindex_signal_suspend_ (void *cls)
434 {
435   struct GNUNET_FS_UnindexContext *uc = cls;
436   struct GNUNET_FS_ProgressInfo pi;
437
438   if (uc->fhc != NULL)
439     {
440       GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
441       uc->fhc = NULL;
442     }
443   if (uc->client != NULL)
444     {
445       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
446       uc->client = NULL;
447     }
448   if (NULL != uc->dsh)
449     {
450       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
451       uc->dsh = NULL;
452     }
453   if (NULL != uc->tc)
454     {
455       GNUNET_FS_tree_encoder_finish (uc->tc,
456                                      NULL, 
457                                      NULL);
458       uc->tc = NULL;
459     }
460   if (uc->fh != NULL)
461     {
462       GNUNET_DISK_file_close (uc->fh);
463       uc->fh = NULL;
464     }
465   GNUNET_FS_end_top (uc->h, uc->top);
466   pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
467   GNUNET_FS_unindex_make_status_ (&pi, uc, 
468                                   (uc->state == UNINDEX_STATE_COMPLETE)
469                                   ? uc->file_size : 0);
470   GNUNET_break (NULL == uc->client_info);
471   GNUNET_free (uc->filename);
472   GNUNET_free_non_null (uc->serialization);
473   GNUNET_free (uc);
474 }
475
476
477 /**
478  * Unindex a file.
479  *
480  * @param h handle to the file sharing subsystem
481  * @param filename file to unindex
482  * @param cctx initial value for the client context
483  * @return NULL on error, otherwise handle 
484  */
485 struct GNUNET_FS_UnindexContext *
486 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
487                          const char *filename,
488                          void *cctx)
489 {
490   struct GNUNET_FS_UnindexContext *ret;
491   struct GNUNET_FS_ProgressInfo pi;
492   uint64_t size;
493
494   if (GNUNET_OK !=
495       GNUNET_DISK_file_size (filename,
496                              &size,
497                              GNUNET_YES))
498     return NULL;
499   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
500   ret->h = h;
501   ret->filename = GNUNET_strdup (filename);
502   ret->start_time = GNUNET_TIME_absolute_get ();
503   ret->file_size = size;
504   ret->client_info = cctx;
505   GNUNET_FS_unindex_sync_ (ret);
506   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
507   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
508   GNUNET_FS_unindex_make_status_ (&pi, ret, 0);
509   ret->fhc = GNUNET_CRYPTO_hash_file (h->sched,
510                                       GNUNET_SCHEDULER_PRIORITY_IDLE,
511                                       filename,
512                                       HASHING_BLOCKSIZE,
513                                       &GNUNET_FS_unindex_process_hash_,
514                                       ret);
515   ret->top = GNUNET_FS_make_top (h,
516                                  &GNUNET_FS_unindex_signal_suspend_,
517                                  ret);
518   return ret;
519 }
520
521
522 /**
523  * Clean up after completion of an unindex operation.
524  *
525  * @param uc handle
526  */
527 void
528 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
529 {  
530   struct GNUNET_FS_ProgressInfo pi;
531   
532   if (uc->fhc != NULL)
533     {
534       GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
535       uc->fhc = NULL;
536     }
537   if (uc->client != NULL)
538     {
539       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
540       uc->client = NULL;
541     }
542   if (NULL != uc->dsh)
543     {
544       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
545       uc->dsh = NULL;
546     }
547   if (NULL != uc->tc)
548     {
549       GNUNET_FS_tree_encoder_finish (uc->tc,
550                                      NULL, 
551                                      NULL);
552       uc->tc = NULL;
553     }
554   if (uc->fh != NULL)
555     {
556       GNUNET_DISK_file_close (uc->fh);
557       uc->fh = NULL;
558     }
559   GNUNET_FS_end_top (uc->h, uc->top);
560   if (uc->serialization != NULL)
561     {
562       GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
563       GNUNET_free (uc->serialization);
564       uc->serialization = NULL;
565     }
566   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
567   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
568   GNUNET_FS_unindex_make_status_ (&pi, uc, 
569                                   (uc->state == UNINDEX_STATE_COMPLETE)
570                                   ? uc->file_size : 0);
571   GNUNET_break (NULL == uc->client_info);
572   GNUNET_free (uc->filename);
573   GNUNET_free (uc);
574 }
575
576 /* end of fs_unindex.c */