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 2, 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 Bennett
24 * @author Christian Grothoff
25 * @brief Unindex file.
28 * - code cleanup (share more with upload.c)
31 #include "gnunet_constants.h"
32 #include "gnunet_fs_service.h"
33 #include "gnunet_protocols.h"
39 * Function called by the tree encoder to obtain
40 * a block of plaintext data (for the lowest level
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
52 unindex_reader (void *cls,
58 struct GNUNET_FS_UnindexContext *uc = cls;
61 pt_size = GNUNET_MIN(max,
62 uc->file_size - offset);
64 GNUNET_DISK_file_seek (uc->fh, offset, GNUNET_DISK_SEEK_SET))
66 *emsg = GNUNET_strdup (_("Failed to find given position in file"));
70 GNUNET_DISK_file_read (uc->fh,
74 *emsg = GNUNET_strdup (_("Failed to read file"));
82 * Continuation called to notify client about result of the
83 * datastore removal operation.
86 * @param success GNUNET_SYSERR on failure
87 * @param msg NULL on success, otherwise an error message
90 process_cont (void *cls,
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);
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".
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)
115 unindex_process (void *cls,
116 const GNUNET_HashCode *query,
122 struct GNUNET_FS_UnindexContext *uc = cls;
125 struct OnDemandBlock odb;
127 if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
132 else /* on-demand encoded DBLOCK */
134 size = sizeof(struct OnDemandBlock);
136 odb.file_id = uc->file_id;
139 GNUNET_DATASTORE_remove (uc->dsh,
145 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
150 * Function called with information about our
151 * progress in computing the tree encoding.
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
160 unindex_progress (void *cls,
162 const void *pt_block,
166 struct GNUNET_FS_UnindexContext *uc = cls;
167 // FIXME: call callback!
173 * Fill in all of the generic fields for
176 * @param pc structure to fill in
177 * @param sc overall unindex context
180 make_unindex_status (struct GNUNET_FS_ProgressInfo *pi,
181 struct GNUNET_FS_UnindexContext *uc,
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,
192 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
193 pi->value.publish.completed = offset;
198 * We've encountered an error during
199 * unindexing. Signal the client.
201 * @param uc context for the failed unindexing operation
202 * @param emsg the error message
205 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc,
208 struct GNUNET_FS_ProgressInfo pi;
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;
215 = uc->h->upcb (uc->h->upcb_cls,
221 * Function called when the tree encoder has
222 * processed all blocks. Clean up.
224 * @param cls our unindexing context
228 unindex_finish (void *cls,
229 const struct GNUNET_SCHEDULER_TaskContext *tc)
231 struct GNUNET_FS_UnindexContext *uc = cls;
233 struct GNUNET_FS_Uri *uri;
234 struct GNUNET_FS_ProgressInfo pi;
236 GNUNET_FS_tree_encoder_finish (uc->tc,
240 GNUNET_FS_uri_destroy (uri);
241 GNUNET_DISK_file_close (uc->fh);
243 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
247 signal_unindex_error (uc, emsg);
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;
256 = uc->h->upcb (uc->h->upcb_cls,
263 * Function called with the response from the
264 * FS service to our unindexing request.
266 * @param cls closure, unindex context
267 * @param msg NULL on timeout, otherwise the response
270 process_fs_response (void *cls,
271 const struct GNUNET_MessageHeader *msg)
273 struct GNUNET_FS_UnindexContext *uc = cls;
275 GNUNET_CLIENT_disconnect (uc->client);
277 if (uc->state != UNINDEX_STATE_FS_NOTIFY)
279 GNUNET_FS_unindex_stop (uc);
284 uc->state = UNINDEX_STATE_ERROR;
285 signal_unindex_error (uc,
286 _("Timeout waiting for `fs' service."));
289 if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
291 uc->state = UNINDEX_STATE_ERROR;
292 signal_unindex_error (uc,
293 _("Invalid response from `fs' service."));
296 uc->state = UNINDEX_STATE_DS_REMOVE;
297 uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
301 uc->state = UNINDEX_STATE_ERROR;
302 signal_unindex_error (uc,
303 _("Failed to connect to `datastore' service."));
306 uc->fh = GNUNET_DISK_file_open (uc->filename,
307 GNUNET_DISK_OPEN_READ);
310 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
312 uc->state = UNINDEX_STATE_ERROR;
313 signal_unindex_error (uc,
314 _("Failed to open file for unindexing."));
317 uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
324 GNUNET_FS_tree_encoder_next (uc->tc);
329 * Function called once the hash of the file
330 * that is being unindexed has been computed.
332 * @param cls closure, unindex context
333 * @param file_id computed hash, NULL on error
336 process_hash (void *cls,
337 const GNUNET_HashCode *file_id)
339 struct GNUNET_FS_UnindexContext *uc = cls;
340 struct UnindexMessage req;
342 if (uc->state != UNINDEX_STATE_HASHING)
344 GNUNET_FS_unindex_stop (uc);
349 uc->state = UNINDEX_STATE_ERROR;
350 signal_unindex_error (uc,
351 _("Failed to compute hash of file."));
354 uc->file_id = *file_id;
355 uc->state = UNINDEX_STATE_FS_NOTIFY;
356 uc->client = GNUNET_CLIENT_connect (uc->h->sched,
359 req.header.size = htons (sizeof (struct UnindexMessage));
360 req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
362 req.file_id = *file_id;
363 GNUNET_CLIENT_transmit_and_get_response (uc->client,
365 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
366 &process_fs_response,
374 * @param h handle to the file sharing subsystem
375 * @param filename file to unindex
376 * @return NULL on error, otherwise handle
378 struct GNUNET_FS_UnindexContext *
379 GNUNET_FS_unindex (struct GNUNET_FS_Handle *h,
380 const char *filename)
382 struct GNUNET_FS_UnindexContext *ret;
383 struct GNUNET_FS_ProgressInfo pi;
387 GNUNET_DISK_file_size (filename,
391 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
393 ret->filename = GNUNET_strdup (filename);
394 ret->start_time = GNUNET_TIME_absolute_get ();
395 ret->file_size = size;
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;
402 = h->upcb (h->upcb_cls,
404 GNUNET_CRYPTO_hash_file (h->sched,
405 GNUNET_SCHEDULER_PRIORITY_IDLE,
416 * Clean up after completion of an unindex operation.
421 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
423 struct GNUNET_FS_ProgressInfo pi;
425 if ( (uc->state != UNINDEX_STATE_COMPLETE) &&
426 (uc->state != UNINDEX_STATE_ERROR) )
428 uc->state = UNINDEX_STATE_ABORTED;
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;
438 = uc->h->upcb (uc->h->upcb_cls,
440 GNUNET_break (NULL == uc->client_info);
441 GNUNET_free (uc->filename);
445 /* end of fs_unindex.c */