tree-wide: Some glnx_fstatat_allow_noent() porting

The new API is definitely nicer.

Closes: #1180
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-09-15 16:29:22 -04:00 committed by Atomic Bot
parent c62b36046b
commit 13c3898cc2
7 changed files with 55 additions and 100 deletions

View File

@ -1249,12 +1249,10 @@ cleanup_tmpdir (OstreeRepo *self,
if (strcmp (dent->d_name, "cache") == 0) if (strcmp (dent->d_name, "cache") == 0)
continue; continue;
if (TEMP_FAILURE_RETRY (fstatat (dfd_iter.fd, dent->d_name, &stbuf, AT_SYMLINK_NOFOLLOW)) < 0) 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? */ if (errno == ENOENT) /* Did another cleanup win? */
continue; continue;
return glnx_throw_errno_prefix (error, "fstatat(%s)", dent->d_name);
}
/* First, if it's a directory which needs locking, but it's /* First, if it's a directory which needs locking, but it's
* busy, skip it. * busy, skip it.

View File

@ -545,12 +545,10 @@ _ostree_repo_list_refs_internal (OstreeRepo *self,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
g_autoptr(GHashTable) ret_all_refs = NULL;
g_autofree char *remote = NULL; g_autofree char *remote = NULL;
g_autofree char *ref_prefix = 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) if (refspec_prefix)
{ {
struct stat stbuf; struct stat stbuf;
@ -571,12 +569,9 @@ _ostree_repo_list_refs_internal (OstreeRepo *self,
path = glnx_strjoina (prefix_path, ref_prefix); path = glnx_strjoina (prefix_path, ref_prefix);
} }
if (fstatat (self->repo_dir_fd, path, &stbuf, 0) < 0) if (!glnx_fstatat_allow_noent (self->repo_dir_fd, path, &stbuf, 0, error))
{ return FALSE;
if (errno != ENOENT) if (errno == 0)
return glnx_throw_errno (error);
}
else
{ {
if (S_ISDIR (stbuf.st_mode)) if (S_ISDIR (stbuf.st_mode))
{ {

View File

@ -1806,7 +1806,9 @@ repo_create_at_internal (int dfd,
/* Early return if we have an existing repo */ /* Early return if we have an existing repo */
{ g_autofree char *objects_path = g_build_filename (path, "objects", NULL); { 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; glnx_fd_close int repo_dfd = -1;
if (!glnx_opendirat (dfd, path, TRUE, &repo_dfd, error)) 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); *out_dfd = glnx_steal_fd (&repo_dfd);
return TRUE; return TRUE;
} }
else if (errno != ENOENT)
return glnx_throw_errno_prefix (error, "fstatat");
} }
if (mkdirat (dfd, path, 0755) != 0) 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)) if (!glnx_opendirat (dfd, path, TRUE, &repo_dfd, error))
return FALSE; 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)) if (!ostree_repo_mode_to_string (mode, &mode_str, error))
return FALSE; return FALSE;
g_assert (mode_str); 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; const char *collection_id = NULL;
if (options) if (options)
g_variant_lookup (options, "collection-id", "&s", &collection_id); g_variant_lookup (options, "collection-id", "&s", &collection_id);
if (collection_id != NULL) if (collection_id != NULL)
g_string_append_printf (config_data, "collection-id=%s\n", collection_id); g_string_append_printf (config_data, "collection-id=%s\n", collection_id);
if (!glnx_file_replace_contents_at (repo_dfd, "config", if (!glnx_file_replace_contents_at (repo_dfd, "config",
(guint8*)config_data->str, config_data->len, (guint8*)config_data->str, config_data->len,
0, cancellable, error)) 0, cancellable, error))
return FALSE; return FALSE;
}
else
return glnx_throw_errno_prefix (error, "fstatat");
} }
for (guint i = 0; i < G_N_ELEMENTS (state_dirs); i++) for (guint i = 0; i < G_N_ELEMENTS (state_dirs); i++)

View File

