diff options
-rw-r--r-- | README | 17 | ||||
-rw-r--r-- | c_implementation/Makefile.am | 1 | ||||
-rw-r--r-- | c_implementation/include/libakdatetime.h | 14 | ||||
-rw-r--r-- | c_implementation/include/libakfs.h | 198 | ||||
-rw-r--r-- | c_implementation/src/ak_fs.c | 203 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_defuse.c | 2 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_main.c | 12 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_map_v3.c | 87 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_maps_v3.c | 41 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_mt.c | 52 | ||||
-rw-r--r-- | c_implementation/src/ak_fs_sha512sum.c | 7 | ||||
-rw-r--r-- | c_implementation/src/ak_log.c | 6 | ||||
-rw-r--r-- | c_implementation/tests/test_akfs.c | 83 |
13 files changed, 499 insertions, 224 deletions
@@ -393,3 +393,20 @@ $ ak pkg --help # --uninstall Uninstalls a module selected from a menu # ``` + +Donations +--------- +While this project is developed with enthusiasm and it uses the spare time of +the only developer that is coding for it, donations would help allocating more +time on the project instead of the developer looking for different occupations +to support their life and project. + +If you do appreciate the project's goals and the developers efforts towards it, +take your time and consider donating some satoshis to the developer via BTC at +the following address + +``` +bc1q70rgp65t7acfgpwp74m7vdz0g4eduxm6a43gd8 +``` + +Thank you! 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/libakdatetime.h b/c_implementation/include/libakdatetime.h new file mode 100644 index 0000000..435c3d8 --- /dev/null +++ b/c_implementation/include/libakdatetime.h @@ -0,0 +1,14 @@ +#include <stdbool.h> +#include <stddef.h> +#ifndef AK_DATETIME_H +#define AK_DATETIME_H + +long ak_datetime_unix(); +void ak_datetime_unix_nanosecs(char *buffer, size_t size); +void ak_datetime_human(char *buffer, size_t size); +void ak_datetime_human_date_only(char *buffer, size_t size); +void ak_datetime_human_date_only_yesterday(char *buffer, size_t size); +//static bool is_digits_only(const char *str); +bool ak_datetime_unix_to_human(const char *timestamp_str, char *buffer, size_t size); + +#endif // AK_DATETIME_H diff --git a/c_implementation/include/libakfs.h b/c_implementation/include/libakfs.h index 455baa7..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 @@ -129,9 +128,9 @@ char* ak_fs_return_hash_dir(const char*); * Verify that string looks like a SHA512 hash * * param char* string to be checked - * returns boolean + * @return boolean */ -bool ak_fs_verify_input_is_hash(const char*); +bool ak_fs_verify_input_is_hash(const char*, size_t); /** * Unused @@ -142,103 +141,122 @@ int ak_fs_create_dir_for_hash(const char*); * Converts string hash to struct * @param char* Hash as string * @param sha512sum* Pointer to a sha512sum - * @returns int Status of exit + * @return int Status of exit */ int ak_fs_sha512sum_string_to_struct(const char*, sha512sum*); /** * Returns struct from string hash * @param char* Hash as string - * @returns sha512sum* Pointer to a sha512sum + * @return sha512sum* Pointer to a sha512sum */ sha512sum* ak_fs_sha512sum_from_string(char*); /** * Converts hash struct to string + * @param sha512sum* Pointer to a sha512sum + * @param char* Hash as string */ void ak_fs_sha512sum_struct_to_string(const sha512sum*, char*); /** * Opens a map file to an akfs_map_v3 struct + * @param akfs_map_v3* + * @return int */ int ak_fs_map_v3_open_from_file(akfs_map_v3*); /** - * Prints filenames from a map_store - */ -void ak_fs_print_filenames_from_map_store(akfs_map_v3**, size_t); - -/** * Unused */ int ak_fs_map_v3_to_file(akfs_map_v3); /** - * Unused + * Converts a string to an akfs_map_v3 struct + * @param char* + * @param size_t + * @param akfs_map_v3* + * @return int */ int ak_fs_convert_map_v3_string_to_struct(const char *, size_t, akfs_map_v3*); /** - * Unused + * Prints an akfs_map_v3 in struct-like format + * @param akfs_map_v3* */ -void ak_fs_map_v3_print_map_hash(akfs_map_v3*); +void ak_fs_map_v3_print(akfs_map_v3*); /** - * Unused + * Prints an array of akfs_map_v3 in struct-like format + * @param akfs_map_v3* + * @param size_t */ -void ak_fs_map_v3_print_original_hash(akfs_map_v3*); +void ak_fs_maps_v3_print(akfs_map_v3**, size_t); /** - * Unused + * Prints the map hash out of a akfs_map_v3 + * @param akfs_map_v3* */ -void ak_fs_map_v3_print_root_hash(akfs_map_v3*); +void ak_fs_map_v3_print_map_hash(akfs_map_v3*); /** - * Unused + * Prints the original hash out of a akfs_map_v3 + * @param akfs_map_v3* */ -void ak_fs_map_v3_print_filename(akfs_map_v3*); +void ak_fs_map_v3_print_original_hash(akfs_map_v3*); /** - * Unused + * Prints the root hash out of a akfs_map_v3 + * @param akfs_map_v3* */ -void ak_fs_maps_v3_print_filenames(akfs_map_v3**, size_t); - -void ak_fs_map_v3_print_as_json(akfs_map_v3*); +void ak_fs_map_v3_print_root_hash(akfs_map_v3*); /** - * Unused + * Prints the filename out of a akfs_map_v3 + * @param akfs_map_v3* */ -void ak_fs_map_v3_print(akfs_map_v3*); +void ak_fs_map_v3_print_filename(akfs_map_v3*); /** - * Takes an array of sha512sums (maps) and puts it in an array of maps (v3) + * Prints the filenames out of an array of akfs_map_v3 + * @param akfs_map_v3** + * @param size_t */ -int ak_fs_map_v3_resolve_maps(akfs_map_v3**, size_t); +void ak_fs_maps_v3_print_filenames(akfs_map_v3**, size_t); /** - * Unused + * Prints an array of akfs_map_v3 in JSON format + * @param akfs_map_v3** + * @param size_t */ -void ak_fs_print_available_maps(sha512sum**, size_t); +void ak_fs_maps_v3_print_as_json(akfs_map_v3**, size_t); /** - * Unused + * Prints an akfs_map_v3 in JSON format + * @param akfs_map_v3* */ -void ak_fs_maps_v3_print(akfs_map_v3**, size_t); +void ak_fs_map_v3_print_as_json(akfs_map_v3*); /** - * Reads maps_dir and outputs it in an array of sha512sum + * Prints an array of akfs_map_v3 in bif format + * @param akfs_map_v3** + * @param size_t */ -void ak_fs_prepare_available_maps(sha512sum**, size_t); +void ak_fs_maps_v3_print_bif(akfs_map_v3**, size_t); /** - * Unused + * Prints an akfs_map_v3 in bif format + * @param akfs_map_v3* */ -void ak_fs_get_available_maps_from_fs(sha512sum**, size_t); +void ak_fs_map_v3_print_bif(akfs_map_v3*); /** - * Unused + * Takes an array of sha512sums (maps) and puts it in an array of maps (v3) + * @param akfs_map_v3** Pointer to an array of akfs_map_v3 + * @param size_t Length of the array + * @return int Exit code (0 on success) */ -void ak_fs_print_map_all_avail(sha512sum**, size_t); +int ak_fs_maps_v3_resolve(akfs_map_v3**, size_t); /** * Unused @@ -248,20 +266,29 @@ char* ak_fs_sha512sum_struct_read_as_string(const sha512sum *); /** * Unused */ -void ak_fs_init_string(char *, size_t ); +void ak_fs_init_string(char *, size_t); /** - * Unused + * Compares two sha512sum structs + * @param sha512sum* + * @param sha512sum* + * @return bool */ bool ak_fs_sha512sum_compare(const sha512sum*, const sha512sum*); + /** - * Unused + * Checks if an sha512sum struct is NULL + * @param sha512sum* + * @return bool */ bool ak_fs_sha512sum_is_null(const sha512sum*); + /** - * Unused + * Initializes a sha512sum struct + * @param sha512sum* */ void ak_fs_sha512sum_init(sha512sum*); + /** * Unused */ @@ -278,28 +305,44 @@ void ak_fs_map_v3_init(akfs_map_v3*); void ak_fs_maps_v3_init(akfs_map_v3**, size_t); /** - * returns: boolean - * param: akfs_map_v3 + * @param akfs_map_v3 + * @return boolean */ bool ak_fs_map_v3_is_null(akfs_map_v3*); + /** - * Unused + * Gets filename out of the akfs_map_v3 + * @param akfs_map_v3* + * @return pointer to char */ char* ak_fs_map_v3_get_filename(akfs_map_v3*); + /** - * Unused + * Gets map hash out of the akfs_map_v3 + * @param akfs_map_v3 + * @return pointer to sha512sum */ sha512sum* ak_fs_map_v3_get_map_hash(akfs_map_v3*); + /** - * Unused + * Gets root hash out of the akfs_map_v3 + * @param akfs_map_v3 + * @return pointer to sha512sum */ -char* ak_fs_map_v3_get_root_hash(akfs_map_v3*); +sha512sum* ak_fs_map_v3_get_root_hash(akfs_map_v3*); + /** - * Unused + * Gets original hash out of the akfs_map_v3 + * @param akfs_map_v3 + * @return pointer to sha512sum */ -char* ak_fs_map_v3_get_orig_hash(akfs_map_v3*); +sha512sum* ak_fs_map_v3_get_orig_hash(akfs_map_v3*); + /** - * Unused + * Compares two akfs_map_v3 structs + * @param akfs_map_v3* + * @param akfs_map_v3* + * @return bool */ bool ak_fs_map_v3_compare(akfs_map_v3*, akfs_map_v3*); @@ -308,19 +351,12 @@ bool ak_fs_map_v3_compare(akfs_map_v3*, akfs_map_v3*); */ void ak_fs_init_map_v4_store(akfs_map_v4**, size_t); - /** * Unused */ void ak_fs_map_v4_init(akfs_map_v4*); /** - * Initializes an array of sha512sum - */ -void ak_fs_init_map_avail(sha512sum**, size_t); - - -/** * Unused */ bool ak_fs_map_v4_compare(akfs_map_v4*, akfs_map_v4*); @@ -346,19 +382,53 @@ sha512sum* ak_fs_map_v4_get_root_hash(akfs_map_v4*); sha512sum* ak_fs_map_v4_get_orig_hash(akfs_map_v4*); /** - * Returns number of files found in maps fs location + * + * @return size_t Number of files found in maps fs location */ size_t ak_fs_maps_v3_found_in_fs(); /** - * Unused + * Prints a list of the maps (version 3 format) available on the local fs along + * with their root hash and file name. + * + * @return int Status value */ int ak_fs_ls(); /** - * Main function + * Main function for call from other programs + * + * @return int Exit value */ 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 1d30744..8ede3f6 100644 --- a/c_implementation/src/ak_fs.c +++ b/c_implementation/src/ak_fs.c @@ -12,7 +12,7 @@ char* ak_fs_return_hash_path(const char* str) { - if ( ak_fs_verify_input_is_hash(str) ) + if ( ak_fs_verify_input_is_hash(str, strlen(str)) ) { unsigned int i = 0; char *result = malloc((128*2)+1); @@ -43,7 +43,7 @@ char* ak_fs_return_hash_path(const char* str) char* ak_fs_return_hash_dir(const char* str) { - if ( ak_fs_verify_input_is_hash(str) ) + if ( ak_fs_verify_input_is_hash(str, strlen(str)) ) { unsigned int i = 0; char *result = malloc((128*2)+1); @@ -69,19 +69,23 @@ char* ak_fs_return_hash_dir(const char* str) } } -bool ak_fs_verify_input_is_hash(const char* str) +bool ak_fs_verify_input_is_hash(const char* str, size_t len) { size_t i = 0; + if (len != 128) + { + return false; + } while ( str[i] != '\0' ) { if ( i < 128 && !( ( str[i] >= 0x30 ) && - (( str[i] <= 0x39) || ( str[i] >= 0x61 )) && + ( str[i] <= 0x39 || str[i] >= 0x61 ) && ( str[i] <= 0x66 ) - ) - ) + ) + ) { return false; } @@ -107,7 +111,7 @@ int ak_fs_create_dir_for_hash(const char* str) * 2. We might need to "lock" onto some version of glibc and be aware of * other systems that do not use that one. */ - if ( ak_fs_verify_input_is_hash(str) ) + if ( ak_fs_verify_input_is_hash(str, strlen(str)) ) { char* dir_path = ak_fs_return_hash_dir(str); // We will need to separate the string so we can create the path one @@ -180,12 +184,10 @@ int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_ma { if ( str[i] == ' ' ) { - // spaces_found++; sa[++spaces_found] = i; } if ( str[i] == '\n' ) { - // newlines_found++; na[++newlines_found] = i; } } @@ -196,7 +198,7 @@ int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_ma si++; } original_hash_str[si] = '\0'; - if( !ak_fs_verify_input_is_hash(original_hash_str) ) + if( !ak_fs_verify_input_is_hash(original_hash_str, strlen(original_hash_str)) ) { ak_log_error(__func__, "original_hash_str not a hash"); return 1; @@ -213,7 +215,7 @@ int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_ma si++; } root_hash_str[si] = '\0'; - if( !ak_fs_verify_input_is_hash(root_hash_str) ) + if( !ak_fs_verify_input_is_hash(root_hash_str, strlen(root_hash_str)) ) { ak_log_error(__func__, "root_hash_str not a hash"); return 1; @@ -237,56 +239,26 @@ int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_ma return 0; } -void ak_fs_get_available_maps_from_fs(sha512sum **ma, size_t length) +void ak_fs_maps_v3_get_from_fs(akfs_map_v3 **ma, size_t length) { + (void)length; DIR *d; d = opendir(ak_fs_maps_v3_get_dir()); - sha512sum *ptr = NULL; + akfs_map_v3 *ptr = NULL; + ptr = *ma; if (d) { - for ( ptr = *ma; ptr < *ma+length; ++ptr) + const struct dirent *dir; + while((dir = readdir(d)) != NULL ) { - const struct dirent *dir; - if ((dir = readdir(d)) == NULL ) break; - if (!ak_fs_verify_input_is_hash(dir->d_name)) continue; - ak_fs_sha512sum_string_to_struct(dir->d_name, ptr); + if (!ak_fs_verify_input_is_hash(dir->d_name, 128)) continue; + ak_fs_sha512sum_string_to_struct(dir->d_name, &(ptr->mh)); + ++ptr; } } closedir(d); } -int ak_fs_map_v3_resolve_maps(akfs_map_v3 **ms, size_t ms_len) -{ - akfs_map_v3 *ptr = NULL; - for ( ptr = *ms; ptr < *ms+ms_len; ++ptr) - { - if ( ak_fs_sha512sum_is_null(&(ptr->mh)) ) - { - continue; - } - if( ak_fs_map_v3_open_from_file(ptr) != 2) - { - ++(ptr); - continue; - } - else - { - ++(ptr); - // return 1; - } - } - return 0; -} - -// void ak_fs_print_available_maps(sha512sum **ma, size_t ma_len) -// { -// sha512sum *ptr = NULL; -// for ( ptr = *ma; ptr < *ma+ma_len; ++ptr) -// { -// ak_log_debug(__func__, ak_fs_sha512sum_struct_read_as_string(ptr)); -// } -// } - void ak_fs_init_string(char *str, size_t len) { for (size_t i = 0; i < len; ++i) @@ -295,38 +267,117 @@ void ak_fs_init_string(char *str, size_t len) } } -// void ak_fs_print_map_avail(const sha512sum* m) -// { -// printf(" .MA: %s\n", ak_fs_sha512sum_struct_read_as_string(m)); -// } -// -// void ak_fs_print_map_all_avail(sha512sum** m, size_t s) -// { -// sha512sum *ptr = NULL; -// for (ptr = *m; ptr < *m+s; ++ptr) -// { -// ak_fs_print_map_avail(ptr); -// } -// } - int ak_fs_ls() { size_t len = ak_fs_maps_v3_found_in_fs(); - printf("Found: %lu\n", len); akfs_map_v3 map_store[len]; akfs_map_v3* maps_ptr = &map_store[0]; - void* mps_start = &map_store[0]; - (void)mps_start; ak_fs_maps_v3_init(&maps_ptr, len); - ak_fs_maps_v3_print(&maps_ptr, len); - - // TODO Rename the following to "ak_fs_resolve_map_v3_array" or close to it - ak_fs_map_v3_resolve_maps(&maps_ptr, len); + ak_fs_maps_v3_get_from_fs(&maps_ptr, len); + ak_fs_maps_v3_resolve(&maps_ptr, len); + ak_fs_maps_v3_print_bif(&maps_ptr, len); + return 0; +} - // TODO Decide what we should be printing - // Possibly, something like "maphex(6)_filename" so we can put multiple - // files with the same name into the list - ak_fs_maps_v3_print(&maps_ptr, len); - ak_fs_maps_v3_print_filenames(&maps_ptr, len); +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_defuse.c b/c_implementation/src/ak_fs_defuse.c index 10617cc..76514f6 100644 --- a/c_implementation/src/ak_fs_defuse.c +++ b/c_implementation/src/ak_fs_defuse.c @@ -50,7 +50,7 @@ static int akfs_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler void* mps_start = &map_store[0]; (void)mps_start; ak_fs_maps_v3_init(&mps_ptr, ms_len); - ak_fs_map_v3_resolve_maps(&mps_ptr, ms_len); + ak_fs_maps_v3_resolve(&mps_ptr, ms_len); akfs_map_v3 *ptr = NULL; for (ptr = mps_ptr; ptr < mps_ptr + ms_len; ++ptr) { 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 1b26d89..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> @@ -84,15 +85,27 @@ sha512sum* ak_fs_map_v3_get_map_hash(akfs_map_v3 *map) return &(map->mh); } -char* ak_fs_map_v3_get_root_hash(akfs_map_v3 *map) +sha512sum* ak_fs_map_v3_get_root_hash(akfs_map_v3 *map) { if (!ak_fs_sha512sum_is_null(&(map->rh))) { - return ak_fs_sha512sum_struct_read_as_string(&(map->rh)); + return &(map->rh); } else { - return ""; + return NULL; + } +} + +sha512sum* ak_fs_map_v3_get_orig_hash(akfs_map_v3 *map) +{ + if (!ak_fs_sha512sum_is_null(&(map->oh))) + { + return &(map->oh); + } + else + { + return NULL; } } @@ -103,12 +116,12 @@ char* ak_fs_map_v3_get_filename(akfs_map_v3 *map) void ak_fs_map_v3_print_filename(akfs_map_v3 *map) { - printf(" .fn: %s\n", ak_fs_map_v3_get_filename(map)); + printf("%s", ak_fs_map_v3_get_filename(map)); } void ak_fs_map_v3_print(akfs_map_v3 *map) { - printf("map_v3 {\n"); + printf("map_v3 {"); printf("\n .mh: "); ak_fs_map_v3_print_map_hash(map); printf("\n .oh: "); @@ -126,21 +139,29 @@ void ak_fs_map_v3_print_as_json(akfs_map_v3 *map) printf("\"map\":\""); ak_fs_map_v3_print_map_hash(map); printf("\","); - printf("{\"original\":\""); + printf("\"original\":\""); ak_fs_map_v3_print_original_hash(map); printf("\","); - printf("{\"root\":\""); + printf("\"root\":\""); ak_fs_map_v3_print_root_hash(map); printf("\","); - printf("{\"filename\":\""); + printf("\"filename\":\""); ak_fs_map_v3_print_filename(map); printf("\""); printf("}\n"); } +void ak_fs_map_v3_print_bif(akfs_map_v3 *map) +{ + ak_fs_map_v3_print_map_hash(map); + printf(" "); + ak_fs_map_v3_print_root_hash(map); + printf(" "); + ak_fs_map_v3_print_filename(map); +} + int ak_fs_map_v3_open_from_file(akfs_map_v3 * map) { - ak_log_debug(__func__, "Started"); if (map==0x0) { ak_log_debug(__func__, "Zeropointer"); @@ -159,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); @@ -175,53 +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); - return 0; -} - -int ak_fs_open_map_v3_file(char* maphash, akfs_map_v3 * map) -{ - ak_log_debug(__func__, "Started"); - if (map==0x0) - { - ak_log_debug(__func__, "Zeropointer"); - return 1; - } - if ( !ak_fs_verify_input_is_hash(maphash) ) - { - ak_log_debug(__func__,"not a hash"); - return 1; - } - FILE *fd; - char *full_path = {0}; - asprintf(&full_path, "%s/%s", ak_fs_maps_v3_get_dir(), maphash); - printf("Trying path: %s\n", full_path); - fd = fopen(full_path, "rb"); - if (!fd) - { - // perror("fopen"); - ak_log_debug(__func__, "file not found"); - return 1; - } - struct stat sb; - if (stat(full_path, &sb) == -1) { - perror("stat"); - fclose(fd); - return 2; - } - // File size: %lld in bytes: (long long) sb.st_size); - char buffer[(long long) sb.st_size+1]; - fread(&buffer, sizeof(buffer), (long long) sb.st_size, fd); - ak_fs_sha512sum_string_to_struct(maphash, &(map->mh)); - if ( ak_fs_convert_map_v3_string_to_struct(buffer, strlen(buffer), map) != 0 ) - { - ak_log_debug(__func__,"conversion failed"); - fclose(fd); - return 1; - } - fclose(fd); + free(full_path); return 0; } diff --git a/c_implementation/src/ak_fs_maps_v3.c b/c_implementation/src/ak_fs_maps_v3.c index 9b047b0..a5060b2 100644 --- a/c_implementation/src/ak_fs_maps_v3.c +++ b/c_implementation/src/ak_fs_maps_v3.c @@ -1,6 +1,8 @@ -#include <libakfs.h> #include <stdlib.h> +#include <stdio.h> +#include <string.h> #include <dirent.h> +#include "libakfs.h" const char* ak_fs_maps_v3_get_dir() { @@ -30,7 +32,11 @@ void ak_fs_maps_v3_print_filenames(akfs_map_v3** m, size_t s) akfs_map_v3 *ptr = NULL; for (ptr = *m; ptr < *m+s; ++ptr) { - if ( ptr != NULL ) ak_fs_map_v3_print_filename(ptr); + if ( ptr != NULL ) + { + ak_fs_map_v3_print_filename(ptr); + printf("\n"); + } } } @@ -40,10 +46,28 @@ void ak_fs_maps_v3_print(akfs_map_v3 **map_store, size_t length) for ( ptr = *map_store; ptr < *map_store + length; ++ptr) { ak_fs_map_v3_print(ptr); + printf("\n"); + } +} + +void ak_fs_maps_v3_print_as_json(akfs_map_v3 **map_store, size_t length) +{ + akfs_map_v3 *ptr = NULL; + for ( ptr = *map_store; ptr < *map_store + length; ++ptr) + { ak_fs_map_v3_print_as_json(ptr); } } +void ak_fs_maps_v3_print_bif(akfs_map_v3 **map_store, size_t length) +{ + akfs_map_v3 *ptr = NULL; + for ( ptr = *map_store; ptr < *map_store + length; ++ptr) + { + ak_fs_map_v3_print_bif(ptr); + printf("\n"); + } +} size_t ak_fs_maps_v3_found_in_fs() { @@ -55,9 +79,20 @@ size_t ak_fs_maps_v3_found_in_fs() const struct dirent *dir; while ((dir = readdir(d)) != NULL ) { - if (ak_fs_verify_input_is_hash(dir->d_name)) counter++; + if (ak_fs_verify_input_is_hash(dir->d_name, strlen(dir->d_name))) counter++; } } closedir(d); return counter; } + +int ak_fs_maps_v3_resolve(akfs_map_v3 **ms, size_t ms_len) +{ + akfs_map_v3 *ptr = NULL; + for ( ptr = *ms; ptr < *ms+ms_len; ++ptr) + { + if ( ak_fs_sha512sum_is_null(&(ptr->mh)) ) continue; + ak_fs_map_v3_open_from_file(ptr); + } + 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, &am |