fileops: add git_futils_rmdir_recurs() git_futils_rmdir_recurs() shall remove the given directory and all subdirectories. This happens only if the directories are empty. Signed-off-by: schu <schu-github@schulog.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
diff --git a/src/fileops.c b/src/fileops.c
index edde3aa..afca032 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -301,6 +301,31 @@ int git_futils_mkdir_r(const char *path, int mode)
return GIT_SUCCESS;
}
+static int _rmdir_recurs_cb(void *GIT_UNUSED(nil), char *path)
+{
+ int error = GIT_SUCCESS;
+
+ error = git_futils_isdir(path);
+ if (error == GIT_SUCCESS) {
+ size_t root_size = strlen(path);
+
+ if ((error = git_futils_direach(path, GIT_PATH_MAX, _rmdir_recurs_cb, NULL)) < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to remove directory `%s`", path);
+
+ path[root_size] = '\0';
+ return p_rmdir(path);
+ }
+
+ return git__rethrow(error, "Failed to remove directory. `%s` is not a directory", path);
+}
+
+int git_futils_rmdir_recurs(const char *path)
+{
+ char p[GIT_PATH_MAX];
+ strncpy(p, path, GIT_PATH_MAX);
+ return _rmdir_recurs_cb(NULL, p);
+}
+
int git_futils_cmp_path(const char *name1, int len1, int isdir1,
const char *name2, int len2, int isdir2)
{
diff --git a/src/fileops.h b/src/fileops.h
index cc88682..a2f66a5 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -102,6 +102,9 @@ extern int git_futils_mv_withpath(const char *from, const char *to);
*/
extern git_off_t git_futils_filesize(git_file fd);
+/* Recursively remove an empty directory structure */
+extern int git_futils_rmdir_recurs(const char *path);
+
/* Taken from git.git */
static inline int is_dot_or_dotdot(const char *name)
{