[libglnx] tree-wide: Some new style porting



commit d15a3790074fd982f2611a5b450dea61052dfc0b
Author: Colin Walters <walters verbum org>
Date:   Mon Oct 16 14:16:16 2017 -0400

    tree-wide: Some new style porting
    
    Use decl-after-stmt where applicable.

 glnx-dirfd.c  |   21 +++++-----------
 glnx-fdio.c   |   70 ++++++++++++++++++++++++++-------------------------------
 glnx-shutil.c |    9 +++----
 3 files changed, 43 insertions(+), 57 deletions(-)
---
diff --git a/glnx-dirfd.c b/glnx-dirfd.c
index e0c244c..6d1e2d2 100644
--- a/glnx-dirfd.c
+++ b/glnx-dirfd.c
@@ -190,15 +190,12 @@ glnx_dirfd_iterator_next_dent_ensure_dtype (GLnxDirFdIterator  *dfd_iter,
                                             GCancellable       *cancellable,
                                             GError            **error)
 {
-  struct dirent *ret_dent;
-
   g_return_val_if_fail (out_dent, FALSE);
 
   if (!glnx_dirfd_iterator_next_dent (dfd_iter, out_dent, cancellable, error))
     return FALSE;
 
-  ret_dent = *out_dent;
-
+  struct dirent *ret_dent = *out_dent;
   if (ret_dent)
     {
 
@@ -265,20 +262,16 @@ glnx_fdrel_abspath (int         dfd,
 void
 glnx_gen_temp_name (gchar *tmpl)
 {
-  size_t len;
-  char *XXXXXX;
-  int i;
-  static const char letters[] =
-    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
-  static const int NLETTERS = sizeof (letters) - 1;
-
   g_return_if_fail (tmpl != NULL);
-  len = strlen (tmpl);
+  const size_t len = strlen (tmpl);
   g_return_if_fail (len >= 6);
 
-  XXXXXX = tmpl + (len - 6);
+  static const char letters[] =
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
+  static const int NLETTERS = sizeof (letters) - 1;
 
-  for (i = 0; i < 6; i++)
+  char *XXXXXX = tmpl + (len - 6);
+  for (int i = 0; i < 6; i++)
     XXXXXX[i] = letters[g_random_int_range(0, NLETTERS)];
 }
 
diff --git a/glnx-fdio.c b/glnx-fdio.c
index b24d03f..a1f1903 100644
--- a/glnx-fdio.c
+++ b/glnx-fdio.c
@@ -191,9 +191,8 @@ open_tmpfile_core (int dfd, const char *subpath,
                    GLnxTmpfile *out_tmpf,
                    GError **error)
 {
+  /* Picked this to match mkstemp() */
   const guint mode = 0600;
-  glnx_autofd int fd = -1;
-  int count;
 
   dfd = glnx_dirfd_canonicalize (dfd);
 
@@ -204,33 +203,35 @@ open_tmpfile_core (int dfd, const char *subpath,
    * link_tmpfile() below to rename the result after writing the file
    * in full. */
 #if defined(O_TMPFILE) && !defined(DISABLE_OTMPFILE) && !defined(ENABLE_WRPSEUDO_COMPAT)
-  fd = openat (dfd, subpath, O_TMPFILE|flags, mode);
-  if (fd == -1 && !(G_IN_SET(errno, ENOSYS, EISDIR, EOPNOTSUPP)))
-    return glnx_throw_errno_prefix (error, "open(O_TMPFILE)");
-  if (fd != -1)
-    {
-      /* Workaround for https://sourceware.org/bugzilla/show_bug.cgi?id=17523
-       * See also https://github.com/ostreedev/ostree/issues/991
-       */
-      if (fchmod (fd, mode) < 0)
-        return glnx_throw_errno_prefix (error, "fchmod");
-      out_tmpf->initialized = TRUE;
-      out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
-      out_tmpf->fd = glnx_steal_fd (&fd);
-      out_tmpf->path = NULL;
-      return TRUE;
-    }
+  {
+    glnx_autofd int fd = openat (dfd, subpath, O_TMPFILE|flags, mode);
+    if (fd == -1 && !(G_IN_SET(errno, ENOSYS, EISDIR, EOPNOTSUPP)))
+      return glnx_throw_errno_prefix (error, "open(O_TMPFILE)");
+    if (fd != -1)
+      {
+        /* Workaround for https://sourceware.org/bugzilla/show_bug.cgi?id=17523
+         * See also https://github.com/ostreedev/ostree/issues/991
+         */
+        if (fchmod (fd, mode) < 0)
+          return glnx_throw_errno_prefix (error, "fchmod");
+        out_tmpf->initialized = TRUE;
+        out_tmpf->src_dfd = dfd; /* Copied; caller must keep open */
+        out_tmpf->fd = glnx_steal_fd (&fd);
+        out_tmpf->path = NULL;
+        return TRUE;
+      }
+  }
   /* Fallthrough */
 #endif
 
+  const guint count_max = 100;
   { g_autofree char *tmp = g_strconcat (subpath, "/tmp.XXXXXX", NULL);
-    const guint count_max = 100;
 
-    for (count = 0; count < count_max; count++)
+    for (int count = 0; count < count_max; count++)
       {
         glnx_gen_temp_name (tmp);
 
-        fd = openat (dfd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, mode);
+        glnx_autofd int fd = openat (dfd, tmp, O_CREAT|O_EXCL|O_NOFOLLOW|O_NOCTTY|flags, mode);
         if (fd < 0)
           {
             if (errno == EEXIST)
@@ -249,7 +250,7 @@ open_tmpfile_core (int dfd, const char *subpath,
       }
   }
   g_set_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
-               "Exhausted %u attempts to create temporary file", count);
+               "Exhausted %u attempts to create temporary file", count_max);
   return FALSE;
 }
 
@@ -359,9 +360,9 @@ glnx_link_tmpfile_at (GLnxTmpfile *tmpf,
           char *dnbuf = strdupa (target);
           const char *dn = dirname (dnbuf);
           char *tmpname_buf = glnx_strjoina (dn, "/tmp.XXXXXX");
-          guint count;
-          const guint count_max = 100;
 
+          const guint count_max = 100;
+          guint count;
           for (count = 0; count < count_max; count++)
             {
               glnx_gen_temp_name (tmpname_buf);
@@ -602,17 +603,13 @@ glnx_readlinkat_malloc (int            dfd,
                         GCancellable  *cancellable,
                         GError       **error)
 {
-  size_t l = 100;
-
   dfd = glnx_dirfd_canonicalize (dfd);
 
+  size_t l = 100;
   for (;;)
     {
-      g_autofree char *c = NULL;
-      ssize_t n;
-
-      c = g_malloc (l);
-      n = TEMP_FAILURE_RETRY (readlinkat (dfd, subpath, c, l-1));
+      g_autofree char *c = g_malloc (l);
+      ssize_t n = TEMP_FAILURE_RETRY (readlinkat (dfd, subpath, c, l-1));
       if (n < 0)
         return glnx_null_throw_errno_prefix (error, "readlinkat");
 
@@ -682,18 +679,15 @@ copy_symlink_at (int                   src_dfd,
 int
 glnx_loop_write(int fd, const void *buf, size_t nbytes)
 {
-  const uint8_t *p = buf;
-
-  g_return_val_if_fail(fd >= 0, -1);
-  g_return_val_if_fail(buf, -1);
+  g_return_val_if_fail (fd >= 0, -1);
+  g_return_val_if_fail (buf, -1);
 
   errno = 0;
 
+  const uint8_t *p = buf;
   while (nbytes > 0)
     {
-      ssize_t k;
-
-      k = write(fd, p, nbytes);
+      ssize_t k = write(fd, p, nbytes);
       if (k < 0)
         {
           if (errno == EINTR)
diff --git a/glnx-shutil.c b/glnx-shutil.c
index 8014c0c..75d0593 100644
--- a/glnx-shutil.c
+++ b/glnx-shutil.c
@@ -84,14 +84,12 @@ glnx_shutil_rm_rf_at (int                   dfd,
                       GCancellable         *cancellable,
                       GError              **error)
 {
-  glnx_autofd int target_dfd = -1;
-  g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
-
   dfd = glnx_dirfd_canonicalize (dfd);
 
+
   /* With O_NOFOLLOW first */
-  target_dfd = openat (dfd, path,
-                       O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
+  glnx_autofd int target_dfd =
+    openat (dfd, path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
 
   if (target_dfd == -1)
     {
@@ -110,6 +108,7 @@ glnx_shutil_rm_rf_at (int                   dfd,
     }
   else
     {
+      g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
       if (!glnx_dirfd_iterator_init_take_fd (&target_dfd, &dfd_iter, error))
         return FALSE;
 


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