aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-02-12 23:43:35 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-02-12 23:43:35 -0500
commite92c218adfbcaa35591a0dd7bdc634a3187967f0 (patch)
treefc65bce72cbd1d709066c96587ec7f754962c43d
parent3b9e0be6169b790b90ced5f03e2a4ddbf9be2a46 (diff)
downloadstasis-e92c218adfbcaa35591a0dd7bdc634a3187967f0.tar.gz
Add path_store function
-rw-r--r--include/utils.h1
-rw-r--r--src/utils.c42
2 files changed, 43 insertions, 0 deletions
diff --git a/include/utils.h b/include/utils.h
index 4816a31..f0400fb 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -33,6 +33,7 @@ char *find_program(const char *name);
int touch(const char *filename);
int git_clone(struct Process *proc, char *url, char *destdir, char *gitref);
char *git_describe(const char *path);
+int path_store(char **destptr, size_t maxlen, const char *base, const char *path);
#define OMC_MSG_SUCCESS 0
#define OMC_MSG_NOP 1 << 0
diff --git a/src/utils.c b/src/utils.c
index 9e941f1..12cf22c 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -483,4 +483,46 @@ int isempty_dir(const char *path) {
}
return count == 0;
+}
+
+int path_store(char **destptr, size_t maxlen, const char *base, const char *path) {
+ char *path_tmp;
+ size_t base_len = 0;
+ size_t path_len = 0;
+
+ // Both path elements need to be defined to continue
+ if (!base || !path) {
+ return -1;
+ }
+
+ // Initialize destination pointer to length of maxlen
+ path_tmp = calloc(maxlen, sizeof(*path_tmp));
+ if (!path_tmp) {
+ return -1;
+ }
+
+ // Ensure generated path will fit in destination
+ base_len = strlen(base);
+ path_len = strlen(path);
+ // 2 = directory separator and NUL terminator
+ if (2 + (base_len + path_len) > maxlen) {
+ goto l_path_setup_error;
+ }
+
+ snprintf(path_tmp, maxlen - 1, "%s/%s", base, path);
+ if (mkdirs(path_tmp, 0755)) {
+ goto l_path_setup_error;
+ }
+
+ (*destptr) = realpath(path_tmp, NULL);
+ if (!*destptr) {
+ goto l_path_setup_error;
+ }
+
+ guard_free(path_tmp);
+ return 0;
+
+ l_path_setup_error:
+ guard_free(path_tmp);
+ return -1;
} \ No newline at end of file