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 pi structure to fill in
86 * @param uc overall unindex context
87 * @param offset where we are in the file (for progress)
90 make_unindex_status (struct GNUNET_FS_ProgressInfo *pi,
91 struct GNUNET_FS_UnindexContext *uc,
94 pi->value.unindex.uc = uc;
95 pi->value.unindex.cctx = uc->client_info;
96 pi->value.unindex.filename = uc->filename;
97 pi->value.unindex.size = uc->file_size;
99 = GNUNET_TIME_calculate_eta (uc->start_time,
102 pi->value.publish.duration = GNUNET_TIME_absolute_get_duration (uc->start_time);
103 pi->value.publish.completed = offset;
108 * Function called with information about our
109 * progress in computing the tree encoding.
112 * @param offset where are we in the file
113 * @param pt_block plaintext of the currently processed block
114 * @param pt_size size of pt_block
115 * @param depth depth of the block in the tree
118 unindex_progress (void *cls,
120 const void *pt_block,
124 struct GNUNET_FS_UnindexContext *uc = cls;
125 struct GNUNET_FS_ProgressInfo pi;
127 pi.status = GNUNET_FS_STATUS_UNINDEX_PROGRESS;
128 make_unindex_status (&pi, uc, offset);
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;
134 = uc->h->upcb (uc->h->upcb_cls,
140 * We've encountered an error during
141 * unindexing. Signal the client.
143 * @param uc context for the failed unindexing operation
144 * @param emsg the error message
147 signal_unindex_error (struct GNUNET_FS_UnindexContext *uc,
150 struct GNUNET_FS_ProgressInfo pi;
152 pi.status = GNUNET_FS_STATUS_UNINDEX_ERROR;
153 make_unindex_status (&pi, uc, 0);
154 pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
155 pi.value.unindex.specifics.error.message = emsg;
157 = uc->h->upcb (uc->h->upcb_cls,
163 * Continuation called to notify client about result of the
164 * datastore removal operation.
167 * @param success GNUNET_SYSERR on failure
168 * @param msg NULL on success, otherwise an error message
171 process_cont (void *cls,
175 struct GNUNET_FS_UnindexContext *uc = cls;
176 if (success == GNUNET_SYSERR)
178 signal_unindex_error (uc,
183 GNUNET_FS_tree_encoder_next (uc->tc);
188 * Function called asking for the current (encoded)
189 * block to be processed. After processing the
190 * client should either call "GNUNET_FS_tree_encode_next"
191 * or (on error) "GNUNET_FS_tree_encode_finish".
194 * @param query the query for the block (key for lookup in the datastore)
195 * @param offset offset of the block
196 * @param type type of the block (IBLOCK or DBLOCK)
197 * @param block the (encrypted) block
198 * @param block_size size of block (in bytes)
201 unindex_process (void *cls,
202 const GNUNET_HashCode *query,
208 struct GNUNET_FS_UnindexContext *uc = cls;
211 struct OnDemandBlock odb;
213 if (type != GNUNET_DATASTORE_BLOCKTYPE_DBLOCK)
218 else /* on-demand encoded DBLOCK */
220 size = sizeof(struct OnDemandBlock);
222 odb.file_id = uc->file_id;
225 GNUNET_DATASTORE_remove (uc->dsh,
231 GNUNET_CONSTANTS_SERVICE_TIMEOUT);
236 * Function called when the tree encoder has
237 * processed all blocks. Clean up.
239 * @param cls our unindexing context
243 unindex_finish (void *cls,
244 const struct GNUNET_SCHEDULER_TaskContext *tc)
246 struct GNUNET_FS_UnindexContext *uc = cls;
248 struct GNUNET_FS_Uri *uri;
249 struct GNUNET_FS_ProgressInfo pi;
251 GNUNET_FS_tree_encoder_finish (uc->tc,
255 GNUNET_FS_uri_destroy (uri);
256 GNUNET_DISK_file_close (uc->fh);
258 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
262 signal_unindex_error (uc, emsg);
267 pi.status = GNUNET_FS_STATUS_UNINDEX_COMPLETED;
268 make_unindex_status (&pi, uc, uc->file_size);
269 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
271 = uc->h->upcb (uc->h->upcb_cls,
278 * Function called with the response from the
279 * FS service to our unindexing request.
281 * @param cls closure, unindex context
282 * @param msg NULL on timeout, otherwise the response
285 process_fs_response (void *cls,
286 const struct GNUNET_MessageHeader *msg)
288 struct GNUNET_FS_UnindexContext *uc = cls;
290 GNUNET_CLIENT_disconnect (uc->client);
292 if (uc->state != UNINDEX_STATE_FS_NOTIFY)
294 GNUNET_FS_unindex_stop (uc);
299 uc->state = UNINDEX_STATE_ERROR;
300 signal_unindex_error (uc,
301 _("Timeout waiting for `fs' service."));
304 if (ntohs(msg->type) != GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK)
306 uc->state = UNINDEX_STATE_ERROR;
307 signal_unindex_error (uc,
308 _("Invalid response from `fs' service."));
311 uc->state = UNINDEX_STATE_DS_REMOVE;
312 uc->dsh = GNUNET_DATASTORE_connect (uc->h->cfg,
316 uc->state = UNINDEX_STATE_ERROR;
317 signal_unindex_error (uc,
318 _("Failed to connect to `datastore' service."));
321 uc->fh = GNUNET_DISK_file_open (uc->filename,
322 GNUNET_DISK_OPEN_READ,
323 GNUNET_DISK_PERM_NONE);
326 GNUNET_DATASTORE_disconnect (uc->dsh, GNUNET_NO);
328 uc->state = UNINDEX_STATE_ERROR;
329 signal_unindex_error (uc,
330 _("Failed to open file for unindexing."));
333 uc->tc = GNUNET_FS_tree_encoder_create (uc->h,
340 GNUNET_FS_tree_encoder_next (uc->tc);
345 * Function called once the hash of the file
346 * that is being unindexed has been computed.
348 * @param cls closure, unindex context
349 * @param file_id computed hash, NULL on error
352 process_hash (void *cls,
353 const GNUNET_HashCode *file_id)
355 struct GNUNET_FS_UnindexContext *uc = cls;
356 struct UnindexMessage req;
358 if (uc->state != UNINDEX_STATE_HASHING)
360 GNUNET_FS_unindex_stop (uc);
365 uc->state = UNINDEX_STATE_ERROR;
366 signal_unindex_error (uc,
367 _("Failed to compute hash of file."));
370 uc->file_id = *file_id;
371 uc->state = UNINDEX_STATE_FS_NOTIFY;
372 uc->client = GNUNET_CLIENT_connect (uc->h->sched,
375 req.header.size = htons (sizeof (struct UnindexMessage));
376 req.header.type = htons (GNUNET_MESSAGE_TYPE_FS_UNINDEX);
378 req.file_id = *file_id;
379 GNUNET_CLIENT_transmit_and_get_response (uc->client,
381 GNUNET_CONSTANTS_SERVICE_TIMEOUT,
383 &process_fs_response,
391 * @param h handle to the file sharing subsystem
392 * @param filename file to unindex
393 * @param cctx initial value for the client context
394 * @return NULL on error, otherwise handle
396 struct GNUNET_FS_UnindexContext *
397 GNUNET_FS_unindex_start (struct GNUNET_FS_Handle *h,
398 const char *filename,
401 struct GNUNET_FS_UnindexContext *ret;
402 struct GNUNET_FS_ProgressInfo pi;
406 GNUNET_DISK_file_size (filename,
410 ret = GNUNET_malloc (sizeof (struct GNUNET_FS_UnindexContext));
412 ret->filename = GNUNET_strdup (filename);
413 ret->start_time = GNUNET_TIME_absolute_get ();
414 ret->file_size = size;
415 ret->client_info = cctx;
417 // FIXME: make persistent!
418 pi.status = GNUNET_FS_STATUS_UNINDEX_START;
419 make_unindex_status (&pi, ret, 0);
420 pi.value.unindex.eta = GNUNET_TIME_UNIT_FOREVER_REL;
422 = h->upcb (h->upcb_cls,
424 GNUNET_CRYPTO_hash_file (h->sched,
425 GNUNET_SCHEDULER_PRIORITY_IDLE,
436 * Clean up after completion of an unindex operation.
441 GNUNET_FS_unindex_stop (struct GNUNET_FS_UnindexContext *uc)
443 struct GNUNET_FS_ProgressInfo pi;
445 if ( (uc->state != UNINDEX_STATE_COMPLETE) &&
446 (uc->state != UNINDEX_STATE_ERROR) )
448 uc->state = UNINDEX_STATE_ABORTED;
451 // FIXME: make unpersistent!
452 make_unindex_status (&pi, uc,
453 (uc->state == UNINDEX_STATE_COMPLETE)
454 ? uc->file_size : 0);
455 pi.status = GNUNET_FS_STATUS_UNINDEX_STOPPED;
456 pi.value.unindex.eta = GNUNET_TIME_UNIT_ZERO;
458 = uc->h->upcb (uc->h->upcb_cls,
460 GNUNET_break (NULL == uc->client_info);
461 GNUNET_free (uc->filename);
465 /* end of fs_unindex.c */