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