[libglnx] fdio: Add wrappers for renameat(), unlinkat()



commit 4d34066a2ff5d806db35c3ac765f87ace460fa7e
Author: Colin Walters <walters verbum org>
Date:   Mon Jun 26 13:04:35 2017 -0400

    fdio: Add wrappers for renameat(), unlinkat()
    
    Besides doing `TEMP_FAILURE_RETRY` and `GError` conversion,
    these also prefix the error with arguments.

 glnx-fdio.c               |    6 ++----
 glnx-fdio.h               |   37 ++++++++++++++++++++++++++++++++++++-
 tests/test-libglnx-fdio.c |    7 ++-----
 3 files changed, 40 insertions(+), 10 deletions(-)
---
diff --git a/glnx-fdio.c b/glnx-fdio.c
index e496828..b5eaa9e 100644
--- a/glnx-fdio.c
+++ b/glnx-fdio.c
@@ -319,15 +319,13 @@ glnx_link_tmpfile_at (GLnxTmpfile *tmpf,
                "Exhausted %u attempts to create temporary file", count);
               return FALSE;
             }
-          if (renameat (target_dfd, tmpname_buf, target_dfd, target) < 0)
+          if (!glnx_renameat (target_dfd, tmpname_buf, target_dfd, target, error))
             {
               /* This is currently the only case where we need to have
                * a cleanup unlinkat() still with O_TMPFILE.
                */
-              int errsv = errno;
               (void) unlinkat (target_dfd, tmpname_buf, 0);
-              errno = errsv;
-              return glnx_throw_errno_prefix (error, "renameat");
+              return FALSE;
             }
         }
       else
diff --git a/glnx-fdio.h b/glnx-fdio.h
index 95574bd..bdccbe5 100644
--- a/glnx-fdio.h
+++ b/glnx-fdio.h
@@ -27,6 +27,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <string.h>
+#include <stdio.h>
 #include <sys/xattr.h>
 /* From systemd/src/shared/util.h */
 /* When we include libgen.h because we need dirname() we immediately
@@ -244,8 +245,42 @@ glnx_fstatat (int           dfd,
               GError      **error)
 {
   if (TEMP_FAILURE_RETRY (fstatat (dfd, path, buf, flags)) != 0)
-    return glnx_throw_errno (error);
+    return glnx_throw_errno_prefix (error, "fstatat(%s)", path);
+  return TRUE;
+}
+
+/**
+ * glnx_renameat:
+ *
+ * Wrapper around renameat() which adds #GError support and ensures that it
+ * retries on %EINTR.
+ */
+static inline gboolean
+glnx_renameat (int           src_dfd,
+               const gchar  *src_path,
+               int           dest_dfd,
+               const gchar  *dest_path,
+               GError      **error)
+{
+  if (TEMP_FAILURE_RETRY (renameat (src_dfd, src_path, dest_dfd, dest_path)) != 0)
+    return glnx_throw_errno_prefix (error, "renameat(%s, %s)", src_path, dest_path);
+  return TRUE;
+}
 
+/**
+ * glnx_unlinkat:
+ *
+ * Wrapper around unlinkat() which adds #GError support and ensures that it
+ * retries on %EINTR.
+ */
+static inline gboolean
+glnx_unlinkat (int           dfd,
+               const gchar  *path,
+               int           flags,
+               GError      **error)
+{
+  if (TEMP_FAILURE_RETRY (unlinkat (dfd, path, flags)) != 0)
+    return glnx_throw_errno_prefix (error, "unlinkat(%s)", path);
   return TRUE;
 }
 
diff --git a/tests/test-libglnx-fdio.c b/tests/test-libglnx-fdio.c
index 9830c10..bc2d7ac 100644
--- a/tests/test-libglnx-fdio.c
+++ b/tests/test-libglnx-fdio.c
@@ -80,11 +80,8 @@ test_renameat2_noreplace (void)
       glnx_set_error_from_errno (error);
       goto out;
     }
-  if (fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
-    {
-      glnx_set_error_from_errno (error);
-      goto out;
-    }
+  if (!glnx_fstatat (destfd, "bar", &stbuf, AT_SYMLINK_NOFOLLOW))
+    goto out;
 
   if (fstatat (srcfd, "foo", &stbuf, AT_SYMLINK_NOFOLLOW) == 0)
     g_assert_not_reached ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]