summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--entry.c42
1 files changed, 16 insertions, 26 deletions
diff --git a/entry.c b/entry.c
index d6b263f78e..50fff7f239 100644
--- a/entry.c
+++ b/entry.c
@@ -3,6 +3,8 @@
#include "dir.h"
#include "streaming.h"
#include "submodule.h"
+#include "iterator.h"
+#include "dir-iterator.h"
static void create_directories(const char *path, int path_len,
const struct checkout *state)
@@ -45,33 +47,21 @@ static void create_directories(const char *path, int path_len,
free(buf);
}
-static void remove_subtree(struct strbuf *path)
+static void remove_subtree(const char *path)
{
- DIR *dir = opendir(path->buf);
- struct dirent *de;
- int origlen = path->len;
-
- if (!dir)
- die_errno("cannot opendir '%s'", path->buf);
- while ((de = readdir(dir)) != NULL) {
- struct stat st;
-
- if (is_dot_or_dotdot(de->d_name))
- continue;
-
- strbuf_addch(path, '/');
- strbuf_addstr(path, de->d_name);
- if (lstat(path->buf, &st))
- die_errno("cannot lstat '%s'", path->buf);
- if (S_ISDIR(st.st_mode))
- remove_subtree(path);
- else if (unlink(path->buf))
- die_errno("cannot unlink '%s'", path->buf);
- strbuf_setlen(path, origlen);
+ struct dir_iterator *diter = dir_iterator_begin(path,
+ DIR_ITERATOR_POST_ORDER_TRAVERSAL | DIR_ITERATOR_LIST_ROOT_DIR);
+ if (!diter) {
+ die_errno("cannot remove path '%s'", path);
+ }
+
+ while (dir_iterator_advance(diter) == ITER_OK) {
+ if (S_ISDIR(diter->st.st_mode)) {
+ if (rmdir(diter->path.buf))
+ die_errno("cannot rmdir '%s'", diter->path.buf);
+ } else if (unlink(diter->path.buf))
+ die_errno("cannot unlink '%s'", diter->path.buf);
}
- closedir(dir);
- if (rmdir(path->buf))
- die_errno("cannot rmdir '%s'", path->buf);
}
static int create_file(const char *path, unsigned int mode)
@@ -312,7 +302,7 @@ int checkout_entry(struct cache_entry *ce,
return 0;
if (!state->force)
return error("%s is a directory", path.buf);
- remove_subtree(&path);
+ remove_subtree(path.buf);
} else if (unlink(path.buf))
return error_errno("unable to unlink old '%s'", path.buf);
} else if (state->not_new)