From 5e13dd95a5e96b3d9bc9e28b6d8912625b92ce20 Mon Sep 17 00:00:00 2001
From: tanyifeng <tanyifeng1@huawei.com>
Date: Sat, 16 Mar 2019 10:06:13 +0800
Subject: [PATCH 067/131] lxc: report error when remove directory failed

Signed-off-by: tanyifeng <tanyifeng1@huawei.com>
Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/lxccontainer.c | 3 +++
 src/lxc/utils.c        | 9 ++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index bfbf223b..3fd1a66c 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -3148,8 +3148,11 @@ static bool container_destroy(struct lxc_container *c,
 	else
 		ret = lxc_rmdir_onedev(path, "snaps");
 	if (ret < 0) {
+		char msg[BUFSIZ] = { 0 };
 		ERROR("Failed to destroy directory \"%s\" for \"%s\"", path,
 		      c->name);
+		sprintf(msg, "Failed to destroy directory \"%s\": %s", path, errno ? strerror(errno) : "error");
+		c->error_string = strdup(msg);
 		goto out;
 	}
 	INFO("Destroyed directory \"%s\" for \"%s\"", path, c->name);
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 91ba4935..480e6d0a 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -88,6 +88,7 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
 	int ret, failed = 0;
 	char pathname[PATH_MAX];
 	bool hadexclude = false;
+	int saved_errno = 0;
 
 	dir = opendir(dirname);
 	if (!dir) {
@@ -153,6 +154,9 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
 				failed=1;
 		} else {
 			if (unlink(pathname) < 0) {
+				if (saved_errno == 0) {
+		                    saved_errno = errno;
+		                }
 				SYSERROR("Failed to delete \"%s\"", pathname);
 				failed=1;
 			}
@@ -160,6 +164,9 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
 	}
 
 	if (rmdir(dirname) < 0 && !btrfs_try_remove_subvol(dirname) && !hadexclude) {
+		if (saved_errno == 0) {
+			saved_errno = errno;
+		}
 		SYSERROR("Failed to delete \"%s\"", dirname);
 		failed=1;
 	}
@@ -169,7 +176,7 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev,
 		SYSERROR("Failed to close directory \"%s\"", dirname);
 		failed=1;
 	}
-
+	errno = saved_errno;
 	return failed ? -1 : 0;
 }
 
-- 
2.23.0

