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