summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2018-12-06 22:02:35 +0000
committerColin Walters <walters@verbum.org>2018-12-07 14:55:11 +0000
commitb1cb19b6b2d712b492e6376248f3010d18e59daa (patch)
tree5cb78bbdc9908027db5646f4cb0a48e7e9b50573
parent1e9b30838ea1101d7e3e924ddc58057842b8f3bf (diff)
downloadlibglnx-b1cb19b6b2d712b492e6376248f3010d18e59daa.tar.gz
shutil: Prefix error with path in rm_rf()
First, let's ensure the filename is prefixed consistently. Second, add the entrypoint as a prefix when recursing. This is best practice to help debugging. Motivated by https://discussion.fedoraproject.org/t/boot-partition-of-silverblue-is-without-space/771/9
-rw-r--r--glnx-shutil.c40
1 files changed, 24 insertions, 16 deletions
diff --git a/glnx-shutil.c b/glnx-shutil.c
index 75d0593..78042fe 100644
--- a/glnx-shutil.c
+++ b/glnx-shutil.c
@@ -24,9 +24,24 @@
#include <glnx-shutil.h>
#include <glnx-errors.h>
+#include <glnx-fdio.h>
#include <glnx-local-alloc.h>
static gboolean
+unlinkat_allow_noent (int dfd,
+ const char *path,
+ int flags,
+ GError **error)
+{
+ if (unlinkat (dfd, path, flags) == -1)
+ {
+ if (errno != ENOENT)
+ return glnx_throw_errno_prefix (error, "unlinkat(%s)", path);
+ }
+ return TRUE;
+}
+
+static gboolean
glnx_shutil_rm_rf_children (GLnxDirFdIterator *dfd_iter,
GCancellable *cancellable,
GError **error)
@@ -51,16 +66,13 @@ glnx_shutil_rm_rf_children (GLnxDirFdIterator *dfd_iter,
if (!glnx_shutil_rm_rf_children (&child_dfd_iter, cancellable, error))
return FALSE;
- if (unlinkat (dfd_iter->fd, dent->d_name, AT_REMOVEDIR) == -1)
- return glnx_throw_errno_prefix (error, "unlinkat");
+ if (!glnx_unlinkat (dfd_iter->fd, dent->d_name, AT_REMOVEDIR, error))
+ return FALSE;
}
else
{
- if (unlinkat (dfd_iter->fd, dent->d_name, 0) == -1)
- {
- if (errno != ENOENT)
- return glnx_throw_errno_prefix (error, "unlinkat");
- }
+ if (!unlinkat_allow_noent (dfd_iter->fd, dent->d_name, 0, error))
+ return FALSE;
}
}
@@ -86,7 +98,6 @@ glnx_shutil_rm_rf_at (int dfd,
{
dfd = glnx_dirfd_canonicalize (dfd);
-
/* With O_NOFOLLOW first */
glnx_autofd int target_dfd =
openat (dfd, path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
@@ -100,8 +111,8 @@ glnx_shutil_rm_rf_at (int dfd,
}
else if (errsv == ENOTDIR || errsv == ELOOP)
{
- if (unlinkat (dfd, path, 0) != 0)
- return glnx_throw_errno_prefix (error, "unlinkat");
+ if (!glnx_unlinkat (dfd, path, 0, error))
+ return FALSE;
}
else
return glnx_throw_errno_prefix (error, "open(%s)", path);
@@ -113,13 +124,10 @@ glnx_shutil_rm_rf_at (int dfd,
return FALSE;
if (!glnx_shutil_rm_rf_children (&dfd_iter, cancellable, error))
- return FALSE;
+ return glnx_prefix_error (error, "Removing %s", path);
- if (unlinkat (dfd, path, AT_REMOVEDIR) == -1)
- {
- if (errno != ENOENT)
- return glnx_throw_errno_prefix (error, "unlinkat");
- }
+ if (!unlinkat_allow_noent (dfd, path, AT_REMOVEDIR, error))
+ return FALSE;
}
return TRUE;