diff options
-rw-r--r-- | c_implementation/Makefile.am | 1 | ||||
-rw-r--r-- | c_implementation/include/libakfs.h | 37 | ||||
-rw-r--r-- | c_implementation/src/ak_fs.c | 103 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_main.c | 12 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_map_v3.c | 5 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_mt.c | 52 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_sha512sum.c | 2 | ||||
-rw-r--r-- | c_implementation/src/ak_log.c | 6 | ||||
-rw-r--r-- | c_implementation/tests/test_akfs.c | 66 |
9 files changed, 255 insertions, 29 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 e31c5ec..3eb0ba3 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 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 * 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; +} mt_branch; /** * This is the current structure of an akfs_map. Due to potential short-comings @@ -403,5 +402,33 @@ int ak_fs_ls(); */ int ak_fs_main(int, char**); +/** + * Compares an mt_branch with a NULL one + * @param mt_branch* + * @return boolean + */ +bool ak_fs_mt_branch_is_null(mt_branch*); + +/** + * Compares two mt_branch between them + * @param mt_branch* + * @return boolean + */ +bool ak_fs_mt_branch_compare(mt_branch*, mt_branch*); + +/** + * Concatenates a file from a root hash. + * @param sha512sum* + * @return int status + */ +int ak_fs_cat_file_from_root_hash(const sha512sum*); + +/** + * Concatenates a file from a akfs_map_v3 map + * @param akfs_map_v3* + * @return int status + */ +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..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); +} diff --git a/c_implementation/src/ak_fs_main.c b/c_implementation/src/ak_fs_main.c index f1660cf..e399544 100644 --- a/c_implementation/src/ak_fs_main.c +++ b/c_implementation/src/ak_fs_main.c @@ -6,7 +6,8 @@ static int ak_fs_usage() { ak_log_debug(__func__, "Available commands:"); - ak_log_debug(__func__, "ak fs --list"); + ak_log_debug(__func__, "akfs --list"); + ak_log_debug(__func__, "akfs --cfm <map hash>"); return 1; } @@ -14,14 +15,16 @@ int ak_fs_main(int argc, char** argv) { int option; int logind = 0; + akfs_map_v3 map; static struct option long_options[] = { {"help", no_argument, 0, 'h'}, {"list", no_argument, 0, 'l'}, + {"cfm", required_argument, 0, 'C'}, {0,0,0,0} }; while(1) { - option = getopt_long(argc, argv, "hl", long_options, &logind); + option = getopt_long(argc, argv, "hlC:", long_options, &logind); if ( option == -1 ) return ak_fs_usage(); switch(option) { @@ -29,6 +32,11 @@ int ak_fs_main(int argc, char** argv) return ak_fs_usage(); case 'l': return ak_fs_ls(); + case 'C': + ak_fs_map_v3_init(&map); + if ( ak_fs_sha512sum_string_to_struct(optarg, &map.mh) != 0 ) return -1; + if ( ak_fs_map_v3_open_from_file(&map) != 0 ) return -2; + return ak_fs_cfm(&map); default: printf("double lol\n"); return 4; diff --git a/c_implementation/src/ak_fs_map_v3.c b/c_implementation/src/ak_fs_map_v3.c index 6774f65..1d3e37e 100644 --- a/c_implementation/src/ak_fs_map_v3.c +++ b/c_implementation/src/ak_fs_map_v3.c @@ -1,5 +1,6 @@ #include <stdio.h> #include <string.h> +#include <stdlib.h> #include <libakfs.h> #include <libaklog.h> #include <sys/stat.h> @@ -179,12 +180,14 @@ int ak_fs_map_v3_open_from_file(akfs_map_v3 * map) { // perror("fopen"); ak_log_debug(__func__, "File not found or other error"); + free(full_path); return 1; } struct stat sb; if (stat(full_path, &sb) == -1) { perror("stat"); fclose(fd); + free(full_path); return 2; } // File size: %lld in bytes: (long long) sb.st_size); @@ -195,9 +198,11 @@ int ak_fs_map_v3_open_from_file(akfs_map_v3 * map) { ak_log_debug(__func__,"conversion failed"); fclose(fd); + free(full_path); return 1; } fclose(fd); + free(full_path); return 0; } diff --git a/c_implementation/src/ak_fs_mt.c b/c_implementation/src/ak_fs_mt.c new file mode 100644 index 0000000..2c3414b --- /dev/null +++ b/c_implementation/src/ak_fs_mt.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <libakfs.h> +#include <libaklog.h> +#include <stdbool.h> + +bool ak_fs_mt_branch_is_null(mt_branch* n) +{ + if ( n == NULL ) + { + ak_log_warning(__func__, "A NULL mt_branch* node was given"); + return false; + } + if ( + ak_fs_sha512sum_is_null(&n->root) && + ak_fs_sha512sum_is_null(&n->head) && + ak_fs_sha512sum_is_null(&n->tail) + ) + { + return true; + } + return false; +} + +bool ak_fs_mt_branch_compare(mt_branch *a, mt_branch *b) +{ + if ( a == NULL || b == NULL ) + { + ak_log_warning(__func__, "One or two NULL mt_branch* node was given"); + return false; + } + 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; +} + +void ak_fs_mt_branch_print(mt_branch *n) +{ + if ( n == NULL ) + { + ak_log_warning(__func__, "NULL mt_branch* was given"); + return; + } + printf("r: %s\n", ak_fs_sha512sum_struct_read_as_string(&n->root)); + printf("h: %s\n", ak_fs_sha512sum_struct_read_as_string(&n->head)); + printf("t: %s\n", ak_fs_sha512sum_struct_read_as_string(&n->tail)); +} diff --git a/c_implementation/src/ak_fs_sha512sum.c b/c_implementation/src/ak_fs_sha512sum.c index 6a2ebaa..619269f 100644 --- a/c_implementation/src/ak_fs_sha512sum.c +++ b/c_implementation/src/ak_fs_sha512sum.c @@ -40,7 +40,7 @@ void ak_fs_sha512sum_init_avail(sha512sum** m, size_t s) char* ak_fs_sha512sum_struct_read_as_string(const sha512sum *ptr) { - char *str = malloc(129*sizeof(char)); + static char str[129] = {0}; // = malloc(129*sizeof(char)); ak_fs_sha512sum_struct_to_string(ptr, str); // ak_log_debug(__func__, str); return str; diff --git a/c_implementation/src/ak_log.c b/c_implementation/src/ak_log.c index e3af865..65e81d3 100644 --- a/c_implementation/src/ak_log.c +++ b/c_implementation/src/ak_log.c @@ -22,11 +22,13 @@ int ak_log_write_to_file(const char* message) if (!fp) { perror("fopen"); + free(fullpath_to_log_file); return EXIT_FAILURE; } fwrite(message, strlen(message),1,fp); fwrite("\n", strlen("\n"),1,fp); fclose(fp); + free(fullpath_to_log_file); return 0; } @@ -169,6 +171,7 @@ void ak_log_message(const char* program, LogMessageType lmtype, char* message) asprintf(&some_string, "%ld <NULL> [ERROR] No arguments given\n", ts); ak_log_write_to_file(some_string); if ( AK_DEBUG ) ak_log_print_log_line(some_string); + free(some_string); return; } if ( message == NULL ) @@ -176,6 +179,7 @@ void ak_log_message(const char* program, LogMessageType lmtype, char* message) asprintf(&some_string, "%ld <%s> [ERROR] No message\n", ts, program); ak_log_write_to_file(some_string); if ( AK_DEBUG ) ak_log_print_log_line(some_string); + free(some_string); return; } switch(lmtype) @@ -202,11 +206,13 @@ void ak_log_message(const char* program, LogMessageType lmtype, char* message) asprintf(&some_string, "%ld <%s> [ERROR] No message type\n", ts, program); ak_log_write_to_file(some_string); if ( AK_DEBUG ) ak_log_print_log_line(some_string); + free(some_string); return; } asprintf(&some_string, "%ld <%s> [%s] %s", ts, program, type, message); ak_log_write_to_file(some_string); if ( lmtype <= AK_DEBUG_LEVEL ) ak_log_print_log_line(some_string); + free(some_string); } void ak_log_exit(const char* program, char* message) diff --git a/c_implementation/tests/test_akfs.c b/c_implementation/tests/test_akfs.c index 55607c4..d08ea3b 100644 --- a/c_implementation/tests/test_akfs.c +++ b/c_implementation/tests/test_akfs.c @@ -17,11 +17,11 @@ static void test_correct_string_correct_length() // printf("Hash returned:\t%s\n", resulted_string); if ( strcmp(queried_string, resulted_string) == 0 ) { - ak_log_info(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_error(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -38,11 +38,11 @@ static void test_bad_string_correct_length() // printf("Hash returned:\t%s\n", resulted_string); if ( strcmp(queried_string, resulted_string) != 0 ) { - ak_log_info(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_error(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -59,11 +59,11 @@ static void test_less_than_length() // printf("Hash returned:\t%s\n", resulted_string); if ( strcmp(queried_string, resulted_string) != 0 ) { - ak_log_info(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_error(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -80,11 +80,11 @@ static void test_more_than_length() // printf("Hash returned:\t%s\n", resulted_string); if ( strcmp(queried_string, resulted_string) != 0 ) { - ak_log_info(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_error(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -101,11 +101,11 @@ static void test_string_is_empty() // printf("Hash returned:\t%s\n", resulted_string); if ( strcmp(queried_string, resulted_string) != 0 ) { - ak_log_info(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_error(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -118,11 +118,11 @@ static void test_hash_path_test() // printf("Path returned:\t%s\n", resulted_string); if ( strcmp(queried_string, resulted_string) != 0 ) { - ak_log_info(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_error(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -135,11 +135,11 @@ static void test_hash_dir_test() // printf("Path returned:\t%s\n", resulted_string); if ( strcmp(queried_string, resulted_string) != 0 ) { - ak_log_info(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_error(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -171,11 +171,11 @@ static void test_hash_save_to_file() ak_fs_sha512sum_struct_to_string(resulted_hash, resulted_string); if ( strcmp(queried_string, resulted_string) == 0 ) { - ak_log_debug(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_debug(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } fclose(fd); } @@ -189,11 +189,11 @@ static void test_hash_check() ak_fs_sha512sum_string_to_struct(queried_string, &b); if ( ak_fs_sha512sum_compare(&a,&b) ) { - ak_log_debug(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); } else { - ak_log_debug(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); } } @@ -205,7 +205,7 @@ static void test_map_opener() 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"); + ak_log_test(__func__, "FAILED"); return; } const char *orig_string = "fa19bdc471bedc42abf3ff52069214bc7339a7eafc03f8551e8af892a0e3ce175cff0dde6f815da031cd0566fded455c937f7cae27181f7a90ab92e6131ba2be"; @@ -225,12 +225,12 @@ static void test_map_opener() (strcmp(root_string, ak_fs_sha512sum_struct_read_as_string(&(map.rh)))!=0) || (strcmp(filename, map.filename)!=0)) { - ak_log_debug(__func__, "FAILED"); + ak_log_test(__func__, "FAILED"); return; } else { - ak_log_debug(__func__, "PASSED"); + ak_log_test(__func__, "PASSED"); return; } } @@ -241,6 +241,27 @@ static void test_ak_fs_ls() ak_fs_ls(); } +static void test_ak_fs_cfm() +{ + 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_test(__func__, "FAILED"); + return; + } + if ( ak_fs_cfm(&map) == 0 ) + { + ak_log_test(__func__, "PASSED"); + } + else + { + ak_log_test(__func__, "FAILED"); + } +} + int main(void) { @@ -279,5 +300,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; } |