GNUnet  0.10.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...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util-crypto-hash-file", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)   GNUNET_log_from_strerror_file (kind, "util-crypto-hash-file", syscall, filename)
 

Functions

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

Author
Christian Grothoff

Definition in file crypto_hash_file.c.

Macro Definition Documentation

◆ LOG

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

Definition at line 30 of file crypto_hash_file.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)    GNUNET_log_from_strerror_file (kind, "util-crypto-hash-file", syscall, filename)

Definition at line 32 of file crypto_hash_file.c.

Referenced by file_hash_task().

Function Documentation

◆ file_hash_finish()

static void file_hash_finish ( struct GNUNET_CRYPTO_FileHashContext fhc,
const struct GNUNET_HashCode res 
)
static

Report result of hash computation to callback and free associated resources.

Definition at line 104 of file crypto_hash_file.c.

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, and GNUNET_CRYPTO_FileHashContext::md.

Referenced by file_hash_task().

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 }
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.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
gcry_md_hd_t md
Cummulated hash.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
int GNUNET_DISK_handle_invalid(const struct GNUNET_DISK_FileHandle *h)
Checks whether a handle is invalid.
Definition: disk.c:191
char * filename
Name of the file we are hashing.
GNUNET_CRYPTO_HashCompletedCallback callback
Function to call upon completion.
void * callback_cls
Closure for callback.
#define GNUNET_free(ptr)
Wrapper around free.
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)
static

File hashing task.

Parameters
clsclosure

Definition at line 122 of file crypto_hash_file.c.

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().

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 }
struct GNUNET_SCHEDULER_Task * task
Current task for hashing.
struct GNUNET_DISK_FileHandle * fh
File descriptor.
static struct GNUNET_TIME_Relative delta
Definition: speedup.c:35
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.
gcry_md_hd_t md
Cummulated hash.
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.
enum GNUNET_SCHEDULER_Priority priority
Priority we use.
static void file_hash_task(void *cls)
File hashing task.
uint64_t offset
Current offset.
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.

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

Definition at line 230 of file crypto_hash_file.c.

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().

231 {
233  GNUNET_free (fhc->filename);
235  GNUNET_DISK_file_close (fhc->fh));
236  GNUNET_free (fhc);
237 }
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.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
char * filename
Name of the file we are hashing.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965
Here is the call graph for this function:
Here is the caller graph for this function: