commit ceb0eb3392cb2c1c426f1747b02c140901541e54
parent e48de796ff6ede0b2cfd7751fb69d22dea226f54
Author: nolash <dev@holbrook.no>
Date: Tue, 14 Sep 2021 22:36:36 +0200
Add static callback field
Diffstat:
3 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/src/swarmfile.c b/src/swarmfile.c
@@ -5,7 +5,7 @@
static inline void filehash_callback(filehash_t *fctx, const char *hash, const char *data, const size_t data_length) {
if (fctx->callback != NULL) {
- (*fctx->callback)(hash, data, data_length);
+ (*fctx->callback)(hash, data, data_length, fctx->callback_static);
}
}
@@ -70,7 +70,7 @@ bmt_spansize_t filehash_sum(filehash_t *fctx) {
}
-void filehash_init_callback(filehash_t *fctx, void *callback) {
+void filehash_init_callback(filehash_t *fctx, void (*callback)(const char*, const char*, const size_t, void*), void *callback_static) {
int i;
int l;
@@ -82,12 +82,13 @@ void filehash_init_callback(filehash_t *fctx, void *callback) {
}
fctx->target = fctx->buf + _SWARM_WORD_SIZE;
fctx->callback = callback;
+ fctx->callback_static = callback_static;
filehash_reset(fctx);
}
void filehash_init(filehash_t *fctx) {
- return filehash_init_callback(fctx, NULL);
+ return filehash_init_callback(fctx, NULL, NULL);
}
diff --git a/src/swarmfile.h b/src/swarmfile.h
@@ -13,13 +13,14 @@ typedef struct filehash {
long long writes[SWARM_LEVELS];
long long spans[SWARM_LEVELS];
long long length;
- void (*callback)(const char*, const char*, const size_t);
+ void (*callback)(const char*, const char*, const size_t, void*);
+ void *callback_static;
bmt_t bmt_context;
} filehash_t;
void filehash_reset(filehash_t *filehash_context);
void filehash_init(filehash_t *filehash_context);
-void filehash_init_callback(filehash_t *filehash_context, void *callback);
+void filehash_init_callback(filehash_t *filehash_context, void (*callback)(const char*, const char*, const size_t, void*), void *callback_static);
int filehash_write(filehash_t *filehash_context, const char *data, const size_t data_length);
bmt_spansize_t filehash_sum(filehash_t *filehash_content);
diff --git a/test/check_file.c b/test/check_file.c
@@ -11,10 +11,11 @@
#include "common.h"
+const char *callback_static_foo = "foo";
int file_callback_serial = 0;
int file_callback_dir_fd = -1;
-void file_callback(const char *hash, const char *data, const size_t data_length) {
+void file_callback(const char *hash, const char *data, const size_t data_length, void *callback_static) {
int fd;
char filename[32];
@@ -30,6 +31,7 @@ void file_callback(const char *hash, const char *data, const size_t data_length)
write(fd, hash, _SWARM_WORD_SIZE);
close(fd);
+ ck_assert_str_eq((char*)callback_static, callback_static_foo);
file_callback_serial++;
}
@@ -105,7 +107,7 @@ START_TEST(check_file_callback_data) {
file_callback_serial = 0;
file_callback_dir_fd = open(cbdir, O_DIRECTORY | O_SYNC);
- filehash_init_callback(&fh, file_callback);
+ filehash_init_callback(&fh, file_callback, "foo");
r = filehash_write(&fh, data, strlen(data));
ck_assert_int_eq(r, 3);
@@ -135,7 +137,7 @@ START_TEST(check_file_callback_intermediate) {
file_callback_dir_fd = open(cbdir, O_DIRECTORY | O_SYNC);
- filehash_init_callback(&fh, file_callback);
+ filehash_init_callback(&fh, file_callback, callback_static_foo);
bg.v = 0;
bg.m = 255;