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 * Fill in all of the generic fields for
85 * @param pc structure to fill in
86 * @param sc overall unindex context
89 make_unindex_status (struct GNUNET_FS_ProgressInfo *pi,
90 struct GNUNET_FS_UnindexContext *uc,
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;
98 = GNUNET_TIME_calculate_eta (uc->start_time,
101 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
102 pi->value.publish.completed = offset;
107 * Function called with information about our
108 * progress in computing the tree encoding.
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
117 unindex_progress (void *cls,
119 const void *pt_block,
123 struct GNUNET_FS_UnindexContext *uc = cls;
124 struct GNUNET_FS_ProgressInfo pi;
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;
133 = uc->h->upcb (uc->h->upcb_cls,
139 * We've encountered an error during
140 * unindexing. Signal the client.
142 * @param uc context for the failed unindexing operation
143 * @param emsg the error message
146 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc,
149 struct GNUNET_FS_ProgressInfo pi;
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;
156 = uc->h->upcb (uc->h->upcb_cls,
162 * Continuation called to notify client about result of the
163 * datastore removal operation.
166 * @param success GNUNET_SYSERR on failure
167 * @param msg NULL on success, otherwise an error message
170 process_cont (void *cls,
174 struct GNUNET_FS_UnindexContext *uc = cls;
175 if (success == GNUNET_SYSERR)
177 signal_unindex_error (uc,
182 GNUNET_FS_tree_encoder_next (uc->tc);
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".
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)
200 unindex_process (void *cls,
201 const GNUNET_HashCode *query,
207 struct GNUNET_FS_UnindexContext *uc = cls;
210 struct OnDemandBlock odb;
212 if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
217 else /* on-demand encoded DBLOCK */
219 size = sizeof(struct OnDemandBlock);
221 odb.file_id = uc->file_id;
224 GNUNET_DATASTORE_remove (uc->dsh,
230 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
235 * Function called when the tree encoder has
236 * processed all blocks. Clean up.
238 * @param cls our unindexing context
242 unindex_finish (void *cls,
243 const struct GNUNET_SCHEDULER_TaskContext *tc)
245 struct GNUNET_FS_UnindexContext *uc = cls;
247 struct GNUNET_FS_Uri *uri;
248 struct GNUNET_FS_ProgressInfo pi;
250 GNUNET_FS_tree_encoder_finish (uc->tc,
254 GNUNET_FS_uri_destroy (uri);
255 GNUNET_DISK_file_close (uc->fh);
257 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
261 signal_unindex_error (uc, emsg);
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;
270 = uc->h->upcb (uc->h->upcb_cls,
277 * Function called with the response from the
278 * FS service to our unindexing request.
280 * @param cls closure, unindex context
281 * @param msg NULL on timeout, otherwise the response
284 process_fs_response (void *cls,
285 const struct GNUNET_MessageHeader *msg)
287 struct GNUNET_FS_UnindexContext *uc = cls;
289 GNUNET_CLIENT_disconnect (uc->client);
291 if (uc->state != UNINDEX_STATE_FS_NOTIFY)
293 GNUNET_FS_unindex_stop (uc);
298 uc->state = UNINDEX_STATE_ERROR;
299 signal_unindex_error (uc,
300 _("Timeout waiting for `fs' service."));
303 if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
305 uc->state = UNINDEX_STATE_ERROR;
306 signal_unindex_error (uc,
307 _("Invalid response from `fs' service."));
310 uc->state = UNINDEX_STATE_DS_REMOVE;
311 uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
315 uc->state = UNINDEX_STATE_ERROR;
316 signal_unindex_error (uc,
317 _("Failed to connect to `datastore' service."));
320 uc->fh = GNUNET_DISK_file_open (uc->filename,
321 GNUNET_DISK_OPEN_READ);
324 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
326 uc->state = UNINDEX_STATE_ERROR;
327 signal_unindex_error (uc,
328 _("Failed to open file for unindexing."));
331 uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
338 GNUNET_FS_tree_encoder_next (uc->tc);
343 * Function called once the hash of the file
344 * that is being unindexed has been computed.
346 * @param cls closure, unindex context
347 * @param file_id computed hash, NULL on error
350 process_hash (void *cls,
351 const GNUNET_HashCode *file_id)
353 struct GNUNET_FS_UnindexContext *uc = cls;
354 struct UnindexMessage req;
356 if (uc->state != UNINDEX_STATE_HASHING)
358 GNUNET_FS_unindex_stop (uc);
363 uc->state = UNINDEX_STATE_ERROR;
364 signal_unindex_error (uc,
365 _("Failed to compute hash of file."));
368 uc->file_id = *file_id;
369 uc->state = UNINDEX_STATE_FS_NOTIFY;
370 uc->client = GNUNET_CLIENT_connect (uc->h->sched,
373 req.header.size = htons (sizeof (struct UnindexMessage));
374 req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
376 req.file_id = *file_id;
377 GNUNET_CLIENT_transmit_and_get_response (uc->client,
379 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
380 &process_fs_response,
388 * @param h handle to the file sharing subsystem
389 * @param filename file to unindex
390 * @return NULL on error, otherwise handle
392 struct GNUNET_FS_UnindexContext *
393 GNUNET_FS_unindex (struct GNUNET_FS_Handle *h,
394 const char *filename)
396 struct GNUNET_FS_UnindexContext *ret;
397 struct GNUNET_FS_ProgressInfo pi;
401 GNUNET_DISK_file_size (filename,
405 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
407 ret->filename = GNUNET_strdup (filename);
408 ret->start_time = GNUNET_TIME_absolute_get ();
409 ret->file_size = size;
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;
416 = h->upcb (h->upcb_cls,
418 GNUNET_CRYPTO_hash_file (h->sched,
419 GNUNET_SCHEDULER_PRIORITY_IDLE,
430 * Clean up after completion of an unindex operation.
435 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
437 struct GNUNET_FS_ProgressInfo pi;
439 if ( (uc->state != UNINDEX_STATE_COMPLETE) &&
440 (uc->state != UNINDEX_STATE_ERROR) )
442 uc->state = UNINDEX_STATE_ABORTED;
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;
452 = uc->h->upcb (uc->h->upcb_cls,
454 GNUNET_break (NULL == uc->client_info);
455 GNUNET_free (uc->filename);
459 /* end of fs_unindex.c */