aboutsummaryrefslogtreecommitdiff
path: root/c_implementation/src/ak_fs.c
diff options
context:
space:
mode:
Diffstat (limited to 'c_implementation/src/ak_fs.c')
-rw-r--r--c_implementation/src/ak_fs.c103
1 files changed, 103 insertions, 0 deletions
diff --git a/c_implementation/src/ak_fs.c b/c_implementation/src/ak_fs.c
index 6594f2d..8ede3f6 100644
--- a/c_implementation/src/ak_fs.c
+++ b/c_implementation/src/ak_fs.c
@@ -278,3 +278,106 @@ int ak_fs_ls()
ak_fs_maps_v3_print_bif(&maps_ptr, len);
return 0;
}
+
+int ak_fs_cat_file_from_root_hash(const sha512sum* rh)
+{
+ const char* chunks_dir = getenv("AK_CHUNKSDIR");
+ if ( chunks_dir == NULL )
+ {
+ ak_log_error(__func__, "No directory for chunks found");
+ return -2;
+ }
+ const char* leafs_dir = getenv("AK_LEAFSDIR");
+ if ( leafs_dir == NULL )
+ {
+ ak_log_error(__func__, "No directory for leafs found");
+ return -3;
+ }
+ FILE *fd;
+ char *fullpath;
+ bool is_chunk = false;
+ char root_hash_str[129] = {0};
+ ak_fs_sha512sum_struct_to_string(rh, root_hash_str);
+ if ( asprintf(&fullpath, "%s/%s", leafs_dir, root_hash_str) == -1 ) return -1;
+ fd = fopen(fullpath, "r");
+ if ( fd == NULL )
+ {
+ free(fullpath);
+ if ( asprintf(&fullpath, "%s/%s", chunks_dir, ak_fs_sha512sum_struct_read_as_string(rh)) == -1 ) return -1;
+ fd = fopen(fullpath, "r");
+ if ( fd == NULL )
+ {
+ ak_log_error(__func__, "Could not be found");
+ free(fullpath);
+ return 1;
+ }
+ is_chunk = true;
+ }
+ if ( !is_chunk )
+ {
+ char buffer[258] = {0};
+ size_t bytes_read = fread(&buffer, 1, sizeof(buffer), fd);
+ if ( bytes_read < sizeof(buffer) )
+ {
+ ak_log_error(__func__, "File is smaller than expected. Wrong format?");
+ fclose(fd);
+ free(fullpath);
+ return 2;
+ }
+ if ( buffer[128] != '\n' || buffer[257] != '\n' )
+ {
+ ak_log_error(__func__, "Unknown format");
+ fclose(fd);
+ free(fullpath);
+ return 2;
+ }
+ char h_str[129] = {0};
+ char t_str[129] = {0};
+ mt_branch h0;
+ ak_fs_sha512sum_init(&h0.root);
+ ak_fs_sha512sum_init(&h0.head);
+ ak_fs_sha512sum_init(&h0.tail);
+ h0.root = *rh;
+ memcpy(h_str, buffer, 128);
+ h_str[128] = '\0';
+ memcpy(t_str, buffer + 129, 128);
+ t_str[128] = '\0';
+ fclose(fd);
+ // free(fullpath);
+ ak_fs_sha512sum_string_to_struct(h_str, &h0.head);
+ ak_fs_sha512sum_string_to_struct(t_str, &h0.tail);
+ ak_fs_cat_file_from_root_hash(&h0.head);
+ if ( !ak_fs_sha512sum_compare(&h0.head, &h0.tail) ) ak_fs_cat_file_from_root_hash(&h0.tail);
+ }
+ else
+ {
+ struct stat sb;
+ if (stat(fullpath, &sb) == -1) {
+ perror("stat");
+ fclose(fd);
+ free(fullpath);
+ return 2;
+ }
+ char buffer[(long long) sb.st_size+1];
+ fread(&buffer, sizeof(buffer), 1, fd);
+ fclose(fd);
+ buffer[sizeof(buffer)-1] = '\0';
+ printf("%s", buffer);
+ }
+ free(fullpath);
+ return 0;
+}
+
+int ak_fs_cfm(akfs_map_v3* map)
+{
+ sha512sum x;
+ ak_fs_sha512sum_init(&x);
+ const sha512sum *rh_ptr = ak_fs_map_v3_get_root_hash(map);
+ if ( rh_ptr == NULL )
+ {
+ ak_log_debug(__func__, "No root hash found on the map");
+ return -1;
+ }
+ x = *rh_ptr;
+ return ak_fs_cat_file_from_root_hash(&x);
+}