2d16e9aeea725f388f7e2576db6a484e754077ce
[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 2, 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 Bennett
24  * @author Christian Grothoff
25  * @brief Unindex file.
26  *
27  * TODO:
28  * - code cleanup (share more with upload.c)
29  */
30 #include "platform.h"
31 #include "gnunet_constants.h"
32 #include "gnunet_fs_service.h"
33 #include "gnunet_protocols.h"
34 #include "fs.h"
35 #include "fs_tree.h"
36
37
38 /**
39  * Function called by the tree encoder to obtain
40  * a block of plaintext data (for the lowest level
41  * of the tree).
42  *
43  * @param cls our publishing context
44  * @param offset identifies which block to get
45  * @param max (maximum) number of bytes to get; returning
46  *        fewer will also cause errors
47  * @param buf where to copy the plaintext buffer
48  * @param emsg location to store an error message (on error)
49  * @return number of bytes copied to buf, 0 on error
50  */
51 static size_t
52 unindex_reader (void *cls,
53                 uint64_t offset,
54                 size_t max, 
55                 void *buf,
56                 char **emsg)
57 {
58   struct GNUNET_FS_UnindexContext *uc = cls;
59   size_t pt_size;
60
61   pt_size = GNUNET_MIN(max,
62                        uc->file_size - offset);
63   if (offset != 
64       GNUNET_DISK_file_seek (uc->fh, offset, GNUNET_DISK_SEEK_SET))
65     {
66       *emsg = GNUNET_strdup (_("Failed to find given position in file"));
67       return 0;
68     }
69   if (pt_size !=
70       GNUNET_DISK_file_read (uc->fh,
71                              buf,
72                              pt_size))
73     {
74       *emsg = GNUNET_strdup (_("Failed to read file"));
75       return 0;
76     }
77   return pt_size;
78 }
79
80
81 /**
82  * Continuation called to notify client about result of the
83  * datastore removal operation.
84  *
85  * @param cls closure
86  * @param success GNUNET_SYSERR on failure
87  * @param msg NULL on success, otherwise an error message
88  */
89 static void
90 process_cont (void *cls,
91               int success,
92               const char *msg)
93 {
94   struct GNUNET_FS_UnindexContext *uc = cls;
95   // FIXME: may want to check for errors
96   // OTHER than content-not-present!
97   GNUNET_FS_tree_encoder_next (uc->tc);
98 }
99
100
101 /**
102  * Function called asking for the current (encoded)
103  * block to be processed.  After processing the
104  * client should either call "GNUNET_FS_tree_encode_next"
105  * or (on error) "GNUNET_FS_tree_encode_finish".
106  *
107  * @param cls closure
108  * @param query the query for the block (key for lookup in the datastore)
109  * @param offset offset of the block
110  * @param type type of the block (IBLOCK or DBLOCK)
111  * @param block the (encrypted) block
112  * @param block_size size of block (in bytes)
113  */
114 static void 
115 unindex_process (void *cls,
116                  const GNUNET_HashCode *query,
117                  uint64_t offset,
118                  unsigned int type,
119                  const void *block,
120                  uint16_t block_size)
121 {
122   struct GNUNET_FS_UnindexContext *uc = cls;
123   uint32_t size;
124   const void *data;
125   struct OnDemandBlock odb;
126
127   if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
128     {
129       size = block_size;
130       data = block;
131     }
132   else /* on-demand encoded DBLOCK */
133     {
134       size = sizeof(struct OnDemandBlock);
135       odb.offset = offset;
136       odb.file_id = uc->file_id;
137       data = &odb;
138     }
139   GNUNET_DATASTORE_remove (uc->dsh,
140                            query,
141                            block_size,
142                            block,
143                            &process_cont,
144                            uc,
145                            GNUNET_CONSTANTS_SERVICE_TIMEOUT);
146 }
147
148
149 /**
150  * Fill in all of the generic fields for 
151  * an unindex event.
152  *
153  * @param pc structure to fill in
154  * @param sc overall unindex context
155  */
156 static void
157 make_unindex_status (struct GNUNET_FS_ProgressInfo *pi,
158                      struct GNUNET_FS_UnindexContext *uc,
159                      uint64_t offset)
160 {
161   pi->value.unindex.uc = uc;
162   pi->value.unindex.cctx = uc->client_info;
163   pi->value.unindex.filename = uc->filename;
164   pi->value.unindex.size = uc->file_size;
165   pi->value.unindex.eta 
166     = GNUNET_TIME_calculate_eta (uc->start_time,
167                                  offset,
168                                  uc->file_size);
169   pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
170   pi->value.publish.completed = offset;
171 }
172
173
174 /**
175  * Function called with information about our
176  * progress in computing the tree encoding.
177  *
178  * @param cls closure
179  * @param offset where are we in the file
180  * @param pt_block plaintext of the currently processed block
181  * @param pt_size size of pt_block
182  * @param depth depth of the block in the tree
183  */
184 static void
185 unindex_progress (void *cls,
186                   uint64_t offset,
187                   const void *pt_block,
188                   size_t pt_size,
189                   unsigned int depth)
190 {
191   struct GNUNET_FS_UnindexContext *uc = cls;
192   struct GNUNET_FS_ProgressInfo pi;
193
194   pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
195   make_unindex_status (&pi, uc, offset);
196   pi.value.unindex.specifics.progress.data = pt_block;
197   pi.value.unindex.specifics.progress.offset = offset;
198   pi.value.unindex.specifics.progress.data_len = pt_size;
199   pi.value.unindex.specifics.progress.depth = depth;
200   uc->client_info 
201     = uc->h->upcb (uc->h->upcb_cls,
202                    &pi);
203 }
204                                                
205
206 /**
207  * We've encountered an error during
208  * unindexing.  Signal the client.
209  *
210  * @param uc context for the failed unindexing operation
211  * @param emsg the error message
212  */
213 static void
214 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc,
215                       const char *emsg)
216 {
217   struct GNUNET_FS_ProgressInfo pi;
218   
219   pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
220   make_unindex_status (&pi, uc, 0);
221   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
222   pi.value.unindex.specifics.error.message = emsg;
223   uc->client_info
224     = uc->h->upcb (uc->h->upcb_cls,
225                    &pi);
226 }
227
228
229 /**
230  * Function called when the tree encoder has
231  * processed all blocks.  Clean up.
232  *
233  * @param cls our unindexing context
234  * @param tc not used
235  */
236 static void
237 unindex_finish (void *cls,
238                 const struct GNUNET_SCHEDULER_TaskContext *tc)
239 {
240   struct GNUNET_FS_UnindexContext *uc = cls;
241   char *emsg;
242   struct GNUNET_FS_Uri *uri;
243   struct GNUNET_FS_ProgressInfo pi;
244
245   GNUNET_FS_tree_encoder_finish (uc->tc,
246                                  &uri,
247                                  &emsg);
248   if (uri != NULL)
249     GNUNET_FS_uri_destroy (uri);
250   GNUNET_DISK_file_close (uc->fh);
251   uc->fh = NULL;
252   GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
253   uc->dsh = NULL;
254   if (emsg != NULL)
255     {
256       signal_unindex_error (uc, emsg);
257       GNUNET_free (emsg);
258     }
259   else
260     {   
261       pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
262       make_unindex_status (&pi, uc, uc->file_size);
263       pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
264       uc->client_info
265         = uc->h->upcb (uc->h->upcb_cls,
266                        &pi);
267     }
268 }
269
270
271 /**
272  * Function called with the response from the
273  * FS service to our unindexing request.
274  *
275  * @param cls closure, unindex context
276  * @param msg NULL on timeout, otherwise the response
277  */
278 static void
279 process_fs_response (void *cls,
280                      const struct GNUNET_MessageHeader *msg)
281 {
282   struct GNUNET_FS_UnindexContext *uc = cls;
283
284   GNUNET_CLIENT_disconnect (uc->client);
285   uc->client = NULL;
286   if (uc->state != UNINDEX_STATE_FS_NOTIFY) 
287     {
288       GNUNET_FS_unindex_stop (uc);
289       return;
290     }
291   if (NULL == msg)
292     {
293       uc->state = UNINDEX_STATE_ERROR;
294       signal_unindex_error (uc, 
295                             _("Timeout waiting for `fs' service."));
296       return;
297     }
298   if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
299     {
300       uc->state = UNINDEX_STATE_ERROR;
301       signal_unindex_error (uc, 
302                             _("Invalid response from `fs' service."));
303       return;      
304     }
305   uc->state = UNINDEX_STATE_DS_REMOVE;
306   uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
307                                       uc->h->sched);
308   if (NULL == uc->dsh)
309     {
310       uc->state = UNINDEX_STATE_ERROR;
311       signal_unindex_error (uc, 
312                             _("Failed to connect to `datastore' service."));
313       return;
314     }
315   uc->fh = GNUNET_DISK_file_open (uc->filename,
316                                   GNUNET_DISK_OPEN_READ);
317   if (NULL == uc->fh)
318     {
319       GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
320       uc->dsh = NULL;
321       uc->state = UNINDEX_STATE_ERROR;
322       signal_unindex_error (uc, 
323                             _("Failed to open file for unindexing."));
324       return;
325     }
326   uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
327                                           uc->file_size,
328                                           uc,
329                                           &unindex_reader,
330                                           &unindex_process,
331                                           &unindex_progress,
332                                           &unindex_finish);
333   GNUNET_FS_tree_encoder_next (uc->tc);
334 }
335
336
337 /**
338  * Function called once the hash of the file
339  * that is being unindexed has been computed.
340  *
341  * @param cls closure, unindex context
342  * @param file_id computed hash, NULL on error
343  */
344 static void 
345 process_hash (void *cls,
346               const GNUNET_HashCode *file_id)
347 {
348   struct GNUNET_FS_UnindexContext *uc = cls;
349   struct UnindexMessage req;
350
351   if (uc->state != UNINDEX_STATE_HASHING) 
352     {
353       GNUNET_FS_unindex_stop (uc);
354       return;
355     }
356   if (file_id == NULL)
357     {
358       uc->state = UNINDEX_STATE_ERROR;
359       signal_unindex_error (uc, 
360                             _("Failed to compute hash of file."));
361       return;
362     }
363   uc->file_id = *file_id;
364   uc->state = UNINDEX_STATE_FS_NOTIFY;
365   uc->client = GNUNET_CLIENT_connect (uc->h->sched,
366                                       "fs",
367                                       uc->h->cfg);
368   req.header.size = htons (sizeof (struct UnindexMessage));
369   req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
370   req.reserved = 0;
371   req.file_id = *file_id;
372   GNUNET_CLIENT_transmit_and_get_response (uc->client,
373                                            &req.header,
374                                            GNUNET_CONSTANTS_SERVICE_TIMEOUT,
375                                            &process_fs_response,
376                                            uc);
377 }
378
379
380 /**
381  * Unindex a file.
382  *
383  * @param h handle to the file sharing subsystem
384  * @param filename file to unindex
385  * @return NULL on error, otherwise handle 
386  */
387 struct GNUNET_FS_UnindexContext *
388 GNUNET_FS_unindex (struct GNUNET_FS_Handle *h,
389                    const char *filename)
390 {
391   struct GNUNET_FS_UnindexContext *ret;
392   struct GNUNET_FS_ProgressInfo pi;
393   uint64_t size;
394
395   if (GNUNET_OK !=
396       GNUNET_DISK_file_size (filename,
397                              &size,
398                              GNUNET_YES))
399     return NULL;
400   ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
401   ret->h = h;
402   ret->filename = GNUNET_strdup (filename);
403   ret->start_time = GNUNET_TIME_absolute_get ();
404   ret->file_size = size;
405
406   // FIXME: make persistent!
407   pi.status = GNUNET_FS_STATUS_UNINDEX_START;
408   make_unindex_status (&pi, ret, 0);
409   pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
410   ret->client_info
411     = h->upcb (h->upcb_cls,
412                &pi);
413   GNUNET_CRYPTO_hash_file (h->sched,
414                            GNUNET_SCHEDULER_PRIORITY_IDLE,
415                            GNUNET_NO,
416                            filename,
417                            HASHING_BLOCKSIZE,
418                            &process_hash,
419                            ret);
420   return ret;
421 }
422
423
424 /**
425  * Clean up after completion of an unindex operation.
426  *
427  * @param uc handle
428  */
429 void
430 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
431 {  
432   struct GNUNET_FS_ProgressInfo pi;
433
434   if ( (uc->state != UNINDEX_STATE_COMPLETE) &&
435        (uc->state != UNINDEX_STATE_ERROR) )
436     {
437       uc->state = UNINDEX_STATE_ABORTED;
438       return;
439     }
440   // FIXME: make unpersistent!
441   make_unindex_status (&pi, uc, 
442                        (uc->state == UNINDEX_STATE_COMPLETE)
443                        ? uc->file_size : 0);
444   pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
445   pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
446   uc->client_info
447     = uc->h->upcb (uc->h->upcb_cls,
448                    &pi);
449   GNUNET_break (NULL == uc->client_info);
450   GNUNET_free (uc->filename);
451   GNUNET_free (uc);
452 }
453
454 /* end of fs_unindex.c */