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