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"
34 #define DEBUG_UNINDEX GNUNET_NO
37 * Function called by the tree encoder to obtain
38 * a block of plaintext data (for the lowest level
41 * @param cls our publishing context
42 * @param offset identifies which block to get
43 * @param max (maximum) number of bytes to get; returning
44 * fewer will also cause errors
45 * @param buf where to copy the plaintext buffer
46 * @param emsg location to store an error message (on error)
47 * @return number of bytes copied to buf, 0 on error
50 unindex_reader (void *cls,
56 struct GNUNET_FS_UnindexContext *uc = cls;
59 pt_size = GNUNET_MIN(max,
60 uc->file_size - offset);
62 GNUNET_DISK_file_seek (uc->fh, offset, GNUNET_DISK_SEEK_SET))
64 *emsg = GNUNET_strdup (_("Failed to find given position in file"));
68 GNUNET_DISK_file_read (uc->fh,
72 *emsg = GNUNET_strdup (_("Failed to read file"));
80 * Fill in all of the generic fields for
81 * an unindex event and call the callback.
83 * @param pi structure to fill in
84 * @param uc overall unindex context
85 * @param offset where we are in the file (for progress)
88 GNUNET_FS_unindex_make_status_ (struct GNUNET_FS_ProgressInfo *pi,
89 struct GNUNET_FS_UnindexContext *uc,
92 pi->value.unindex.uc = uc;
93 pi->value.unindex.cctx = uc->client_info;
94 pi->value.unindex.filename = uc->filename;
95 pi->value.unindex.size = uc->file_size;
97 = GNUNET_TIME_calculate_eta (uc->start_time,
100 pi->value.unindex.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
101 pi->value.unindex.completed = offset;
103 = uc->h->upcb (uc->h->upcb_cls,
110 * Function called with information about our
111 * progress in computing the tree encoding.
114 * @param offset where are we in the file
115 * @param pt_block plaintext of the currently processed block
116 * @param pt_size size of pt_block
117 * @param depth depth of the block in the tree, 0 for DBLOCK
120 unindex_progress (void *cls,
122 const void *pt_block,
126 struct GNUNET_FS_UnindexContext *uc = cls;
127 struct GNUNET_FS_ProgressInfo pi;
129 pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
130 pi.value.unindex.specifics.progress.data = pt_block;
131 pi.value.unindex.specifics.progress.offset = offset;
132 pi.value.unindex.specifics.progress.data_len = pt_size;
133 pi.value.unindex.specifics.progress.depth = depth;
134 GNUNET_FS_unindex_make_status_ (&pi, uc, offset);
139 * We've encountered an error during
140 * unindexing. Signal the client.
142 * @param uc context for the failed unindexing operation
145 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc)
147 struct GNUNET_FS_ProgressInfo pi;
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);
157 * Continuation called to notify client about result of the
158 * datastore removal operation.
161 * @param success GNUNET_SYSERR on failure
162 * @param msg NULL on success, otherwise an error message
165 process_cont (void *cls,
169 struct GNUNET_FS_UnindexContext *uc = cls;
170 if (success == GNUNET_SYSERR)
172 uc->emsg = GNUNET_strdup (msg);
173 signal_unindex_error (uc);
177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178 "Datastore REMOVE operation succeeded\n");
180 GNUNET_FS_tree_encoder_next (uc->tc);
185 * Function called asking for the current (encoded)
186 * block to be processed. After processing the
187 * client should either call "GNUNET_FS_tree_encode_next"
188 * or (on error) "GNUNET_FS_tree_encode_finish".
191 * @param chk content hash key for the block (key for lookup in the datastore)
192 * @param offset offset of the block
193 * @param depth depth of the block, 0 for DBLOCK
194 * @param type type of the block (IBLOCK or DBLOCK)
195 * @param block the (encrypted) block
196 * @param block_size size of block (in bytes)
199 unindex_process (void *cls,
200 const struct ContentHashKey *chk,
203 enum GNUNET_BLOCK_Type type,
207 struct GNUNET_FS_UnindexContext *uc = cls;
210 struct OnDemandBlock odb;
212 if (type != GNUNET_BLOCK_TYPE_FS_DBLOCK)
217 else /* on-demand encoded DBLOCK */
219 size = sizeof(struct OnDemandBlock);
220 odb.offset = GNUNET_htonll (offset);
221 odb.file_id = uc->file_id;
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Sending REMOVE request to DATASTORE service\n");
228 GNUNET_DATASTORE_remove (uc->dsh,
233 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
240 * Function called with the response from the
241 * FS service to our unindexing request.
243 * @param cls closure, unindex context
244 * @param msg NULL on timeout, otherwise the response
247 process_fs_response (void *cls,
248 const struct GNUNET_MessageHeader *msg)
250 struct GNUNET_FS_UnindexContext *uc = cls;
251 struct GNUNET_FS_ProgressInfo pi;
253 if (uc->client != NULL)
255 GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
258 if (uc->state != UNINDEX_STATE_FS_NOTIFY)
260 uc->state = UNINDEX_STATE_ERROR;
261 uc->emsg = GNUNET_strdup (_("Unexpected time for a response from `fs' service."));
262 GNUNET_FS_unindex_sync_ (uc);
263 signal_unindex_error (uc);
268 uc->state = UNINDEX_STATE_ERROR;
269 uc->emsg = GNUNET_strdup (_("Timeout waiting for `fs' service."));
270 GNUNET_FS_unindex_sync_ (uc);
271 signal_unindex_error (uc);
274 if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
276 uc->state = UNINDEX_STATE_ERROR;
277 uc->emsg = GNUNET_strdup (_("Invalid response from `fs' service."));
278 GNUNET_FS_unindex_sync_ (uc);
279 signal_unindex_error (uc);
282 uc->state = UNINDEX_STATE_COMPLETE;
283 pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
284 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
285 GNUNET_FS_unindex_sync_ (uc);
286 GNUNET_FS_unindex_make_status_ (&pi, uc, uc->file_size);
291 * Function called when the tree encoder has
292 * processed all blocks. Clean up.
294 * @param cls our unindexing context
298 unindex_finish (void *cls,
299 const struct GNUNET_SCHEDULER_TaskContext *tc)
301 struct GNUNET_FS_UnindexContext *uc = cls;
303 struct GNUNET_FS_Uri *uri;
304 struct UnindexMessage req;
306 /* generate final progress message */
307 unindex_progress (uc,
311 GNUNET_FS_tree_encoder_finish (uc->tc,
316 GNUNET_FS_uri_destroy (uri);
317 GNUNET_DISK_file_close (uc->fh);
319 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
321 uc->state = UNINDEX_STATE_FS_NOTIFY;
322 GNUNET_FS_unindex_sync_ (uc);
323 uc->client = GNUNET_CLIENT_connect ("fs",
325 if (uc->client == NULL)
327 uc->state = UNINDEX_STATE_ERROR;
328 uc->emsg = GNUNET_strdup (_("Failed to connect to FS service for unindexing."));
329 GNUNET_FS_unindex_sync_ (uc);
330 signal_unindex_error (uc);
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Sending UNINDEX message to FS service\n");
337 req.header.size = htons (sizeof (struct UnindexMessage));
338 req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
340 req.file_id = uc->file_id;
341 GNUNET_break (GNUNET_OK ==
342 GNUNET_CLIENT_transmit_and_get_response (uc->client,
344 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
346 &process_fs_response,
352 * Connect to the datastore and remove the blocks.
354 * @param uc context for the unindex operation.
357 GNUNET_FS_unindex_do_remove_ (struct GNUNET_FS_UnindexContext *uc)
359 uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg);
362 uc->state = UNINDEX_STATE_ERROR;
363 uc->emsg = GNUNET_strdup (_("Failed to connect to `datastore' service."));
364 GNUNET_FS_unindex_sync_ (uc);
365 signal_unindex_error (uc);
368 uc->fh = GNUNET_DISK_file_open (uc->filename,
369 GNUNET_DISK_OPEN_READ,
370 GNUNET_DISK_PERM_NONE);
373 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
375 uc->state = UNINDEX_STATE_ERROR;
376 uc->emsg = GNUNET_strdup (_("Failed to open file for unindexing."));
377 GNUNET_FS_unindex_sync_ (uc);
378 signal_unindex_error (uc);
381 uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
388 GNUNET_FS_tree_encoder_next (uc->tc);
393 * Function called once the hash of the file
394 * that is being unindexed has been computed.
396 * @param cls closure, unindex context
397 * @param file_id computed hash, NULL on error
400 GNUNET_FS_unindex_process_hash_ (void *cls,
401 const GNUNET_HashCode *file_id)
403 struct GNUNET_FS_UnindexContext *uc = cls;
406 if (uc->state != UNINDEX_STATE_HASHING)
408 GNUNET_FS_unindex_stop (uc);
413 uc->state = UNINDEX_STATE_ERROR;
414 uc->emsg = GNUNET_strdup (_("Failed to compute hash of file."));
415 GNUNET_FS_unindex_sync_ (uc);
416 signal_unindex_error (uc);
419 uc->file_id = *file_id;
420 uc->state = UNINDEX_STATE_DS_REMOVE;
421 GNUNET_FS_unindex_sync_ (uc);
422 GNUNET_FS_unindex_do_remove_ (uc);
427 * Create SUSPEND event for the given unindex operation
428 * and then clean up our state (without stop signal).
430 * @param cls the 'struct GNUNET_FS_UnindexContext' to signal for
433 GNUNET_FS_unindex_signal_suspend_ (void *cls)
435 struct GNUNET_FS_UnindexContext *uc = cls;
436 struct GNUNET_FS_ProgressInfo pi;
440 GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
443 if (uc->client != NULL)
445 GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
450 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
455 GNUNET_FS_tree_encoder_finish (uc->tc,
462 GNUNET_DISK_file_close (uc->fh);
465 GNUNET_FS_end_top (uc->h, uc->top);
466 pi.status = GNUNET_FS_STATUS_UNINDEX_SUSPEND;
467 GNUNET_FS_unindex_make_status_ (&pi, uc,
468 (uc->state == UNINDEX_STATE_COMPLETE)
469 ? uc->file_size : 0);
470 GNUNET_break (NULL == uc->client_info);
471 GNUNET_free (uc->filename);
472 GNUNET_free_non_null (uc->serialization);
473 GNUNET_free_non_null (uc->emsg);
481 * @param h handle to the file sharing subsystem
482 * @param filename file to unindex
483 * @param cctx initial value for the client context
484 * @return NULL on error, otherwise handle
486 struct GNUNET_FS_UnindexContext *
487 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
488 const char *filename,
491 struct GNUNET_FS_UnindexContext *ret;
492 struct GNUNET_FS_ProgressInfo pi;
496 GNUNET_DISK_file_size (filename,
500 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
502 ret->filename = GNUNET_strdup (filename);
503 ret->start_time = GNUNET_TIME_absolute_get ();
504 ret->file_size = size;
505 ret->client_info = cctx;
506 GNUNET_FS_unindex_sync_ (ret);
507 pi.status = GNUNET_FS_STATUS_UNINDEX_START;
508 pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
509 GNUNET_FS_unindex_make_status_ (&pi, ret, 0);
510 ret->fhc = GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_IDLE,
513 &GNUNET_FS_unindex_process_hash_,
515 ret->top = GNUNET_FS_make_top (h,
516 &GNUNET_FS_unindex_signal_suspend_,
523 * Clean up after completion of an unindex operation.
528 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
530 struct GNUNET_FS_ProgressInfo pi;
534 GNUNET_CRYPTO_hash_file_cancel (uc->fhc);
537 if (uc->client != NULL)
539 GNUNET_CLIENT_disconnect (uc->client, GNUNET_NO);
544 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
549 GNUNET_FS_tree_encoder_finish (uc->tc,
556 GNUNET_DISK_file_close (uc->fh);
559 GNUNET_FS_end_top (uc->h, uc->top);
560 if (uc->serialization != NULL)
562 GNUNET_FS_remove_sync_file_ (uc->h, GNUNET_FS_SYNC_PATH_MASTER_UNINDEX, uc->serialization);
563 GNUNET_free (uc->serialization);
564 uc->serialization = NULL;
566 pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
567 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
568 GNUNET_FS_unindex_make_status_ (&pi, uc,
569 (uc->state == UNINDEX_STATE_COMPLETE)
570 ? uc->file_size : 0);
571 GNUNET_break (NULL == uc->client_info);
572 GNUNET_free (uc->filename);
576 /* end of fs_unindex.c */