arg
[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, 0 for DBLOCK
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 chk content hash key for the block (key for lookup in the datastore)
192  * @param offset offset of the block
193  * @param depth depth of the block, 0 for DBLOCK
194  * @param type type of the block (IBLOCK or DBLOCK)
195  * @param block the (encrypted) block
196  * @param block_size size of block (in bytes)
197  */
198 static void 
199 unindex_process (void *cls,
200                  const struct ContentHashKey *chk,
201                  uint64_t offset,
202                  unsigned int depth,
203                  enum GNUNET_BLOCK_Type type,
204                  const void *block,
205                  uint16_t block_size)
206 {
207   struct GNUNET_FS_UnindexContext *uc = cls;
208   uint32_t size;
209   const void *data;
210   struct OnDemandBlock odb;
211
212   if (type != GNUNET_BLOCK_TYPE_FS_DBLOCK)
213     {
214       size = block_size;
215       data = block;
216     }
217   else /* on-demand encoded DBLOCK */
218     {
219       size = sizeof(struct OnDemandBlock);
220       odb.offset = GNUNET_htonll (offset);
221       odb.file_id = uc->file_id;
222       data = &odb;
223     }
224 #if DEBUG_UNINDEX
225   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226               "Sending REMOVE request to DATASTORE service\n");
227 #endif
228   GNUNET_DATASTORE_remove (uc->dsh,
229                            &chk->query,
230                            size,
231                            data,
232                            -2, 1,
233                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
234                            &process_cont,
235                            uc);
236 }
237
238
239 /**
240  * Function called with the response from the
241  * FS service to our unindexing request.
242  *
243  * @param cls closure, unindex context
244  * @param msg NULL on timeout, otherwise the response
245  */
246 static void
247 process_fs_response (void *cls,
248                      const struct GNUNET_MessageHeader *msg)
249 {
250   struct GNUNET_FS_UnindexContext *uc = cls;
251   struct GNUNET_FS_ProgressInfo pi;
252
253   if (uc->client != NULL)
254     {
255       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
256       uc->client = NULL;
257     }
258   if (uc->state != UNINDEX_STATE_FS_NOTIFY) 
259     {
260       uc->state = UNINDEX_STATE_ERROR;
261       uc->emsg = GNUNET_strdup (_("Unexpected time for a response from `fs' service."));
262       GNUNET_FS_unindex_sync_ (uc);
263       signal_unindex_error (uc);
264       return;
265     }
266   if (NULL == msg)
267     {
268       uc->state = UNINDEX_STATE_ERROR;
269       uc->emsg = GNUNET_strdup (_("Timeout waiting for `fs' service."));
270       GNUNET_FS_unindex_sync_ (uc);
271       signal_unindex_error (uc);
272       return;
273     }
274   if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
275     {
276       uc->state = UNINDEX_STATE_ERROR;
277       uc->emsg = GNUNET_strdup (_("Invalid response from `fs' service."));
278       GNUNET_FS_unindex_sync_ (uc);
279       signal_unindex_error (uc);                            
280       return;      
281     }
282   uc->state = UNINDEX_STATE_COMPLETE;
283   pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
284   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
285   GNUNET_FS_unindex_sync_ (uc);
286   GNUNET_FS_unindex_make_status_ (&pi, uc, uc->file_size);
287 }
288
289
290 /**
291  * Function called when the tree encoder has
292  * processed all blocks.  Clean up.
293  *
294  * @param cls our unindexing context
295  * @param tc not used
296  */
297 static void
298 unindex_finish (void *cls,
299                 const struct GNUNET_SCHEDULER_TaskContext *tc)
300 {
301   struct GNUNET_FS_UnindexContext *uc = cls;
302   char *emsg;
303   struct GNUNET_FS_Uri *uri;
304   struct UnindexMessage req;
305
306   /* generate final progress message */
307   unindex_progress (uc, 
308                     uc->file_size,
309                     NULL,
310                     0, 0);
311   GNUNET_FS_tree_encoder_finish (uc->tc,
312                                  &uri,
313                                  &emsg);
314   uc->tc = NULL;
315   if (uri != NULL)
316     GNUNET_FS_uri_destroy (uri);
317   GNUNET_DISK_file_close (uc->fh);
318   uc->fh = NULL;
319   GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
320   uc->dsh = NULL;
321   uc->state = UNINDEX_STATE_FS_NOTIFY;
322   GNUNET_FS_unindex_sync_ (uc);
323   uc->client = GNUNET_CLIENT_connect ("fs",
324                                       uc->h->cfg);
325   if (uc->client == NULL)
326     {
327       uc->state = UNINDEX_STATE_ERROR;
328       uc->emsg = GNUNET_strdup (_("Failed to connect to FS service for unindexing."));
329       GNUNET_FS_unindex_sync_ (uc);
330       signal_unindex_error (uc);
331       return;
332     }
333 #if DEBUG_UNINDEX
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335               "Sending UNINDEX message to FS service\n");
336 #endif
337   req.header.size = htons (sizeof (struct UnindexMessage));
338   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
339   req.reserved = 0;
340   req.file_id = uc->file_id;
341   GNUNET_break (GNUNET_OK == 
342                 GNUNET_CLIENT_transmit_and_get_response (uc->client,
343                                                          &req.header,
344                                                          GNUNET_CONSTANTS_SERVICE_TIMEOUT,
345                                                          GNUNET_YES,
346                                                          &process_fs_response,
347                                                          uc));
348 }
349
350
351 /**
352  * Connect to the datastore and remove the blocks.
353  *
354  * @param uc context for the unindex operation.
355  */
356 void 
357 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
358 {
359   uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
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 (GNUNET_SCHEDULER_PRIORITY_IDLE,
510                                       filename,
511                                       HASHING_BLOCKSIZE,
512                                       &GNUNET_FS_unindex_process_hash_,
513                                       ret);
514   ret->top = GNUNET_FS_make_top (h,
515                                  &GNUNET_FS_unindex_signal_suspend_,
516                                  ret);
517   return ret;
518 }
519
520
521 /**
522  * Clean up after completion of an unindex operation.
523  *
524  * @param uc handle
525  */
526 void
527 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
528 {  
529   struct GNUNET_FS_ProgressInfo pi;
530   
531   if (uc->fhc != NULL)
532     {
533       GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
534       uc->fhc = NULL;
535     }
536   if (uc->client != NULL)
537     {
538       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
539       uc->client = NULL;
540     }
541   if (NULL != uc->dsh)
542     {
543       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
544       uc->dsh = NULL;
545     }
546   if (NULL != uc->tc)
547     {
548       GNUNET_FS_tree_encoder_finish (uc->tc,
549                                      NULL, 
550                                      NULL);
551       uc->tc = NULL;
552     }
553   if (uc->fh != NULL)
554     {
555       GNUNET_DISK_file_close (uc->fh);
556       uc->fh = NULL;
557     }
558   GNUNET_FS_end_top (uc->h, uc->top);
559   if (uc->serialization != NULL)
560     {
561       GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
562       GNUNET_free (uc->serialization);
563       uc->serialization = NULL;
564     }
565   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
566   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
567   GNUNET_FS_unindex_make_status_ (&pi, uc, 
568                                   (uc->state == UNINDEX_STATE_COMPLETE)
569                                   ? uc->file_size : 0);
570   GNUNET_break (NULL == uc->client_info);
571   GNUNET_free (uc->filename);
572   GNUNET_free (uc);
573 }
574
575 /* end of fs_unindex.c */