[libglnx] tree-wide: Use glnx_autofd and glnx_close_fd()



commit 771d7a01d1104bbb8240cb29393314c7a4a69c4d
Author: Colin Walters <walters verbum org>
Date:   Mon Oct 16 14:09:39 2017 -0400

    tree-wide: Use glnx_autofd and glnx_close_fd()
    
    Port to `glnx_autofd` tree wide, and add one missed `glnx_close_fd()` use in the
    tmpfile code.

 README.md                   |    2 +-
 glnx-dirfd.c                |    4 ++--
 glnx-fdio.c                 |   12 ++++--------
 glnx-lockfile.c             |    2 +-
 glnx-shutil.c               |    2 +-
 tests/test-libglnx-fdio.c   |   12 ++++++------
 tests/test-libglnx-shutil.c |    2 +-
 tests/test-libglnx-xattrs.c |    6 +++---
 8 files changed, 19 insertions(+), 23 deletions(-)
---
diff --git a/README.md b/README.md
index 8fb2faa..f2ae6ae 100644
--- a/README.md
+++ b/README.md
@@ -37,7 +37,7 @@ applicable.
 
 For local allocation macros, you should start using the `g_auto`
 macros from GLib.  A backport is included in libglnx.  There are a few
-APIs not defined in GLib yet, such as `glnx_fd_close`.
+APIs not defined in GLib yet, such as `glnx_autofd`.
 
 `gs_transfer_out_value` is replaced by `g_steal_pointer`.
 
