summaryrefslogtreecommitdiff
path: root/index.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2022-07-26 17:53:55 -0300
committermrb0nk500 <b0nk@b0nk.xyz>2022-07-26 17:53:55 -0300
commit8c633723964993a2ce6fb1717823caf46514a167 (patch)
tree5bb4a59cc083364c24eb14d586f4be14bef85951 /index.c
parent4fc5243fa6c249c1f1e118f95f42870b7ccf8c6e (diff)
index: Add `index_path_exists()`
This function checks if the converted path of a given index exists, or not. It also returns the converted path in `path` if the path exists.
Diffstat (limited to 'index.c')
-rw-r--r--index.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/index.c b/index.c
index d996b45..af65925 100644
--- a/index.c
+++ b/index.c
@@ -1,6 +1,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
+#include <sys/stat.h>
#include "index.h"
#include "macros.h"
@@ -96,3 +97,24 @@ char *index_to_str(index *idx) {
return NULL;
}
}
+
+int index_path_exists(index *idx, const char *root, char **path) {
+ char *dummy;
+ path = (path != NULL) ? path : &dummy;
+ for (int i = 0; i < 2; ++i) {
+ struct stat st;
+ *path = make_index_path(root, idx, i);
+ /* Does the index path exist? */
+ if (*path != NULL && stat(*path, &st) == 0) {
+ if (path == &dummy) {
+ free(*path);
+ *path = NULL;
+ }
+ return 1;
+ } else {
+ free(*path);
+ *path = NULL;
+ }
+ }
+ return 0;
+}