libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit 0041f9888bc1f4fc797f7d9cb749cd161f19d8a3
parent 0b0759e1f402682a269c94c928237ee5984206ca
Author: lash <dev@holbrook.no>
Date:   Wed, 26 Mar 2025 03:08:51 +0000

Add fs entry counter

Diffstat:
Msrc/io/Makefile | 3+++
Msrc/io/std.c | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/lq/crypto.h | 2+-
Msrc/lq/io.h | 6++++++
Msrc/lq/mem.h | 11++++++++++-
Msrc/lq/store.h | 1+
Msrc/mem/std.c | 4++++
Msrc/store/file.c | 17+++++++++++++++++
Msrc/test/Makefile | 29+++++++++++++++++------------
Msrc/test/test_cert.c | 3+++
Asrc/test/test_store.c | 50++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/test/testdata/0aaa | 0
Asrc/test/testdata/0ab | 0
Asrc/test/testdata/1aaa | 0
Asrc/test/testdata/1aab | 0
Asrc/test/testdata/1b | 0
Asrc/test/testdata/1bbc | 0
Asrc/test/testdata/2ab | 0
18 files changed, 189 insertions(+), 14 deletions(-)

diff --git a/src/io/Makefile b/src/io/Makefile @@ -1,3 +1,6 @@ +INCLUDES := -I.. -I../aux/include +CFLAGS += $(INCLUDES) -Wall + std: $(CC) $(CFLAGS) -g3 -c std.c diff --git a/src/io/std.c b/src/io/std.c @@ -2,6 +2,11 @@ #include <stdlib.h> #include <fcntl.h> #include <unistd.h> +#include <dirent.h> +#include <string.h> + +#include "lq/mem.h" + char *mktempdir(char *s) { return mkdtemp(s); @@ -18,3 +23,75 @@ int lq_read(int f, void *buf, size_t c) { void lq_close(int fd) { close(fd); } + +static int fltr_files(const struct dirent *d) { + int r; + if (*((char*)d->d_name) == '.') { + return 0; + } + return (d->d_type & (DT_LNK | DT_REG)) > 0 ? 1 : 0; + return r; +} + +/** + * \todo scandir calls malloc, so lq_alloc needs malloc alias that maps to it. + */ +int lq_files(const char *path, char **files, size_t files_len) { + int r; + int i; + struct dirent **ls; + + r = 0; + r = scandir(path, &ls, fltr_files, alphasort); + if (r < 0) { + return -1; + } + if (r > files_len + 1) { + return -2; + } + for (i = 0; i < r; i++) { + *(files+i) = (*(ls+i))->d_name; + } + *(files+i+1) = NULL; + lq_free(ls); +} + +int lq_files_pfx(const char *path, char **files, size_t files_len, const char *prefix, char prefix_len) { + int r; + int i; + int c; + size_t l; + + c = 0; + r = lq_files(path, files, files_len); + for (i = 0; i < r; i++) { + l = strlen(*(files+i)); + if (l < prefix_len) { + lq_free(*(files+i)); + } + if (!lq_cmp(prefix, *(files+i), prefix_len)) { + *(files+c) = *(files+i); + c++; + } + *(files+i) = NULL; + } + for (i = c + 1; i < r; i++) { + if (*(files+i) != NULL) { + lq_free(*(files+i)); + *(files+i) = NULL; + } + } + return c; +} + +void lq_files_free(char **files) { + int i; + + i = 0; + do { + if (*(files+i) == NULL) { + break; + } + lq_free(*(files+i)); + } while(++i); +} diff --git a/src/lq/crypto.h b/src/lq/crypto.h @@ -8,7 +8,7 @@ #endif #ifndef LQ_PUBKEY_LEN -#define LQ_PUBKEY_LEN 65 +#define LQ_PUBKEY_LEN 64 #endif #ifndef LQ_PRIVKEY_LEN diff --git a/src/lq/io.h b/src/lq/io.h @@ -5,6 +5,10 @@ #define LQ_PATH_MAX 1024 #endif +#ifndef LQ_DIRS_MAX +#define LQ_DIRS_MAX 1024 +#endif + /** * @brief Create temporary directory using template. * @@ -14,6 +18,8 @@ char* mktempdir(char *s); int lq_open(const char *pathname, int flags, int mode); int lq_read(int f, char *buf, int c); +int lq_files(const char *path, char **files, size_t files_len); +int lq_files_pfx(const char *path, char **files, size_t files_len, const char *prefix, size_t prefix_len); void lq_close(int fd); #endif // LIBQAEDA_IO_H_ diff --git a/src/lq/mem.h b/src/lq/mem.h @@ -47,7 +47,6 @@ void* lq_cpy(void *dst, const void *src, size_t len); void* lq_set(void *dst, const char b, size_t len); /** - * * @brief Fill memory region zeros. * * @param[out] Destination memory. @@ -56,4 +55,14 @@ void* lq_set(void *dst, const char b, size_t len); */ void* lq_zero(void *dst, size_t len); +/** + * @brief Compare two memory regions + * + * @param[in] First memory region to compare. + * @param[in] Second memory region to compare. + * @param[in] Size of memory region to compare, in bytes. + * @return 0 if identical, -1 if dst < src + */ +int lq_cmp(const void *dst, const void *src, size_t len); + #endif // LIBQAEDA_MEM_H_ diff --git a/src/lq/store.h b/src/lq/store.h @@ -25,6 +25,7 @@ struct lq_store_t { void *userdata; ///< Implementation specific data required by the specific store. int (*get)(enum payload_e typ, LQStore *store, const char *key, size_t key_len, char *value, size_t *value_len); ///< Interface for retrieving data corresponding to a key. int (*put)(enum payload_e typ, LQStore *store, const char *key, size_t *key_len, char *value, size_t value_len); ///< Interface for storing data corresponding to a key. + int (*count)(enum payload_e typ, LQStore *store, const char *key, size_t key_len); ///< Interface for returning number of entries under a key. void (*free)(LQStore *store); ///< Interface for cleaning up implementation specific resources in use by the store. }; diff --git a/src/mem/std.c b/src/mem/std.c @@ -10,6 +10,10 @@ void lq_free(void *o) { free(o); } +int lq_cmp(const void *dst, const void *src, size_t len) { + return memcmp(dst, src, len); +} + void* lq_cpy(void *dst, const void *src, size_t len) { return memcpy(dst, src, len); } diff --git a/src/store/file.c b/src/store/file.c @@ -13,6 +13,22 @@ static const int store_typ_file = 3; +int lq_file_content_count(enum payload_e typ, LQStore *store, const char *key, size_t key_len) { + int r; + char **out; + char pfx[1024]; + + out = lq_alloc(sizeof(char**) * LQ_DIRS_MAX); + pfx[0] = (char)typ + 0x30; + lq_cpy(pfx+1, key, key_len); + + r = lq_files_pfx(store->userdata, out, LQ_DIRS_MAX, pfx, key_len + 1); + + lq_free(out); + + return r; +} + int lq_file_content_get(enum payload_e typ, LQStore *store, const char *key, size_t key_len, char *value, size_t *value_len) { int f; int r; @@ -113,6 +129,7 @@ struct lq_store_t LQFileContent = { .userdata = "", .get = lq_file_content_get, .put = lq_file_content_put, + .count = lq_file_content_count, .free = lq_file_content_free, }; diff --git a/src/test/Makefile b/src/test/Makefile @@ -1,28 +1,33 @@ OBJS := $(patsubst %.c,%.o,$(wildcard *.c)) -INCLUDES := -I.. -I../aux/include +INCLUDES := -I.. -I../aux/include `pkg-config --cflags libtasn1 libgcrypt` CFLAGS += $(INCLUDES) -Wall -g3 -LIBS := ../asn1/defs_asn1_tab.o `pkg-config --libs libtasn1` -L../aux/lib -llash +LIBS := ../asn1/defs_asn1_tab.o `pkg-config --libs libtasn1 libgcrypt` -L../aux/lib -llash #LDFLAGS := -lcheck -lsubunit -lm $(LIBS) LDFLAGS := -lcheck $(LIBS) all: build - CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_debug_bin - CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_config_bin - CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_crypto_bin - CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_msg_bin - CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_cert_bin - CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_trust_bin +# CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_debug_bin +# CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_config_bin +# CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_crypto_bin +# CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_msg_bin +# CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_cert_bin +# CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_trust_bin + CK_FORK=no LD_LIBRARY_PATH=`realpath ../aux/lib` ./test_store_bin test: all build: $(CC) $(CFLAGS) test_debug.c -o test_debug_bin ../debug.o $(LDFLAGS) $(CC) $(CFLAGS) test_config.c -o test_config_bin ../lq/config.o ../mem/std.o $(LDFLAGS) - #$(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin ../crypto/dummy.o ../mem/std.o $(LDFLAGS) - $(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin ../crypto/gcrypt.o ../debug.o ../mem/std.o ../lq/config.o $(LDFLAGS) -lgcrypt - $(CC) $(CFLAGS) test_msg.c -o test_msg_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o $(LDFLAGS) - $(CC) $(CFLAGS) test_cert.c -o test_cert_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o ../lq/cert.o $(LDFLAGS) +# $(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin ../crypto/dummy.o ../mem/std.o $(LDFLAGS) +# $(CC) $(CFLAGS) test_msg.c -o test_msg_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o $(LDFLAGS) + #$(CC) $(CFLAGS) test_cert.c -o test_cert_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o ../lq/cert.o $(LDFLAGS) $(CC) $(CFLAGS) test_trust.c -o test_trust_bin ../crypto/dummy.o ../mem/std.o ../store/mem.o ../lq/trust.o -lhashmap $(LDFLAGS) + $(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin ../crypto/gcrypt.o ../debug.o ../mem/std.o ../lq/config.o $(LDFLAGS) -lgcrypt + $(CC) $(CFLAGS) test_msg.c -o test_msg_bin ../crypto/gcrypt.o ../debug.o ../mem/std.o ../lq/config.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o $(LDFLAGS) + $(CC) $(CFLAGS) test_cert.c -o test_cert_bin ../crypto/gcrypt.o ../debug.o ../lq/config.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o ../lq/cert.o $(LDFLAGS) + $(CC) $(CFLAGS) test_trust.c -o test_trust_bin ../crypto/gcrypt.o ../debug.o ../lq/config.o ../mem/std.o ../store/mem.o ../lq/trust.o -lhashmap $(LDFLAGS) + $(CC) $(CFLAGS) test_store.c -o test_store_bin ../crypto/gcrypt.o ../debug.o ../lq/config.o ../mem/std.o ../store/file.o ../io/std.o -lhashmap $(LDFLAGS) clean: rm -vf test_*_bin diff --git a/src/test/test_cert.c b/src/test/test_cert.c @@ -34,6 +34,7 @@ START_TEST(check_cert_symmetric_nomsg) { LQCtx ctx; char buf[4096]; + lq_crypto_init(); lq_set(&ctx, 0, sizeof(LQCtx)); cert = lq_certificate_new(NULL, &ctx, NULL, NULL); c = 4096; @@ -78,6 +79,7 @@ START_TEST(check_cert_symmetric_req_sig) { LQCtx ctx; char buf[4096]; + lq_crypto_init(); pk = lq_privatekey_new(privkeydata, 32, passphrase, 32); lq_set(&ctx, 0, sizeof(LQCtx)); req = lq_msg_new(data, strlen(data) + 1); @@ -107,6 +109,7 @@ START_TEST(check_cert_symmetric_rsp_onesig) { LQCtx ctx; char buf[4096]; + lq_crypto_init(); pk = lq_privatekey_new(privkeydata, 32, passphrase, 32); lq_set(&ctx, 0, sizeof(LQCtx)); req = lq_msg_new(data, strlen(data) + 1); diff --git a/src/test/test_store.c b/src/test/test_store.c @@ -0,0 +1,50 @@ +#include <check.h> +#include <stdlib.h> +#include <string.h> + +#include <lq/store.h> +#include <lq/mem.h> + + +extern LQStore LQFileContent; + +START_TEST(check_store_count) { + int r; + LQStore store; + + lq_cpy(&store, &LQFileContent, sizeof(LQStore)); + store.userdata = "./testdata"; + + r = store.count(LQ_CONTENT_MSG, &store, "aa", 2); + + ck_assert_int_eq(r, 2); +} +END_TEST + +Suite * common_suite(void) { + Suite *s; + TCase *tc; + + s = suite_create("store"); + tc = tcase_create("files"); + tcase_add_test(tc, check_store_count); + suite_add_tcase(s, tc); + + return s; +} + +int main(void) { + int n_fail; + + Suite *s; + SRunner *sr; + + s = common_suite(); + sr = srunner_create(s); + + srunner_run_all(sr, CK_VERBOSE); + n_fail = srunner_ntests_failed(sr); + srunner_free(sr); + + return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/src/test/testdata/0aaa b/src/test/testdata/0aaa diff --git a/src/test/testdata/0ab b/src/test/testdata/0ab diff --git a/src/test/testdata/1aaa b/src/test/testdata/1aaa diff --git a/src/test/testdata/1aab b/src/test/testdata/1aab diff --git a/src/test/testdata/1b b/src/test/testdata/1b diff --git a/src/test/testdata/1bbc b/src/test/testdata/1bbc diff --git a/src/test/testdata/2ab b/src/test/testdata/2ab