address #3745
[oweals/gnunet.git] / src / fs / fs_unindex.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2003--2013 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 #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                            GNUNET_CONSTANTS_SERVICE_TIMEOUT, &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                                      GNUNET_TIME_UNIT_FOREVER_REL,
556                                      &continue_after_remove,
557                                      uc);
558   return;
559  get_next:
560   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
561                                       uc->roff++,
562                                       &uc->uquery,
563                                       GNUNET_BLOCK_TYPE_FS_UBLOCK,
564                                       0 /* priority */,
565                                       1 /* queue size */,
566                                       GNUNET_TIME_UNIT_FOREVER_REL,
567                                       &process_kblock_for_unindex,
568                                       uc);
569 }
570
571
572 /**
573  * If necessary, connect to the datastore and remove the KBlocks.
574  *
575  * @param uc context for the unindex operation.
576  */
577 void
578 GNUNET_FS_unindex_do_remove_kblocks_ (struct GNUNET_FS_UnindexContext *uc)
579 {
580   const char *keyword;
581   const struct GNUNET_CRYPTO_EcdsaPrivateKey *anon;
582   struct GNUNET_CRYPTO_EcdsaPublicKey anon_pub;
583   struct GNUNET_CRYPTO_EcdsaPublicKey dpub;
584
585   if (NULL == uc->dsh)
586     uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
587   if (NULL == uc->dsh)
588   {
589     uc->state = UNINDEX_STATE_ERROR;
590     uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
591     GNUNET_FS_unindex_sync_ (uc);
592     signal_unindex_error (uc);
593     return;
594   }
595   if ( (NULL == uc->ksk_uri) ||
596        (uc->ksk_offset >= uc->ksk_uri->data.ksk.keywordCount) )
597   {
598     unindex_finish (uc);
599     return;
600   }
601   anon = GNUNET_CRYPTO_ecdsa_key_get_anonymous ();
602   GNUNET_CRYPTO_ecdsa_key_get_public (anon,
603                                       &anon_pub);
604   keyword = &uc->ksk_uri->data.ksk.keywords[uc->ksk_offset][1];
605   GNUNET_CRYPTO_ecdsa_public_key_derive (&anon_pub,
606                                          keyword,
607                                          "fs-ublock",
608                                          &dpub);
609   GNUNET_CRYPTO_hash (&dpub,
610                       sizeof (dpub),
611                       &uc->uquery);
612   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
613                                       uc->roff++,
614                                       &uc->uquery,
615                                       GNUNET_BLOCK_TYPE_FS_UBLOCK,
616                                       0 /* priority */,
617                                       1 /* queue size */,
618                                       GNUNET_TIME_UNIT_FOREVER_REL,
619                                       &process_kblock_for_unindex,
620                                       uc);
621 }
622
623
624 /**
625  * Function called when the tree encoder has
626  * processed all blocks.  Clean up.
627  *
628  * @param cls our unindexing context
629  * @param tc not used
630  */
631 static void
632 unindex_extract_keywords (void *cls,
633                           const struct GNUNET_SCHEDULER_TaskContext *tc)
634 {
635   struct GNUNET_FS_UnindexContext *uc = cls;
636
637   uc->state = UNINDEX_STATE_EXTRACT_KEYWORDS;
638   GNUNET_FS_unindex_sync_ (uc);
639   GNUNET_FS_unindex_do_extract_keywords_ (uc);
640 }
641
642
643 /**
644  * Connect to the datastore and remove the blocks.
645  *
646  * @param uc context for the unindex operation.
647  */
648 void
649 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
650 {
651   if (NULL == uc->dsh)
652     uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
653   if (NULL == uc->dsh)
654   {
655     uc->state = UNINDEX_STATE_ERROR;
656     uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
657     GNUNET_FS_unindex_sync_ (uc);
658     signal_unindex_error (uc);
659     return;
660   }
661   uc->fh =
662       GNUNET_DISK_file_open (uc->filename, GNUNET_DISK_OPEN_READ,
663                              GNUNET_DISK_PERM_NONE);
664   if (NULL == uc->fh)
665   {
666     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
667     uc->dsh = NULL;
668     uc->state = UNINDEX_STATE_ERROR;
669     uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
670     GNUNET_FS_unindex_sync_ (uc);
671     signal_unindex_error (uc);
672     return;
673   }
674   uc->tc =
675       GNUNET_FS_tree_encoder_create (uc->h,
676                                      uc->file_size,
677                                      uc,
678                                      &unindex_reader,
679                                      &unindex_process,
680                                      &unindex_progress,
681                                      &unindex_extract_keywords);
682   GNUNET_FS_tree_encoder_next (uc->tc);
683 }
684
685
686 /**
687  * Function called once the hash of the file
688  * that is being unindexed has been computed.
689  *
690  * @param cls closure, unindex context
691  * @param file_id computed hash, NULL on error
692  */
693 void
694 GNUNET_FS_unindex_process_hash_ (void *cls,
695                                  const struct GNUNET_HashCode *file_id)
696 {
697   struct GNUNET_FS_UnindexContext *uc = cls;
698
699   uc->fhc = NULL;
700   if (uc->state != UNINDEX_STATE_HASHING)
701   {
702     GNUNET_FS_unindex_stop (uc);
703     return;
704   }
705   if (file_id == NULL)
706   {
707     uc->state = UNINDEX_STATE_ERROR;
708     uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
709     GNUNET_FS_unindex_sync_ (uc);
710     signal_unindex_error (uc);
711     return;
712   }
713   uc->file_id = *file_id;
714   uc->state = UNINDEX_STATE_DS_REMOVE;
715   GNUNET_FS_unindex_sync_ (uc);
716   GNUNET_FS_unindex_do_remove_ (uc);
717 }
718
719
720 /**
721  * Create SUSPEND event for the given unindex operation
722  * and then clean up our state (without stop signal).
723  *
724  * @param cls the `struct GNUNET_FS_UnindexContext` to signal for
725  */
726 void
727 GNUNET_FS_unindex_signal_suspend_ (void *cls)
728 {
729   struct GNUNET_FS_UnindexContext *uc = cls;
730   struct GNUNET_FS_ProgressInfo pi;
731
732   /* FIXME: lots of duplication with unindex_stop here! */
733   if (uc->dscan != NULL)
734   {
735     GNUNET_FS_directory_scan_abort (uc->dscan);
736     uc->dscan = NULL;
737   }
738   if (NULL != uc->dqe)
739   {
740     GNUNET_DATASTORE_cancel (uc->dqe);
741     uc->dqe = NULL;
742   }
743   if (uc->fhc != NULL)
744   {
745     GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
746     uc->fhc = NULL;
747   }
748   if (NULL != uc->ksk_uri)
749   {
750     GNUNET_FS_uri_destroy (uc->ksk_uri);
751     uc->ksk_uri = NULL;
752   }
753   if (uc->client != NULL)
754   {
755     GNUNET_CLIENT_disconnect (uc->client);
756     uc->client = NULL;
757   }
758   if (NULL != uc->dsh)
759   {
760     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
761     uc->dsh = NULL;
762   }
763   if (NULL != uc->tc)
764   {
765     GNUNET_FS_tree_encoder_finish (uc->tc, NULL);
766     uc->tc = NULL;
767   }
768   if (uc->fh != NULL)
769   {
770     GNUNET_DISK_file_close (uc->fh);
771     uc->fh = NULL;
772   }
773   GNUNET_FS_end_top (uc->h, uc->top);
774   pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
775   GNUNET_FS_unindex_make_status_ (&pi, uc,
776                                   (uc->state ==
777                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
778   GNUNET_break (NULL == uc->client_info);
779   GNUNET_free (uc->filename);
780   GNUNET_free_non_null (uc->serialization);
781   GNUNET_free_non_null (uc->emsg);
782   GNUNET_free (uc);
783 }
784
785
786 /**
787  * Unindex a file.
788  *
789  * @param h handle to the file sharing subsystem
790  * @param filename file to unindex
791  * @param cctx initial value for the client context
792  * @return NULL on error, otherwise handle
793  */
794 struct GNUNET_FS_UnindexContext *
795 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
796                          const char *filename,
797                          void *cctx)
798 {
799   struct GNUNET_FS_UnindexContext *uc;
800   struct GNUNET_FS_ProgressInfo pi;
801   uint64_t size;
802
803   if (GNUNET_OK !=
804       GNUNET_DISK_file_size (filename,
805                              &size,
806                              GNUNET_YES,
807                              GNUNET_YES))
808     return NULL;
809   uc = GNUNET_new (struct GNUNET_FS_UnindexContext);
810   uc->h = h;
811   uc->filename = GNUNET_strdup (filename);
812   uc->start_time = GNUNET_TIME_absolute_get ();
813   uc->file_size = size;
814   uc->client_info = cctx;
815   uc->seen_dh = GNUNET_CONTAINER_multihashmap_create (4,
816                                                        GNUNET_NO);
817   GNUNET_FS_unindex_sync_ (uc);
818   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
819   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
820   GNUNET_FS_unindex_make_status_ (&pi, uc, 0);
821   uc->fhc =
822       GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
823                                filename,
824                                HASHING_BLOCKSIZE,
825                                &GNUNET_FS_unindex_process_hash_, uc);
826   uc->top = GNUNET_FS_make_top (h,
827                                 &GNUNET_FS_unindex_signal_suspend_,
828                                 uc);
829   return uc;
830 }
831
832
833 /**
834  * Clean up after completion of an unindex operation.
835  *
836  * @param uc handle
837  */
838 void
839 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
840 {
841   struct GNUNET_FS_ProgressInfo pi;
842
843   if (uc->dscan != NULL)
844   {
845     GNUNET_FS_directory_scan_abort (uc->dscan);
846     uc->dscan = NULL;
847   }
848   if (NULL != uc->dqe)
849   {
850     GNUNET_DATASTORE_cancel (uc->dqe);
851     uc->dqe = NULL;
852   }
853   if (uc->fhc != NULL)
854   {
855     GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
856     uc->fhc = NULL;
857   }
858   if (uc->client != NULL)
859   {
860     GNUNET_CLIENT_disconnect (uc->client);
861     uc->client = NULL;
862   }
863   if (NULL != uc->dsh)
864   {
865     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
866     uc->dsh = NULL;
867   }
868   if (NULL != uc->ksk_uri)
869   {
870     GNUNET_FS_uri_destroy (uc->ksk_uri);
871     uc->ksk_uri = NULL;
872   }
873   if (NULL != uc->tc)
874   {
875     GNUNET_FS_tree_encoder_finish (uc->tc, NULL);
876     uc->tc = NULL;
877   }
878   if (uc->fh != NULL)
879   {
880     GNUNET_DISK_file_close (uc->fh);
881     uc->fh = NULL;
882   }
883   GNUNET_FS_end_top (uc->h, uc->top);
884   if (uc->serialization != NULL)
885   {
886     GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
887                                  uc->serialization);
888     GNUNET_free (uc->serialization);
889     uc->serialization = NULL;
890   }
891   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
892   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
893   GNUNET_FS_unindex_make_status_ (&pi, uc,
894                                   (uc->state ==
895                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
896   GNUNET_break (NULL == uc->client_info);
897   GNUNET_free_non_null (uc->emsg);
898   GNUNET_free (uc->filename);
899   GNUNET_free (uc);
900 }
901
902 /* end of fs_unindex.c */