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