2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/crypto_hash_file.c
23 * @brief incremental hashing of files
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-hash-file", __VA_ARGS__)
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-hash-file", syscall, filename)
36 * Context used when hashing a file.
38 struct GNUNET_CRYPTO_FileHashContext
42 * Function to call upon completion.
44 GNUNET_CRYPTO_HashCompletedCallback callback;
47 * Closure for callback.
54 unsigned char *buffer;
57 * Name of the file we are hashing.
64 struct GNUNET_DISK_FileHandle *fh;
82 * Current task for hashing.
84 struct GNUNET_SCHEDULER_Task * task;
89 enum GNUNET_SCHEDULER_Priority priority;
100 * Report result of hash computation to callback
101 * and free associated resources.
104 file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
105 const struct GNUNET_HashCode * res)
107 fhc->callback (fhc->callback_cls, res);
108 GNUNET_free (fhc->filename);
109 if (!GNUNET_DISK_handle_invalid (fhc->fh))
110 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
111 gcry_md_close (fhc->md);
112 GNUNET_free (fhc); /* also frees fhc->buffer */
122 file_hash_task (void *cls)
124 struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
125 struct GNUNET_HashCode *res;
129 GNUNET_assert (fhc->offset <= fhc->fsize);
131 if (fhc->fsize - fhc->offset < delta)
132 delta = fhc->fsize - fhc->offset;
133 if (delta != GNUNET_DISK_file_read (fhc->fh,
137 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
140 file_hash_finish (fhc, NULL);
143 gcry_md_write (fhc->md, fhc->buffer, delta);
144 fhc->offset += delta;
145 if (fhc->offset == fhc->fsize)
147 res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md,
149 file_hash_finish (fhc, res);
152 fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
159 * Compute the hash of an entire file.
161 * @param priority scheduling priority to use
162 * @param filename name of file to hash
163 * @param blocksize number of bytes to process in one task
164 * @param callback function to call upon completion
165 * @param callback_cls closure for @a callback
166 * @return NULL on (immediate) errror
168 struct GNUNET_CRYPTO_FileHashContext *
169 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
170 const char *filename,
172 GNUNET_CRYPTO_HashCompletedCallback callback,
175 struct GNUNET_CRYPTO_FileHashContext *fhc;
177 GNUNET_assert (blocksize > 0);
179 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
180 fhc->callback = callback;
181 fhc->callback_cls = callback_cls;
182 fhc->buffer = (unsigned char *) &fhc[1];
183 fhc->filename = GNUNET_strdup (filename);
184 if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
190 fhc->bsize = blocksize;
192 GNUNET_DISK_file_size (filename,
197 GNUNET_free (fhc->filename);
201 fhc->fh = GNUNET_DISK_file_open (filename,
202 GNUNET_DISK_OPEN_READ,
203 GNUNET_DISK_PERM_NONE);
206 GNUNET_free (fhc->filename);
210 fhc->priority = priority;
211 fhc->task = GNUNET_SCHEDULER_add_with_priority (priority,
219 * Cancel a file hashing operation.
221 * @param fhc operation to cancel (callback must not yet have been invoked)
224 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
226 GNUNET_SCHEDULER_cancel (fhc->task);
227 GNUNET_free (fhc->filename);
228 GNUNET_break (GNUNET_OK ==
229 GNUNET_DISK_file_close (fhc->fh));
233 /* end of crypto_hash_file.c */