summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-03-28 05:37:43 +0200
committerMitya Selivanov <automainint@guattari.tech>2023-03-28 05:37:43 +0200
commit3eed4aad889609bb46fcf55284e3af0129e31578 (patch)
tree05b3fdf5aa7a66479863ddd2591cf68ca2bc627f /source
parent7cccceaf6b15981a3f441f55097dac5fa5395ccc (diff)
downloadkit-3eed4aad889609bb46fcf55284e3af0129e31578.zip
[test] cache folder
Diffstat (limited to 'source')
-rw-r--r--source/kit/file.c62
-rw-r--r--source/kit/file.h3
-rw-r--r--source/test/unittests/file.test.c31
3 files changed, 81 insertions, 15 deletions
diff --git a/source/kit/file.c b/source/kit/file.c
index edec2bd..956c539 100644
--- a/source/kit/file.c
+++ b/source/kit/file.c
@@ -26,6 +26,23 @@ static int is_delim(char const c) {
return c == '/' || c == '\\';
}
+static kit_string_t kit_get_env_(char *const name,
+ kit_allocator_t const alloc) {
+ char *const val = getenv(name);
+ ptrdiff_t const size = val != NULL ? (ptrdiff_t) strlen(val) : 0;
+
+ string_t result;
+ DA_INIT(result, size, alloc);
+ assert(result.size == size);
+
+ if (result.size == size && size > 0)
+ memcpy(result.values, val, result.size);
+ else
+ DA_RESIZE(result, 0);
+
+ return result;
+}
+
kit_string_t kit_path_norm(kit_str_t const path,
kit_allocator_t const alloc) {
str_t const parent = SZ("..");
@@ -113,26 +130,41 @@ kit_string_t kit_path_join(kit_str_t const left,
}
kit_string_t kit_path_user(kit_allocator_t const alloc) {
- char *home = getenv(KIT_ENV_HOME);
- ptrdiff_t const size = home != NULL ? (ptrdiff_t) strlen(home) : 0;
-
- string_t user;
- DA_INIT(user, size, alloc);
- assert(user.size == size);
-
- if (user.size == size)
- memcpy(user.values, home, user.size);
- else {
+ kit_string_t user = kit_get_env_(KIT_ENV_HOME, alloc);
+ if (user.size == 0) {
DA_RESIZE(user, 1);
- assert(user.size == 1);
-
if (user.size == 1)
user.values[0] = '.';
}
-
return user;
}
+kit_string_t kit_path_cache(kit_allocator_t alloc) {
+ kit_string_t cache, user;
+
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ user = kit_get_env_("APPDATA", alloc);
+ if (user.size != 0) {
+ cache = kit_path_join(WRAP_STR(user), SZ("Local"), alloc);
+ DA_DESTROY(user);
+ return cache;
+ }
+#endif
+
+#ifdef __APPLE__
+ user = kit_path_user(alloc);
+ cache = kit_path_join(WRAP_STR(user),
+ SZ("Library" PATH_DELIM "Caches"), alloc);
+ DA_DESTROY(user);
+#else
+ user = kit_path_user(alloc);
+ cache = kit_path_join(WRAP_STR(user), SZ(".cache"), alloc);
+ DA_DESTROY(user);
+#endif
+
+ return cache;
+}
+
kit_str_t kit_path_index(kit_str_t const path,
ptrdiff_t const index) {
str_t s = { .size = 0, .values = NULL };
@@ -214,7 +246,7 @@ static void win32_prepare_path_(WCHAR *const buf,
WCHAR buf[PATH_BUF_SIZE]; \
win32_prepare_path_(buf, path)
#else
-static void unix_prepare_path_(char *const buf,
+static void unix_prepare_path_(char *const buf,
kit_str_t const path) {
assert(path.size == 0 || path.values != NULL);
assert(path.size + 1 < PATH_BUF_SIZE);
@@ -370,7 +402,7 @@ kit_file_info_t kit_file_info(kit_str_t const path) {
result.time_modified_sec = (int64_t) info.st_mtim.tv_sec;
result.time_modified_nsec = (int32_t) info.st_mtim.tv_nsec;
# endif
- result.status = KIT_OK;
+ result.status = KIT_OK;
return result;
}
#endif
diff --git a/source/kit/file.h b/source/kit/file.h
index 865e1b8..baa5cbe 100644
--- a/source/kit/file.h
+++ b/source/kit/file.h
@@ -34,6 +34,8 @@ kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
kit_string_t kit_path_user(kit_allocator_t alloc);
+kit_string_t kit_path_cache(kit_allocator_t alloc);
+
kit_str_t kit_path_index(kit_str_t path, ptrdiff_t index);
kit_str_t kit_path_take(kit_str_t path, ptrdiff_t count);
@@ -75,6 +77,7 @@ void kit_path_list_destroy(kit_path_list_t list);
# define path_norm kit_path_norm
# define path_join kit_path_join
# define path_user kit_path_user
+# define path_cache kit_path_cache
# define path_index kit_path_index
# define path_take kit_path_take
# define file_create_folder kit_file_create_folder
diff --git a/source/test/unittests/file.test.c b/source/test/unittests/file.test.c
index 3b6d6d0..2207269 100644
--- a/source/test/unittests/file.test.c
+++ b/source/test/unittests/file.test.c
@@ -11,6 +11,37 @@
# define S_DELIM_ "/"
#endif
+#include <stdio.h>
+
+TEST("file path cache") {
+ kit_allocator_t alloc = kit_alloc_default();
+
+ string_t user = path_user(alloc);
+ string_t cache = path_cache(alloc);
+
+ DA_RESIZE(cache, cache.size + 1);
+ cache.values[cache.size - 1] = '\0';
+ DA_RESIZE(cache, cache.size - 1);
+ printf("\nCache folder: %s\n", cache.values);
+
+ string_t expected =
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ path_join(WRAP_STR(user), SZ("AppData" PATH_DELIM "Local"),
+ alloc);
+#elif defined(__APPLE__)
+ path_join(WRAP_STR(user), SZ("Library" PATH_DELIM "Caches"),
+ alloc);
+#else
+ path_join(WRAP_STR(user), SZ(".cache"), alloc);
+#endif
+
+ REQUIRE(AR_EQUAL(cache, expected));
+
+ DA_DESTROY(user);
+ DA_DESTROY(cache);
+ DA_DESTROY(expected);
+}
+
TEST("file path normalize one") {
str_t foo = SZ("foo/bar/../baz");
str_t foo_norm = SZ("foo" S_DELIM_ "baz");