(no commit message)
[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_FS_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 ("fs",
322                                       uc->h->cfg);
323   if (uc->client == NULL)
324     {
325       uc->state = UNINDEX_STATE_ERROR;
326       uc->emsg = GNUNET_strdup (_("Failed to connect to FS service for unindexing."));
327       GNUNET_FS_unindex_sync_ (uc);
328       signal_unindex_error (uc);
329       return;
330     }
331 #if DEBUG_UNINDEX
332   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333               "Sending UNINDEX message to FS service\n");
334 #endif
335   req.header.size = htons (sizeof (struct UnindexMessage));
336   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
337   req.reserved = 0;
338   req.file_id = uc->file_id;
339   GNUNET_break (GNUNET_OK == 
340                 GNUNET_CLIENT_transmit_and_get_response (uc->client,
341                                                          &req.header,
342                                                          GNUNET_CONSTANTS_SERVICE_TIMEOUT,
343                                                          GNUNET_YES,
344                                                          &process_fs_response,
345                                                          uc));
346 }
347
348
349 /**
350  * Connect to the datastore and remove the blocks.
351  *
352  * @param uc context for the unindex operation.
353  */
354 void 
355 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
356 {
357   uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
358   if (NULL == uc->dsh)
359     {
360       uc->state = UNINDEX_STATE_ERROR;
361       uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
362       GNUNET_FS_unindex_sync_ (uc);
363       signal_unindex_error (uc);
364       return;
365     }
366   uc->fh = GNUNET_DISK_file_open (uc->filename,
367                                   GNUNET_DISK_OPEN_READ,
368                                   GNUNET_DISK_PERM_NONE);
369   if (NULL == uc->fh)
370     {
371       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
372       uc->dsh = NULL;
373       uc->state = UNINDEX_STATE_ERROR;
374       uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
375       GNUNET_FS_unindex_sync_ (uc);
376       signal_unindex_error (uc);
377       return;
378     }
379   uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
380                                           uc->file_size,
381                                           uc,
382                                           &unindex_reader,
383                                           &unindex_process,
384                                           &unindex_progress,
385                                           &unindex_finish);
386   GNUNET_FS_tree_encoder_next (uc->tc);
387 }
388
389
390 /**
391  * Function called once the hash of the file
392  * that is being unindexed has been computed.
393  *
394  * @param cls closure, unindex context
395  * @param file_id computed hash, NULL on error
396  */
397 void 
398 GNUNET_FS_unindex_process_hash_ (void *cls,
399                                  const GNUNET_HashCode *file_id)
400 {
401   struct GNUNET_FS_UnindexContext *uc = cls;
402
403   uc->fhc = NULL;
404   if (uc->state != UNINDEX_STATE_HASHING) 
405     {
406       GNUNET_FS_unindex_stop (uc);
407       return;
408     }
409   if (file_id == NULL)
410     {
411       uc->state = UNINDEX_STATE_ERROR;
412       uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
413       GNUNET_FS_unindex_sync_ (uc);
414       signal_unindex_error (uc);
415       return;
416     }
417   uc->file_id = *file_id;
418   uc->state = UNINDEX_STATE_DS_REMOVE;
419   GNUNET_FS_unindex_sync_ (uc);
420   GNUNET_FS_unindex_do_remove_ (uc);
421 }
422
423
424 /**
425  * Create SUSPEND event for the given unindex operation
426  * and then clean up our state (without stop signal).
427  *
428  * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
429  */
430 void
431 GNUNET_FS_unindex_signal_suspend_ (void *cls)
432 {
433   struct GNUNET_FS_UnindexContext *uc = cls;
434   struct GNUNET_FS_ProgressInfo pi;
435
436   if (uc->fhc != NULL)
437     {
438       GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
439       uc->fhc = NULL;
440     }
441   if (uc->client != NULL)
442     {
443       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
444       uc->client = NULL;
445     }
446   if (NULL != uc->dsh)
447     {
448       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
449       uc->dsh = NULL;
450     }
451   if (NULL != uc->tc)
452     {
453       GNUNET_FS_tree_encoder_finish (uc->tc,
454                                      NULL, 
455                                      NULL);
456       uc->tc = NULL;
457     }
458   if (uc->fh != NULL)
459     {
460       GNUNET_DISK_file_close (uc->fh);
461       uc->fh = NULL;
462     }
463   GNUNET_FS_end_top (uc->h, uc->top);
464   pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
465   GNUNET_FS_unindex_make_status_ (&pi, uc, 
466                                   (uc->state == UNINDEX_STATE_COMPLETE)
467                                   ? uc->file_size : 0);
468   GNUNET_break (NULL == uc->client_info);
469   GNUNET_free (uc->filename);
470   GNUNET_free_non_null (uc->serialization);
471   GNUNET_free (uc);
472 }
473
474
475 /**
476  * Unindex a file.
477  *
478  * @param h handle to the file sharing subsystem
479  * @param filename file to unindex
480  * @param cctx initial value for the client context
481  * @return NULL on error, otherwise handle 
482  */
483 struct GNUNET_FS_UnindexContext *
484 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
485                          const char *filename,
486                          void *cctx)
487 {
488   struct GNUNET_FS_UnindexContext *ret;
489   struct GNUNET_FS_ProgressInfo pi;
490   uint64_t size;
491
492   if (GNUNET_OK !=
493       GNUNET_DISK_file_size (filename,
494                              &size,
495                              GNUNET_YES))
496     return NULL;
497   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
498   ret->h = h;
499   ret->filename = GNUNET_strdup (filename);
500   ret->start_time = GNUNET_TIME_absolute_get ();
501   ret->file_size = size;
502   ret->client_info = cctx;
503   GNUNET_FS_unindex_sync_ (ret);
504   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
505   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
506   GNUNET_FS_unindex_make_status_ (&pi, ret, 0);
507   ret->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
508                                       filename,
509                                       HASHING_BLOCKSIZE,
510                                       &GNUNET_FS_unindex_process_hash_,
511                                       ret);
512   ret->top = GNUNET_FS_make_top (h,
513                                  &GNUNET_FS_unindex_signal_suspend_,
514                                  ret);
515   return ret;
516 }
517
518
519 /**
520  * Clean up after completion of an unindex operation.
521  *
522  * @param uc handle
523  */
524 void
525 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
526 {  
527   struct GNUNET_FS_ProgressInfo pi;
528   
529   if (uc->fhc != NULL)
530     {
531       GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
532       uc->fhc = NULL;
533     }
534   if (uc->client != NULL)
535     {
536       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
537       uc->client = NULL;
538     }
539   if (NULL != uc->dsh)
540     {
541       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
542       uc->dsh = NULL;
543     }
544   if (NULL != uc->tc)
545     {
546       GNUNET_FS_tree_encoder_finish (uc->tc,
547                                      NULL, 
548                                      NULL);
549       uc->tc = NULL;
550     }
551   if (uc->fh != NULL)
552     {
553       GNUNET_DISK_file_close (uc->fh);
554       uc->fh = NULL;
555     }
556   GNUNET_FS_end_top (uc->h, uc->top);
557   if (uc->serialization != NULL)
558     {
559       GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
560       GNUNET_free (uc->serialization);
561       uc->serialization = NULL;
562     }
563   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
564   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
565   GNUNET_FS_unindex_make_status_ (&pi, uc, 
566                                   (uc->state == UNINDEX_STATE_COMPLETE)
567                                   ? uc->file_size : 0);
568   GNUNET_break (NULL == uc->client_info);
569   GNUNET_free (uc->filename);
570   GNUNET_free (uc);
571 }
572
573 /* end of fs_unindex.c */