diff --git a/glnx-dirfd.c b/glnx-dirfd.c
index e6c3319..e0c244c 100644
--- a/glnx-dirfd.c
+++ b/glnx-dirfd.c
@@ -99,7 +99,7 @@ glnx_dirfd_iterator_init_at (int                     dfd,
                              GLnxDirFdIterator      *out_dfd_iter,
                              GError                **error)
 {
-  glnx_fd_close int fd = -1;
+  glnx_autofd int fd = -1;
   if (!glnx_opendirat (dfd, path, follow, &fd, error))
     return FALSE;
 
@@ -326,7 +326,7 @@ glnx_mkdtempat (int dfd, const char *tmpl, int mode,
         }
 
       /* And open it */
-      glnx_fd_close int ret_dfd = -1;
+      glnx_autofd int ret_dfd = -1;
       if (!glnx_opendirat (dfd, path, FALSE, &ret_dfd, error))
         {
           /* If we fail to open, let's try to clean up */
diff --git a/glnx-fdio.c b/glnx-fdio.c
index 892be2a..b24d03f 100644
--- a/glnx-fdio.c
+++ b/glnx-fdio.c
@@ -175,11 +175,7 @@ glnx_tmpfile_clear (GLnxTmpfile *tmpf)
     return;
   if (!tmpf->initialized)
     return;
-  if (tmpf->fd != -1)
-    {
-      if (close (tmpf->fd) < 0)
-        g_assert (errno != EBADF);
-    }
+  glnx_close_fd (&tmpf->fd);
   /* If ->path is set, we're likely aborting due to an error. Clean it up */
   if (tmpf->path)
     {
@@ -196,7 +192,7 @@ open_tmpfile_core (int dfd, const char *subpath,
                    GError **error)
 {
   const guint mode = 0600;
-  glnx_fd_close int fd = -1;
+  glnx_autofd int fd = -1;
   int count;
 
   dfd = glnx_dirfd_canonicalize (dfd);
@@ -576,7 +572,7 @@ glnx_file_get_contents_utf8_at (int                   dfd,
 {
   dfd = glnx_dirfd_canonicalize (dfd);
 
-  glnx_fd_close int fd = -1;
+  glnx_autofd int fd = -1;
   if (!glnx_openat_rdonly (dfd, subpath, TRUE, &fd, error))
     return NULL;
 
@@ -933,7 +929,7 @@ glnx_file_copy_at (int                   src_dfd,
 
   /* Regular file path below here */
 
-  glnx_fd_close int src_fd = -1;
+  glnx_autofd int src_fd = -1;
   if (!glnx_openat_rdonly (src_dfd, src_subpath, FALSE, &src_fd, error))
     return FALSE;
 
diff --git a/glnx-lockfile.c b/glnx-lockfile.c
index 2956eda..f1d52de 100644
--- a/glnx-lockfile.c
+++ b/glnx-lockfile.c
@@ -61,7 +61,7 @@
  */
 gboolean
 glnx_make_lock_file(int dfd, const char *p, int operation, GLnxLockFile *out_lock, GError **error) {
-        glnx_fd_close int fd = -1;
+        glnx_autofd int fd = -1;
         g_autofree char *t = NULL;
         int r;
 
diff --git a/glnx-shutil.c b/glnx-shutil.c
index 8438b5d..8014c0c 100644
--- a/glnx-shutil.c
+++ b/glnx-shutil.c
@@ -84,7 +84,7 @@ glnx_shutil_rm_rf_at (int                   dfd,
                       GCancellable         *cancellable,
                       GError              **error)
 {
-  glnx_fd_close int target_dfd = -1;
+  glnx_autofd int target_dfd = -1;
   g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
 
   dfd = glnx_dirfd_canonicalize (dfd);
diff --git a/tests/test-libglnx-fdio.c b/tests/test-libglnx-fdio.c
index c5ff949..81636e5 100644
--- a/tests/test-libglnx-fdio.c
+++ b/tests/test-libglnx-fdio.c
@@ -32,8 +32,8 @@ static gboolean
 renameat_test_setup (int *out_srcfd, int *out_destfd,
                      GError **error)
 {
-  glnx_fd_close int srcfd = -1;
-  glnx_fd_close int destfd = -1;
+  glnx_autofd int srcfd = -1;
+  glnx_autofd int destfd = -1;
 
   (void) glnx_shutil_rm_rf_at (AT_FDCWD, "srcdir", NULL, NULL);
   if (mkdir ("srcdir", 0755) < 0)
@@ -62,8 +62,8 @@ static void
 test_renameat2_noreplace (void)
 {
   _GLNX_TEST_DECLARE_ERROR(local_error, error);
-  glnx_fd_close int srcfd = -1;
-  glnx_fd_close int destfd = -1;
+  glnx_autofd int srcfd = -1;
+  glnx_autofd int destfd = -1;
   struct stat stbuf;
 
   if (!renameat_test_setup (&srcfd, &destfd, error))
@@ -92,8 +92,8 @@ test_renameat2_exchange (void)
 {
   _GLNX_TEST_DECLARE_ERROR(local_error, error);
 
-  glnx_fd_close int srcfd = -1;
-  glnx_fd_close int destfd = -1;
+  glnx_autofd int srcfd = -1;
+  glnx_autofd int destfd = -1;
   if (!renameat_test_setup (&srcfd, &destfd, error))
     return;
 
diff --git a/tests/test-libglnx-shutil.c b/tests/test-libglnx-shutil.c
index 39f261b..6917b89 100644
--- a/tests/test-libglnx-shutil.c
+++ b/tests/test-libglnx-shutil.c
@@ -32,7 +32,7 @@ static void
 test_mkdir_p_enoent (void)
 {
   _GLNX_TEST_DECLARE_ERROR(local_error, error);
-  glnx_fd_close int dfd = -1;
+  glnx_autofd int dfd = -1;
 
   if (!glnx_ensure_dir (AT_FDCWD, "test", 0755, error))
     return;
diff --git a/tests/test-libglnx-xattrs.c b/tests/test-libglnx-xattrs.c
index 0076b71..63e1231 100644
--- a/tests/test-libglnx-xattrs.c
+++ b/tests/test-libglnx-xattrs.c
@@ -82,7 +82,7 @@ do_write_run (GLnxDirFdIterator *dfd_iter, GError **error)
     {
       guint32 randname_v = g_random_int ();
       g_autofree char *randname = g_strdup_printf ("file%u", randname_v);
-      glnx_fd_close int fd = -1;
+      glnx_autofd int fd = -1;
 
     again:
       fd = openat (dfd_iter->fd, randname, O_CREAT | O_EXCL, 0644);
@@ -113,7 +113,7 @@ do_write_run (GLnxDirFdIterator *dfd_iter, GError **error)
           if (!dent)
             break;
 
-          glnx_fd_close int fd = -1;
+          glnx_autofd int fd = -1;
           if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
             return FALSE;
 
@@ -157,7 +157,7 @@ do_read_run (GLnxDirFdIterator *dfd_iter,
       if (!dent)
         break;
 
-      glnx_fd_close int fd = -1;
+      glnx_autofd int fd = -1;
       if (!glnx_openat_rdonly (dfd_iter->fd, dent->d_name, FALSE, &fd, error))
         return FALSE;
 


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