summaryrefslogtreecommitdiff
path: root/src/fileops.c
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-04-09 02:28:31 +0200
committernulltoken <emeric.fermas@gmail.com>2012-04-10 21:39:05 +0200
commit555aa453baefec98dbd026592b68214048bedac3 (patch)
tree860b7c0f36816afe736c13c3a2901d63d72cd91f /src/fileops.c
parent731df57080704183cad128c17fd065e5e25fa886 (diff)
downloadlibgit2-555aa453baefec98dbd026592b68214048bedac3.tar.gz
fileops: Make git_futils_mkdir_r() able to skip non-empty directories
Diffstat (limited to 'src/fileops.c')
-rw-r--r--src/fileops.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/src/fileops.c b/src/fileops.c
index b3bb3890e..9da1bf789 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -298,13 +298,20 @@ int git_futils_mkdir_r(const char *path, const char *base, const mode_t mode)
static int _rmdir_recurs_foreach(void *opaque, git_buf *path)
{
- int force = *(int *)opaque;
+ enum git_directory_removal_type removal_type = *(enum git_directory_removal_type *)opaque;
+
+ assert(removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY
+ || removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS
+ || removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS);
if (git_path_isdir(path->ptr) == true) {
if (git_path_direach(path, _rmdir_recurs_foreach, opaque) < 0)
return -1;
if (p_rmdir(path->ptr) < 0) {
+ if (removal_type == GIT_DIRREMOVAL_ONLY_EMPTY_DIRS && errno == ENOTEMPTY)
+ return 0;
+
giterr_set(GITERR_OS, "Could not remove directory '%s'", path->ptr);
return -1;
}
@@ -312,7 +319,7 @@ static int _rmdir_recurs_foreach(void *opaque, git_buf *path)
return 0;
}
- if (force) {
+ if (removal_type == GIT_DIRREMOVAL_FILES_AND_DIRS) {
if (p_unlink(path->ptr) < 0) {
giterr_set(GITERR_OS, "Could not remove directory. File '%s' cannot be removed", path->ptr);
return -1;
@@ -321,18 +328,22 @@ static int _rmdir_recurs_foreach(void *opaque, git_buf *path)
return 0;
}
- giterr_set(GITERR_OS, "Could not remove directory. File '%s' still present", path->ptr);
- return -1;
+ if (removal_type == GIT_DIRREMOVAL_EMPTY_HIERARCHY) {
+ giterr_set(GITERR_OS, "Could not remove directory. File '%s' still present", path->ptr);
+ return -1;
+ }
+
+ return 0;
}
-int git_futils_rmdir_r(const char *path, int force)
+int git_futils_rmdir_r(const char *path, enum git_directory_removal_type removal_type)
{
int error;
git_buf p = GIT_BUF_INIT;
error = git_buf_sets(&p, path);
if (!error)
- error = _rmdir_recurs_foreach(&force, &p);
+ error = _rmdir_recurs_foreach(&removal_type, &p);
git_buf_free(&p);
return error;
}