tree-wide: Some glnx_fstatat_allow_noent() porting
authorColin Walters <walters@verbum.org>
Fri, 15 Sep 2017 20:29:22 +0000 (16:29 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Tue, 19 Sep 2017 15:03:05 +0000 (15:03 +0000)
The new API is definitely nicer.

Closes: #1180
Approved by: jlebon

src/libostree/ostree-repo-commit.c
src/libostree/ostree-repo-refs.c
src/libostree/ostree-repo.c
src/libostree/ostree-sysroot-deploy.c
src/libostree/ostree-sysroot.c
src/libotutil/ot-fs-utils.c
src/libotutil/ot-fs-utils.h

index ed13b3bf7dec238752c1fe3bd46d0a2893f30509..e226d500297ca6dae3f6f013f3ee913dc98e7109 100644 (file)
@@ -1249,12 +1249,10 @@ cleanup_tmpdir (OstreeRepo        *self,
       if (strcmp (dent->d_name, "cache") == 0)
         continue;
 
-      if (TEMP_FAILURE_RETRY (fstatat (dfd_iter.fd, dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW)) < 0)
-        {
-          if (errno == ENOENT) /* Did another cleanup win? */
-            continue;
-          return glnx_throw_errno_prefix (error, "fstatat(%s)", dent->d_name);
-        }
+      if (!glnx_fstatat_allow_noent (dfd_iter.fd, dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW, error))
+        return FALSE;
+      if (errno == ENOENT) /* Did another cleanup win? */
+        continue;
 
       /* First, if it's a directory which needs locking, but it's
        * busy, skip it.
index b9b008966cc40ca835017ad72ebc13560ac1f002..78ac9121abdb79f6cd49d2f5c6574f5040b3d99b 100644 (file)
@@ -545,12 +545,10 @@ _ostree_repo_list_refs_internal (OstreeRepo       *self,
                                  GCancellable     *cancellable,
                                  GError          **error)
 {
-  g_autoptr(GHashTable) ret_all_refs = NULL;
   g_autofree char *remote = NULL;
   g_autofree char *ref_prefix = NULL;
 
-  ret_all_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
-
+  g_autoptr(GHashTable) ret_all_refs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
   if (refspec_prefix)
     {
       struct stat stbuf;
@@ -571,12 +569,9 @@ _ostree_repo_list_refs_internal (OstreeRepo       *self,
           path = glnx_strjoina (prefix_path, ref_prefix);
         }
 
-      if (fstatat (self->repo_dir_fd, path, &stbuf, 0) < 0)
-        {
-          if (errno != ENOENT)
-            return glnx_throw_errno (error);
-        }
-      else
+      if (!glnx_fstatat_allow_noent (self->repo_dir_fd, path, &stbuf, 0, error))
+        return FALSE;
+      if (errno == 0)
         {
           if (S_ISDIR (stbuf.st_mode))
             {
index e6d8a747da1f024e9e72c71264f287ab56dd72bf..f779db85ee0bb5701966f5faa31f97a793820d85 100644 (file)
@@ -1806,7 +1806,9 @@ repo_create_at_internal (int             dfd,
   /* Early return if we have an existing repo */
   { g_autofree char *objects_path = g_build_filename (path, "objects", NULL);
 
-    if (fstatat (dfd, objects_path, &stbuf, 0) == 0)
+    if (!glnx_fstatat_allow_noent (dfd, objects_path, &stbuf, 0, error))
+      return FALSE;
+    if (errno == 0)
       {
         glnx_fd_close int repo_dfd = -1;
         if (!glnx_opendirat (dfd, path, TRUE, &repo_dfd, error))
@@ -1816,8 +1818,6 @@ repo_create_at_internal (int             dfd,
         *out_dfd = glnx_steal_fd (&repo_dfd);
         return TRUE;
       }
-    else if (errno != ENOENT)
-      return glnx_throw_errno_prefix (error, "fstatat");
   }
 
   if (mkdirat (dfd, path, 0755) != 0)
@@ -1830,32 +1830,29 @@ repo_create_at_internal (int             dfd,
   if (!glnx_opendirat (dfd, path, TRUE, &repo_dfd, error))
     return FALSE;
 
-  if (fstatat (repo_dfd, "config", &stbuf, 0) < 0)
+  if (!glnx_fstatat_allow_noent (repo_dfd, "config", &stbuf, 0, error))
+    return FALSE;
+  if (errno == ENOENT)
     {
-      if (errno == ENOENT)
-        {
-          const char *mode_str = NULL;
-          g_autoptr(GString) config_data = g_string_new (DEFAULT_CONFIG_CONTENTS);
+      const char *mode_str = NULL;
+      g_autoptr(GString) config_data = g_string_new (DEFAULT_CONFIG_CONTENTS);
 
-          if (!ostree_repo_mode_to_string (mode, &mode_str, error))
-            return FALSE;
-          g_assert (mode_str);
+      if (!ostree_repo_mode_to_string (mode, &mode_str, error))
+        return FALSE;
+      g_assert (mode_str);
 
-          g_string_append_printf (config_data, "mode=%s\n", mode_str);
+      g_string_append_printf (config_data, "mode=%s\n", mode_str);
 
-          const char *collection_id = NULL;
-          if (options)
-            g_variant_lookup (options, "collection-id", "&s", &collection_id);
-          if (collection_id != NULL)
-            g_string_append_printf (config_data, "collection-id=%s\n", collection_id);
+      const char *collection_id = NULL;
+      if (options)
+        g_variant_lookup (options, "collection-id", "&s", &collection_id);
+      if (collection_id != NULL)
+        g_string_append_printf (config_data, "collection-id=%s\n", collection_id);
 
-          if (!glnx_file_replace_contents_at (repo_dfd, "config",
-                                              (guint8*)config_data->str, config_data->len,
-                                              0, cancellable, error))
-            return FALSE;
-        }
-      else
-        return glnx_throw_errno_prefix (error, "fstatat");
+      if (!glnx_file_replace_contents_at (repo_dfd, "config",
+                                          (guint8*)config_data->str, config_data->len,
+                                          0, cancellable, error))
+        return FALSE;
     }
 
   for (guint i = 0; i < G_N_ELEMENTS (state_dirs); i++)
index 62ebd6b6e3b386266a4db405ed428e3736aa446a..911c2854770159dece32a86fef3eb9ea93ddc741 100644 (file)
@@ -689,12 +689,10 @@ selinux_relabel_var_if_needed (OstreeSysroot                 *sysroot,
    * when doing a deployment.
    */
   const char selabeled[] = "var/.ostree-selabeled";
-  gboolean deployment_var_labeled;
-
-  if (!ot_query_exists_at (os_deploy_dfd, selabeled, &deployment_var_labeled, error))
+  struct stat stbuf;
+  if (!glnx_fstatat_allow_noent (os_deploy_dfd, selabeled, &stbuf, AT_SYMLINK_NOFOLLOW, error))
     return FALSE;
-
-  if (!deployment_var_labeled)
+  if (errno == ENOENT)
     {
       { g_autofree char *msg =
           g_strdup_printf ("Relabeling /var (no stamp file '%s' found)", selabeled);
@@ -764,12 +762,13 @@ merge_configuration (OstreeSysroot         *sysroot,
         }
     }
 
-  gboolean etc_exists = FALSE;
-  if (!ot_query_exists_at (deployment_dfd, "etc", &etc_exists, error))
+  struct stat stbuf;
+  if (!glnx_fstatat_allow_noent (deployment_dfd, "etc", &stbuf, AT_SYMLINK_NOFOLLOW, error))
     return FALSE;
-  gboolean usretc_exists = FALSE;
-  if (!ot_query_exists_at (deployment_dfd, "usr/etc", &usretc_exists, error))
+  gboolean etc_exists = (errno == 0);
+  if (!glnx_fstatat_allow_noent (deployment_dfd, "usr/etc", &stbuf, AT_SYMLINK_NOFOLLOW, error))
     return FALSE;
+  gboolean usretc_exists = (errno == 0);
 
   if (etc_exists && usretc_exists)
     return glnx_throw (error, "Tree contains both /etc and /usr/etc");
@@ -1568,10 +1567,10 @@ install_deployment_kernel (OstreeSysroot   *sysroot,
    * it doesn't exist already.
    */
   struct stat stbuf;
-  if (fstatat (bootcsum_dfd, kernel_layout->kernel_namever, &stbuf, 0) != 0)
+  if (!glnx_fstatat_allow_noent (bootcsum_dfd, kernel_layout->kernel_namever, &stbuf, 0, error))
+    return FALSE;
+  if (errno == ENOENT)
     {
-      if (errno != ENOENT)
-        return glnx_throw_errno_prefix (error, "fstat %s", kernel_layout->kernel_namever);
       if (!hardlink_or_copy_at (kernel_layout->boot_dfd,
                                 kernel_layout->kernel_srcpath,
                                 bootcsum_dfd, kernel_layout->kernel_namever,
@@ -1586,10 +1585,10 @@ install_deployment_kernel (OstreeSysroot   *sysroot,
   if (kernel_layout->initramfs_srcpath)
     {
       g_assert (kernel_layout->initramfs_namever);
-      if (fstatat (bootcsum_dfd, kernel_layout->initramfs_namever, &stbuf, 0) != 0)
+      if (!glnx_fstatat_allow_noent (bootcsum_dfd, kernel_layout->initramfs_namever, &stbuf, 0, error))
+        return FALSE;
+      if (errno == ENOENT)
         {
-          if (errno != ENOENT)
-            return glnx_throw_errno_prefix (error, "fstat %s", kernel_layout->initramfs_namever);
           if (!hardlink_or_copy_at (kernel_layout->boot_dfd, kernel_layout->initramfs_srcpath,
                                     bootcsum_dfd, kernel_layout->initramfs_namever,
                                     sysroot->debug_flags,
@@ -1599,19 +1598,14 @@ install_deployment_kernel (OstreeSysroot   *sysroot,
     }
 
   g_autofree char *contents = NULL;
-  if (fstatat (deployment_dfd, "usr/lib/os-release", &stbuf, 0) != 0)
+  if (!glnx_fstatat_allow_noent (deployment_dfd, "usr/lib/os-release", &stbuf, 0, error))
+    return FALSE;
+  if (errno == ENOENT)
     {
-      if (errno != ENOENT)
-        {
-          return glnx_throw_errno (error);
-        }
-      else
-        {
-          contents = glnx_file_get_contents_utf8_at (deployment_dfd, "etc/os-release", NULL,
-                                                     cancellable, error);
-          if (!contents)
-            return glnx_prefix_error (error, "Reading /etc/os-release");
-        }
+      contents = glnx_file_get_contents_utf8_at (deployment_dfd, "etc/os-release", NULL,
+                                                 cancellable, error);
+      if (!contents)
+        return glnx_prefix_error (error, "Reading /etc/os-release");
     }
   else
     {
index 18475d35d8b4b4af68502237b7d2ece502e20db2..ee9b4818b9290c6db98179a6ad17ddccd46465c3 100644 (file)
@@ -541,12 +541,9 @@ parse_origin (OstreeSysroot   *self,
   g_autoptr(GKeyFile) ret_origin = g_key_file_new ();
 
   struct stat stbuf;
-  if (fstatat (deployment_dfd, origin_path, &stbuf, 0) != 0)
-    {
-      if (errno != ENOENT)
-        return glnx_throw_errno (error);
-    }
-  else
+  if (!glnx_fstatat_allow_noent (deployment_dfd, origin_path, &stbuf, 0, error))
+    return FALSE;
+  if (errno == 0)
     {
       g_autofree char *origin_contents =
         glnx_file_get_contents_utf8_at (deployment_dfd, origin_path,
@@ -808,8 +805,8 @@ ostree_sysroot_load_if_changed (OstreeSysroot  *self,
     return FALSE;
 
   struct stat stbuf;
-  if (fstatat (self->sysroot_fd, "ostree/deploy", &stbuf, 0) < 0)
-    return glnx_throw_errno_prefix (error, "fstatat");
+  if (!glnx_fstatat (self->sysroot_fd, "ostree/deploy", &stbuf, 0, error))
+    return FALSE;
 
   if (out_changed)
     {
index 9100b85b193659b68f8538ad1aeaf31d93c7d427..00b3c34f83f8ba7ba6b2e353434862b581ae124c 100644 (file)
@@ -104,27 +104,6 @@ ot_ensure_unlinked_at (int dfd,
   return TRUE;
 }
 
-gboolean
-ot_query_exists_at (int dfd, const char *path,
-                    gboolean *out_exists,
-                    GError **error)
-{
-  struct stat stbuf;
-  gboolean ret_exists;
-
-  if (fstatat (dfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0)
-    {
-      if (errno != ENOENT)
-        return glnx_throw_errno_prefix (error, "fstatat(%s)", path);
-      ret_exists = FALSE;
-    }
-  else
-    ret_exists = TRUE;
-
-  *out_exists = ret_exists;
-  return TRUE;
-}
-
 gboolean
 ot_openat_ignore_enoent (int dfd,
                          const char *path,
index e25522db10d7dfff98c20ab559b3b7a7ac4ce29f..7f73762b813ef5e32ab1c2db0498da1e92062ad5 100644 (file)
@@ -66,11 +66,6 @@ gboolean ot_openat_read_stream (int             dfd,
                                 GCancellable   *cancellable,
                                 GError        **error);
 
-
-gboolean ot_query_exists_at (int dfd, const char *path,
-                             gboolean *out_exists,
-                             GError **error);
-
 gboolean ot_ensure_unlinked_at (int dfd,
                                 const char *path,
                                 GError **error);