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)
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.

View 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))
{

View 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++)

View 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
{

View 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)
{

View 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,

View 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);