[CRIU] [PATCH 1/3] util: Add rmdirp helper

Pavel Emelyanov xemul at virtuozzo.com
Fri Nov 25 06:57:58 PST 2016


On 11/23/2016 09:28 PM, Cyrill Gorcunov wrote:
> To remove series of dentries.
> 
> Signed-off-by: Cyrill Gorcunov <gorcunov at openvz.org>
> ---
>  criu/include/util.h |  2 ++
>  criu/util.c         | 40 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 42 insertions(+)
> 
> diff --git a/criu/include/util.h b/criu/include/util.h
> index 62f9733cc30e..8ed3e6d0d8e0 100644
> --- a/criu/include/util.h
> +++ b/criu/include/util.h
> @@ -249,6 +249,8 @@ static inline bool issubpath(const char *path, const char *sub_path)
>   */
>  int mkdirpat(int fd, const char *path, int mode);
>  
> +int rmdirp(const char *path, size_t keep);
> +
>  /*
>   * Tests whether a path is a prefix of another path. This is different than
>   * strstartswith because "/foo" is _not_ a path prefix of "/foobar", since they
> diff --git a/criu/util.c b/criu/util.c
> index 6ba787edd299..8068f8877462 100644
> --- a/criu/util.c
> +++ b/criu/util.c
> @@ -836,6 +836,46 @@ int mkdirpat(int fd, const char *path, int mode)
>  	return 0;
>  }
>  
> +/*
> + * Remove directory @path together with parents,
> + * keeping @keep len of path if provided. Directories
> + * must be empty of course.
> + */
> +int rmdirp(const char *path, size_t keep)

Isn't rm_parent_dirs() enough?

> +{
> +	char made_path[PATH_MAX], *pos;
> +	size_t len = strlen(path);
> +	int ret;
> +
> +	if (len >= PATH_MAX) {
> +		pr_err("path %s is longer than PATH_MAX\n", path);
> +		return -ENOSPC;
> +	}
> +
> +	/* Nothing to do */
> +	if (len <= keep)
> +		return 0;
> +
> +	strcpy(made_path, path);
> +
> +	for (pos = strrchr(made_path, '/');
> +	     pos && (pos - made_path) >= keep;
> +	     pos = strrchr(made_path, '/')) {
> +		ret = rmdir(made_path);
> +
> +		if (ret < 0 && errno != ENOENT) {
> +			ret = -errno;
> +			pr_perror("Can't delete %s", made_path);
> +			goto out;
> +		}
> +		*pos = '\0';
> +	}
> +
> +	ret = 0;
> +out:
> +	return ret;
> +}
> +
>  bool is_path_prefix(const char *path, const char *prefix)
>  {
>  	if (strstartswith(path, prefix)) {
> 



More information about the CRIU mailing list