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