349cc42515293840b741ded2fb08ac71aeb8997e
[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 and call the callback.
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 void
87 GNUNET_FS_unindex_make_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.unindex.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
100   pi->value.unindex.completed = offset;
101   uc->client_info 
102     = uc->h->upcb (uc->h->upcb_cls,
103                    pi);
104
105 }
106
107
108 /**
109  * Function called with information about our
110  * progress in computing the tree encoding.
111  *
112  * @param cls closure
113  * @param offset where are we in the file
114  * @param pt_block plaintext of the currently processed block
115  * @param pt_size size of pt_block
116  * @param depth depth of the block in the tree
117  */
118 static void
119 unindex_progress (void *cls,
120                   uint64_t offset,
121                   const void *pt_block,
122                   size_t pt_size,
123                   unsigned int depth)
124 {
125   struct GNUNET_FS_UnindexContext *uc = cls;
126   struct GNUNET_FS_ProgressInfo pi;
127
128   pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
129   pi.value.unindex.specifics.progress.data = pt_block;
130   pi.value.unindex.specifics.progress.offset = offset;
131   pi.value.unindex.specifics.progress.data_len = pt_size;
132   pi.value.unindex.specifics.progress.depth = depth;
133   GNUNET_FS_unindex_make_status_ (&pi, uc, offset);
134 }
135                                                
136
137 /**
138  * We've encountered an error during
139  * unindexing.  Signal the client.
140  *
141  * @param uc context for the failed unindexing operation
142  */
143 static void
144 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc)
145 {
146   struct GNUNET_FS_ProgressInfo pi;
147   
148   pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
149   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
150   pi.value.unindex.specifics.error.message = uc->emsg;
151   GNUNET_FS_unindex_make_status_ (&pi, uc, 0);
152 }
153
154
155 /**
156  * Continuation called to notify client about result of the
157  * datastore removal operation.
158  *
159  * @param cls closure
160  * @param success GNUNET_SYSERR on failure
161  * @param msg NULL on success, otherwise an error message
162  */
163 static void
164 process_cont (void *cls,
165               int success,
166               const char *msg)
167 {
168   struct GNUNET_FS_UnindexContext *uc = cls;
169   if (success == GNUNET_SYSERR)
170     {
171       uc->emsg = GNUNET_strdup (msg);
172       signal_unindex_error (uc);
173       return;
174     }  
175   GNUNET_FS_tree_encoder_next (uc->tc);
176 }
177
178
179 /**
180  * Function called asking for the current (encoded)
181  * block to be processed.  After processing the
182  * client should either call "GNUNET_FS_tree_encode_next"
183  * or (on error) "GNUNET_FS_tree_encode_finish".
184  *
185  * @param cls closure
186  * @param query the query for the block (key for lookup in the datastore)
187  * @param offset offset of the block
188  * @param type type of the block (IBLOCK or DBLOCK)
189  * @param block the (encrypted) block
190  * @param block_size size of block (in bytes)
191  */
192 static void 
193 unindex_process (void *cls,
194                  const GNUNET_HashCode *query,
195                  uint64_t offset,
196                  enum GNUNET_BLOCK_Type type,
197                  const void *block,
198                  uint16_t block_size)
199 {
200   struct GNUNET_FS_UnindexContext *uc = cls;
201   uint32_t size;
202   const void *data;
203   struct OnDemandBlock odb;
204
205   if (type != GNUNET_BLOCK_TYPE_DBLOCK)
206     {
207       size = block_size;
208       data = block;
209     }
210   else /* on-demand encoded DBLOCK */
211     {
212       size = sizeof(struct OnDemandBlock);
213       odb.offset = GNUNET_htonll (offset);
214       odb.file_id = uc->file_id;
215       data = &odb;
216     }
217   GNUNET_DATASTORE_remove (uc->dsh,
218                            query,
219                            size,
220                            data,
221                            &process_cont,
222                            uc,
223                            GNUNET_CONSTANTS_SERVICE_TIMEOUT);
224 }
225
226
227 /**
228  * Function called when the tree encoder has
229  * processed all blocks.  Clean up.
230  *
231  * @param cls our unindexing context
232  * @param tc not used
233  */
234 static void
235 unindex_finish (void *cls,
236                 const struct GNUNET_SCHEDULER_TaskContext *tc)
237 {
238   struct GNUNET_FS_UnindexContext *uc = cls;
239   char *emsg;
240   struct GNUNET_FS_Uri *uri;
241   struct GNUNET_FS_ProgressInfo pi;
242
243   GNUNET_FS_tree_encoder_finish (uc->tc,
244                                  &uri,
245                                  &emsg);
246   uc->tc = NULL;
247   if (uri != NULL)
248     GNUNET_FS_uri_destroy (uri);
249   GNUNET_DISK_file_close (uc->fh);
250   uc->fh = NULL;
251   GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
252   uc->dsh = NULL;
253   if (emsg != NULL)
254     {
255       uc->state = UNINDEX_STATE_ERROR;
256       uc->emsg = emsg;
257       signal_unindex_error (uc);
258     }
259   else 
260     {   
261       uc->state = UNINDEX_STATE_COMPLETE;
262       pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
263       pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
264       GNUNET_FS_unindex_make_status_ (&pi, uc, uc->file_size);
265     }
266   GNUNET_FS_unindex_sync_ (uc);
267 }
268
269
270 /**
271  * Function called with the response from the
272  * FS service to our unindexing request.
273  *
274  * @param cls closure, unindex context
275  * @param msg NULL on timeout, otherwise the response
276  */
277 static void
278 process_fs_response (void *cls,
279                      const struct GNUNET_MessageHeader *msg)
280 {
281   struct GNUNET_FS_UnindexContext *uc = cls;
282
283   if (uc->client != NULL)
284     {
285       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
286       uc->client = NULL;
287     }
288   if (uc->state != UNINDEX_STATE_FS_NOTIFY) 
289     {
290       uc->state = UNINDEX_STATE_ERROR;
291       uc->emsg = GNUNET_strdup (_("Unexpected time for a response from `fs' service."));
292       GNUNET_FS_unindex_sync_ (uc);
293       signal_unindex_error (uc);                            
294       return;
295     }
296   if (NULL == msg)
297     {
298       uc->state = UNINDEX_STATE_ERROR;
299       uc->emsg = GNUNET_strdup (_("Timeout waiting for `fs' service."));
300       GNUNET_FS_unindex_sync_ (uc);
301       signal_unindex_error (uc);
302       return;
303     }
304   if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
305     {
306       uc->state = UNINDEX_STATE_ERROR;
307       uc->emsg = GNUNET_strdup (_("Invalid response from `fs' service."));
308       GNUNET_FS_unindex_sync_ (uc);
309       signal_unindex_error (uc);                            
310       return;      
311     }
312   uc->state = UNINDEX_STATE_DS_REMOVE;
313   GNUNET_FS_unindex_sync_ (uc);
314   GNUNET_FS_unindex_do_remove_ (uc);
315 }
316
317
318 /**
319  * Connect to the datastore and remove the blocks.
320  *
321  * @param uc context for the unindex operation.
322  */
323 void 
324 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
325 {
326   uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
327                                       uc->h->sched);
328   if (NULL == uc->dsh)
329     {
330       uc->state = UNINDEX_STATE_ERROR;
331       uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
332       GNUNET_FS_unindex_sync_ (uc);
333       signal_unindex_error (uc);
334       return;
335     }
336   uc->fh = GNUNET_DISK_file_open (uc->filename,
337                                   GNUNET_DISK_OPEN_READ,
338                                   GNUNET_DISK_PERM_NONE);
339   if (NULL == uc->fh)
340     {
341       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
342       uc->dsh = NULL;
343       uc->state = UNINDEX_STATE_ERROR;
344       uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
345       GNUNET_FS_unindex_sync_ (uc);
346       signal_unindex_error (uc);
347       return;
348     }
349   uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
350                                           uc->file_size,
351                                           uc,
352                                           &unindex_reader,
353                                           &unindex_process,
354                                           &unindex_progress,
355                                           &unindex_finish);
356   GNUNET_FS_tree_encoder_next (uc->tc);
357 }
358
359
360 /**
361  * Function called once the hash of the file
362  * that is being unindexed has been computed.
363  *
364  * @param cls closure, unindex context
365  * @param file_id computed hash, NULL on error
366  */
367 void 
368 GNUNET_FS_unindex_process_hash_ (void *cls,
369                                  const GNUNET_HashCode *file_id)
370 {
371   struct GNUNET_FS_UnindexContext *uc = cls;
372   struct UnindexMessage req;
373
374   uc->fhc = NULL;
375   if (uc->state != UNINDEX_STATE_HASHING) 
376     {
377       GNUNET_FS_unindex_stop (uc);
378       return;
379     }
380   if (file_id == NULL)
381     {
382       uc->state = UNINDEX_STATE_ERROR;
383       uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
384       GNUNET_FS_unindex_sync_ (uc);
385       signal_unindex_error (uc);
386       return;
387     }
388   uc->file_id = *file_id;
389   uc->state = UNINDEX_STATE_FS_NOTIFY;
390   GNUNET_FS_unindex_sync_ (uc);
391   uc->client = GNUNET_CLIENT_connect (uc->h->sched,
392                                       "fs",
393                                       uc->h->cfg);
394   req.header.size = htons (sizeof (struct UnindexMessage));
395   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
396   req.reserved = 0;
397   req.file_id = *file_id;
398   GNUNET_break (GNUNET_OK == 
399                 GNUNET_CLIENT_transmit_and_get_response (uc->client,
400                                                          &req.header,
401                                                          GNUNET_CONSTANTS_SERVICE_TIMEOUT,
402                                                          GNUNET_YES,
403                                                          &process_fs_response,
404                                                          uc));
405 }
406
407
408 /**
409  * Create SUSPEND event for the given unindex operation
410  * and then clean up our state (without stop signal).
411  *
412  * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
413  */
414 void
415 GNUNET_FS_unindex_signal_suspend_ (void *cls)
416 {
417   struct GNUNET_FS_UnindexContext *uc = cls;
418   struct GNUNET_FS_ProgressInfo pi;
419
420   if (uc->fhc != NULL)
421     {
422       GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
423       uc->fhc = NULL;
424     }
425   if (uc->client != NULL)
426     {
427       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
428       uc->client = NULL;
429     }
430   if (NULL != uc->dsh)
431     {
432       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
433       uc->dsh = NULL;
434     }
435   if (NULL != uc->tc)
436     {
437       GNUNET_FS_tree_encoder_finish (uc->tc,
438                                      NULL, 
439                                      NULL);
440       uc->tc = NULL;
441     }
442   if (uc->fh != NULL)
443     {
444       GNUNET_DISK_file_close (uc->fh);
445       uc->fh = NULL;
446     }
447   GNUNET_FS_end_top (uc->h, uc->top);
448   pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
449   GNUNET_FS_unindex_make_status_ (&pi, uc, 
450                                   (uc->state == UNINDEX_STATE_COMPLETE)
451                                   ? uc->file_size : 0);
452   GNUNET_break (NULL == uc->client_info);
453   GNUNET_free (uc->filename);
454   GNUNET_free_non_null (uc->serialization);
455   GNUNET_free (uc);
456 }
457
458
459 /**
460  * Unindex a file.
461  *
462  * @param h handle to the file sharing subsystem
463  * @param filename file to unindex
464  * @param cctx initial value for the client context
465  * @return NULL on error, otherwise handle 
466  */
467 struct GNUNET_FS_UnindexContext *
468 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
469                          const char *filename,
470                          void *cctx)
471 {
472   struct GNUNET_FS_UnindexContext *ret;
473   struct GNUNET_FS_ProgressInfo pi;
474   uint64_t size;
475
476   if (GNUNET_OK !=
477       GNUNET_DISK_file_size (filename,
478                              &size,
479                              GNUNET_YES))
480     return NULL;
481   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
482   ret->h = h;
483   ret->filename = GNUNET_strdup (filename);
484   ret->start_time = GNUNET_TIME_absolute_get ();
485   ret->file_size = size;
486   ret->client_info = cctx;
487   GNUNET_FS_unindex_sync_ (ret);
488   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
489   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
490   GNUNET_FS_unindex_make_status_ (&pi, ret, 0);
491   ret->fhc = GNUNET_CRYPTO_hash_file (h->sched,
492                                       GNUNET_SCHEDULER_PRIORITY_IDLE,
493                                       filename,
494                                       HASHING_BLOCKSIZE,
495                                       &GNUNET_FS_unindex_process_hash_,
496                                       ret);
497   ret->top = GNUNET_FS_make_top (h,
498                                  &GNUNET_FS_unindex_signal_suspend_,
499                                  ret);
500   return ret;
501 }
502
503
504 /**
505  * Clean up after completion of an unindex operation.
506  *
507  * @param uc handle
508  */
509 void
510 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
511 {  
512   struct GNUNET_FS_ProgressInfo pi;
513   
514   if (uc->fhc != NULL)
515     {
516       GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
517       uc->fhc = NULL;
518     }
519   if (uc->client != NULL)
520     {
521       GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
522       uc->client = NULL;
523     }
524   if (NULL != uc->dsh)
525     {
526       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
527       uc->dsh = NULL;
528     }
529   if (NULL != uc->tc)
530     {
531       GNUNET_FS_tree_encoder_finish (uc->tc,
532                                      NULL, 
533                                      NULL);
534       uc->tc = NULL;
535     }
536   if (uc->fh != NULL)
537     {
538       GNUNET_DISK_file_close (uc->fh);
539       uc->fh = NULL;
540     }
541   GNUNET_FS_end_top (uc->h, uc->top);
542   if (uc->serialization != NULL)
543     {
544       GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
545       GNUNET_free (uc->serialization);
546       uc->serialization = NULL;
547     }
548   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
549   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
550   GNUNET_FS_unindex_make_status_ (&pi, uc, 
551                                   (uc->state == UNINDEX_STATE_COMPLETE)
552                                   ? uc->file_size : 0);
553   GNUNET_break (NULL == uc->client_info);
554   GNUNET_free (uc->filename);
555   GNUNET_free (uc);
556 }
557
558 /* end of fs_unindex.c */