Merge branch 'getopt'
[oweals/gnunet.git] / src / util / crypto_hash_file.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2001-2013 GNUnet e.V.
4
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.
9
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.
14
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.
19
20 */
21 /**
22  * @file util/crypto_hash_file.c
23  * @brief incremental hashing of files
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include <gcrypt.h>
29
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-hash-file", __VA_ARGS__)
31
32 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-hash-file", syscall, filename)
33
34
35 /**
36  * Context used when hashing a file.
37  */
38 struct GNUNET_CRYPTO_FileHashContext
39 {
40
41   /**
42    * Function to call upon completion.
43    */
44   GNUNET_CRYPTO_HashCompletedCallback callback;
45
46   /**
47    * Closure for callback.
48    */
49   void *callback_cls;
50
51   /**
52    * IO buffer.
53    */
54   unsigned char *buffer;
55
56   /**
57    * Name of the file we are hashing.
58    */
59   char *filename;
60
61   /**
62    * File descriptor.
63    */
64   struct GNUNET_DISK_FileHandle *fh;
65
66   /**
67    * Cummulated hash.
68    */
69   gcry_md_hd_t md;
70
71   /**
72    * Size of the file.
73    */
74   uint64_t fsize;
75
76   /**
77    * Current offset.
78    */
79   uint64_t offset;
80
81   /**
82    * Current task for hashing.
83    */
84   struct GNUNET_SCHEDULER_Task * task;
85
86   /**
87    * Priority we use.
88    */
89   enum GNUNET_SCHEDULER_Priority priority;
90
91   /**
92    * Blocksize.
93    */
94   size_t bsize;
95
96 };
97
98
99 /**
100  * Report result of hash computation to callback
101  * and free associated resources.
102  */
103 static void
104 file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
105                   const struct GNUNET_HashCode * res)
106 {
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 */
113 }
114
115
116 /**
117  * File hashing task.
118  *
119  * @param cls closure
120  */
121 static void
122 file_hash_task (void *cls)
123 {
124   struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
125   struct GNUNET_HashCode *res;
126   size_t delta;
127
128   fhc->task = NULL;
129   GNUNET_assert (fhc->offset <= fhc->fsize);
130   delta = fhc->bsize;
131   if (fhc->fsize - fhc->offset < delta)
132     delta = fhc->fsize - fhc->offset;
133   if (delta != GNUNET_DISK_file_read (fhc->fh,
134                                       fhc->buffer,
135                                       delta))
136   {
137     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
138                        "read",
139                        fhc->filename);
140     file_hash_finish (fhc, NULL);
141     return;
142   }
143   gcry_md_write (fhc->md, fhc->buffer, delta);
144   fhc->offset += delta;
145   if (fhc->offset == fhc->fsize)
146   {
147     res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md,
148                                                    GCRY_MD_SHA512);
149     file_hash_finish (fhc, res);
150     return;
151   }
152   fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
153                                                   &file_hash_task,
154                                                   fhc);
155 }
156
157
158 /**
159  * Compute the hash of an entire file.
160  *
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
167  */
168 struct GNUNET_CRYPTO_FileHashContext *
169 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
170                          const char *filename,
171                          size_t blocksize,
172                          GNUNET_CRYPTO_HashCompletedCallback callback,
173                          void *callback_cls)
174 {
175   struct GNUNET_CRYPTO_FileHashContext *fhc;
176
177   GNUNET_assert (blocksize > 0);
178   fhc =
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))
185   {
186     GNUNET_break (0);
187     GNUNET_free (fhc);
188     return NULL;
189   }
190   fhc->bsize = blocksize;
191   if (GNUNET_OK !=
192       GNUNET_DISK_file_size (filename,
193                              &fhc->fsize,
194                              GNUNET_NO,
195                              GNUNET_YES))
196   {
197     GNUNET_free (fhc->filename);
198     GNUNET_free (fhc);
199     return NULL;
200   }
201   fhc->fh = GNUNET_DISK_file_open (filename,
202                                    GNUNET_DISK_OPEN_READ,
203                                    GNUNET_DISK_PERM_NONE);
204   if (! fhc->fh)
205   {
206     GNUNET_free (fhc->filename);
207     GNUNET_free (fhc);
208     return NULL;
209   }
210   fhc->priority = priority;
211   fhc->task = GNUNET_SCHEDULER_add_with_priority (priority,
212                                                   &file_hash_task,
213                                                   fhc);
214   return fhc;
215 }
216
217
218 /**
219  * Cancel a file hashing operation.
220  *
221  * @param fhc operation to cancel (callback must not yet have been invoked)
222  */
223 void
224 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
225 {
226   GNUNET_SCHEDULER_cancel (fhc->task);
227   GNUNET_free (fhc->filename);
228   GNUNET_break (GNUNET_OK ==
229                 GNUNET_DISK_file_close (fhc->fh));
230   GNUNET_free (fhc);
231 }
232
233 /* end of crypto_hash_file.c */