2 This file is part of GNUnet.
3 (C) 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
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 3, or (at your
8 option) any later version.
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.
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.
22 * @file fs/fs_unindex.c
23 * @author Krista Grothoff
24 * @author Christian Grothoff
25 * @brief Unindex file.
28 #include "gnunet_constants.h"
29 #include "gnunet_fs_service.h"
30 #include "gnunet_protocols.h"
36 * Function called by the tree encoder to obtain
37 * a block of plaintext data (for the lowest level
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
49 unindex_reader (void *cls,
55 struct GNUNET_FS_UnindexContext *uc = cls;
58 pt_size = GNUNET_MIN(max,
59 uc->file_size - offset);
61 GNUNET_DISK_file_seek (uc->fh, offset, GNUNET_DISK_SEEK_SET))
63 *emsg = GNUNET_strdup (_("Failed to find given position in file"));
67 GNUNET_DISK_file_read (uc->fh,
71 *emsg = GNUNET_strdup (_("Failed to read file"));
79 * Fill in all of the generic fields for
80 * an unindex event and call the callback.
82 * @param pi structure to fill in
83 * @param uc overall unindex context
84 * @param offset where we are in the file (for progress)
87 GNUNET_FS_unindex_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
88 struct GNUNET_FS_UnindexContext *uc,
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;
96 = GNUNET_TIME_calculate_eta (uc->start_time,
99 pi->value.unindex.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
100 pi->value.unindex.completed = offset;
102 = uc->h->upcb (uc->h->upcb_cls,
109 * Function called with information about our
110 * progress in computing the tree encoding.
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
119 unindex_progress (void *cls,
121 const void *pt_block,
125 struct GNUNET_FS_UnindexContext *uc = cls;
126 struct GNUNET_FS_ProgressInfo pi;
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);
138 * We've encountered an error during
139 * unindexing. Signal the client.
141 * @param uc context for the failed unindexing operation
144 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc)
146 struct GNUNET_FS_ProgressInfo pi;
148 pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
149 pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
150 pi.value.unindex.specifics.error.message = uc->emsg;
151 GNUNET_FS_unindex_make_status_ (&pi, uc, 0);
156 * Continuation called to notify client about result of the
157 * datastore removal operation.
160 * @param success GNUNET_SYSERR on failure
161 * @param msg NULL on success, otherwise an error message
164 process_cont (void *cls,
168 struct GNUNET_FS_UnindexContext *uc = cls;
169 if (success == GNUNET_SYSERR)
171 uc->emsg = GNUNET_strdup (msg);
172 signal_unindex_error (uc);
175 GNUNET_FS_tree_encoder_next (uc->tc);
180 * Function called asking for the current (encoded)
181 * block to be processed. After processing the
182 * client should either call "GNUNET_FS_tree_encode_next"
183 * or (on error) "GNUNET_FS_tree_encode_finish".
186 * @param query the query for the block (key for lookup in the datastore)
187 * @param offset offset of the block
188 * @param type type of the block (IBLOCK or DBLOCK)
189 * @param block the (encrypted) block
190 * @param block_size size of block (in bytes)
193 unindex_process (void *cls,
194 const GNUNET_HashCode *query,
196 enum GNUNET_BLOCK_Type type,
200 struct GNUNET_FS_UnindexContext *uc = cls;
203 struct OnDemandBlock odb;
205 if (type != GNUNET_BLOCK_TYPE_DBLOCK)
210 else /* on-demand encoded DBLOCK */
212 size = sizeof(struct OnDemandBlock);
213 odb.offset = GNUNET_htonll (offset);
214 odb.file_id = uc->file_id;
217 GNUNET_DATASTORE_remove (uc->dsh,
222 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
229 * Function called with the response from the
230 * FS service to our unindexing request.
232 * @param cls closure, unindex context
233 * @param msg NULL on timeout, otherwise the response
236 process_fs_response (void *cls,
237 const struct GNUNET_MessageHeader *msg)
239 struct GNUNET_FS_UnindexContext *uc = cls;
240 struct GNUNET_FS_ProgressInfo pi;
242 if (uc->client != NULL)
244 GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
247 if (uc->state != UNINDEX_STATE_FS_NOTIFY)
249 uc->state = UNINDEX_STATE_ERROR;
250 uc->emsg = GNUNET_strdup (_("Unexpected time for a response from `fs' service."));
251 GNUNET_FS_unindex_sync_ (uc);
252 signal_unindex_error (uc);
257 uc->state = UNINDEX_STATE_ERROR;
258 uc->emsg = GNUNET_strdup (_("Timeout waiting for `fs' service."));
259 GNUNET_FS_unindex_sync_ (uc);
260 signal_unindex_error (uc);
263 if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
265 uc->state = UNINDEX_STATE_ERROR;
266 uc->emsg = GNUNET_strdup (_("Invalid response from `fs' service."));
267 GNUNET_FS_unindex_sync_ (uc);
268 signal_unindex_error (uc);
271 uc->state = UNINDEX_STATE_COMPLETE;
272 pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
273 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
274 GNUNET_FS_unindex_sync_ (uc);
275 GNUNET_FS_unindex_make_status_ (&pi, uc, uc->file_size);
280 * Function called when the tree encoder has
281 * processed all blocks. Clean up.
283 * @param cls our unindexing context
287 unindex_finish (void *cls,
288 const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct GNUNET_FS_UnindexContext *uc = cls;
292 struct GNUNET_FS_Uri *uri;
293 struct UnindexMessage req;
295 /* generate final progress message */
296 unindex_progress (uc,
300 GNUNET_FS_tree_encoder_finish (uc->tc,
305 GNUNET_FS_uri_destroy (uri);
306 GNUNET_DISK_file_close (uc->fh);
308 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
310 uc->state = UNINDEX_STATE_FS_NOTIFY;
311 GNUNET_FS_unindex_sync_ (uc);
312 uc->client = GNUNET_CLIENT_connect (uc->h->sched,
315 if (uc->client == NULL)
317 uc->state = UNINDEX_STATE_ERROR;
318 uc->emsg = GNUNET_strdup (_("Failed to connect to FS service for unindexing."));
319 GNUNET_FS_unindex_sync_ (uc);
320 signal_unindex_error (uc);
323 req.header.size = htons (sizeof (struct UnindexMessage));
324 req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
326 req.file_id = uc->file_id;
327 GNUNET_break (GNUNET_OK ==
328 GNUNET_CLIENT_transmit_and_get_response (uc->client,
330 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
332 &process_fs_response,
338 * Connect to the datastore and remove the blocks.
340 * @param uc context for the unindex operation.
343 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
345 uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
349 uc->state = UNINDEX_STATE_ERROR;
350 uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
351 GNUNET_FS_unindex_sync_ (uc);
352 signal_unindex_error (uc);
355 uc->fh = GNUNET_DISK_file_open (uc->filename,
356 GNUNET_DISK_OPEN_READ,
357 GNUNET_DISK_PERM_NONE);
360 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
362 uc->state = UNINDEX_STATE_ERROR;
363 uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
364 GNUNET_FS_unindex_sync_ (uc);
365 signal_unindex_error (uc);
368 uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
375 GNUNET_FS_tree_encoder_next (uc->tc);
380 * Function called once the hash of the file
381 * that is being unindexed has been computed.
383 * @param cls closure, unindex context
384 * @param file_id computed hash, NULL on error
387 GNUNET_FS_unindex_process_hash_ (void *cls,
388 const GNUNET_HashCode *file_id)
390 struct GNUNET_FS_UnindexContext *uc = cls;
393 if (uc->state != UNINDEX_STATE_HASHING)
395 GNUNET_FS_unindex_stop (uc);
400 uc->state = UNINDEX_STATE_ERROR;
401 uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
402 GNUNET_FS_unindex_sync_ (uc);
403 signal_unindex_error (uc);
406 uc->file_id = *file_id;
407 uc->state = UNINDEX_STATE_DS_REMOVE;
408 GNUNET_FS_unindex_sync_ (uc);
409 GNUNET_FS_unindex_do_remove_ (uc);
414 * Create SUSPEND event for the given unindex operation
415 * and then clean up our state (without stop signal).
417 * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
420 GNUNET_FS_unindex_signal_suspend_ (void *cls)
422 struct GNUNET_FS_UnindexContext *uc = cls;
423 struct GNUNET_FS_ProgressInfo pi;
427 GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
430 if (uc->client != NULL)
432 GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
437 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
442 GNUNET_FS_tree_encoder_finish (uc->tc,
449 GNUNET_DISK_file_close (uc->fh);
452 GNUNET_FS_end_top (uc->h, uc->top);
453 pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
454 GNUNET_FS_unindex_make_status_ (&pi, uc,
455 (uc->state == UNINDEX_STATE_COMPLETE)
456 ? uc->file_size : 0);
457 GNUNET_break (NULL == uc->client_info);
458 GNUNET_free (uc->filename);
459 GNUNET_free_non_null (uc->serialization);
467 * @param h handle to the file sharing subsystem
468 * @param filename file to unindex
469 * @param cctx initial value for the client context
470 * @return NULL on error, otherwise handle
472 struct GNUNET_FS_UnindexContext *
473 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
474 const char *filename,
477 struct GNUNET_FS_UnindexContext *ret;
478 struct GNUNET_FS_ProgressInfo pi;
482 GNUNET_DISK_file_size (filename,
486 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
488 ret->filename = GNUNET_strdup (filename);
489 ret->start_time = GNUNET_TIME_absolute_get ();
490 ret->file_size = size;
491 ret->client_info = cctx;
492 GNUNET_FS_unindex_sync_ (ret);
493 pi.status = GNUNET_FS_STATUS_UNINDEX_START;
494 pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
495 GNUNET_FS_unindex_make_status_ (&pi, ret, 0);
496 ret->fhc = GNUNET_CRYPTO_hash_file (h->sched,
497 GNUNET_SCHEDULER_PRIORITY_IDLE,
500 &GNUNET_FS_unindex_process_hash_,
502 ret->top = GNUNET_FS_make_top (h,
503 &GNUNET_FS_unindex_signal_suspend_,
510 * Clean up after completion of an unindex operation.
515 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
517 struct GNUNET_FS_ProgressInfo pi;
521 GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
524 if (uc->client != NULL)
526 GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
531 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
536 GNUNET_FS_tree_encoder_finish (uc->tc,
543 GNUNET_DISK_file_close (uc->fh);
546 GNUNET_FS_end_top (uc->h, uc->top);
547 if (uc->serialization != NULL)
549 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
550 GNUNET_free (uc->serialization);
551 uc->serialization = NULL;
553 pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
554 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
555 GNUNET_FS_unindex_make_status_ (&pi, uc,
556 (uc->state == UNINDEX_STATE_COMPLETE)
557 ? uc->file_size : 0);
558 GNUNET_break (NULL == uc->client_info);
559 GNUNET_free (uc->filename);
563 /* end of fs_unindex.c */