aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaotisk <kaotisk@arching-kaos.org>2025-04-29 03:36:36 +0300
committerkaotisk <kaotisk@arching-kaos.org>2025-04-29 03:36:36 +0300
commite61fa65b03801bb66bc47fc793c8991bd9a1f471 (patch)
tree6fca90b5426ec6815925879507c498f0d1103462
parent8991f05857eddf8fb7769c7c2d024541e838b821 (diff)
downloadarching-kaos-tools-e61fa65b03801bb66bc47fc793c8991bd9a1f471.tar.gz
arching-kaos-tools-e61fa65b03801bb66bc47fc793c8991bd9a1f471.tar.bz2
arching-kaos-tools-e61fa65b03801bb66bc47fc793c8991bd9a1f471.zip
[libakfs] Better naming and utility functions
-rw-r--r--c_implementation/Makefile.am1
-rw-r--r--c_implementation/include/libakfs.h7
-rw-r--r--c_implementation/src/ak_fs.c44
-rw-r--r--c_implementation/src/ak_fs_mt.c68
4 files changed, 76 insertions, 44 deletions
diff --git a/c_implementation/Makefile.am b/c_implementation/Makefile.am
index 9b6cd65..f1834d5 100644
--- a/c_implementation/Makefile.am
+++ b/c_implementation/Makefile.am
@@ -14,6 +14,7 @@ libakfs_la_SOURCES = $(top_srcdir)/src/ak_fs.c \
$(top_srcdir)/src/ak_fs_maps_v3.c \
$(top_srcdir)/src/ak_fs_map_v3.c \
$(top_srcdir)/src/ak_fs_map_v4.c \
+ $(top_srcdir)/src/ak_fs_mt.c \
$(top_srcdir)/src/ak_fs_sha512sum.c
libakutils_la_SOURCES = $(top_srcdir)/src/ak_utils.c
diff --git a/c_implementation/include/libakfs.h b/c_implementation/include/libakfs.h
index 83b820e..31dd35e 100644
--- a/c_implementation/include/libakfs.h
+++ b/c_implementation/include/libakfs.h
@@ -21,7 +21,7 @@ typedef struct {
} sha512sum;
/**
- * This struct describes explicitly the structure of a merkletree_node. Note,
+ * This struct describes explicitly the structure of a mt_branch. 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
@@ -42,7 +42,7 @@ typedef struct {
* Hash of tail
*/
sha512sum tail;
-} merkletree_node;
+} mt_branch;
/**
* This is the current structure of an akfs_map. Due to potential short-comings
@@ -402,6 +402,9 @@ int ak_fs_ls();
*/
int ak_fs_main(int, char**);
+bool ak_fs_mt_branch_is_null(mt_branch*);
+bool ak_fs_mt_branch_compare(mt_branch*, mt_branch*);
+int ak_fs_mt_branch_resolve(mt_branch *);
int ak_fs_cat_file_from_root_hash(sha512sum*);
int ak_fs_cfm(akfs_map_v3*);
diff --git a/c_implementation/src/ak_fs.c b/c_implementation/src/ak_fs.c
index 27939bf..fdf110d 100644
--- a/c_implementation/src/ak_fs.c
+++ b/c_implementation/src/ak_fs.c
@@ -279,46 +279,6 @@ int ak_fs_ls()
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"));
@@ -341,7 +301,7 @@ int ak_fs_cat_file_from_root_hash(sha512sum* rh)
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;
+ mt_branch h0;
ak_fs_sha512sum_init(&h0.root);
ak_fs_sha512sum_init(&h0.head);
ak_fs_sha512sum_init(&h0.tail);
@@ -358,7 +318,7 @@ int ak_fs_cat_file_from_root_hash(sha512sum* rh)
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_fs_mt_branch_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));
diff --git a/c_implementation/src/ak_fs_mt.c b/c_implementation/src/ak_fs_mt.c
new file mode 100644
index 0000000..7bdd6f4
--- /dev/null
+++ b/c_implementation/src/ak_fs_mt.c
@@ -0,0 +1,68 @@
+#include <stdio.h>
+#include <libakfs.h>
+#include <stdlib.h>
+
+bool ak_fs_mt_branch_is_null(mt_branch* node)
+{
+ if (
+ ak_fs_sha512sum_is_null(&node->root) &&
+ ak_fs_sha512sum_is_null(&node->head) &&
+ ak_fs_sha512sum_is_null(&node->tail)
+ )
+ {
+ return true;
+ }
+ return false;
+}
+
+bool ak_fs_mt_branch_compare(mt_branch *a, mt_branch *b)
+{
+ if (
+ ak_fs_sha512sum_compare(&a->root, &b->root) &&
+ ak_fs_sha512sum_compare(&a->head, &b->head) &&
+ ak_fs_sha512sum_compare(&a->tail, &b->tail)
+ )
+ {
+ return true;
+ }
+ return false;
+}
+
+int ak_fs_mt_branch_resolve(mt_branch *node)
+{
+ const char* leafs_dir = getenv("AK_LEAFSDIR");
+ FILE *fd;
+ char *fullpath;
+ asprintf(&fullpath, "%s/%s", leafs_dir, ak_fs_sha512sum_struct_read_as_string(&node->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");
+ mt_branch h0;
+ ak_fs_sha512sum_init(&h0.root);
+ ak_fs_sha512sum_init(&h0.head);
+ ak_fs_sha512sum_init(&h0.tail);
+ h0.root = node->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_mt_branch_resolve(&h0);
+ return 0;
+}