From 5f8db9fb7fe82e00fa7310ef40d38d1ae734b9af Mon Sep 17 00:00:00 2001 From: kaotisk Date: Thu, 18 Jul 2024 07:19:27 +0300 Subject: ak_fs_create_dir_for_hash rough implementation (leaky leaky) --- src/akfs.c | 71 +++++++++++++++++++++++++++++++++++++++++++-- src/build.sh | 4 +++ src/tests/test_akfs_mkdir.c | 38 ++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/tests/test_akfs_mkdir.c (limited to 'src') diff --git a/src/akfs.c b/src/akfs.c index d8f8955..6388a35 100644 --- a/src/akfs.c +++ b/src/akfs.c @@ -2,7 +2,9 @@ #include #include #include +#include #include +#include char* ak_fs_return_hash_path(char* string) { @@ -87,8 +89,73 @@ bool ak_fs_verify_input_is_hash(char* string) int ak_fs_create_dir_for_hash(char* string) { - (void) string; - return -1; + /* 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(string) ) + { + char* dir_path = ak_fs_return_hash_dir(string); + // 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); + } + } + //printf("%d\n", len); + //printf("%s\n", dir_path); + //const char *pathname = dir_path; + //int ec = mkdir(pathname, 0777); + //return ec; + free(dir_path); + return 0; + } + else + { + return -2; + } } sha512sum ak_fs_sha512sum_string_to_struct(char* string) diff --git a/src/build.sh b/src/build.sh index cccc9e6..2c20c8e 100755 --- a/src/build.sh +++ b/src/build.sh @@ -10,3 +10,7 @@ echo "Building tests/test_akfs" && \ gcc -Wextra -Wall -Werror -pedantic -ggdb -Wl,-rpath=lib -I./include tests/test_akfs.c lib/akfs.so -o tests/test_akfs && \ echo "Running test_akfs" && \ time ./tests/test_akfs +echo "Building tests/test_akfs_mkdir.c" && \ +gcc -Wextra -Wall -Werror -pedantic -ggdb -Wl,-rpath=lib -I./include tests/test_akfs_mkdir.c lib/akfs.so -o tests/test_akfs_mkdir && \ +echo "Running test_akfs_mkdir" && \ +time ./tests/test_akfs_mkdir diff --git a/src/tests/test_akfs_mkdir.c b/src/tests/test_akfs_mkdir.c new file mode 100644 index 0000000..2ef86b3 --- /dev/null +++ b/src/tests/test_akfs_mkdir.c @@ -0,0 +1,38 @@ +#include +#include + +void test_non_hash_string(){ + char *path = "tes"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); + path = "tes/t"; + ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +void test_hash_string(){ + char *path = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +void test_hash_string2(){ + char *path = "ee2ab0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +void test_hash_string3(){ + char *path = "ee2ab0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a800"; + int ec = ak_fs_create_dir_for_hash(path); + printf("mkdir return code: %d\n", ec); +} + +int main() +{ + test_non_hash_string(); + test_hash_string(); + test_hash_string2(); + test_hash_string3(); + return 0; +} -- cgit v1.2.3