summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorLibravatar Alex Riesen <raa.lkml@gmail.com>2008-09-27 00:56:46 +0200
committerLibravatar Shawn O. Pearce <spearce@spearce.org>2008-09-29 08:37:07 -0700
commit4a92d1bfb784b09641180d164e7d719080165dc4 (patch)
treeacefaa70210fdc745519f64de33745d3d0c742cc /dir.c
parentgit-submodule: Fix "Unable to checkout" for the initial 'update' (diff)
downloadtgif-4a92d1bfb784b09641180d164e7d719080165dc4.tar.xz
Add remove_path: a function to remove as much as possible of a path
The function has two potential users which both managed to get wrong their implementations (the one in builtin-rm.c one has a memleak, and builtin-merge-recursive.c scribles over its const argument). Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/dir.c b/dir.c
index 109e05b013..cfaa28ff23 100644
--- a/dir.c
+++ b/dir.c
@@ -837,3 +837,23 @@ void setup_standard_excludes(struct dir_struct *dir)
if (excludes_file && !access(excludes_file, R_OK))
add_excludes_from_file(dir, excludes_file);
}
+
+int remove_path(const char *name)
+{
+ char *slash;
+
+ if (unlink(name) && errno != ENOENT)
+ return -1;
+
+ slash = strrchr(name, '/');
+ if (slash) {
+ char *dirs = xstrdup(name);
+ slash = dirs + (slash - name);
+ do {
+ *slash = '\0';
+ } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
+ free(dirs);
+ }
+ return 0;
+}
+