From 8c633723964993a2ce6fb1717823caf46514a167 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Tue, 26 Jul 2022 17:53:55 -0300 Subject: 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. --- index.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'index.c') diff --git a/index.c b/index.c index d996b45..af65925 100644 --- a/index.c +++ b/index.c @@ -1,6 +1,7 @@ #include #include #include +#include #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; +} -- cgit v1.2.3-13-gbd6f