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