@matheusbsbs / matheus-bagliano / commits / 4a92d1bfb7
Fork 0

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>

Alex Riesen committed Sep 27, 2008 at 00:56 UTC 4a92d1bfb784b09641180d164e7d719080165dc4
2 files changed +23
dir.c
+20
@@ -837,3 +837,23 @@ void setup_standard_excludes(struct dir_struct *dir)
837 if (excludes_file && !access(excludes_file, R_OK))
838 add_excludes_from_file(dir, excludes_file);
839 }
840 +
841 +int remove_path(const char *name)
842 +{
843 + char *slash;
844 +
845 + if (unlink(name) && errno != ENOENT)
846 + return -1;
847 +
848 + slash = strrchr(name, '/');
849 + if (slash) {
850 + char *dirs = xstrdup(name);
851 + slash = dirs + (slash - name);
852 + do {
853 + *slash = '\0';
854 + } while (rmdir(dirs) && (slash = strrchr(dirs, '/')));
855 + free(dirs);
856 + }
857 + return 0;
858 +}
859 +
dir.h
+3
@@ -81,4 +81,7 @@ extern int is_inside_dir(const char *dir);
81 extern void setup_standard_excludes(struct dir_struct *dir);
82 extern int remove_dir_recursively(struct strbuf *path, int only_empty);
83
84 +/* tries to remove the path with empty directories along it, ignores ENOENT */
85 +extern int remove_path(const char *path);
86 +
87 #endif