GNUnet  0.11.x
Data Structures | Macros | Functions
crypto_hash_file.c File Reference

incremental hashing of files More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include <gcrypt.h>
Include dependency graph for crypto_hash_file.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_CRYPTO_FileHashContext
 Context used when hashing a file. More...


#define LOG(kind, ...)
#define LOG_STRERROR_FILE(kind, syscall, filename)


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. More...
static void file_hash_task (void *cls)
 File hashing task. More...
struct GNUNET_CRYPTO_FileHashContextGNUNET_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. More...
void GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
 Cancel a file hashing operation. More...

Detailed Description

incremental hashing of files

Christian Grothoff

Definition in file crypto_hash_file.c.

Macro Definition Documentation


#define LOG (   kind,
GNUNET_log_from (kind, "util-crypto-hash-file", \
#define GNUNET_log_from(kind, comp,...)

Definition at line 30 of file crypto_hash_file.c.


#define LOG_STRERROR_FILE (   kind,
"util-crypto-hash-file", \
syscall, \
static char * filename
#define GNUNET_log_from_strerror_file(level, component, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...

Definition at line 33 of file crypto_hash_file.c.

Function Documentation

◆ file_hash_finish()

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.

Definition at line 107 of file crypto_hash_file.c.

109 {
110  fhc->callback (fhc->callback_cls, res);
111  GNUNET_free (fhc->filename);
112  if (! GNUNET_DISK_handle_invalid (fhc->fh))
114  gcry_md_close (fhc->md);
115  GNUNET_free (fhc); /* also frees fhc->buffer */
116 }
static int res
Definition: gnunet_common.h:95
enum GNUNET_GenericReturnValue GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
Checks whether a handle is invalid.
Definition: disk.c:186
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1306
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_free(ptr)
Wrapper around free.
gcry_md_hd_t md
Cummulated hash.
GNUNET_CRYPTO_HashCompletedCallback callback
Function to call upon completion.
struct GNUNET_DISK_FileHandle * fh
File descriptor.
void * callback_cls
Closure for callback.
char * filename
Name of the file we are hashing.

References GNUNET_CRYPTO_FileHashContext::callback, GNUNET_CRYPTO_FileHashContext::callback_cls, GNUNET_CRYPTO_FileHashContext::fh, GNUNET_CRYPTO_FileHashContext::filename, GNUNET_break, GNUNET_DISK_file_close(), GNUNET_DISK_handle_invalid(), GNUNET_free, GNUNET_OK, GNUNET_CRYPTO_FileHashContext::md, and res.

Referenced by file_hash_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ file_hash_task()

static void file_hash_task ( void *  cls)

File hashing task.


Definition at line 125 of file crypto_hash_file.c.

126 {
127  struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
128  struct GNUNET_HashCode *res;
129  size_t delta;
130  ssize_t sret;
132  fhc->task = NULL;
133  GNUNET_assert (fhc->offset <= fhc->fsize);
134  delta = fhc->bsize;
135  if (fhc->fsize - fhc->offset < delta)
136  delta = fhc->fsize - fhc->offset;
137  sret = GNUNET_DISK_file_read (fhc->fh,
138  fhc->buffer,
139  delta);
140  if ((sret < 0) ||
141  (delta != (size_t) sret))
142  {
144  "read",
145  fhc->filename);
146  file_hash_finish (fhc,
147  NULL);
148  return;
149  }
150  gcry_md_write (fhc->md,
151  fhc->buffer,
152  delta);
153  fhc->offset += delta;
154  if (fhc->offset == fhc->fsize)
155  {
156  res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md,
157  GCRY_MD_SHA512);
158  file_hash_finish (fhc, res);
159  return;
160  }
163  fhc);
164 }
static void file_hash_task(void *cls)
File hashing task.
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.
#define LOG_STRERROR_FILE(kind, syscall, filename)
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:622
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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:1223
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
Context used when hashing a file.
uint64_t fsize
Size of the file.
unsigned char * buffer
IO buffer.
uint64_t offset
Current offset.
struct GNUNET_SCHEDULER_Task * task
Current task for hashing.
enum GNUNET_SCHEDULER_Priority priority
Priority we use.
A 512-bit hashcode.

References GNUNET_CRYPTO_FileHashContext::bsize, GNUNET_CRYPTO_FileHashContext::buffer, delta, GNUNET_CRYPTO_FileHashContext::fh, file_hash_finish(), GNUNET_CRYPTO_FileHashContext::filename, GNUNET_CRYPTO_FileHashContext::fsize, GNUNET_assert, GNUNET_DISK_file_read(), GNUNET_ERROR_TYPE_WARNING, GNUNET_SCHEDULER_add_with_priority(), LOG_STRERROR_FILE, GNUNET_CRYPTO_FileHashContext::md, GNUNET_CRYPTO_FileHashContext::offset, GNUNET_CRYPTO_FileHashContext::priority, res, and GNUNET_CRYPTO_FileHashContext::task.

Referenced by GNUNET_CRYPTO_hash_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_CRYPTO_hash_file_cancel()

void GNUNET_CRYPTO_hash_file_cancel ( struct GNUNET_CRYPTO_FileHashContext fhc)

Cancel a file hashing operation.

fhcoperation to cancel (callback must not yet have been invoked)

Definition at line 233 of file crypto_hash_file.c.

234 {
236  GNUNET_free (fhc->filename);
238  GNUNET_DISK_file_close (fhc->fh));
239  GNUNET_free (fhc);
240 }
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:972

References GNUNET_CRYPTO_FileHashContext::fh, GNUNET_CRYPTO_FileHashContext::filename, GNUNET_break, GNUNET_DISK_file_close(), GNUNET_free, GNUNET_OK, GNUNET_SCHEDULER_cancel(), and GNUNET_CRYPTO_FileHashContext::task.

Referenced by client_disconnect_cb(), GNUNET_FS_indexing_done(), GNUNET_FS_unindex_signal_suspend_(), GNUNET_FS_unindex_stop(), and publish_cleanup().

Here is the call graph for this function:
Here is the caller graph for this function: