diff options
| author | kaotisk <kaotisk@arching-kaos.org> | 2025-04-29 04:16:08 +0300 | 
|---|---|---|
| committer | kaotisk <kaotisk@arching-kaos.org> | 2025-04-29 04:16:08 +0300 | 
| commit | 6f2616e0069354c1e415f513144f944b3f3823d8 (patch) | |
| tree | 1d9910f56127bf5f5c4c9b96ddfe934370a89760 /c_implementation | |
| parent | e61fa65b03801bb66bc47fc793c8991bd9a1f471 (diff) | |
| download | arching-kaos-tools-6f2616e0069354c1e415f513144f944b3f3823d8.tar.gz arching-kaos-tools-6f2616e0069354c1e415f513144f944b3f3823d8.tar.bz2 arching-kaos-tools-6f2616e0069354c1e415f513144f944b3f3823d8.zip  | |
Outputs a file from akfs successfully as base64
Diffstat (limited to 'c_implementation')
| -rw-r--r-- | c_implementation/src/ak_fs.c | 83 | ||||
| -rw-r--r-- | c_implementation/src/ak_fs_mt.c | 18 | 
2 files changed, 72 insertions, 29 deletions
diff --git a/c_implementation/src/ak_fs.c b/c_implementation/src/ak_fs.c index fdf110d..3c620fc 100644 --- a/c_implementation/src/ak_fs.c +++ b/c_implementation/src/ak_fs.c @@ -281,44 +281,73 @@ int ak_fs_ls()  int ak_fs_cat_file_from_root_hash(sha512sum* rh)  { -    printf("%s: %s\n", __func__, getenv("AK_CHUNKSDIR")); +    const char* chunks_dir = getenv("AK_CHUNKSDIR");      const char* leafs_dir = getenv("AK_LEAFSDIR"); -    // We always expect root hash to be in the AK_LEAFSDIR directory, however it -    // might as well not be there      FILE *fd; -    // We need to join the root_hash with the directory first      char *fullpath; -    asprintf(&fullpath, "%s/%s", leafs_dir, ak_fs_sha512sum_struct_read_as_string(rh)); +    bool is_chunk = false; +    if ( asprintf(&fullpath, "%s/%s", leafs_dir, ak_fs_sha512sum_struct_read_as_string(rh))  == -1 ) return -1;      fd = fopen(fullpath, "r");      if ( fd == NULL )      { -        perror("fopen"); -        return 1; +        // perror("fopen"); +        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"); +            return 1; +        } +        is_chunk = true;      } -    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 = *rh; -    for( size_t i = 0; i < 128; ++i ) +    if ( !is_chunk )      { -        h1[i] = buffer[i]; +        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' ) +        { +            ak_log_error(__func__, "Unknown format"); +            return 2; +        } +        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); +        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);      } -    h1[128] = '\0'; -    for( size_t i = 0; i < 128; ++i ) +    else      { -        h2[i] = buffer[i+129]; +        struct stat sb; +        if (stat(fullpath, &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), 1, fd); +        fclose(fd); +        buffer[sizeof(buffer)-1] = '\0'; +        printf("%s", buffer);      } -    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);      // 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 index 7bdd6f4..e194980 100644 --- a/c_implementation/src/ak_fs_mt.c +++ b/c_implementation/src/ak_fs_mt.c @@ -1,5 +1,6 @@  #include <stdio.h>  #include <libakfs.h> +#include <libaklog.h>  #include <stdlib.h>  bool ak_fs_mt_branch_is_null(mt_branch* node) @@ -28,12 +29,19 @@ bool ak_fs_mt_branch_compare(mt_branch *a, mt_branch *b)      return false;  } +void ak_fs_mt_branch_print(mt_branch *n) +{ +    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)); +} +  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)); +    if ( asprintf(&fullpath, "%s/%s", leafs_dir, ak_fs_sha512sum_struct_read_as_string(&node->head)) == -1 ) return -1;      fd = fopen(fullpath, "r");      if ( fd == NULL )      { @@ -43,9 +51,14 @@ int ak_fs_mt_branch_resolve(mt_branch *node)      char buffer[258];      fread(&buffer, sizeof(buffer), 1, fd);      fclose(fd); +    free(fullpath);      char h1[129] = {0};      char h2[129] = {0}; -    if ( buffer[128] == '\n' && buffer[257] == '\n' ) printf("\\n found on the expected spot!\n"); +    if ( buffer[128] != '\n' && buffer[257] != '\n' ) +    { +        ak_log_error(__func__, "Unknown format"); +        return 2; +    }      mt_branch h0;      ak_fs_sha512sum_init(&h0.root);      ak_fs_sha512sum_init(&h0.head); @@ -63,6 +76,7 @@ int ak_fs_mt_branch_resolve(mt_branch *node)      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_print(&h0);      ak_fs_mt_branch_resolve(&h0);      return 0;  }  | 
