aboutsummaryrefslogtreecommitdiff
path: root/c_implementation/src/akfs_map_v3.c
blob: a422b6c42865f92bce0734cbd74065b3766a7224 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <stdio.h>
#include <string.h>
#include <libakfs.h>
#include <libaklog.h>
#include <sys/stat.h>

// const char maps_dir[] = "/home/kaotisk/.arching-kaos/akfs/maps/";

void ak_fs_map_v3_init(akfs_map_v3 *map)
{
    ak_fs_sha512sum_init(&map->oh);
    ak_fs_sha512sum_init(&map->rh);
    ak_fs_sha512sum_init(&map->mh);
    ak_fs_init_string(map->filename, 256);
}

void ak_fs_map_v3_init_store(akfs_map_v3** ms, size_t l)
{
    akfs_map_v3 *m = NULL;
    for (m = *ms; m < *ms+l; ++m)
    {
        ak_fs_map_v3_init(m);
    }
}

bool ak_fs_map_v3_is_null(akfs_map_v3* m)
{
    akfs_map_v3 n;
    ak_fs_map_v3_init(&n);
    return ak_fs_map_v3_compare(m, &n);
}

void ak_fs_map_v3_print_original_hash(akfs_map_v3 *map)
{
    if (!ak_fs_map_v3_is_null(map))
    {
        char str[129];
        ak_fs_sha512sum_struct_to_string(&(map->oh), str);
        printf(" .oh: %s\n", str);
    }
    else
    {
        ak_log_debug(__func__,"map is null");
    }
}

void ak_fs_map_v3_print_root_hash(akfs_map_v3 *map)
{
    if (!ak_fs_map_v3_is_null(map))
    {
        char str[129];
        ak_fs_sha512sum_struct_to_string(&(map->rh), str);
        printf(" .rh: %s\n", str );
    }
    else
    {
        ak_log_debug(__func__,"map is null");
    }
}

char* ak_fs_map_v3_get_filename(akfs_map_v3 *map)
{
    if (!ak_fs_map_v3_is_null(map))
    {
        return map->filename;
    }
    else
    {
        return "";
    }
}

void ak_fs_map_v3_print_filename(akfs_map_v3 *map)
{
    if (!ak_fs_map_v3_is_null(map))
    {
        printf("%s\n", ak_fs_map_v3_get_filename(map));
    }
}

void ak_fs_map_v3_print_map_hash(akfs_map_v3 *map)
{
    if (!ak_fs_map_v3_is_null(map))
    {
        char str[129];
        ak_fs_sha512sum_struct_to_string(&(map->mh), str);
        printf(" .mh: %s\n", str );
    }
}

void ak_fs_map_v3_print(akfs_map_v3 *map)
{
    printf("map_v3 {\n");
    ak_fs_map_v3_print_map_hash(map);
    ak_fs_map_v3_print_original_hash(map);
    ak_fs_map_v3_print_root_hash(map);
    ak_fs_map_v3_print_filename(map);
    printf("}\n");
}

int ak_fs_open_map_v3_file(char* maphash, akfs_map_v3 * map)
{
    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");
        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);
    return 0;
}

int ak_fs_map_v3_to_file(akfs_map_v3 maphash)
{
    (void)maphash;
    return 0;
}