Skip to content

Commit

Permalink
Add remove_path: a function to remove as much as possible of a path
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Shawn O. Pearce <[email protected]>
  • Loading branch information
raalkml authored and spearce committed Sep 29, 2008
1 parent b9b378a commit 4a92d1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

3 changes: 3 additions & 0 deletions dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ extern int is_inside_dir(const char *dir);
extern void setup_standard_excludes(struct dir_struct *dir);
extern int remove_dir_recursively(struct strbuf *path, int only_empty);

/* tries to remove the path with empty directories along it, ignores ENOENT */
extern int remove_path(const char *path);

#endif

0 comments on commit 4a92d1b

Please sign in to comment.