|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [Xen-devel] [PATCH 05/20] libxl: provide libxl__remove_file et al.
> +int libxl__remove_directory(libxl__gc *gc, const char *dirpath)
> +{
> + int rc = 0;
> + DIR *d = 0;
> +
> + d = opendir(dirpath);
> + if (!d) {
> + if (errno == ENOENT)
> + goto out;
> +
> + LOGE(ERROR, "failed to opendir %s for removal", dirpath);
> + rc = ERROR_FAIL;
> + goto out;
> + }
> +
> + size_t need = offsetof(struct dirent, d_name) +
> + pathconf(dirpath, _PC_NAME_MAX) + 1;
This was an interesting pattern, but I see that readdir(3) recommends
portable programs do things this way...
> + struct dirent *de_buf = libxl__zalloc(gc, need);
> + struct dirent *de;
> +
> + for (;;) {
> + int r = readdir_r(d, de_buf, &de);
> + if (r) {
> + LOGE(ERROR, "failed to readdir %s for removal", dirpath);
> + rc = ERROR_FAIL;
> + break;
> + }
> + if (!de)
> + break;
> +
> + if (!strcmp(de->d_name, ".") ||
> + !strcmp(de->d_name, ".."))
> + continue;
> +
> + const char *subpath = GCSPRINTF("%s/%s", dirpath, de->d_name);
> + if (libxl__remove_file_or_directory(gc, subpath))
> + rc = ERROR_FAIL;
Deliberately doesn't "goto out" so as to remove as much as possible?
If so then:
Acked-by: Ian Campbell <ian.campbell@xxxxxxxxxx>
> + }
> +
> + for (;;) {
> + int r = rmdir(dirpath);
> + if (!r) break;
> + if (errno == ENOENT) goto out;
> + if (errno == EINTR) continue;
> + LOGE(ERROR, "failed to remove emptied directory %s", dirpath);
> + rc = ERROR_FAIL;
> + }
> +
> + out:
> + if (d) closedir(d);
> +
> + return rc;
> +}
>
> pid_t libxl_fork(libxl_ctx *ctx)
> {
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |