Introduce remove_dir_recursively()

There was a function called remove_empty_dir_recursive() buried
in refs.c.  Expose a slightly enhanced version in dir.h: it can now
optionally remove a non-empty directory.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin
2007-09-28 16:28:54 +01:00
committed by Junio C Hamano
parent 90446a0009
commit 7155b727c9
3 changed files with 55 additions and 45 deletions

57
refs.c
View File

@ -2,6 +2,7 @@
#include "refs.h"
#include "object.h"
#include "tag.h"
#include "dir.h"
/* ISSYMREF=01 and ISPACKED=02 are public interfaces */
#define REF_KNOWS_PEELED 04
@ -671,57 +672,23 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,
return lock;
}
static int remove_empty_dir_recursive(char *path, int len)
{
DIR *dir = opendir(path);
struct dirent *e;
int ret = 0;
if (!dir)
return -1;
if (path[len-1] != '/')
path[len++] = '/';
while ((e = readdir(dir)) != NULL) {
struct stat st;
int namlen;
if ((e->d_name[0] == '.') &&
((e->d_name[1] == 0) ||
((e->d_name[1] == '.') && e->d_name[2] == 0)))
continue; /* "." and ".." */
namlen = strlen(e->d_name);
if ((len + namlen < PATH_MAX) &&
strcpy(path + len, e->d_name) &&
!lstat(path, &st) &&
S_ISDIR(st.st_mode) &&
!remove_empty_dir_recursive(path, len + namlen))
continue; /* happy */
/* path too long, stat fails, or non-directory still exists */
ret = -1;
break;
}
closedir(dir);
if (!ret) {
path[len] = 0;
ret = rmdir(path);
}
return ret;
}
static int remove_empty_directories(char *file)
static int remove_empty_directories(const char *file)
{
/* we want to create a file but there is a directory there;
* if that is an empty directory (or a directory that contains
* only empty directories), remove them.
*/
char path[PATH_MAX];
int len = strlen(file);
struct strbuf path;
int result;
if (len >= PATH_MAX) /* path too long ;-) */
return -1;
strcpy(path, file);
return remove_empty_dir_recursive(path, len);
strbuf_init(&path, 20);
strbuf_addstr(&path, file);
result = remove_dir_recursively(&path, 1);
strbuf_release(&path);
return result;
}
static int is_refname_available(const char *ref, const char *oldref,