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 * Fill in all of the generic fields for
153 * @param pc structure to fill in
154 * @param sc overall unindex context
157 make_unindex_status (struct GNUNET_FS_ProgressInfo *pi,
158 struct GNUNET_FS_UnindexContext *uc,
161 pi->value.unindex.uc = uc;
162 pi->value.unindex.cctx = uc->client_info;
163 pi->value.unindex.filename = uc->filename;
164 pi->value.unindex.size = uc->file_size;
165 pi->value.unindex.eta
166 = GNUNET_TIME_calculate_eta (uc->start_time,
169 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
170 pi->value.publish.completed = offset;
175 * Function called with information about our
176 * progress in computing the tree encoding.
179 * @param offset where are we in the file
180 * @param pt_block plaintext of the currently processed block
181 * @param pt_size size of pt_block
182 * @param depth depth of the block in the tree
185 unindex_progress (void *cls,
187 const void *pt_block,
191 struct GNUNET_FS_UnindexContext *uc = cls;
192 struct GNUNET_FS_ProgressInfo pi;
194 pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
195 make_unindex_status (&pi, uc, offset);
196 pi.value.unindex.specifics.progress.data = pt_block;
197 pi.value.unindex.specifics.progress.offset = offset;
198 pi.value.unindex.specifics.progress.data_len = pt_size;
199 pi.value.unindex.specifics.progress.depth = depth;
201 = uc->h->upcb (uc->h->upcb_cls,
207 * We've encountered an error during
208 * unindexing. Signal the client.
210 * @param uc context for the failed unindexing operation
211 * @param emsg the error message
214 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc,
217 struct GNUNET_FS_ProgressInfo pi;
219 pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
220 make_unindex_status (&pi, uc, 0);
221 pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
222 pi.value.unindex.specifics.error.message = emsg;
224 = uc->h->upcb (uc->h->upcb_cls,
230 * Function called when the tree encoder has
231 * processed all blocks. Clean up.
233 * @param cls our unindexing context
237 unindex_finish (void *cls,
238 const struct GNUNET_SCHEDULER_TaskContext *tc)
240 struct GNUNET_FS_UnindexContext *uc = cls;
242 struct GNUNET_FS_Uri *uri;
243 struct GNUNET_FS_ProgressInfo pi;
245 GNUNET_FS_tree_encoder_finish (uc->tc,
249 GNUNET_FS_uri_destroy (uri);
250 GNUNET_DISK_file_close (uc->fh);
252 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
256 signal_unindex_error (uc, emsg);
261 pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
262 make_unindex_status (&pi, uc, uc->file_size);
263 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
265 = uc->h->upcb (uc->h->upcb_cls,
272 * Function called with the response from the
273 * FS service to our unindexing request.
275 * @param cls closure, unindex context
276 * @param msg NULL on timeout, otherwise the response
279 process_fs_response (void *cls,
280 const struct GNUNET_MessageHeader *msg)
282 struct GNUNET_FS_UnindexContext *uc = cls;
284 GNUNET_CLIENT_disconnect (uc->client);
286 if (uc->state != UNINDEX_STATE_FS_NOTIFY)
288 GNUNET_FS_unindex_stop (uc);
293 uc->state = UNINDEX_STATE_ERROR;
294 signal_unindex_error (uc,
295 _("Timeout waiting for `fs' service."));
298 if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
300 uc->state = UNINDEX_STATE_ERROR;
301 signal_unindex_error (uc,
302 _("Invalid response from `fs' service."));
305 uc->state = UNINDEX_STATE_DS_REMOVE;
306 uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
310 uc->state = UNINDEX_STATE_ERROR;
311 signal_unindex_error (uc,
312 _("Failed to connect to `datastore' service."));
315 uc->fh = GNUNET_DISK_file_open (uc->filename,
316 GNUNET_DISK_OPEN_READ);
319 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
321 uc->state = UNINDEX_STATE_ERROR;
322 signal_unindex_error (uc,
323 _("Failed to open file for unindexing."));
326 uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
333 GNUNET_FS_tree_encoder_next (uc->tc);
338 * Function called once the hash of the file
339 * that is being unindexed has been computed.
341 * @param cls closure, unindex context
342 * @param file_id computed hash, NULL on error
345 process_hash (void *cls,
346 const GNUNET_HashCode *file_id)
348 struct GNUNET_FS_UnindexContext *uc = cls;
349 struct UnindexMessage req;
351 if (uc->state != UNINDEX_STATE_HASHING)
353 GNUNET_FS_unindex_stop (uc);
358 uc->state = UNINDEX_STATE_ERROR;
359 signal_unindex_error (uc,
360 _("Failed to compute hash of file."));
363 uc->file_id = *file_id;
364 uc->state = UNINDEX_STATE_FS_NOTIFY;
365 uc->client = GNUNET_CLIENT_connect (uc->h->sched,
368 req.header.size = htons (sizeof (struct UnindexMessage));
369 req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
371 req.file_id = *file_id;
372 GNUNET_CLIENT_transmit_and_get_response (uc->client,
374 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
375 &process_fs_response,
383 * @param h handle to the file sharing subsystem
384 * @param filename file to unindex
385 * @return NULL on error, otherwise handle
387 struct GNUNET_FS_UnindexContext *
388 GNUNET_FS_unindex (struct GNUNET_FS_Handle *h,
389 const char *filename)
391 struct GNUNET_FS_UnindexContext *ret;
392 struct GNUNET_FS_ProgressInfo pi;
396 GNUNET_DISK_file_size (filename,
400 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
402 ret->filename = GNUNET_strdup (filename);
403 ret->start_time = GNUNET_TIME_absolute_get ();
404 ret->file_size = size;
406 // FIXME: make persistent!
407 pi.status = GNUNET_FS_STATUS_UNINDEX_START;
408 make_unindex_status (&pi, ret, 0);
409 pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
411 = h->upcb (h->upcb_cls,
413 GNUNET_CRYPTO_hash_file (h->sched,
414 GNUNET_SCHEDULER_PRIORITY_IDLE,
425 * Clean up after completion of an unindex operation.
430 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
432 struct GNUNET_FS_ProgressInfo pi;
434 if ( (uc->state != UNINDEX_STATE_COMPLETE) &&
435 (uc->state != UNINDEX_STATE_ERROR) )
437 uc->state = UNINDEX_STATE_ABORTED;
440 // FIXME: make unpersistent!
441 make_unindex_status (&pi, uc,
442 (uc->state == UNINDEX_STATE_COMPLETE)
443 ? uc->file_size : 0);
444 pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
445 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
447 = uc->h->upcb (uc->h->upcb_cls,
449 GNUNET_break (NULL == uc->client_info);
450 GNUNET_free (uc->filename);
454 /* end of fs_unindex.c */