aboutsummaryrefslogtreecommitdiff
path: root/c_implementation/src/ak_fs_mt.c
blob: e19498043891e13955b16321354712579084cf41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <libakfs.h>
#include <libaklog.h>
#include <stdlib.h>

bool ak_fs_mt_branch_is_null(mt_branch* node)
{
    if (
            ak_fs_sha512sum_is_null(&node->root) &&
            ak_fs_sha512sum_is_null(&node->head) &&
            ak_fs_sha512sum_is_null(&node->tail)
       )
    {
        return true;
    }
    return false;
}

bool ak_fs_mt_branch_compare(mt_branch *a, mt_branch *b)
{
    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)
{
    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;
    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 )
    {
        perror("fopen");
        return 1;
    }
    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' )
    {
        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 = node->root;
    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_mt_branch_print(&h0);
    ak_fs_mt_branch_resolve(&h0);
    return 0;
}