aboutsummaryrefslogtreecommitdiff
path: root/c_implementation/src/akfs.c
blob: f9abc3492ff0a4b0b4af4f18b6f27bdcf3e2b2d9 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <libakfs.h>
#include <libaklog.h>
#include <libaksettings.h>
#include <sys/stat.h>
#include <dirent.h>

const char* ak_fs_maps_v3_get_dir()
{
    return getenv("AK_MAPSDIR");
}

char* ak_fs_return_hash_path(const char* str)
{
    if ( ak_fs_verify_input_is_hash(str) )
    {
        unsigned int i = 0;
        char *result = malloc((128*2)+1);
        if ( result == NULL )
        {
            return "";
        }
        while ( str[i] != '\0' )
        {
            result[i*2] = str[i];
            if ( (i*2) + 1 <= 254 )
            {
                result[(i*2)+1] = '/';
            }
            else
            {
                result[(i*2)+1] = '\0';
            }
            ++i;
        }
        return result;
    }
    else
    {
        return NULL;
    }
}

char* ak_fs_return_hash_dir(const char* str)
{
    if ( ak_fs_verify_input_is_hash(str) )
    {
        unsigned int i = 0;
        char *result = malloc((128*2)+1);
        if ( result == NULL ) return "";
        while ( str[i] != '\0' )
        {
            result[i*2] = str[i];
            if ( (i*2) + 1 <= 254-2 )
            {
                result[(i*2)+1] = '/';
            }
            else
            {
                result[(i*2)+1] = '\0';
            }
            ++i;
        }
        return result;
    }
    else
    {
        return NULL;
    }
}

bool ak_fs_verify_input_is_hash(const char* str)
{
    size_t i = 0;
    while ( str[i] != '\0' )
    {
        if (
                i < 128 &&
                !(
                    ( str[i] >= 0x30 ) &&
                    (( str[i] <= 0x39) || ( str[i] >= 0x61 )) &&
                    ( str[i] <= 0x66 )
                )
            )
        {
            return false;
        }
        else {
            i++;
        }
    }
    if ( i > 128 )
    {
        return false;
    }
    return true;
}

