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