GNUnet  0.10.x
crypto_hash_file.c
Go to the documentation of this file.
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 it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 
20 */
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 
39 {
40 
45 
49  void *callback_cls;
50 
54  unsigned char *buffer;
55 
59  char *filename;
60 
65 
69  gcry_md_hd_t md;
70 
74  uint64_t fsize;
75 
79  uint64_t offset;
80 
85 
90 
94  size_t bsize;
95 
96 };
97 
98 
103 static void
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))
111  gcry_md_close (fhc->md);
112  GNUNET_free (fhc); /* also frees fhc->buffer */
113 }
114 
115 
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  ssize_t sret;
128 
129  fhc->task = NULL;
130  GNUNET_assert (fhc->offset <= fhc->fsize);
131  delta = fhc->bsize;
132  if (fhc->fsize - fhc->offset < delta)
133  delta = fhc->fsize - fhc->offset;
134  sret = GNUNET_DISK_file_read (fhc->fh,
135  fhc->buffer,
136  delta);
137  if ( (sret < 0) ||
138  (delta != (size_t) sret) )
139  {
141  "read",
142  fhc->filename);
143  file_hash_finish (fhc,
144  NULL);
145  return;
146  }
147  gcry_md_write (fhc->md,
148  fhc->buffer,
149  delta);
150  fhc->offset += delta;
151  if (fhc->offset == fhc->fsize)
152  {
153  res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md,
154  GCRY_MD_SHA512);
155  file_hash_finish (fhc, res);
156  return;
157  }
160  fhc);
161 }
162 
163 
176  const char *filename,
177  size_t blocksize,
179  void *callback_cls)
180 {
181  struct GNUNET_CRYPTO_FileHashContext *fhc;
182 
183  GNUNET_assert (blocksize > 0);
184  fhc =
185  GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
186  fhc->callback = callback;
187  fhc->callback_cls = callback_cls;
188  fhc->buffer = (unsigned char *) &fhc[1];
189  fhc->filename = GNUNET_strdup (filename);
190  if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
191  {
192  GNUNET_break (0);
193  GNUNET_free (fhc);
194  return NULL;
195  }
196  fhc->bsize = blocksize;
197  if (GNUNET_OK !=
198  GNUNET_DISK_file_size (filename,
199  &fhc->fsize,
200  GNUNET_NO,
201  GNUNET_YES))
202  {
203  GNUNET_free (fhc->filename);
204  GNUNET_free (fhc);
205  return NULL;
206  }
207  fhc->fh = GNUNET_DISK_file_open (filename,
210  if (! fhc->fh)
211  {
212  GNUNET_free (fhc->filename);
213  GNUNET_free (fhc);
214  return NULL;
215  }
216  fhc->priority = priority;
217  fhc->task = GNUNET_SCHEDULER_add_with_priority (priority,
219  fhc);
220  return fhc;
221 }
222 
223 
229 void
231 {
233  GNUNET_free (fhc->filename);
235  GNUNET_DISK_file_close (fhc->fh));
236  GNUNET_free (fhc);
237 }
238 
239 /* end of crypto_hash_file.c */
Open the file for reading.
struct GNUNET_SCHEDULER_Task * task
Current task for hashing.
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1817
struct GNUNET_DISK_FileHandle * fh
File descriptor.
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
void(* GNUNET_CRYPTO_HashCompletedCallback)(void *cls, const struct GNUNET_HashCode *res)
Function called once the hash computation over the specified file has completed.
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:881
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void file_hash_finish(struct GNUNET_CRYPTO_FileHashContext *fhc, const struct GNUNET_HashCode *res)
Report result of hash computation to callback and free associated resources.
Nobody is allowed to do anything to the file.
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
GNUNET_SCHEDULER_Priority
Valid task priorities.
struct GNUNET_CRYPTO_FileHashContext * GNUNET_CRYPTO_hash_file(enum GNUNET_SCHEDULER_Priority priority, const char *filename, size_t blocksize, GNUNET_CRYPTO_HashCompletedCallback callback, void *callback_cls)
Compute the hash of an entire file.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
gcry_md_hd_t md
Cummulated hash.
int GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
Checks whether a handle is invalid.
Definition: disk.c:191
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_with_priority(enum GNUNET_SCHEDULER_Priority prio, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified priority.
Definition: scheduler.c:1200
#define LOG_STRERROR_FILE(kind, syscall, filename)
unsigned char * buffer
IO buffer.
A 512-bit hashcode.
static int res
char * filename
Name of the file we are hashing.
uint64_t fsize
Size of the file.
Context used when hashing a file.
int GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:289
enum GNUNET_SCHEDULER_Priority priority
Priority we use.
Entry in list of pending tasks.
Definition: scheduler.c:134
GNUNET_CRYPTO_HashCompletedCallback callback
Function to call upon completion.
#define GNUNET_YES
Definition: gnunet_common.h:80
static void file_hash_task(void *cls)
File hashing task.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1673
Handle used to access files (and pipes).
uint64_t offset
Current offset.
#define GNUNET_malloc(size)
Wrapper around malloc.
void * callback_cls
Closure for callback.
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_CRYPTO_hash_file_cancel(struct GNUNET_CRYPTO_FileHashContext *fhc)
Cancel a file hashing operation.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965