2 This file is part of GNUnet.
3 Copyright (C) 2001-2013 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 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
23 * @file util/crypto_hash_file.c
24 * @brief incremental hashing of files
25 * @author Christian Grothoff
30 * Context used when hashing a file.
32 struct GNUNET_CRYPTO_FileHashContext
36 * Function to call upon completion.
38 GNUNET_CRYPTO_HashCompletedCallback callback;
41 * Closure for callback.
48 unsigned char *buffer;
51 * Name of the file we are hashing.
58 struct GNUNET_DISK_FileHandle *fh;
76 * Current task for hashing.
78 struct GNUNET_SCHEDULER_Task * task;
83 enum GNUNET_SCHEDULER_Priority priority;
94 * Report result of hash computation to callback
95 * and free associated resources.
98 file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
99 const struct GNUNET_HashCode * res)
101 fhc->callback (fhc->callback_cls, res);
102 GNUNET_free (fhc->filename);
103 if (!GNUNET_DISK_handle_invalid (fhc->fh))
104 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
105 gcry_md_close (fhc->md);
106 GNUNET_free (fhc); /* also frees fhc->buffer */
117 file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
119 struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
120 struct GNUNET_HashCode *res;
124 GNUNET_assert (fhc->offset <= fhc->fsize);
126 if (fhc->fsize - fhc->offset < delta)
127 delta = fhc->fsize - fhc->offset;
128 if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
130 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
131 file_hash_finish (fhc, NULL);
134 gcry_md_write (fhc->md, fhc->buffer, delta);
135 fhc->offset += delta;
136 if (fhc->offset == fhc->fsize)
138 res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512);
139 file_hash_finish (fhc, res);
142 fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
143 &file_hash_task, fhc);
148 * Compute the hash of an entire file.
150 * @param priority scheduling priority to use
151 * @param filename name of file to hash
152 * @param blocksize number of bytes to process in one task
153 * @param callback function to call upon completion
154 * @param callback_cls closure for callback
155 * @return NULL on (immediate) errror
157 struct GNUNET_CRYPTO_FileHashContext *
158 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
159 const char *filename, size_t blocksize,
160 GNUNET_CRYPTO_HashCompletedCallback callback,
163 struct GNUNET_CRYPTO_FileHashContext *fhc;
165 GNUNET_assert (blocksize > 0);
167 GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
168 fhc->callback = callback;
169 fhc->callback_cls = callback_cls;
170 fhc->buffer = (unsigned char *) &fhc[1];
171 fhc->filename = GNUNET_strdup (filename);
172 if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
178 fhc->bsize = blocksize;
179 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO, GNUNET_YES))
181 GNUNET_free (fhc->filename);
186 GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
187 GNUNET_DISK_PERM_NONE);
190 GNUNET_free (fhc->filename);
194 fhc->priority = priority;
196 GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
202 * Cancel a file hashing operation.
204 * @param fhc operation to cancel (callback must not yet have been invoked)
207 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
209 GNUNET_SCHEDULER_cancel (fhc->task);
210 GNUNET_free (fhc->filename);
211 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
215 /* end of crypto_hash_file.c */