-ensure stats queues do not grow too big
[oweals/gnunet.git] / src / fs / fs_unindex.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2003--2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 #include "block_fs.h"
34 #include "fs_publish_ublock.h"
35
36
37 /**
38  * Function called by the tree encoder to obtain
39  * a block of plaintext data (for the lowest level
40  * of the tree).
41  *
42  * @param cls our publishing context
43  * @param offset identifies which block to get
44  * @param max (maximum) number of bytes to get; returning
45  *        fewer will also cause errors
46  * @param buf where to copy the plaintext buffer
47  * @param emsg location to store an error message (on error)
48  * @return number of bytes copied to buf, 0 on error
49  */
50 static size_t
51 unindex_reader (void *cls,
52                 uint64_t offset,
53                 size_t max,
54                 void *buf,
55                 char **emsg)
56 {
57   struct GNUNET_FS_UnindexContext *uc = cls;
58   size_t pt_size;
59
60   pt_size = GNUNET_MIN (max, uc->file_size - offset);
61   if (offset != 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 != GNUNET_DISK_file_read (uc->fh, buf, pt_size))
67   {
68     *emsg = GNUNET_strdup (_("Failed to read file"));
69     return 0;
70   }
71   return pt_size;
72 }
73
74
75 /**
76  * Fill in all of the generic fields for
77  * an unindex event and call the callback.
78  *
79  * @param pi structure to fill in
80  * @param uc overall unindex context
81  * @param offset where we are in the file (for progress)
82  */
83 void
84 GNUNET_FS_unindex_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
85                                 struct GNUNET_FS_UnindexContext *uc,
86                                 uint64_t offset)
87 {
88   pi->value.unindex.uc = uc;
89   pi->value.unindex.cctx = uc->client_info;
90   pi->value.unindex.filename = uc->filename;
91   pi->value.unindex.size = uc->file_size;
92   pi->value.unindex.eta =
93       GNUNET_TIME_calculate_eta (uc->start_time, offset, uc->file_size);
94   pi->value.unindex.duration =
95       GNUNET_TIME_absolute_get_duration (uc->start_time);
96   pi->value.unindex.completed = offset;
97   pi->fsh = uc->h;
98   uc->client_info = uc->h->upcb (uc->h->upcb_cls, pi);
99 }
100
101
102 /**
103  * Function called with information about our
104  * progress in computing the tree encoding.
105  *
106  * @param cls closure
107  * @param offset where are we in the file
108  * @param pt_block plaintext of the currently processed block
109  * @param pt_size size of pt_block
110  * @param depth depth of the block in the tree, 0 for DBLOCK
111  */
112 static void
113 unindex_progress (void *cls, uint64_t offset, const void *pt_block,
114                   size_t pt_size, unsigned int depth)
115 {
116   struct GNUNET_FS_UnindexContext *uc = cls;
117   struct GNUNET_FS_ProgressInfo pi;
118
119   pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
120   pi.value.unindex.specifics.progress.data = pt_block;
121   pi.value.unindex.specifics.progress.offset = offset;
122   pi.value.unindex.specifics.progress.data_len = pt_size;
123   pi.value.unindex.specifics.progress.depth = depth;
124   GNUNET_FS_unindex_make_status_ (&pi, uc, offset);
125 }
126
127
128 /**
129  * We've encountered an error during
130  * unindexing.  Signal the client.
131  *
132  * @param uc context for the failed unindexing operation
133  */
134 static void
135 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc)
136 {
137   struct GNUNET_FS_ProgressInfo pi;
138
139   pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
140   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
141   pi.value.unindex.specifics.error.message = uc->emsg;
142   GNUNET_FS_unindex_make_status_ (&pi, uc, 0);
143 }
144
145
146 /**
147  * Continuation called to notify client about result of the
148  * datastore removal operation.
149  *
150  * @param cls closure
151  * @param success #GNUNET_SYSERR on failure
152  * @param min_expiration minimum expiration time required for content to be stored
153  * @param msg NULL on success, otherwise an error message
154  */
155 static void
156 process_cont (void *cls,
157               int success,
158               struct GNUNET_TIME_Absolute min_expiration,
159               const char *msg)
160 {
161   struct GNUNET_FS_UnindexContext *uc = cls;
162
163   if (success == GNUNET_SYSERR)
164   {
165     uc->emsg = GNUNET_strdup (msg);
166     signal_unindex_error (uc);
167     return;
168   }
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170               "Datastore REMOVE operation succeeded\n");
171   GNUNET_FS_tree_encoder_next (uc->tc);
172 }
173
174
175 /**
176  * Function called asking for the current (encoded)
177  * block to be processed.  After processing the
178  * client should either call "GNUNET_FS_tree_encode_next"
179  * or (on error) "GNUNET_FS_tree_encode_finish".
180  *
181  * @param cls closure
182  * @param chk content hash key for the block (key for lookup in the datastore)
183  * @param offset offset of the block
184  * @param depth depth of the block, 0 for DBLOCK
185  * @param type type of the block (IBLOCK or DBLOCK)
186  * @param block the (encrypted) block
187  * @param block_size size of block (in bytes)
188  */
189 static void
190 unindex_process (void *cls,
191                  const struct ContentHashKey *chk,
192                  uint64_t offset,
193                  unsigned int depth,
194                  enum GNUNET_BLOCK_Type type,
195                  const void *block,
196                  uint16_t block_size)
197 {
198   struct GNUNET_FS_UnindexContext *uc = cls;
199   uint32_t size;
200   const void *data;
201   struct OnDemandBlock odb;
202
203   if (type != GNUNET_BLOCK_TYPE_FS_DBLOCK)
204   {
205     size = block_size;
206     data = block;
207   }
208   else                          /* on-demand encoded DBLOCK */
209   {
210     size = sizeof (struct OnDemandBlock);
211     odb.offset = GNUNET_htonll (offset);
212     odb.file_id = uc->file_id;
213     data = &odb;
214   }
215   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216               "Sending REMOVE request to DATASTORE service\n");
217   GNUNET_DATASTORE_remove (uc->dsh, &chk->query, size, data, -2, 1,
218                            &process_cont, uc);
219   uc->chk = *chk;
220 }
221
222
223 /**
224  * Function called with the response from the
225  * FS service to our unindexing request.
226  *
227  * @param cls closure, unindex context
228  * @param msg NULL on timeout, otherwise the response
229  */
230 static void
231 process_fs_response (void *cls, const struct GNUNET_MessageHeader *msg)
232 {
233   struct GNUNET_FS_UnindexContext *uc = cls;
234   struct GNUNET_FS_ProgressInfo pi;
235
236   if (uc->client != NULL)
237   {
238     GNUNET_CLIENT_disconnect (uc->client);
239     uc->client = NULL;
240   }
241   if (uc->state != UNINDEX_STATE_FS_NOTIFY)
242   {
243     uc->state = UNINDEX_STATE_ERROR;
244     uc->emsg =
245         GNUNET_strdup (_("Unexpected time for a response from `fs' service."));
246     GNUNET_FS_unindex_sync_ (uc);
247     signal_unindex_error (uc);
248     return;
249   }
250   if (NULL == msg)
251   {
252     uc->state = UNINDEX_STATE_ERROR;
253     uc->emsg = GNUNET_strdup (_("Timeout waiting for `fs' service."));
254     GNUNET_FS_unindex_sync_ (uc);
255     signal_unindex_error (uc);
256     return;
257   }
258   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
259   {
260     uc->state = UNINDEX_STATE_ERROR;
261     uc->emsg = GNUNET_strdup (_("Invalid response from `fs' service."));
262     GNUNET_FS_unindex_sync_ (uc);
263     signal_unindex_error (uc);
264     return;
265   }
266   uc->state = UNINDEX_STATE_COMPLETE;
267   pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
268   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
269   GNUNET_FS_unindex_sync_ (uc);
270   GNUNET_FS_unindex_make_status_ (&pi, uc, uc->file_size);
271 }
272
273
274 /**
275  * Function called when we are done with removing UBlocks.
276  * Disconnect from datastore and notify FS service about
277  * the unindex event.
278  *
279  * @param uc our unindexing context
280  */
281 static void
282 unindex_finish (struct GNUNET_FS_UnindexContext *uc)
283 {
284   char *emsg;
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, &emsg);
290   uc->tc = NULL;
291   GNUNET_DISK_file_close (uc->fh);
292   uc->fh = NULL;
293   GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
294   uc->dsh = NULL;
295   GNUNET_CONTAINER_multihashmap_destroy (uc->seen_dh);
296   uc->seen_dh = 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   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310               "Sending UNINDEX message to FS service\n");
311   req.header.size = htons (sizeof (struct UnindexMessage));
312   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
313   req.reserved = 0;
314   req.file_id = uc->file_id;
315   GNUNET_break (GNUNET_OK ==
316                 GNUNET_CLIENT_transmit_and_get_response (uc->client,
317                                                          &req.header,
318                                                          GNUNET_CONSTANTS_SERVICE_TIMEOUT,
319                                                          GNUNET_YES,
320                                                          &process_fs_response,
321                                                          uc));
322 }
323
324
325
326 /**
327  * Function called by the directory scanner as we extract keywords
328  * that we will need to remove UBlocks.
329  *
330  * @param cls the 'struct GNUNET_FS_UnindexContext *'
331  * @param filename which file we are making progress on
332  * @param is_directory #GNUNET_YES if this is a directory,
333  *                     #GNUNET_NO if this is a file
334  *                     #GNUNET_SYSERR if it is neither (or unknown)
335  * @param reason kind of progress we are making
336  */
337 static void
338 unindex_directory_scan_cb (void *cls,
339                            const char *filename,
340                            int is_directory,
341                            enum GNUNET_FS_DirScannerProgressUpdateReason reason)
342 {
343   struct GNUNET_FS_UnindexContext *uc = cls;
344   static struct GNUNET_FS_ShareTreeItem * directory_scan_result;
345
346   switch (reason)
347   {
348   case GNUNET_FS_DIRSCANNER_FINISHED:
349     directory_scan_result = GNUNET_FS_directory_scan_get_result (uc->dscan);
350     uc->dscan = NULL;
351     if (NULL != directory_scan_result->ksk_uri)
352     {
353       uc->ksk_uri = GNUNET_FS_uri_dup (directory_scan_result->ksk_uri);
354       uc->state = UNINDEX_STATE_DS_REMOVE_KBLOCKS;
355       GNUNET_FS_unindex_sync_ (uc);
356       GNUNET_FS_unindex_do_remove_kblocks_ (uc);
357     }
358     else
359     {
360       uc->emsg = GNUNET_strdup (_("Failed to get KSKs from directory scan."));
361       GNUNET_FS_unindex_sync_ (uc);
362       unindex_finish (uc);
363     }
364     GNUNET_FS_share_tree_free (directory_scan_result);
365     break;
366   case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
367     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
368                 _("Internal error scanning `%s'.\n"),
369                 uc->filename);
370     GNUNET_FS_directory_scan_abort (uc->dscan);
371     uc->dscan = NULL;
372     uc->emsg = GNUNET_strdup (_("Failed to get KSKs from directory scan."));
373     GNUNET_FS_unindex_sync_ (uc);
374     unindex_finish (uc);
375     break;
376   default:
377     break;
378   }
379 }
380
381
382 /**
383  * If necessary, connect to the datastore and remove the UBlocks.
384  *
385  * @param uc context for the unindex operation.
386  */
387 void
388 GNUNET_FS_unindex_do_extract_keywords_ (struct GNUNET_FS_UnindexContext *uc)
389 {
390   char *ex;
391
392   if (GNUNET_OK !=
393       GNUNET_CONFIGURATION_get_value_string (uc->h->cfg, "FS", "EXTRACTORS", &ex))
394     ex = NULL;
395   uc->dscan = GNUNET_FS_directory_scan_start (uc->filename,
396                                               GNUNET_NO, ex,
397                                               &unindex_directory_scan_cb,
398                                               uc);
399   GNUNET_free_non_null (ex);
400 }
401
402
403 /**
404  * Continuation called to notify client about result of the remove
405  * operation for the UBlock.
406  *
407  * @param cls the 'struct GNUNET_FS_UnindexContext *'
408  * @param success GNUNET_SYSERR on failure (including timeout/queue drop)
409  *                GNUNET_NO if content was already there
410  *                GNUNET_YES (or other positive value) on success
411  * @param min_expiration minimum expiration time required for 0-priority content to be stored
412  *                by the datacache at this time, zero for unknown, forever if we have no
413  *                space for 0-priority content
414  * @param msg NULL on success, otherwise an error message
415  */
416 static void
417 continue_after_remove (void *cls,
418                        int32_t success,
419                        struct GNUNET_TIME_Absolute min_expiration,
420                        const char *msg)
421 {
422   struct GNUNET_FS_UnindexContext *uc = cls;
423
424   uc->dqe = NULL;
425   if (success != GNUNET_YES)
426     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
427                 _("Failed to remove UBlock: %s\n"),
428                 msg);
429   GNUNET_CONTAINER_multihashmap_clear (uc->seen_dh);
430   uc->ksk_offset++;
431   GNUNET_FS_unindex_do_remove_kblocks_ (uc);
432 }
433
434
435 /**
436  * Function called from datastore with result from us looking for
437  * a UBlock.  There are four cases:
438  * 1) no result, means we move on to the next keyword
439  * 2) UID is the same as the first UID, means we move on to next keyword
440  * 3) UBlock for a different CHK, means we keep looking for more
441  * 4) UBlock is for our CHK, means we remove the block and then move
442  *           on to the next keyword
443  *
444  * @param cls the 'struct GNUNET_FS_UnindexContext *'
445  * @param key key for the content
446  * @param size number of bytes in data
447  * @param data content stored
448  * @param type type of the content
449  * @param priority priority of the content
450  * @param anonymity anonymity-level for the content
451  * @param expiration expiration time for the content
452  * @param uid unique identifier for the datum;
453  *        maybe 0 if no unique identifier is available
454  */
455 static void
456 process_kblock_for_unindex (void *cls,
457                             const struct GNUNET_HashCode *key,
458                             size_t size,
459                             const void *data,
460                             enum GNUNET_BLOCK_Type type,
461                             uint32_t priority,
462                             uint32_t anonymity,
463                             struct GNUNET_TIME_Absolute expiration,
464                             uint64_t uid)
465 {
466   struct GNUNET_FS_UnindexContext *uc = cls;
467   const struct UBlock *ub;
468   struct GNUNET_FS_Uri *chk_uri;
469   struct GNUNET_HashCode query;
470   struct GNUNET_HashCode dh;
471
472   uc->dqe = NULL;
473   if (NULL == data)
474   {
475     /* no result */
476     GNUNET_CONTAINER_multihashmap_clear (uc->seen_dh);
477     uc->ksk_offset++;
478     GNUNET_FS_unindex_do_remove_kblocks_ (uc);
479     return;
480   }
481   GNUNET_CRYPTO_hash (data,
482                       size,
483                       &dh);
484   if (GNUNET_YES ==
485       GNUNET_CONTAINER_multihashmap_contains (uc->seen_dh,
486                                               &dh))
487   {
488     GNUNET_CONTAINER_multihashmap_clear (uc->seen_dh);
489     uc->ksk_offset++;
490     GNUNET_FS_unindex_do_remove_kblocks_ (uc);
491     return;
492   }
493   GNUNET_assert (GNUNET_OK ==
494                  GNUNET_CONTAINER_multihashmap_put (uc->seen_dh,
495                                                     &dh,
496                                                     uc,
497                                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
498   GNUNET_assert (GNUNET_BLOCK_TYPE_FS_UBLOCK == type);
499   if (size < sizeof (struct UBlock))
500   {
501     GNUNET_break (0);
502     goto get_next;
503   }
504   ub = data;
505   GNUNET_CRYPTO_hash (&ub->verification_key,
506                       sizeof (ub->verification_key),
507                       &query);
508   if (0 != memcmp (&query,
509                    key,
510                    sizeof (struct GNUNET_HashCode)))
511   {
512     /* result does not match our keyword, skip */
513     goto get_next;
514   }
515   {
516     char pt[size - sizeof (struct UBlock)];
517     struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
518     const char *keyword;
519
520     GNUNET_CRYPTO_ecdsa_key_get_public (GNUNET_CRYPTO_ecdsa_key_get_anonymous (),
521                                         &anon_pub);
522     keyword = &uc->ksk_uri->data.ksk.keywords[uc->ksk_offset][1];
523     GNUNET_FS_ublock_decrypt_ (&ub[1], size - sizeof (struct UBlock),
524                                &anon_pub,
525                                keyword,
526                                pt);
527     if (NULL == memchr (&pt[1], 0, sizeof (pt) - 1))
528     {
529       GNUNET_break_op (0); /* malformed UBlock */
530       goto get_next;
531     }
532     chk_uri = GNUNET_FS_uri_parse (&pt[1], NULL);
533     if (NULL == chk_uri)
534     {
535       GNUNET_break_op (0); /* malformed UBlock */
536       goto get_next;
537     }
538   }
539   if (0 != memcmp (&uc->chk,
540                    &chk_uri->data.chk.chk,
541                    sizeof (struct ContentHashKey)))
542   {
543     /* different CHK, ignore */
544     GNUNET_FS_uri_destroy (chk_uri);
545     goto get_next;
546   }
547   GNUNET_FS_uri_destroy (chk_uri);
548   /* matches! */
549   uc->dqe = GNUNET_DATASTORE_remove (uc->dsh,
550                                      key,
551                                      size,
552                                      data,
553                                      0 /* priority */,
554                                      1 /* queue size */,
555                                      &continue_after_remove,
556                                      uc);
557   return;
558  get_next:
559   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
560                                       uc->roff++,
561                                       &uc->uquery,
562                                       GNUNET_BLOCK_TYPE_FS_UBLOCK,
563                                       0 /* priority */,
564                                       1 /* queue size */,
565                                       &process_kblock_for_unindex,
566                                       uc);
567 }
568
569
570 /**
571  * If necessary, connect to the datastore and remove the KBlocks.
572  *
573  * @param uc context for the unindex operation.
574  */
575 void
576 GNUNET_FS_unindex_do_remove_kblocks_ (struct GNUNET_FS_UnindexContext *uc)
577 {
578   const char *keyword;
579   const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
580   struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
581   struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
582
583   if (NULL == uc->dsh)
584     uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
585   if (NULL == uc->dsh)
586   {
587     uc->state = UNINDEX_STATE_ERROR;
588     uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
589     GNUNET_FS_unindex_sync_ (uc);
590     signal_unindex_error (uc);
591     return;
592   }
593   if ( (NULL == uc->ksk_uri) ||
594        (uc->ksk_offset >= uc->ksk_uri->data.ksk.keywordCount) )
595   {
596     unindex_finish (uc);
597     return;
598   }
599   anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
600   GNUNET_CRYPTO_ecdsa_key_get_public (anon,
601                                       &anon_pub);
602   keyword = &uc->ksk_uri->data.ksk.keywords[uc->ksk_offset][1];
603   GNUNET_CRYPTO_ecdsa_public_key_derive (&anon_pub,
604                                          keyword,
605                                          "fs-ublock",
606                                          &dpub);
607   GNUNET_CRYPTO_hash (&dpub,
608                       sizeof (dpub),
609                       &uc->uquery);
610   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
611                                       uc->roff++,
612                                       &uc->uquery,
613                                       GNUNET_BLOCK_TYPE_FS_UBLOCK,
614                                       0 /* priority */,
615                                       1 /* queue size */,
616                                       &process_kblock_for_unindex,
617                                       uc);
618 }
619
620
621 /**
622  * Function called when the tree encoder has
623  * processed all blocks.  Clean up.
624  *
625  * @param cls our unindexing context
626  */
627 static void
628 unindex_extract_keywords (void *cls)
629 {
630   struct GNUNET_FS_UnindexContext *uc = cls;
631
632   uc->state = UNINDEX_STATE_EXTRACT_KEYWORDS;
633   GNUNET_FS_unindex_sync_ (uc);
634   GNUNET_FS_unindex_do_extract_keywords_ (uc);
635 }
636
637
638 /**
639  * Connect to the datastore and remove the blocks.
640  *
641  * @param uc context for the unindex operation.
642  */
643 void
644 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
645 {
646   if (NULL == uc->dsh)
647     uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
648   if (NULL == uc->dsh)
649   {
650     uc->state = UNINDEX_STATE_ERROR;
651     uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
652     GNUNET_FS_unindex_sync_ (uc);
653     signal_unindex_error (uc);
654     return;
655   }
656   uc->fh =
657       GNUNET_DISK_file_open (uc->filename, GNUNET_DISK_OPEN_READ,
658                              GNUNET_DISK_PERM_NONE);
659   if (NULL == uc->fh)
660   {
661     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
662     uc->dsh = NULL;
663     uc->state = UNINDEX_STATE_ERROR;
664     uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
665     GNUNET_FS_unindex_sync_ (uc);
666     signal_unindex_error (uc);
667     return;
668   }
669   uc->tc =
670       GNUNET_FS_tree_encoder_create (uc->h,
671                                      uc->file_size,
672                                      uc,
673                                      &unindex_reader,
674                                      &unindex_process,
675                                      &unindex_progress,
676                                      &unindex_extract_keywords);
677   GNUNET_FS_tree_encoder_next (uc->tc);
678 }
679
680
681 /**
682  * Function called once the hash of the file
683  * that is being unindexed has been computed.
684  *
685  * @param cls closure, unindex context
686  * @param file_id computed hash, NULL on error
687  */
688 void
689 GNUNET_FS_unindex_process_hash_ (void *cls,
690                                  const struct GNUNET_HashCode *file_id)
691 {
692   struct GNUNET_FS_UnindexContext *uc = cls;
693
694   uc->fhc = NULL;
695   if (uc->state != UNINDEX_STATE_HASHING)
696   {
697     GNUNET_FS_unindex_stop (uc);
698     return;
699   }
700   if (file_id == NULL)
701   {
702     uc->state = UNINDEX_STATE_ERROR;
703     uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
704     GNUNET_FS_unindex_sync_ (uc);
705     signal_unindex_error (uc);
706     return;
707   }
708   uc->file_id = *file_id;
709   uc->state = UNINDEX_STATE_DS_REMOVE;
710   GNUNET_FS_unindex_sync_ (uc);
711   GNUNET_FS_unindex_do_remove_ (uc);
712 }
713
714
715 /**
716  * Create SUSPEND event for the given unindex operation
717  * and then clean up our state (without stop signal).
718  *
719  * @param cls the `struct GNUNET_FS_UnindexContext` to signal for
720  */
721 void
722 GNUNET_FS_unindex_signal_suspend_ (void *cls)
723 {
724   struct GNUNET_FS_UnindexContext *uc = cls;
725   struct GNUNET_FS_ProgressInfo pi;
726
727   /* FIXME: lots of duplication with unindex_stop here! */
728   if (uc->dscan != NULL)
729   {
730     GNUNET_FS_directory_scan_abort (uc->dscan);
731     uc->dscan = NULL;
732   }
733   if (NULL != uc->dqe)
734   {
735     GNUNET_DATASTORE_cancel (uc->dqe);
736     uc->dqe = NULL;
737   }
738   if (uc->fhc != NULL)
739   {
740     GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
741     uc->fhc = NULL;
742   }
743   if (NULL != uc->ksk_uri)
744   {
745     GNUNET_FS_uri_destroy (uc->ksk_uri);
746     uc->ksk_uri = NULL;
747   }
748   if (uc->client != NULL)
749   {
750     GNUNET_CLIENT_disconnect (uc->client);
751     uc->client = NULL;
752   }
753   if (NULL != uc->dsh)
754   {
755     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
756     uc->dsh = NULL;
757   }
758   if (NULL != uc->tc)
759   {
760     GNUNET_FS_tree_encoder_finish (uc->tc, NULL);
761     uc->tc = NULL;
762   }
763   if (uc->fh != NULL)
764   {
765     GNUNET_DISK_file_close (uc->fh);
766     uc->fh = NULL;
767   }
768   GNUNET_FS_end_top (uc->h, uc->top);
769   pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
770   GNUNET_FS_unindex_make_status_ (&pi, uc,
771                                   (uc->state ==
772                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
773   GNUNET_break (NULL == uc->client_info);
774   GNUNET_free (uc->filename);
775   GNUNET_free_non_null (uc->serialization);
776   GNUNET_free_non_null (uc->emsg);
777   GNUNET_free (uc);
778 }
779
780
781 /**
782  * Unindex a file.
783  *
784  * @param h handle to the file sharing subsystem
785  * @param filename file to unindex
786  * @param cctx initial value for the client context
787  * @return NULL on error, otherwise handle
788  */
789 struct GNUNET_FS_UnindexContext *
790 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
791                          const char *filename,
792                          void *cctx)
793 {
794   struct GNUNET_FS_UnindexContext *uc;
795   struct GNUNET_FS_ProgressInfo pi;
796   uint64_t size;
797
798   if (GNUNET_OK !=
799       GNUNET_DISK_file_size (filename,
800                              &size,
801                              GNUNET_YES,
802                              GNUNET_YES))
803     return NULL;
804   uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
805   uc->h = h;
806   uc->filename = GNUNET_strdup (filename);
807   uc->start_time = GNUNET_TIME_absolute_get ();
808   uc->file_size = size;
809   uc->client_info = cctx;
810   uc->seen_dh = GNUNET_CONTAINER_multihashmap_create (4,
811                                                        GNUNET_NO);
812   GNUNET_FS_unindex_sync_ (uc);
813   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
814   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
815   GNUNET_FS_unindex_make_status_ (&pi, uc, 0);
816   uc->fhc =
817       GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
818                                filename,
819                                HASHING_BLOCKSIZE,
820                                &GNUNET_FS_unindex_process_hash_, uc);
821   uc->top = GNUNET_FS_make_top (h,
822                                 &GNUNET_FS_unindex_signal_suspend_,
823                                 uc);
824   return uc;
825 }
826
827
828 /**
829  * Clean up after completion of an unindex operation.
830  *
831  * @param uc handle
832  */
833 void
834 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
835 {
836   struct GNUNET_FS_ProgressInfo pi;
837
838   if (uc->dscan != NULL)
839   {
840     GNUNET_FS_directory_scan_abort (uc->dscan);
841     uc->dscan = NULL;
842   }
843   if (NULL != uc->dqe)
844   {
845     GNUNET_DATASTORE_cancel (uc->dqe);
846     uc->dqe = NULL;
847   }
848   if (uc->fhc != NULL)
849   {
850     GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
851     uc->fhc = NULL;
852   }
853   if (uc->client != NULL)
854   {
855     GNUNET_CLIENT_disconnect (uc->client);
856     uc->client = NULL;
857   }
858   if (NULL != uc->dsh)
859   {
860     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
861     uc->dsh = NULL;
862   }
863   if (NULL != uc->ksk_uri)
864   {
865     GNUNET_FS_uri_destroy (uc->ksk_uri);
866     uc->ksk_uri = NULL;
867   }
868   if (NULL != uc->tc)
869   {
870     GNUNET_FS_tree_encoder_finish (uc->tc, NULL);
871     uc->tc = NULL;
872   }
873   if (uc->fh != NULL)
874   {
875     GNUNET_DISK_file_close (uc->fh);
876     uc->fh = NULL;
877   }
878   GNUNET_FS_end_top (uc->h, uc->top);
879   if (uc->serialization != NULL)
880   {
881     GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
882                                  uc->serialization);
883     GNUNET_free (uc->serialization);
884     uc->serialization = NULL;
885   }
886   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
887   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
888   GNUNET_FS_unindex_make_status_ (&pi, uc,
889                                   (uc->state ==
890                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
891   GNUNET_break (NULL == uc->client_info);
892   GNUNET_free_non_null (uc->emsg);
893   GNUNET_free (uc->filename);
894   GNUNET_free (uc);
895 }
896
897 /* end of fs_unindex.c */