aboutsummaryrefslogtreecommitdiff
path: root/c_implementation/src
diff options
context:
space:
mode:
Diffstat (limited to 'c_implementation/src')
-rw-r--r--c_implementation/src/akfs.c117
-rw-r--r--c_implementation/src/akfs_map_v3.c23
-rw-r--r--c_implementation/src/akfs_sha512sum.c60
3 files changed, 118 insertions, 82 deletions
diff --git a/c_implementation/src/akfs.c b/c_implementation/src/akfs.c
index f1dcd38..f9abc34 100644
--- a/c_implementation/src/akfs.c
+++ b/c_implementation/src/akfs.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <unistd.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
@@ -14,15 +15,19 @@ const char* ak_fs_maps_v3_get_dir()
return getenv("AK_MAPSDIR");
}
-char* ak_fs_return_hash_path(char* string)
+char* ak_fs_return_hash_path(const char* str)
{
- if ( ak_fs_verify_input_is_hash(string) )
+ if ( ak_fs_verify_input_is_hash(str) )
{
unsigned int i = 0;
char *result = malloc((128*2)+1);
- while ( string[i] != '\0' )
+ if ( result == NULL )
{
- result[i*2] = string[i];
+ return "";
+ }
+ while ( str[i] != '\0' )
+ {
+ result[i*2] = str[i];
if ( (i*2) + 1 <= 254 )
{
result[(i*2)+1] = '/';
@@ -41,15 +46,16 @@ char* ak_fs_return_hash_path(char* string)
}
}
-char* ak_fs_return_hash_dir(char* string)
+char* ak_fs_return_hash_dir(const char* str)
{
- if ( ak_fs_verify_input_is_hash(string) )
+ if ( ak_fs_verify_input_is_hash(str) )
{
unsigned int i = 0;
char *result = malloc((128*2)+1);
- while ( string[i] != '\0' )
+ if ( result == NULL ) return "";
+ while ( str[i] != '\0' )
{
- result[i*2] = string[i];
+ result[i*2] = str[i];
if ( (i*2) + 1 <= 254-2 )
{
result[(i*2)+1] = '/';
@@ -68,17 +74,17 @@ char* ak_fs_return_hash_dir(char* string)
}
}
-bool ak_fs_verify_input_is_hash(char* string)
+bool ak_fs_verify_input_is_hash(const char* str)
{
size_t i = 0;
- while ( string[i] != '\0' )
+ while ( str[i] != '\0' )
{
if (
i < 128 &&
!(
- ( string[i] >= 0x30 ) &&
- (( string[i] <= 0x39) || ( string[i] >= 0x61 )) &&
- ( string[i] <= 0x66 )
+ ( str[i] >= 0x30 ) &&
+ (( str[i] <= 0x39) || ( str[i] >= 0x61 )) &&
+ ( str[i] <= 0x66 )
)
)
{
@@ -95,7 +101,7 @@ bool ak_fs_verify_input_is_hash(char* string)
return true;
}
-int ak_fs_create_dir_for_hash(char* string)
+int ak_fs_create_dir_for_hash(const char* str)
{
/* TODO
* Some aspects of this function
@@ -106,9 +112,9 @@ int ak_fs_create_dir_for_hash(char* string)
* 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(string) )
+ if ( ak_fs_verify_input_is_hash(str) )
{
- char* dir_path = ak_fs_return_hash_dir(string);
+ 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);
@@ -161,28 +167,28 @@ int ak_fs_create_dir_for_hash(char* string)
}
}
-int ak_fs_convert_map_v3_string_to_struct(char *string, size_t ssize, akfs_map_v3* map)
+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_string[129] = {0};
- char root_hash_string[129] = {0};
+ char original_hash_str[129] = {0};
+ char root_hash_str[129] = {0};
char filename[256] = {0};
- if ( map == 0x0 || map == NULL)
+ if ( map == 0x0 )
{
printf("FAILED IN %s! : %p \n", __func__, (void*)map);
return 1;
}
for ( size_t i = 0; i < ssize; ++i)
{
- if ( string[i] == ' ' )
+ if ( str[i] == ' ' )
{
spaces_found++;
sa[spaces_found] = i;
}
- if ( string[i] == '\n' )
+ if ( str[i] == '\n' )
{
newlines_found++;
na[newlines_found] = i;
@@ -191,35 +197,35 @@ int ak_fs_convert_map_v3_string_to_struct(char *string, size_t ssize, akfs_map_v
int si = 0;
for ( size_t i = 0; i < sa[1]; ++i)
{
- original_hash_string[si] = string[i];
+ original_hash_str[si] = str[i];
si++;
}
- original_hash_string[si] = '\0';
- if( !ak_fs_verify_input_is_hash(original_hash_string) )
+ original_hash_str[si] = '\0';
+ if( !ak_fs_verify_input_is_hash(original_hash_str) )
{
- ak_log_error(__func__, "original_hash_string not a hash");
+ ak_log_error(__func__, "original_hash_str not a hash");
return 1;
}
- if ( ak_fs_sha512sum_string_to_struct(original_hash_string, &(map->oh)) != 0 )
+ if ( ak_fs_sha512sum_string_to_struct(original_hash_str, &(map->oh)) != 0 )
{
- ak_log_error(__func__, "String convertion of original_hash_string");
+ 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_string[si] = string[i];
+ root_hash_str[si] = str[i];
si++;
}
- root_hash_string[si] = '\0';
- if( !ak_fs_verify_input_is_hash(root_hash_string) )
+ root_hash_str[si] = '\0';
+ if( !ak_fs_verify_input_is_hash(root_hash_str) )
{
- ak_log_error(__func__, "root_hash_string not a hash");
+ ak_log_error(__func__, "root_hash_str not a hash");
return 1;
}
- if ( ak_fs_sha512sum_string_to_struct(root_hash_string, &(map->rh)) != 0 )
+ if ( ak_fs_sha512sum_string_to_struct(root_hash_str, &(map->rh)) != 0 )
{
- ak_log_error(__func__, "String convertion of root_hash_string");
+ ak_log_error(__func__, "String convertion of root_hash_str");
return 1;
}
si = 0;
@@ -227,7 +233,7 @@ int ak_fs_convert_map_v3_string_to_struct(char *string, size_t ssize, akfs_map_v
{
for ( size_t i = sa[2]+1; i < na[1]; ++i)
{
- filename[si] = string[i];
+ filename[si] = str[i];
si++;
}
filename[si] = '\0';
@@ -239,14 +245,13 @@ int ak_fs_convert_map_v3_string_to_struct(char *string, size_t ssize, akfs_map_v
void ak_fs_get_available_maps_from_fs(sha512sum **ma, size_t length)
{
DIR *d;
- struct dirent *dir;
d = opendir(ak_fs_maps_v3_get_dir());
sha512sum *ptr = NULL;
- ptr = *ma;
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);
@@ -317,16 +322,16 @@ void ak_fs_print_available_maps(sha512sum **ma, size_t ma_len)
}
-void ak_fs_init_string(char *string, size_t len)
+void ak_fs_init_string(char *str, size_t len)
{
for (size_t i = 0; i < len; ++i)
{
- string[i] = '\0';
+ str[i] = '\0';
}
}
-void ak_fs_print_map_avail(sha512sum* m)
+void ak_fs_print_map_avail(const sha512sum* m)
{
printf(" .MA: %s\n", ak_fs_sha512sum_struct_read_as_string(m));
}
@@ -384,8 +389,38 @@ int ak_fs_ls()
ak_fs_print_all_maps_from_map_store(&mps_ptr, ms_len);
ak_fs_get_available_maps_from_fs(&mav_ptr, ma_len);
ak_fs_load_available_maps(&mav_ptr, ma_len, &mps_ptr, ms_len);
- // ak_fs_print_loaded_maps(&mps_ptr, ms_len);
+ ak_fs_print_loaded_maps(&mps_ptr, ms_len);
ak_fs_print_filenames_from_map_store(&mps_ptr, ms_len);
return 0;
}
+int ak_fs_usage()
+{
+ ak_log_debug(__func__, "Available commands:");
+ ak_log_debug(__func__, "ak fs --list");
+ return 1;
+}
+
+int ak_fs_main(int c, char** v)
+{
+ (void)c;
+ (void)v;
+ int option;
+ while ( (option = getopt(c, v, ":h|:help")) != -1 )
+ {
+ printf("%d\n", option);
+ switch(option)
+ {
+ case 'h':
+ return ak_fs_usage();
+ case ':':
+ printf("kek\n");
+ return 1;
+ case '?':
+ printf("lol\n");
+ return 2;
+ }
+ }
+ return 0;
+}
+
diff --git a/c_implementation/src/akfs_map_v3.c b/c_implementation/src/akfs_map_v3.c
index 7b7e75b..0eb1ea1 100644
--- a/c_implementation/src/akfs_map_v3.c
+++ b/c_implementation/src/akfs_map_v3.c
@@ -27,9 +27,9 @@ void ak_fs_map_v3_print_original_hash(akfs_map_v3 *map)
{
if (!ak_fs_map_v3_is_null(map))
{
- char string[129];
- ak_fs_sha512sum_struct_to_string(&(map->oh), string);
- printf(" .oh: %s\n", string);
+ char str[129];
+ ak_fs_sha512sum_struct_to_string(&(map->oh), str);
+ printf(" .oh: %s\n", str);
}
else
{
@@ -41,9 +41,9 @@ void ak_fs_map_v3_print_root_hash(akfs_map_v3 *map)
{
if (!ak_fs_map_v3_is_null(map))
{
- char string[129];
- ak_fs_sha512sum_struct_to_string(&(map->rh), string);
- printf(" .rh: %s\n", string );
+ char str[129];
+ ak_fs_sha512sum_struct_to_string(&(map->rh), str);
+ printf(" .rh: %s\n", str );
}
else
{
@@ -67,7 +67,7 @@ void ak_fs_map_v3_print_filename(akfs_map_v3 *map)
{
if (!ak_fs_map_v3_is_null(map))
{
- printf(" .fn: %s\n", ak_fs_map_v3_get_filename(map));
+ printf("%s\n", ak_fs_map_v3_get_filename(map));
}
}
@@ -75,9 +75,9 @@ void ak_fs_map_v3_print_map_hash(akfs_map_v3 *map)
{
if (!ak_fs_map_v3_is_null(map))
{
- char string[129];
- ak_fs_sha512sum_struct_to_string(&(map->mh), string);
- printf(" .mh: %s\n", string );
+ char str[129];
+ ak_fs_sha512sum_struct_to_string(&(map->mh), str);
+ printf(" .mh: %s\n", str );
}
}
@@ -93,7 +93,7 @@ void ak_fs_map_v3_print(akfs_map_v3 *map)
int ak_fs_open_map_v3_file(char* maphash, akfs_map_v3 * map)
{
- if (map==0x0||map==NULL)
+ if (map==0x0)
{
ak_log_debug(__func__, "Zeropointer");
return 1;
@@ -116,6 +116,7 @@ int ak_fs_open_map_v3_file(char* maphash, akfs_map_v3 * map)
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);
diff --git a/c_implementation/src/akfs_sha512sum.c b/c_implementation/src/akfs_sha512sum.c
index 7752bf8..6db6397 100644
--- a/c_implementation/src/akfs_sha512sum.c
+++ b/c_implementation/src/akfs_sha512sum.c
@@ -10,7 +10,7 @@ void ak_fs_sha512sum_init(sha512sum *hash)
}
}
-bool ak_fs_sha512sum_compare(sha512sum* a, sha512sum* b)
+bool ak_fs_sha512sum_compare(const sha512sum* a, const sha512sum* b)
{
for ( int i = 0; i < 8; ++i )
{
@@ -36,23 +36,23 @@ void ak_fs_sha512sum_init_avail(sha512sum** m, size_t s)
}
}
-char* ak_fs_sha512sum_struct_read_as_string(sha512sum *ptr)
+char* ak_fs_sha512sum_struct_read_as_string(const sha512sum *ptr)
{
- char *string = malloc(129*sizeof(char));
- ak_fs_sha512sum_struct_to_string(ptr, string);
- return string;
+ char *str = malloc(129*sizeof(char));
+ ak_fs_sha512sum_struct_to_string(ptr, str);
+ return str;
}
-bool ak_fs_sha512sum_is_null(sha512sum *h)
+bool ak_fs_sha512sum_is_null(const sha512sum *h)
{
sha512sum n;
ak_fs_sha512sum_init(&n);
return ak_fs_sha512sum_compare(h,&n);
}
-int ak_fs_sha512sum_string_to_struct(char* string, sha512sum* hash)
+int ak_fs_sha512sum_string_to_struct(const char* str, sha512sum* hash)
{
- if ( ak_fs_verify_input_is_hash(string) )
+ if ( ak_fs_verify_input_is_hash(str) )
{
for (size_t l = 0; l < 8; ++l)
{
@@ -61,11 +61,11 @@ int ak_fs_sha512sum_string_to_struct(char* string, sha512sum* hash)
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 4;
- while ( string[i] != '\0' )
+ while ( str[i] != '\0' )
{
assert( i < 128 && "Length exceeded limit");
if ( i % 16 == 0 ) j = i / 16;
- switch (string[i])
+ switch (str[i])
{
case 0x30:
hash->sum[j] = hash->sum[j] << k;
@@ -148,9 +148,9 @@ int ak_fs_sha512sum_string_to_struct(char* string, sha512sum* hash)
}
}
-void ak_fs_sha512sum_struct_to_string(sha512sum* hash, char* string)
+void ak_fs_sha512sum_struct_to_string(const sha512sum* hash, char* str)
{
- int counter = 0;
+ int count = 0;
sha512sum lhash = *hash;
for (size_t i = 0; i < 8; ++i)
{
@@ -159,60 +159,60 @@ void ak_fs_sha512sum_struct_to_string(sha512sum* hash, char* string)
long unsigned first = lhash.sum[i]/0xfffffffffffffff;
switch(first){
case 0:
- string[counter] = '0';
+ str[count] = '0';
break;
case 1:
- string[counter] = '1';
+ str[count] = '1';
break;
case 2:
- string[counter] = '2';
+ str[count] = '2';
break;
case 3:
- string[counter] = '3';
+ str[count] = '3';
break;
case 4:
- string[counter] = '4';
+ str[count] = '4';
break;
case 5:
- string[counter] = '5';
+ str[count] = '5';
break;
case 6:
- string[counter] = '6';
+ str[count] = '6';
break;
case 7:
- string[counter] = '7';
+ str[count] = '7';
break;
case 8:
- string[counter] = '8';
+ str[count] = '8';
break;
case 9:
- string[counter] = '9';
+ str[count] = '9';
break;
case 0xa:
- string[counter] = 'a';
+ str[count] = 'a';
break;
case 0xb:
- string[counter] = 'b';
+ str[count] = 'b';
break;
case 0xc:
- string[counter] = 'c';
+ str[count] = 'c';
break;
case 0xd:
- string[counter] = 'd';
+ str[count] = 'd';
break;
case 0xe:
- string[counter] = 'e';
+ str[count] = 'e';
break;
case 0xf:
- string[counter] = 'f';
+ str[count] = 'f';
break;
default:
assert(0 && "Should be unreachable");
}
- counter++;
+ count++;
lhash.sum[i] = lhash.sum[i] << 4;
}
}
- string[128] = '\0';
+ str[128] = '\0';
}