int ak_fs_create_dir_for_hash(const char* str)
{
    /* TODO
     * Some aspects of this function
     * 1. We need a "root" place to put our dirs into, this is not specified
     *    anywhere in this code but it is spartially specified in other files
     *    like lib/_ak_fs bash script and the rc/config file we currently source
     *    in $HOME/.bashrc
     * 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) )
    {
        char* dir_path = ak_fs_return_hash_dir(str);
        // We will need to separate the string so we can create the path one
        // directory at the time
        int len = strlen(dir_path);
        for ( int i = 0; i < len+1; ++i)
        {
            if ( dir_path[i] == '/' )
            {
                //printf("%c\n", dir_path[i]);
                //char* test = strndup(dir_path, i);
                //printf("A: [i:%d] [c:%c] - %s\n", i, dir_path[i], test);
                continue;
            }
            else
            {
                char* incremental_dir_path = strndup(dir_path, i+1);
                // printf("B: [i:%d] [c:%c] - %s\n", i, dir_path[i], test);
                struct stat sb;
                if (stat(incremental_dir_path, &sb) == 0 && S_ISDIR(sb.st_mode))
                {
                    continue;
                }
                else
                {
                    int return_code = mkdir(incremental_dir_path, 0777);
                    if ( return_code == 0 )
                    {
                        continue;
                    }
                    else
                    {
                        // should be unreachable I guess since previous checks
                        // though it could be caused by some other kind of error
                        // like, no permission, or exists but is not a directory
                        // but a file, dev, char, pipe whatever this thing
                        // supports anyway
                        free(incremental_dir_path);
                        free(dir_path);
                        return -3;
                    }
                }
                free(incremental_dir_path);
            }
        }
        free(dir_path);
        return 0;
    }
    else
    {
        return -2;
    }
}

int ak_fs_convert_map_v3_string_to_struct(const char *str, size_t ssize, akfs_map_v3* map)
{
    size_t sa[] = { -1, -1, -1, -1, -1 };
    size_t na[] = { -1, -1, -1 };
    int spaces_found = 0;
    int newlines_found = 0;
    char original_hash_str[129] = {0};
    char root_hash_str[129] = {0};
    char filename[256] = {0};
    if ( map == 0x0 )
    {
        printf("FAILED IN %s! : %p \n", __func__, (void*)map);
        return 1;
    }
    for ( size_t i = 0; i < ssize; ++i)
    {
        if ( str[i] == ' ' )
        {
            spaces_found++;
            sa[spaces_found] = i;
        }
        if ( str[i] == '\n' )
        {
            newlines_found++;
            na[newlines_found] = i;
        }
    }
    int si = 0;
    for ( size_t i = 0; i < sa[1]; ++i)
    {
        original_hash_str[si] = str[i];
        si++;
    }
    original_hash_str[si] = '\0';
    if( !ak_fs_verify_input_is_hash(original_hash_str) )
    {
        ak_log_error(__func__, "original_hash_str not a hash");
        return 1;
    }
    if ( ak_fs_sha512sum_string_to_struct(original_hash_str, &(map->oh)) != 0 )
    {
        ak_log_error(__func__, "String convertion of original_hash_str");
        return 1;
    }
    si = 0;
    for ( size_t i = na[1]+1; i < sa[3]; ++i)
    {
        root_hash_str[si] = str[i];
        si++;
    }
    root_hash_str[si] = '\0';
    if( !ak_fs_verify_input_is_hash(root_hash_str) )
    {
        ak_log_error(__func__, "root_hash_str not a hash");
        return 1;
    }
    if ( ak_fs_sha512sum_string_to_struct(root_hash_str, &(map->rh)) != 0 )
    {
        ak_log_error(__func__, "String convertion of root_hash_str");
        return 1;
    }
    si = 0;
    if ( sa[2] < na[1] )
    {
        for ( size_t i = sa[2]+1; i < na[1]; ++i)
        {
            filename[si] = str[i];
            si++;
        }
        filename[si] = '\0';
    }
    strncpy(map->filename, filename, sizeof(map->filename));
    return 0;
}

void ak_fs_get_available_maps_from_fs(sha512sum **ma, size_t length)
{
    DIR *d;
    d = opendir(ak_fs_maps_v3_get_dir());
    sha512sum *ptr = NULL;
    if (d)
    {
        for ( ptr = *ma; ptr < *ma+length; ++ptr)
        {
            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);
        }
    }
    closedir(d);
}

int ak_fs_load_available_maps(sha512sum **ma, size_t ma_len, akfs_map_v3 **ms, size_t ms_len)
{
    sha512sum *ptr = NULL;
    akfs_map_v3 *mptr = NULL;
    mptr = *ms;
    (void)ms_len;
    for ( ptr = *ma; ptr < *ma+ma_len; ++ptr)
    {
        if ( ak_fs_sha512sum_is_null(ptr))
        {
            continue;
        }
        if( ak_fs_open_map_v3_file(ak_fs_sha512sum_struct_read_as_string(ptr), mptr) != 2)
        {
            ++(mptr);
            continue;
        }
        else
        {
            ++(mptr);
            // return 1;
        }
    }
    return 0;
}

void ak_fs_print_loaded_maps(akfs_map_v3 **map_store, size_t length)
{
    akfs_map_v3 *ptr = NULL;
    for ( ptr = *map_store; ptr < *map_store + length; ++ptr)
    {
        if ( !ak_fs_map_v3_is_null(ptr) ) ak_fs_map_v3_print(ptr);
    }
}

bool ak_fs_map_v3_compare(akfs_map_v3* a, akfs_map_v3* b)
{
    return (
            (ak_fs_sha512sum_compare(&a->oh, &b->oh) == true) &&
            (ak_fs_sha512sum_compare(&a->rh, &b->rh) == true) &&
            (ak_fs_sha512sum_compare(&a->mh, &b->mh) == true) &&
            (strcmp(a->filename, b->filename) == 0)
           );
}

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_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)
    {
        str[i] = '\0';
    }
}


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);
    }
}

void ak_fs_print_filenames_from_map_store(akfs_map_v3** m,