[sheepdog] [PATCH 2/8] util: add a helper to purge directory

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Mon Mar 11 02:08:09 CET 2013


At Sun, 10 Mar 2013 22:19:23 +0800,
Liu Yuan wrote:
> 
> From: Liu Yuan <tailai.ly at taobao.com>
> 
> This is preparation patch for raid series.
> 
> Signed-off-by: Liu Yuan <tailai.ly at taobao.com>
> ---
>  include/util.h |    1 +
>  lib/util.c     |   79 ++++++++++++++++++++++++++++++--------------------------
>  2 files changed, 44 insertions(+), 36 deletions(-)
> 
> diff --git a/include/util.h b/include/util.h
> index 3307d7e..eae4e9a 100644
> --- a/include/util.h
> +++ b/include/util.h
> @@ -84,6 +84,7 @@ ssize_t xpread(int fd, void *buf, size_t count, off_t offset);
>  ssize_t xpwrite(int fd, const void *buf, size_t count, off_t offset);
>  void pstrcpy(char *buf, int buf_size, const char *str);
>  int rmdir_r(char *dir_path);
> +int purge_directory(char *dir_path);
>  bool is_numeric(const char *p);
>  int install_sighandler(int signum, void (*handler)(int), bool once);
>  int install_crash_handler(void (*handler)(int));
> diff --git a/lib/util.c b/lib/util.c
> index 41396da..360e568 100644
> --- a/lib/util.c
> +++ b/lib/util.c
> @@ -255,51 +255,58 @@ void notrace pstrcpy(char *buf, int buf_size, const char *str)
>  	*q = '\0';
>  }
>  
> +#define RMDIR_PROPER \
> +	int ret;\
> +	struct stat s;\
> +	DIR *dir;\
> +	struct dirent *d;\
> +	char path[PATH_MAX];\
> +	dir = opendir(dir_path);\
> +	if (!dir) {\
> +		if (errno != ENOENT)\
> +			sd_eprintf("failed to open %s: %m", dir_path);\
> +		return -errno;\
> +	}\
> +	while ((d = readdir(dir))) {\
> +		if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))\
> +			continue;\
> +		snprintf(path, sizeof(path), "%s/%s", dir_path, d->d_name);\
> +		ret = stat(path, &s);\
> +		if (ret) {\
> +			sd_eprintf("failed to stat %s: %m", path);\
> +			goto out;\
> +		}\
> +		if (S_ISDIR(s.st_mode))\
> +			ret = rmdir_r(path);\
> +		else\
> +			ret = unlink(path);\
> +		if (ret != 0) {\
> +			sd_eprintf("failed to remove %s %s: %m",\
> +				   S_ISDIR(s.st_mode) ? "directory" : "file",\
> +				   path);\
> +			goto out;\
> +		}\
> +	}
> +

Can we make this an inline function?

Thanks,

Kazutaka



More information about the sheepdog mailing list