diff options
author | kaotisk <kaotisk@arching-kaos.org> | 2025-04-28 06:10:28 +0300 |
---|---|---|
committer | kaotisk <kaotisk@arching-kaos.org> | 2025-04-28 06:10:28 +0300 |
commit | f8cdf68c9d1e561057b1f17278a6e182dae9e01c (patch) | |
tree | 9a562ab0f8031271c5a3fa52a74ce67137472c5f | |
parent | 97a197df7d29496d1a5de5499d4f3d15edfa8ad8 (diff) | |
download | arching-kaos-tools-f8cdf68c9d1e561057b1f17278a6e182dae9e01c.tar.gz arching-kaos-tools-f8cdf68c9d1e561057b1f17278a6e182dae9e01c.tar.bz2 arching-kaos-tools-f8cdf68c9d1e561057b1f17278a6e182dae9e01c.zip |
[libakfs] updatesHEADorigin/masterorigin/HEADmaster
-rw-r--r-- | c_implementation/include/libakfs.h | 10 | ||||
-rw-r--r-- | c_implementation/src/ak_fs.c | 60 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_map_v3.c | 28 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_maps_v3.c | 22 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_sha512sum.c | 5 | ||||
-rw-r--r-- | c_implementation/tests/test_akfs.c | 35 |
6 files changed, 122 insertions, 38 deletions
diff --git a/c_implementation/include/libakfs.h b/c_implementation/include/libakfs.h index 455baa7..80fea58 100644 --- a/c_implementation/include/libakfs.h +++ b/c_implementation/include/libakfs.h @@ -131,7 +131,7 @@ char* ak_fs_return_hash_dir(const char*); * param char* string to be checked * returns boolean */ -bool ak_fs_verify_input_is_hash(const char*); +bool ak_fs_verify_input_is_hash(const char*, size_t); /** * Unused @@ -293,11 +293,13 @@ sha512sum* ak_fs_map_v3_get_map_hash(akfs_map_v3*); /** * Unused */ -char* ak_fs_map_v3_get_root_hash(akfs_map_v3*); +sha512sum* ak_fs_map_v3_get_root_hash(akfs_map_v3*); /** - * Unused + * Gets original hash out of the akfs_map_v3 + * param: akfs_map_v3 + * return: pointer to sha512sum */ -char* ak_fs_map_v3_get_orig_hash(akfs_map_v3*); +sha512sum* ak_fs_map_v3_get_orig_hash(akfs_map_v3*); /** * Unused */ diff --git a/c_implementation/src/ak_fs.c b/c_implementation/src/ak_fs.c index 1d30744..c037fd8 100644 --- a/c_implementation/src/ak_fs.c +++ b/c_implementation/src/ak_fs.c @@ -12,7 +12,7 @@ char* ak_fs_return_hash_path(const char* str) { - if ( ak_fs_verify_input_is_hash(str) ) + if ( ak_fs_verify_input_is_hash(str, strlen(str)) ) { unsigned int i = 0; char *result = malloc((128*2)+1); @@ -43,7 +43,7 @@ char* ak_fs_return_hash_path(const char* str) char* ak_fs_return_hash_dir(const char* str) { - if ( ak_fs_verify_input_is_hash(str) ) + if ( ak_fs_verify_input_is_hash(str, strlen(str)) ) { unsigned int i = 0; char *result = malloc((128*2)+1); @@ -69,20 +69,27 @@ char* ak_fs_return_hash_dir(const char* str) } } -bool ak_fs_verify_input_is_hash(const char* str) +bool ak_fs_verify_input_is_hash(const char* str, size_t len) { size_t i = 0; + printf("%s: this %lu is %lu: %s\n", __func__, i, len, str); + if (len != 128) + { + ak_log_debug(__func__, "Hash string length not right"); + return false; + } while ( str[i] != '\0' ) { if ( i < 128 && !( ( str[i] >= 0x30 ) && - (( str[i] <= 0x39) || ( str[i] >= 0x61 )) && + ( str[i] <= 0x39 || str[i] >= 0x61 ) && ( str[i] <= 0x66 ) - ) - ) + ) + ) { + ak_log_debug(__func__, "Char out of range"); return false; } else { @@ -91,8 +98,10 @@ bool ak_fs_verify_input_is_hash(const char* str) } if ( i > 128 ) { + ak_log_debug(__func__, "String exceeds limit"); return false; } + ak_log_debug(__func__, "String okay"); return true; } @@ -107,7 +116,7 @@ int ak_fs_create_dir_for_hash(const char* str) * 2. We might need to "lock" onto some version of glibc and be aware of * other systems that do not use that one. */ - if ( ak_fs_verify_input_is_hash(str) ) + if ( ak_fs_verify_input_is_hash(str, strlen(str)) ) { char* dir_path = ak_fs_return_hash_dir(str); // We will need to separate the string so we can create the path one @@ -196,7 +205,7 @@ int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_ma si++; } original_hash_str[si] = '\0'; - if( !ak_fs_verify_input_is_hash(original_hash_str) ) + if( !ak_fs_verify_input_is_hash(original_hash_str, strlen(original_hash_str)) ) { ak_log_error(__func__, "original_hash_str not a hash"); return 1; @@ -213,7 +222,7 @@ int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_ma si++; } root_hash_str[si] = '\0'; - if( !ak_fs_verify_input_is_hash(root_hash_str) ) + if( !ak_fs_verify_input_is_hash(root_hash_str, strlen(root_hash_str)) ) { ak_log_error(__func__, "root_hash_str not a hash"); return 1; @@ -237,40 +246,55 @@ int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_ma return 0; } -void ak_fs_get_available_maps_from_fs(sha512sum **ma, size_t length) +void ak_fs_maps_v3_get_from_fs(akfs_map_v3 **ma, size_t length) { + (void)length; DIR *d; d = opendir(ak_fs_maps_v3_get_dir()); - sha512sum *ptr = NULL; + akfs_map_v3 *ptr = NULL; + ptr = *ma; + ak_log_debug(__func__, "gonna if"); if (d) { - for ( ptr = *ma; ptr < *ma+length; ++ptr) + ak_log_debug(__func__, "in if"); + const struct dirent *dir; + while((dir = readdir(d)) != NULL ) { - const struct dirent *dir; - if ((dir = readdir(d)) == NULL ) break; - if (!ak_fs_verify_input_is_hash(dir->d_name)) continue; - ak_fs_sha512sum_string_to_struct(dir->d_name, ptr); + ak_log_debug(__func__, "in while"); + // if ( ptr >= *ma+length ) break; + printf("iMH: %s\n", dir->d_name); + if (!ak_fs_verify_input_is_hash(dir->d_name, 128)) continue; + ak_fs_sha512sum_string_to_struct(dir->d_name, &(ptr->mh)); + ++ptr; } + ak_log_debug(__func__, "out while"); } + ak_log_debug(__func__, "out if"); closedir(d); } int ak_fs_map_v3_resolve_maps(akfs_map_v3 **ms, size_t ms_len) { akfs_map_v3 *ptr = NULL; + // char s[129] = {0}; + printf("%s: %lu\n", __func__, ms_len); for ( ptr = *ms; ptr < *ms+ms_len; ++ptr) { + printf("MH: %s\n", ak_fs_sha512sum_struct_read_as_string(&(ptr->mh))); if ( ak_fs_sha512sum_is_null(&(ptr->mh)) ) { + ak_log_debug(__func__, "map hash is null"); continue; } if( ak_fs_map_v3_open_from_file(ptr) != 2) { + ak_log_debug(__func__, "not 2"); ++(ptr); continue; } else { + ak_log_debug(__func__, "2"); ++(ptr); // return 1; } @@ -318,9 +342,9 @@ int ak_fs_ls() void* mps_start = &map_store[0]; (void)mps_start; ak_fs_maps_v3_init(&maps_ptr, len); + // ak_fs_maps_v3_print(&maps_ptr, len); + ak_fs_maps_v3_get_from_fs(&maps_ptr, len); ak_fs_maps_v3_print(&maps_ptr, len); - - // TODO Rename the following to "ak_fs_resolve_map_v3_array" or close to it ak_fs_map_v3_resolve_maps(&maps_ptr, len); // TODO Decide what we should be printing diff --git a/c_implementation/src/ak_fs_map_v3.c b/c_implementation/src/ak_fs_map_v3.c index 1b26d89..92bef5d 100644 --- a/c_implementation/src/ak_fs_map_v3.c +++ b/c_implementation/src/ak_fs_map_v3.c @@ -84,15 +84,27 @@ sha512sum* ak_fs_map_v3_get_map_hash(akfs_map_v3 *map) return &(map->mh); } -char* ak_fs_map_v3_get_root_hash(akfs_map_v3 *map) +sha512sum* ak_fs_map_v3_get_root_hash(akfs_map_v3 *map) { if (!ak_fs_sha512sum_is_null(&(map->rh))) { - return ak_fs_sha512sum_struct_read_as_string(&(map->rh)); + return &(map->rh); } else { - return ""; + return NULL; + } +} + +sha512sum* ak_fs_map_v3_get_orig_hash(akfs_map_v3 *map) +{ + if (!ak_fs_sha512sum_is_null(&(map->oh))) + { + return &(map->oh); + } + else + { + return NULL; } } @@ -103,7 +115,7 @@ char* ak_fs_map_v3_get_filename(akfs_map_v3 *map) void ak_fs_map_v3_print_filename(akfs_map_v3 *map) { - printf(" .fn: %s\n", ak_fs_map_v3_get_filename(map)); + printf("%s", ak_fs_map_v3_get_filename(map)); } void ak_fs_map_v3_print(akfs_map_v3 *map) @@ -126,13 +138,13 @@ void ak_fs_map_v3_print_as_json(akfs_map_v3 *map) printf("\"map\":\""); ak_fs_map_v3_print_map_hash(map); printf("\","); - printf("{\"original\":\""); + printf("\"original\":\""); ak_fs_map_v3_print_original_hash(map); printf("\","); - printf("{\"root\":\""); + printf("\"root\":\""); ak_fs_map_v3_print_root_hash(map); printf("\","); - printf("{\"filename\":\""); + printf("\"filename\":\""); ak_fs_map_v3_print_filename(map); printf("\""); printf("}\n"); @@ -189,7 +201,7 @@ int ak_fs_open_map_v3_file(char* maphash, akfs_map_v3 * map) ak_log_debug(__func__, "Zeropointer"); return 1; } - if ( !ak_fs_verify_input_is_hash(maphash) ) + if ( !ak_fs_verify_input_is_hash(maphash, strlen(maphash)) ) { ak_log_debug(__func__,"not a hash"); return 1; diff --git a/c_implementation/src/ak_fs_maps_v3.c b/c_implementation/src/ak_fs_maps_v3.c index 9b047b0..a89dd79 100644 --- a/c_implementation/src/ak_fs_maps_v3.c +++ b/c_implementation/src/ak_fs_maps_v3.c @@ -1,6 +1,8 @@ -#include <libakfs.h> #include <stdlib.h> +#include <stdio.h> +#include <string.h> #include <dirent.h> +#include "libakfs.h" const char* ak_fs_maps_v3_get_dir() { @@ -30,7 +32,11 @@ void ak_fs_maps_v3_print_filenames(akfs_map_v3** m, size_t s) akfs_map_v3 *ptr = NULL; for (ptr = *m; ptr < *m+s; ++ptr) { - if ( ptr != NULL ) ak_fs_map_v3_print_filename(ptr); + if ( ptr != NULL ) + { + ak_fs_map_v3_print_filename(ptr); + printf("\n"); + } } } @@ -40,10 +46,18 @@ void ak_fs_maps_v3_print(akfs_map_v3 **map_store, size_t length) for ( ptr = *map_store; ptr < *map_store + length; ++ptr) { ak_fs_map_v3_print(ptr); - ak_fs_map_v3_print_as_json(ptr); + printf("\n"); } } +void ak_fs_maps_v3_print_as_json(akfs_map_v3 **map_store, size_t length) +{ + akfs_map_v3 *ptr = NULL; + for ( ptr = *map_store; ptr < *map_store + length; ++ptr) + { + ak_fs_map_v3_print_as_json(ptr); + } +} size_t ak_fs_maps_v3_found_in_fs() { @@ -55,7 +69,7 @@ size_t ak_fs_maps_v3_found_in_fs() const struct dirent *dir; while ((dir = readdir(d)) != NULL ) { - if (ak_fs_verify_input_is_hash(dir->d_name)) counter++; + if (ak_fs_verify_input_is_hash(dir->d_name, strlen(dir->d_name))) counter++; } } closedir(d); diff --git a/c_implementation/src/ak_fs_sha512sum.c b/c_implementation/src/ak_fs_sha512sum.c index 2e10544..6a2ebaa 100644 --- a/c_implementation/src/ak_fs_sha512sum.c +++ b/c_implementation/src/ak_fs_sha512sum.c @@ -2,6 +2,7 @@ #include <libaklog.h> #include <stdlib.h> #include <assert.h> +#include <string.h> void ak_fs_sha512sum_init(sha512sum *hash) { @@ -41,7 +42,7 @@ char* ak_fs_sha512sum_struct_read_as_string(const sha512sum *ptr) { char *str = malloc(129*sizeof(char)); ak_fs_sha512sum_struct_to_string(ptr, str); - ak_log_debug(__func__, str); + // ak_log_debug(__func__, str); return str; } @@ -54,7 +55,7 @@ bool ak_fs_sha512sum_is_null(const sha512sum *h) int ak_fs_sha512sum_string_to_struct(const char* str, sha512sum* hash) { - if ( ak_fs_verify_input_is_hash(str) ) + if ( ak_fs_verify_input_is_hash(str, strlen(str)) ) { for (size_t l = 0; l < 8; ++l) { diff --git a/c_implementation/tests/test_akfs.c b/c_implementation/tests/test_akfs.c index 237f490..9506ec9 100644 --- a/c_implementation/tests/test_akfs.c +++ b/c_implementation/tests/test_akfs.c @@ -200,11 +200,25 @@ static void test_hash_check() static void test_map_opener() { akfs_map_v3 map; + ak_fs_map_v3_init(&map); char *map_string = "28bde5fa7aacd8da0ec84b61cf3a69141686906c00f8cff904c9a0b12f5a4cf061da254feb188c32b711b2e1d6a3853d5ac3fb0bcd3564899bae55dd30470392"; - ak_fs_open_map_v3_file(map_string, &map); + ak_fs_sha512sum_string_to_struct(map_string, &(map.mh)); + if ( ak_fs_map_v3_open_from_file(&map) != 0 ) + { + ak_log_debug(__func__, "FAILED"); + return; + } const char *orig_string = "fa19bdc471bedc42abf3ff52069214bc7339a7eafc03f8551e8af892a0e3ce175cff0dde6f815da031cd0566fded455c937f7cae27181f7a90ab92e6131ba2be"; const char *root_string = "438aebe24c89d36f84a68ea29327b27af1abc05f8f85e69af650159c4928834bd6fd2b3df690de74d42f861a8dbe30cebc6cba6afe07fabb1066d1380cd3adea"; const char *filename = "mixtapes-v0.0.0.tar.gz"; + // printf("%s\n",ak_fs_sha512sum_struct_read_as_string((const sha512sum*)&(map.mh))); + // printf("%s\n",ak_fs_sha512sum_struct_read_as_string((const sha512sum*)&(map.oh))); + // printf("%s\n",ak_fs_sha512sum_struct_read_as_string((const sha512sum*)&(map.rh))); + // printf("%s\n",map.filename); + + ak_fs_map_v3_print(&map); + ak_fs_map_v3_print_as_json(&map); + if ( (strcmp(map_string, ak_fs_sha512sum_struct_read_as_string(&(map.mh)))!=0) || (strcmp(orig_string, ak_fs_sha512sum_struct_read_as_string(&(map.oh)))!=0) || @@ -212,18 +226,35 @@ static void test_map_opener() (strcmp(filename, map.filename)!=0)) { ak_log_debug(__func__, "FAILED"); + return; } else { ak_log_debug(__func__, "PASSED"); + return; } } static void test_ak_fs_ls() { ak_log_test(__func__, ".....=====....."); + // size_t len = ak_fs_maps_v3_found_in_fs(); + // akfs_map_v3 map_store[len]; + // akfs_map_v3* mps_ptr = &map_store[0]; + // void* mps_start = &map_store[0]; + // (void)mps_start; + // ak_fs_map_v3_init_store(&mps_ptr, len); + + // // TODO Rename the following to "ak_fs_resolve_map_v3_array" or close to it + // ak_fs_map_v3_resolve_maps(&mps_ptr, len); + + // // TODO Decide what we should be printing + // // Possibly, something like "maphex(6)_filename" so we can put multiple + // // files with the same name into the list + // ak_fs_maps_v3_print(&mps_ptr, len); + // ak_fs_print_filenames_from_map_store(&mps_ptr, len); ak_fs_ls(); - ak_log_test(__func__, ".....=END=....."); + // ak_log_test(__func__, ".....=END=....."); } |