@ -689,12 +689,10 @@ selinux_relabel_var_if_needed (OstreeSysroot *sysroot,
* when doing a deployment. * when doing a deployment.
*/ */
const char selabeled[] = "var/.ostree-selabeled"; const char selabeled[] = "var/.ostree-selabeled";
gboolean deployment_var_labeled; struct stat stbuf;
if (!glnx_fstatat_allow_noent (os_deploy_dfd, selabeled, &stbuf, AT_SYMLINK_NOFOLLOW, error))
if (!ot_query_exists_at (os_deploy_dfd, selabeled, &deployment_var_labeled, error))
return FALSE; return FALSE;
if (errno == ENOENT)
if (!deployment_var_labeled)
{ {
{ g_autofree char *msg = { g_autofree char *msg =
g_strdup_printf ("Relabeling /var (no stamp file '%s' found)", selabeled); g_strdup_printf ("Relabeling /var (no stamp file '%s' found)", selabeled);
@ -764,12 +762,13 @@ merge_configuration (OstreeSysroot *sysroot,
} }
} }
gboolean etc_exists = FALSE; struct stat stbuf;
if (!ot_query_exists_at (deployment_dfd, "etc", &etc_exists, error)) if (!glnx_fstatat_allow_noent (deployment_dfd, "etc", &stbuf, AT_SYMLINK_NOFOLLOW, error))
return FALSE; return FALSE;
gboolean usretc_exists = FALSE; gboolean etc_exists = (errno == 0);
if (!ot_query_exists_at (deployment_dfd, "usr/etc", &usretc_exists, error)) if (!glnx_fstatat_allow_noent (deployment_dfd, "usr/etc", &stbuf, AT_SYMLINK_NOFOLLOW, error))
return FALSE; return FALSE;
gboolean usretc_exists = (errno == 0);
if (etc_exists && usretc_exists) if (etc_exists && usretc_exists)
return glnx_throw (error, "Tree contains both /etc and /usr/etc"); 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. * it doesn't exist already.
*/ */
struct stat stbuf; 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, if (!hardlink_or_copy_at (kernel_layout->boot_dfd,
kernel_layout->kernel_srcpath, kernel_layout->kernel_srcpath,
bootcsum_dfd, kernel_layout->kernel_namever, bootcsum_dfd, kernel_layout->kernel_namever,
@ -1586,10 +1585,10 @@ install_deployment_kernel (OstreeSysroot *sysroot,
if (kernel_layout->initramfs_srcpath) if (kernel_layout->initramfs_srcpath)
{ {
g_assert (kernel_layout->initramfs_namever); 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, if (!hardlink_or_copy_at (kernel_layout->boot_dfd, kernel_layout->initramfs_srcpath,
bootcsum_dfd, kernel_layout->initramfs_namever, bootcsum_dfd, kernel_layout->initramfs_namever,
sysroot->debug_flags, sysroot->debug_flags,
@ -1599,19 +1598,14 @@ install_deployment_kernel (OstreeSysroot *sysroot,
} }
g_autofree char *contents = NULL; 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) contents = glnx_file_get_contents_utf8_at (deployment_dfd, "etc/os-release", NULL,
{ cancellable, error);
return glnx_throw_errno (error); if (!contents)
} return glnx_prefix_error (error, "Reading /etc/os-release");
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");
}
} }
else else
{ {

View File

@ -541,12 +541,9 @@ parse_origin (OstreeSysroot *self,
g_autoptr(GKeyFile) ret_origin = g_key_file_new (); g_autoptr(GKeyFile) ret_origin = g_key_file_new ();
struct stat stbuf; struct stat stbuf;
if (fstatat (deployment_dfd, origin_path, &stbuf, 0) != 0) if (!glnx_fstatat_allow_noent (deployment_dfd, origin_path, &stbuf, 0, error))
{ return FALSE;
if (errno != ENOENT) if (errno == 0)
return glnx_throw_errno (error);
}
else
{ {
g_autofree char *origin_contents = g_autofree char *origin_contents =
glnx_file_get_contents_utf8_at (deployment_dfd, origin_path, glnx_file_get_contents_utf8_at (deployment_dfd, origin_path,
@ -808,8 +805,8 @@ ostree_sysroot_load_if_changed (OstreeSysroot *self,
return FALSE; return FALSE;
struct stat stbuf; struct stat stbuf;
if (fstatat (self->sysroot_fd, "ostree/deploy", &stbuf, 0) < 0) if (!glnx_fstatat (self->sysroot_fd, "ostree/deploy", &stbuf, 0, error))
return glnx_throw_errno_prefix (error, "fstatat"); return FALSE;
if (out_changed) if (out_changed)
{ {

View File

@ -104,27 +104,6 @@ ot_ensure_unlinked_at (int dfd,
return TRUE; 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 gboolean
ot_openat_ignore_enoent (int dfd, ot_openat_ignore_enoent (int dfd,
const char *path, const char *path,

View File

@ -66,11 +66,6 @@ gboolean ot_openat_read_stream (int dfd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);
gboolean ot_query_exists_at (int dfd, const char *path,
gboolean *out_exists,
GError **error);
gboolean ot_ensure_unlinked_at (int dfd, gboolean ot_ensure_unlinked_at (int dfd,
const char *path, const char *path,
GError **error); GError **error);