diff options
-rw-r--r-- | c_implementation/include/libakfs.h | 24 | ||||
-rw-r--r-- | c_implementation/src/ak_fs.c | 51 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_main.c | 9 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_map_v3.c | 5 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_mt.c | 15 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_sha512sum.c | 2 |
6 files changed, 72 insertions, 34 deletions
diff --git a/c_implementation/include/libakfs.h b/c_implementation/include/libakfs.h index 31dd35e..f223c28 100644 --- a/c_implementation/include/libakfs.h +++ b/c_implementation/include/libakfs.h @@ -402,10 +402,32 @@ 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*); -int ak_fs_mt_branch_resolve(mt_branch *); + +/** + * Concatenates a file from a root hash. + * @param sha512sum* + * @return int status + */ int ak_fs_cat_file_from_root_hash(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 a5eee10..07178bc 100644 --- a/c_implementation/src/ak_fs.c +++ b/c_implementation/src/ak_fs.c @@ -300,48 +300,52 @@ int ak_fs_cat_file_from_root_hash(sha512sum* rh) fd = fopen(fullpath, "r"); if ( fd == NULL ) { - // perror("fopen"); + 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]; - fread(&buffer, sizeof(buffer), 1, fd); - fclose(fd); - char h1[129] = {0}; - char h2[129] = {0}; + 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; - 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); + 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); - // ak_fs_mt_branch_resolve(&h0); } else { @@ -349,20 +353,16 @@ int ak_fs_cat_file_from_root_hash(sha512sum* rh) if (stat(fullpath, &sb) == -1) { perror("stat"); fclose(fd); + free(fullpath); return 2; } - // File size: %lld in bytes: (long long) sb.st_size); char buffer[(long long) sb.st_size+1]; fread(&buffer, sizeof(buffer), 1, fd); fclose(fd); buffer[sizeof(buffer)-1] = '\0'; printf("%s", buffer); } - // 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); + free(fullpath); return 0; } @@ -371,7 +371,6 @@ 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/src/ak_fs_main.c b/c_implementation/src/ak_fs_main.c index f1660cf..b25e308 100644 --- a/c_implementation/src/ak_fs_main.c +++ b/c_implementation/src/ak_fs_main.c @@ -14,14 +14,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 +31,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 index d083eae..2c3414b 100644 --- a/c_implementation/src/ak_fs_mt.c +++ b/c_implementation/src/ak_fs_mt.c @@ -3,17 +3,17 @@ #include <libaklog.h> #include <stdbool.h> -bool ak_fs_mt_branch_is_null(mt_branch* node) +bool ak_fs_mt_branch_is_null(mt_branch* n) { - if ( node == NULL ) + if ( n == NULL ) { ak_log_warning(__func__, "A NULL mt_branch* node was given"); return false; } if ( - ak_fs_sha512sum_is_null(&node->root) && - ak_fs_sha512sum_is_null(&node->head) && - ak_fs_sha512sum_is_null(&node->tail) + ak_fs_sha512sum_is_null(&n->root) && + ak_fs_sha512sum_is_null(&n->head) && + ak_fs_sha512sum_is_null(&n->tail) ) { return true; @@ -41,6 +41,11 @@ bool ak_fs_mt_branch_compare(mt_branch *a, mt_branch *b) 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; |