From 8991f05857eddf8fb7769c7c2d024541e838b821 Mon Sep 17 00:00:00 2001 From: kaotisk Date: Tue, 29 Apr 2025 01:19:28 +0300 Subject: [libakfs] Basic test, headers and functions for the feature --- c_implementation/include/libakfs.h | 12 +++-- c_implementation/src/ak_fs.c | 98 ++++++++++++++++++++++++++++++++++++++ c_implementation/tests/test_akfs.c | 18 +++++++ 3 files changed, 123 insertions(+), 5 deletions(-) diff --git a/c_implementation/include/libakfs.h b/c_implementation/include/libakfs.h index e31c5ec..83b820e 100644 --- a/c_implementation/include/libakfs.h +++ b/c_implementation/include/libakfs.h @@ -21,10 +21,9 @@ typedef struct { } sha512sum; /** - * This struct describes explicitly the structure of a root_hash. It is the root - * of a hash merkle tree. Note, that this structure can be used for roots and - * branches. Possibly, the name will change to something more generic in the - * future. + * This struct describes explicitly the structure of a merkletree_node. Note, + * that this structure can be used for roots and branches. + * * Another note is that instead of approaching this as left and right, as seen * in other codebases, we do a head-tail naming. That's because of the BASH * implementation that you can find at lib/_ak_fs. @@ -43,7 +42,7 @@ typedef struct { * Hash of tail */ sha512sum tail; -} root_hash; +} merkletree_node; /** * This is the current structure of an akfs_map. Due to potential short-comings @@ -403,5 +402,8 @@ int ak_fs_ls(); */ int ak_fs_main(int, char**); +int ak_fs_cat_file_from_root_hash(sha512sum*); +int ak_fs_cfm(akfs_map_v3*); + #endif // AKFS diff --git a/c_implementation/src/ak_fs.c b/c_implementation/src/ak_fs.c index 6594f2d..27939bf 100644 --- a/c_implementation/src/ak_fs.c +++ b/c_implementation/src/ak_fs.c @@ -278,3 +278,101 @@ int ak_fs_ls() ak_fs_maps_v3_print_bif(&maps_ptr, len); return 0; } + +int ak_fs_root_hash_resolve(merkletree_node *h) +{ + const char* leafs_dir = getenv("AK_LEAFSDIR"); + FILE *fd; + char *fullpath; + asprintf(&fullpath, "%s/%s", leafs_dir, ak_fs_sha512sum_struct_read_as_string(&h->head)); + fd = fopen(fullpath, "r"); + if ( fd == NULL ) + { + perror("fopen"); + return 1; + } + char buffer[258]; + fread(&buffer, sizeof(buffer), 1, fd); + fclose(fd); + char h1[129] = {0}; + char h2[129] = {0}; + if ( buffer[128] == '\n' && buffer[257] == '\n' ) printf("\\n found on the expected spot!\n"); + merkletree_node h0; + ak_fs_sha512sum_init(&h0.root); + ak_fs_sha512sum_init(&h0.head); + ak_fs_sha512sum_init(&h0.tail); + h0.root = h->root; + for( size_t i = 0; i < 128; ++i ) + { + h1[i] = buffer[i]; + } + h1[128] = '\0'; + for( size_t i = 0; i < 128; ++i ) + { + h2[i] = buffer[i+129]; + } + h2[128] = '\0'; + ak_fs_sha512sum_string_to_struct(h1, &h0.head); + ak_fs_sha512sum_string_to_struct(h2, &h0.tail); + ak_fs_root_hash_resolve(&h0); + + return 0; +} + +int ak_fs_cat_file_from_root_hash(sha512sum* rh) +{ + printf("%s: %s\n", __func__, getenv("AK_CHUNKSDIR")); + const char* leafs_dir = getenv("AK_LEAFSDIR"); + // We always expect root hash to be in the AK_LEAFSDIR directory, however it + // might as well not be there + FILE *fd; + // We need to join the root_hash with the directory first + char *fullpath; + asprintf(&fullpath, "%s/%s", leafs_dir, ak_fs_sha512sum_struct_read_as_string(rh)); + fd = fopen(fullpath, "r"); + if ( fd == NULL ) + { + perror("fopen"); + return 1; + } + char buffer[258]; + fread(&buffer, sizeof(buffer), 1, fd); + fclose(fd); + char h1[129] = {0}; + char h2[129] = {0}; + if ( buffer[128] == '\n' && buffer[257] == '\n' ) printf("\\n found on the expected spot!\n"); + merkletree_node h0; + ak_fs_sha512sum_init(&h0.root); + ak_fs_sha512sum_init(&h0.head); + ak_fs_sha512sum_init(&h0.tail); + h0.root = *rh; + for( size_t i = 0; i < 128; ++i ) + { + h1[i] = buffer[i]; + } + h1[128] = '\0'; + for( size_t i = 0; i < 128; ++i ) + { + h2[i] = buffer[i+129]; + } + h2[128] = '\0'; + ak_fs_sha512sum_string_to_struct(h1, &h0.head); + ak_fs_sha512sum_string_to_struct(h2, &h0.tail); + ak_fs_root_hash_resolve(&h0); + // ak_log_debug(__func__, ak_fs_sha512sum_struct_read_as_string(&h0.root)); + // ak_log_debug(__func__, ak_fs_sha512sum_struct_read_as_string(&h0.head)); + // ak_log_debug(__func__, ak_fs_sha512sum_struct_read_as_string(&h0.tail)); + // if ( ak_fs_verify_input_is_hash(h1, strlen(h1))) printf("head:\n%s\n---\n", h1); + // if ( ak_fs_verify_input_is_hash(h2, strlen(h2))) printf("tail:\n%s\n---\n", h2); + return 0; +} + +int ak_fs_cfm(akfs_map_v3* map) +{ + sha512sum x; + ak_fs_sha512sum_init(&x); + x = *(ak_fs_map_v3_get_root_hash(map)); + // printf("%s\n", ak_fs_sha512sum_struct_read_as_string(&x)); + ak_fs_cat_file_from_root_hash(&x); + return 0; +} diff --git a/c_implementation/tests/test_akfs.c b/c_implementation/tests/test_akfs.c index 55607c4..247a59a 100644 --- a/c_implementation/tests/test_akfs.c +++ b/c_implementation/tests/test_akfs.c @@ -241,6 +241,21 @@ static void test_ak_fs_ls() ak_fs_ls(); } +static void test_ak_fs_cfm() +{ + ak_log_test(__func__, ".....=====....."); + akfs_map_v3 map; + ak_fs_map_v3_init(&map); + char *map_string = "28bde5fa7aacd8da0ec84b61cf3a69141686906c00f8cff904c9a0b12f5a4cf061da254feb188c32b711b2e1d6a3853d5ac3fb0bcd3564899bae55dd30470392"; + 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; + } + ak_fs_cfm(&map); +} + int main(void) { @@ -279,5 +294,8 @@ int main(void) printf("%lu\n", (unsigned long)sizeof(sha512sum)); printf("%lu\n", (unsigned long)sizeof(akfs_map_v3)); printf("%lu\n", (unsigned long)sizeof(akfs_map_v4)); + + // Test ak_fs_cfm + test_ak_fs_cfm(); return 0; } -- cgit v1.2.3