-unindex needs to decrypt KBlock
[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 #include "block_fs.h"
34
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   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162               "Datastore REMOVE operation succeeded\n");
163   GNUNET_FS_tree_encoder_next (uc->tc);
164 }
165
166
167 /**
168  * Function called asking for the current (encoded)
169  * block to be processed.  After processing the
170  * client should either call "GNUNET_FS_tree_encode_next"
171  * or (on error) "GNUNET_FS_tree_encode_finish".
172  *
173  * @param cls closure
174  * @param chk content hash key for the block (key for lookup in the datastore)
175  * @param offset offset of the block
176  * @param depth depth of the block, 0 for DBLOCK
177  * @param type type of the block (IBLOCK or DBLOCK)
178  * @param block the (encrypted) block
179  * @param block_size size of block (in bytes)
180  */
181 static void
182 unindex_process (void *cls, const struct ContentHashKey *chk, uint64_t offset,
183                  unsigned int depth, enum GNUNET_BLOCK_Type type,
184                  const void *block, uint16_t block_size)
185 {
186   struct GNUNET_FS_UnindexContext *uc = cls;
187   uint32_t size;
188   const void *data;
189   struct OnDemandBlock odb;
190
191   if (type != GNUNET_BLOCK_TYPE_FS_DBLOCK)
192   {
193     size = block_size;
194     data = block;
195   }
196   else                          /* on-demand encoded DBLOCK */
197   {
198     size = sizeof (struct OnDemandBlock);
199     odb.offset = GNUNET_htonll (offset);
200     odb.file_id = uc->file_id;
201     data = &odb;
202   }
203   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204               "Sending REMOVE request to DATASTORE service\n");
205   GNUNET_DATASTORE_remove (uc->dsh, &chk->query, size, data, -2, 1,
206                            GNUNET_CONSTANTS_SERVICE_TIMEOUT, &process_cont, uc);
207   uc->chk = *chk;
208 }
209
210
211 /**
212  * Function called with the response from the
213  * FS service to our unindexing request.
214  *
215  * @param cls closure, unindex context
216  * @param msg NULL on timeout, otherwise the response
217  */
218 static void
219 process_fs_response (void *cls, const struct GNUNET_MessageHeader *msg)
220 {
221   struct GNUNET_FS_UnindexContext *uc = cls;
222   struct GNUNET_FS_ProgressInfo pi;
223
224   if (uc->client != NULL)
225   {
226     GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
227     uc->client = NULL;
228   }
229   if (uc->state != UNINDEX_STATE_FS_NOTIFY)
230   {
231     uc->state = UNINDEX_STATE_ERROR;
232     uc->emsg =
233         GNUNET_strdup (_("Unexpected time for a response from `fs' service."));
234     GNUNET_FS_unindex_sync_ (uc);
235     signal_unindex_error (uc);
236     return;
237   }
238   if (NULL == msg)
239   {
240     uc->state = UNINDEX_STATE_ERROR;
241     uc->emsg = GNUNET_strdup (_("Timeout waiting for `fs' service."));
242     GNUNET_FS_unindex_sync_ (uc);
243     signal_unindex_error (uc);
244     return;
245   }
246   if (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
247   {
248     uc->state = UNINDEX_STATE_ERROR;
249     uc->emsg = GNUNET_strdup (_("Invalid response from `fs' service."));
250     GNUNET_FS_unindex_sync_ (uc);
251     signal_unindex_error (uc);
252     return;
253   }
254   uc->state = UNINDEX_STATE_COMPLETE;
255   pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
256   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
257   GNUNET_FS_unindex_sync_ (uc);
258   GNUNET_FS_unindex_make_status_ (&pi, uc, uc->file_size);
259 }
260
261
262 /**
263  * Function called when we are done with removing KBlocks.
264  * Disconnect from datastore and notify FS service about
265  * the unindex event.
266  *
267  * @param uc our unindexing context
268  */
269 static void
270 unindex_finish (struct GNUNET_FS_UnindexContext *uc)
271 {
272   char *emsg;
273   struct GNUNET_FS_Uri *uri;
274   struct UnindexMessage req;
275
276   /* generate final progress message */
277   unindex_progress (uc, uc->file_size, NULL, 0, 0);
278   GNUNET_FS_tree_encoder_finish (uc->tc, &uri, &emsg);
279   uc->tc = NULL;
280   if (uri != NULL)
281     GNUNET_FS_uri_destroy (uri);
282   GNUNET_DISK_file_close (uc->fh);
283   uc->fh = NULL;
284   GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
285   uc->dsh = NULL;
286   uc->state = UNINDEX_STATE_FS_NOTIFY;
287   GNUNET_FS_unindex_sync_ (uc);
288   uc->client = GNUNET_CLIENT_connect ("fs", uc->h->cfg);
289   if (uc->client == NULL)
290   {
291     uc->state = UNINDEX_STATE_ERROR;
292     uc->emsg =
293         GNUNET_strdup (_("Failed to connect to FS service for unindexing."));
294     GNUNET_FS_unindex_sync_ (uc);
295     signal_unindex_error (uc);
296     return;
297   }
298   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299               "Sending UNINDEX message to FS service\n");
300   req.header.size = htons (sizeof (struct UnindexMessage));
301   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
302   req.reserved = 0;
303   req.file_id = uc->file_id;
304   GNUNET_break (GNUNET_OK ==
305                 GNUNET_CLIENT_transmit_and_get_response (uc->client,
306                                                          &req.header,
307                                                          GNUNET_CONSTANTS_SERVICE_TIMEOUT,
308                                                          GNUNET_YES,
309                                                          &process_fs_response,
310                                                          uc));
311 }
312
313
314
315 /**
316  * Function called by the directory scanner as we extract keywords
317  * that we will need to remove KBlocks.
318  *
319  * @param cls the 'struct GNUNET_FS_UnindexContext *'
320  * @param filename which file we are making progress on
321  * @param is_directory GNUNET_YES if this is a directory,
322  *                     GNUNET_NO if this is a file
323  *                     GNUNET_SYSERR if it is neither (or unknown)
324  * @param reason kind of progress we are making
325  */
326 static void
327 unindex_directory_scan_cb (void *cls, 
328                            const char *filename, 
329                            int is_directory,
330                            enum GNUNET_FS_DirScannerProgressUpdateReason reason)
331 {
332   struct GNUNET_FS_UnindexContext *uc = cls;
333   static struct GNUNET_FS_ShareTreeItem * directory_scan_result;
334
335   switch (reason)
336   {
337   case GNUNET_FS_DIRSCANNER_FINISHED:
338     directory_scan_result = GNUNET_FS_directory_scan_get_result (uc->dscan);
339     uc->dscan = NULL;
340     if (NULL != directory_scan_result->ksk_uri)
341     {
342       uc->ksk_uri = GNUNET_FS_uri_dup (directory_scan_result->ksk_uri);
343       uc->state = UNINDEX_STATE_DS_REMOVE_KBLOCKS;
344       uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
345       GNUNET_FS_unindex_sync_ (uc);
346       GNUNET_FS_unindex_do_remove_kblocks_ (uc);
347     }
348     else
349     {
350       unindex_finish (uc);
351     }
352     GNUNET_FS_share_tree_free (directory_scan_result);
353     break;
354   case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
355     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
356                 _("Internal error scanning `%s'.\n"),
357                 uc->filename);
358     break;
359   default:
360     break;
361   }
362
363 }
364
365
366 /**
367  * If necessary, connect to the datastore and remove the KBlocks.
368  *
369  * @param uc context for the unindex operation.
370  */
371 void
372 GNUNET_FS_unindex_do_extract_keywords_ (struct GNUNET_FS_UnindexContext *uc)
373 {
374   char *ex;
375
376   if (GNUNET_OK !=
377       GNUNET_CONFIGURATION_get_value_string (uc->h->cfg, "FS", "EXTRACTORS", &ex))
378     ex = NULL;
379   uc->dscan = GNUNET_FS_directory_scan_start (uc->filename,
380                                               GNUNET_NO, ex,
381                                               &unindex_directory_scan_cb, 
382                                               uc);
383   GNUNET_free_non_null (ex);
384 }
385
386
387 /**
388  * Continuation called to notify client about result of the remove
389  * operation for the KBlock.
390  *
391  * @param cls the 'struct GNUNET_FS_UnindexContext *' 
392  * @param success GNUNET_SYSERR on failure (including timeout/queue drop)
393  *                GNUNET_NO if content was already there
394  *                GNUNET_YES (or other positive value) on success
395  * @param min_expiration minimum expiration time required for 0-priority content to be stored
396  *                by the datacache at this time, zero for unknown, forever if we have no
397  *                space for 0-priority content
398  * @param msg NULL on success, otherwise an error message
399  */
400 static void
401 continue_after_remove (void *cls,
402                        int32_t success,
403                        struct GNUNET_TIME_Absolute min_expiration,
404                        const char *msg)
405 {
406   struct GNUNET_FS_UnindexContext *uc = cls;
407
408   if (success != GNUNET_YES)  
409     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
410                 _("Failed to remove KBlock: %s\n"),
411                 msg);  
412   uc->ksk_offset++;
413   GNUNET_FS_unindex_do_remove_kblocks_ (uc);
414 }
415
416
417 /**
418  * Function called from datastore with result from us looking for
419  * a KBlock.  There are four cases:
420  * 1) no result, means we move on to the next keyword
421  * 2) UID is the same as the first UID, means we move on to next keyword
422  * 3) KBlock for a different CHK, means we keep looking for more
423  * 4) KBlock is for our CHK, means we remove the block and then move
424  *           on to the next keyword
425  *
426  * @param cls the 'struct GNUNET_FS_UnindexContext *'
427  * @param key key for the content
428  * @param size number of bytes in data
429  * @param data content stored
430  * @param type type of the content
431  * @param priority priority of the content
432  * @param anonymity anonymity-level for the content
433  * @param expiration expiration time for the content
434  * @param uid unique identifier for the datum;
435  *        maybe 0 if no unique identifier is available
436  */
437 static void
438 process_kblock_for_unindex (void *cls,
439                             const GNUNET_HashCode * key,
440                             size_t size, const void *data,
441                             enum GNUNET_BLOCK_Type type,
442                             uint32_t priority,
443                             uint32_t anonymity,
444                             struct GNUNET_TIME_Absolute
445                             expiration, uint64_t uid)
446 {
447   struct GNUNET_FS_UnindexContext *uc = cls;
448   const struct KBlock *kb;
449   struct GNUNET_FS_Uri *chk_uri;
450
451   uc->dqe = NULL;
452   if (NULL == data)
453   {
454     /* no result */
455     uc->ksk_offset++;
456     GNUNET_FS_unindex_do_remove_kblocks_ (uc);
457     return;
458   }
459   if (0 == uc->first_uid)
460   {
461     /* remember UID of first result to detect cycles */
462     uc->first_uid = uid;    
463   }
464   else if (uid == uc->first_uid)
465   {
466     /* no more additional results */
467     uc->ksk_offset++;
468     GNUNET_FS_unindex_do_remove_kblocks_ (uc);
469     return;  
470   }
471   GNUNET_assert (GNUNET_BLOCK_TYPE_FS_KBLOCK == type);
472   if (size < sizeof (struct KBlock))
473   {
474     GNUNET_break (0);
475     goto get_next;
476   }
477   kb = data;
478   {
479     char pt[size - sizeof (struct KBlock)];  
480     struct GNUNET_CRYPTO_AesSessionKey skey;
481     struct GNUNET_CRYPTO_AesInitializationVector iv;
482  
483     GNUNET_CRYPTO_hash_to_aes_key (&uc->key, &skey, &iv);
484     if (-1 ==
485         GNUNET_CRYPTO_aes_decrypt (&kb[1], size - sizeof (struct KBlock), &skey,
486                                    &iv, pt))
487     {
488       GNUNET_break (0);
489       goto get_next;
490     }       
491     if (NULL == memchr (pt, 0, sizeof (pt)))
492     {
493       GNUNET_break (0);
494       goto get_next;
495     }
496     chk_uri = GNUNET_FS_uri_parse (pt, NULL);
497     if (NULL == chk_uri)
498     {
499       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500                   _("Failed to parse URI `%s' from KBlock!\n"),
501                   pt);
502       GNUNET_break (0);
503       goto get_next;
504     }
505   }
506   if (0 != memcmp (&uc->chk,
507                    &chk_uri->data.chk.chk,
508                    sizeof (struct ContentHashKey)))
509   {
510     /* different CHK, ignore */
511     GNUNET_FS_uri_destroy (chk_uri);
512     goto get_next;
513   }
514   /* matches! */
515   uc->dqe = GNUNET_DATASTORE_remove (uc->dsh,
516                                      key, size, data,
517                                      0 /* priority */, 1 /* queue size */,
518                                      GNUNET_TIME_UNIT_FOREVER_REL,
519                                      &continue_after_remove,
520                                      uc);
521   return;
522  get_next:
523   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
524                                       uc->roff++,
525                                       &uc->query,
526                                       GNUNET_BLOCK_TYPE_FS_KBLOCK,
527                                       0 /* priority */, 1 /* queue size */,
528                                       GNUNET_TIME_UNIT_FOREVER_REL,
529                                       &process_kblock_for_unindex,
530                                       uc);
531 }
532
533
534 /**
535  * If necessary, connect to the datastore and remove the KBlocks.
536  *
537  * @param uc context for the unindex operation.
538  */
539 void
540 GNUNET_FS_unindex_do_remove_kblocks_ (struct GNUNET_FS_UnindexContext *uc)
541 {
542   const char *keyword;
543   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
544   struct GNUNET_CRYPTO_RsaPrivateKey *pk;
545
546   if (NULL != uc->dsh)
547     uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
548   if (NULL == uc->dsh)
549   {
550     uc->state = UNINDEX_STATE_ERROR;
551     uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
552     GNUNET_FS_unindex_sync_ (uc);
553     signal_unindex_error (uc);
554     return;
555   }
556   if ( (NULL == uc->ksk_uri) ||
557        (uc->ksk_offset >= uc->ksk_uri->data.ksk.keywordCount) )
558   {
559     unindex_finish (uc);
560     return;
561   }
562   /* FIXME: code duplication with fs_search.c here... */
563   keyword = &uc->ksk_uri->data.ksk.keywords[uc->ksk_offset][1];
564   GNUNET_CRYPTO_hash (keyword, strlen (keyword), &uc->key);
565   pk = GNUNET_CRYPTO_rsa_key_create_from_hash (&uc->key);
566   GNUNET_assert (pk != NULL);
567   GNUNET_CRYPTO_rsa_key_get_public (pk, &pub);
568   GNUNET_CRYPTO_rsa_key_free (pk);
569   GNUNET_CRYPTO_hash (&pub,
570                       sizeof (struct
571                               GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
572                       &uc->query);
573   uc->first_uid = 0;
574   uc->dqe = GNUNET_DATASTORE_get_key (uc->dsh,
575                                       uc->roff++,
576                                       &uc->query,
577                                       GNUNET_BLOCK_TYPE_FS_KBLOCK,
578                                       0 /* priority */, 1 /* queue size */,
579                                       GNUNET_TIME_UNIT_FOREVER_REL,
580                                       &process_kblock_for_unindex,
581                                       uc);
582 }
583
584
585 /**
586  * Function called when the tree encoder has
587  * processed all blocks.  Clean up.
588  *
589  * @param cls our unindexing context
590  * @param tc not used
591  */
592 static void
593 unindex_extract_keywords (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
594 {
595   struct GNUNET_FS_UnindexContext *uc = cls;
596
597   uc->state = UNINDEX_STATE_EXTRACT_KEYWORDS;
598   GNUNET_FS_unindex_sync_ (uc);
599   GNUNET_FS_unindex_do_extract_keywords_ (uc);
600 }
601
602
603 /**
604  * Connect to the datastore and remove the blocks.
605  *
606  * @param uc context for the unindex operation.
607  */
608 void
609 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
610 {
611   uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
612   if (NULL == uc->dsh)
613   {
614     uc->state = UNINDEX_STATE_ERROR;
615     uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
616     GNUNET_FS_unindex_sync_ (uc);
617     signal_unindex_error (uc);
618     return;
619   }
620   uc->fh =
621       GNUNET_DISK_file_open (uc->filename, GNUNET_DISK_OPEN_READ,
622                              GNUNET_DISK_PERM_NONE);
623   if (NULL == uc->fh)
624   {
625     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
626     uc->dsh = NULL;
627     uc->state = UNINDEX_STATE_ERROR;
628     uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
629     GNUNET_FS_unindex_sync_ (uc);
630     signal_unindex_error (uc);
631     return;
632   }
633   uc->tc =
634       GNUNET_FS_tree_encoder_create (uc->h, uc->file_size, uc, &unindex_reader,
635                                      &unindex_process, &unindex_progress,
636                                      &unindex_extract_keywords);
637   GNUNET_FS_tree_encoder_next (uc->tc);
638 }
639
640
641 /**
642  * Function called once the hash of the file
643  * that is being unindexed has been computed.
644  *
645  * @param cls closure, unindex context
646  * @param file_id computed hash, NULL on error
647  */
648 void
649 GNUNET_FS_unindex_process_hash_ (void *cls, const GNUNET_HashCode * file_id)
650 {
651   struct GNUNET_FS_UnindexContext *uc = cls;
652
653   uc->fhc = NULL;
654   if (uc->state != UNINDEX_STATE_HASHING)
655   {
656     GNUNET_FS_unindex_stop (uc);
657     return;
658   }
659   if (file_id == NULL)
660   {
661     uc->state = UNINDEX_STATE_ERROR;
662     uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
663     GNUNET_FS_unindex_sync_ (uc);
664     signal_unindex_error (uc);
665     return;
666   }
667   uc->file_id = *file_id;
668   uc->state = UNINDEX_STATE_DS_REMOVE;
669   GNUNET_FS_unindex_sync_ (uc);
670   GNUNET_FS_unindex_do_remove_ (uc);
671 }
672
673
674 /**
675  * Create SUSPEND event for the given unindex operation
676  * and then clean up our state (without stop signal).
677  *
678  * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
679  */
680 void
681 GNUNET_FS_unindex_signal_suspend_ (void *cls)
682 {
683   struct GNUNET_FS_UnindexContext *uc = cls;
684   struct GNUNET_FS_ProgressInfo pi;
685
686   /* FIXME: lots of duplication with unindex_stop here! */
687   if (uc->dscan != NULL)
688   {
689     GNUNET_FS_directory_scan_abort (uc->dscan);
690     uc->dscan = NULL;
691   }
692   if (NULL != uc->dqe)
693   {
694     GNUNET_DATASTORE_cancel (uc->dqe);
695     uc->dqe = NULL;
696   }
697   if (uc->fhc != NULL)
698   {
699     GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
700     uc->fhc = NULL;
701   }
702   if (NULL != uc->ksk_uri)
703   {
704     GNUNET_FS_uri_destroy (uc->ksk_uri);
705     uc->ksk_uri = NULL;
706   }
707   if (uc->client != NULL)
708   {
709     GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
710     uc->client = NULL;
711   }
712   if (NULL != uc->dsh)
713   {
714     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
715     uc->dsh = NULL;
716   }
717   if (NULL != uc->tc)
718   {
719     GNUNET_FS_tree_encoder_finish (uc->tc, NULL, NULL);
720     uc->tc = NULL;
721   }
722   if (uc->fh != NULL)
723   {
724     GNUNET_DISK_file_close (uc->fh);
725     uc->fh = NULL;
726   }
727   GNUNET_FS_end_top (uc->h, uc->top);
728   pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
729   GNUNET_FS_unindex_make_status_ (&pi, uc,
730                                   (uc->state ==
731                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
732   GNUNET_break (NULL == uc->client_info);
733   GNUNET_free (uc->filename);
734   GNUNET_free_non_null (uc->serialization);
735   GNUNET_free_non_null (uc->emsg);
736   GNUNET_free (uc);
737 }
738
739
740 /**
741  * Unindex a file.
742  *
743  * @param h handle to the file sharing subsystem
744  * @param filename file to unindex
745  * @param cctx initial value for the client context
746  * @return NULL on error, otherwise handle
747  */
748 struct GNUNET_FS_UnindexContext *
749 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h, const char *filename,
750                          void *cctx)
751 {
752   struct GNUNET_FS_UnindexContext *ret;
753   struct GNUNET_FS_ProgressInfo pi;
754   uint64_t size;
755
756   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &size, GNUNET_YES))
757     return NULL;
758   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
759   ret->h = h;
760   ret->filename = GNUNET_strdup (filename);
761   ret->start_time = GNUNET_TIME_absolute_get ();
762   ret->file_size = size;
763   ret->client_info = cctx;
764   GNUNET_FS_unindex_sync_ (ret);
765   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
766   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
767   GNUNET_FS_unindex_make_status_ (&pi, ret, 0);
768   ret->fhc =
769       GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE, filename,
770                                HASHING_BLOCKSIZE,
771                                &GNUNET_FS_unindex_process_hash_, ret);
772   ret->top = GNUNET_FS_make_top (h, &GNUNET_FS_unindex_signal_suspend_, ret);
773   return ret;
774 }
775
776
777 /**
778  * Clean up after completion of an unindex operation.
779  *
780  * @param uc handle
781  */
782 void
783 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
784 {
785   struct GNUNET_FS_ProgressInfo pi;
786
787   if (uc->dscan != NULL)
788   {
789     GNUNET_FS_directory_scan_abort (uc->dscan);
790     uc->dscan = NULL;
791   }
792   if (NULL != uc->dqe)
793   {
794     GNUNET_DATASTORE_cancel (uc->dqe);
795     uc->dqe = NULL;
796   }
797   if (uc->fhc != NULL)
798   {
799     GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
800     uc->fhc = NULL;
801   }
802   if (uc->client != NULL)
803   {
804     GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
805     uc->client = NULL;
806   }
807   if (NULL != uc->dsh)
808   {
809     GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
810     uc->dsh = NULL;
811   }
812   if (NULL != uc->ksk_uri)
813   {
814     GNUNET_FS_uri_destroy (uc->ksk_uri);
815     uc->ksk_uri = NULL;
816   }
817   if (NULL != uc->tc)
818   {
819     GNUNET_FS_tree_encoder_finish (uc->tc, NULL, NULL);
820     uc->tc = NULL;
821   }
822   if (uc->fh != NULL)
823   {
824     GNUNET_DISK_file_close (uc->fh);
825     uc->fh = NULL;
826   }
827   GNUNET_FS_end_top (uc->h, uc->top);
828   if (uc->serialization != NULL)
829   {
830     GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX,
831                                  uc->serialization);
832     GNUNET_free (uc->serialization);
833     uc->serialization = NULL;
834   }
835   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
836   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
837   GNUNET_FS_unindex_make_status_ (&pi, uc,
838                                   (uc->state ==
839                                    UNINDEX_STATE_COMPLETE) ? uc->file_size : 0);
840   GNUNET_break (NULL == uc->client_info);
841   GNUNET_free (uc->filename);
842   GNUNET_free (uc);
843 }
844
845 /* end of fs_unindex.c */