From 066210f0b9044a190784d3c4c553539b8013b24e Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 23 Mar 2020 16:06:40 +0100 Subject: [PATCH] utils: allow removal of immutable files Closes #3185. Signed-off-by: Christian Brauner --- src/lxc/utils.c | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 25ae794b0..c34519084 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -19,6 +19,8 @@ #include #include #include +/* Needs to be after sys/mount.h header */ +#include #include #include #include @@ -129,9 +131,28 @@ static int _recursive_rmdir(const char *dirname, dev_t pdev, if (_recursive_rmdir(pathname, pdev, exclude, level + 1, onedev) < 0) failed = 1; } else { - if (unlink(pathname) < 0) { - SYSERROR("Failed to delete \"%s\"", pathname); - failed = 1; + ret = unlink(pathname); + if (ret < 0) { + __do_close int fd = -EBADF; + + fd = open(pathname, O_RDONLY | O_CLOEXEC | O_NONBLOCK); + if (fd >= 0) { + /* The file might be marked immutable. */ + int attr = 0; + ret = ioctl(fd, FS_IOC_GETFLAGS, &attr); + if (ret < 0) + SYSERROR("Failed to retrieve file flags"); + attr &= ~FS_IMMUTABLE_FL; + ret = ioctl(fd, FS_IOC_SETFLAGS, &attr); + if (ret < 0) + SYSERROR("Failed to set file flags"); + } + + ret = unlink(pathname); + if (ret < 0) { + SYSERROR("Failed to delete \"%s\"", pathname); + failed = 1; + } } } } -- 2.47.2