lib: Add "open dfd iter handling noent" helper, port tree-wide
Follow up to a previous patch that addressed a double-close; I realized we already had a helper for doing "open dfd iter, do nothing if we get ENOENT". Raise it to libotuil, and port all consumers. Closes: #863 Approved by: jlebon
This commit is contained in:
parent
90cd7f7234
commit
9380dbb14d
|
|
@ -114,19 +114,14 @@ _ostree_repo_prune_tmp (OstreeRepo *self,
|
||||||
if (self->cache_dir_fd == -1)
|
if (self->cache_dir_fd == -1)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
||||||
glnx_fd_close int fd = glnx_opendirat_with_errno (self->cache_dir_fd, _OSTREE_SUMMARY_CACHE_DIR, FALSE);
|
|
||||||
if (fd < 0)
|
|
||||||
{
|
|
||||||
/* Note early return */
|
|
||||||
if (errno == ENOENT)
|
|
||||||
return TRUE;
|
|
||||||
else
|
|
||||||
return glnx_throw_errno_prefix (error, "opendirat(%s)", _OSTREE_SUMMARY_CACHE_DIR);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
||||||
if (!glnx_dirfd_iterator_init_take_fd (dup (fd), &dfd_iter, error))
|
gboolean exists;
|
||||||
|
if (!ot_dfd_iter_init_allow_noent (self->cache_dir_fd, _OSTREE_SUMMARY_CACHE_DIR,
|
||||||
|
&dfd_iter, &exists, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
/* Note early return */
|
||||||
|
if (!exists)
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
while (TRUE)
|
while (TRUE)
|
||||||
{
|
{
|
||||||
|
|
@ -152,7 +147,7 @@ _ostree_repo_prune_tmp (OstreeRepo *self,
|
||||||
if (has_sig_suffix)
|
if (has_sig_suffix)
|
||||||
dent->d_name[len - 4] = '.';
|
dent->d_name[len - 4] = '.';
|
||||||
|
|
||||||
if (unlinkat (fd, dent->d_name, 0) < 0)
|
if (unlinkat (dfd_iter.fd, dent->d_name, 0) < 0)
|
||||||
return glnx_throw_errno_prefix (error, "unlinkat");
|
return glnx_throw_errno_prefix (error, "unlinkat");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -73,27 +73,19 @@ ostree_repo_list_static_delta_names (OstreeRepo *self,
|
||||||
GCancellable *cancellable,
|
GCancellable *cancellable,
|
||||||
GError **error)
|
GError **error)
|
||||||
{
|
{
|
||||||
g_autoptr(GPtrArray) ret_deltas = NULL;
|
g_autoptr(GPtrArray) ret_deltas = g_ptr_array_new_with_free_func (g_free);
|
||||||
glnx_fd_close int dfd = -1;
|
|
||||||
|
|
||||||
ret_deltas = g_ptr_array_new_with_free_func (g_free);
|
|
||||||
|
|
||||||
dfd = glnx_opendirat_with_errno (self->repo_dir_fd, "deltas", TRUE);
|
|
||||||
if (dfd < 0)
|
|
||||||
{
|
|
||||||
if (errno != ENOENT)
|
|
||||||
{
|
|
||||||
glnx_set_error_from_errno (error);
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
||||||
|
gboolean exists;
|
||||||
if (!glnx_dirfd_iterator_init_take_fd (dfd, &dfd_iter, error))
|
if (!ot_dfd_iter_init_allow_noent (self->repo_dir_fd, "deltas", &dfd_iter,
|
||||||
|
&exists, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
dfd = -1;
|
if (!exists)
|
||||||
|
{
|
||||||
|
/* Note early return */
|
||||||
|
ot_transfer_out_value (out_deltas, &ret_deltas);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
while (TRUE)
|
while (TRUE)
|
||||||
{
|
{
|
||||||
|
|
@ -162,10 +154,8 @@ ostree_repo_list_static_delta_names (OstreeRepo *self,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (out_deltas)
|
ot_transfer_out_value (out_deltas, &ret_deltas);
|
||||||
*out_deltas = g_steal_pointer (&ret_deltas);
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2272,18 +2272,13 @@ list_loose_objects_at (OstreeRepo *self,
|
||||||
{
|
{
|
||||||
GVariant *key, *value;
|
GVariant *key, *value;
|
||||||
|
|
||||||
glnx_fd_close int target_dfd = glnx_opendirat_with_errno (dfd, prefix, FALSE);
|
|
||||||
if (target_dfd < 0)
|
|
||||||
{
|
|
||||||
/* Nothing to do if this dir doesn't exist */
|
|
||||||
if (errno == ENOENT)
|
|
||||||
return TRUE;
|
|
||||||
return glnx_throw_errno (error);
|
|
||||||
}
|
|
||||||
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
||||||
if (!glnx_dirfd_iterator_init_take_fd (target_dfd, &dfd_iter, error))
|
gboolean exists;
|
||||||
|
if (!ot_dfd_iter_init_allow_noent (dfd, prefix, &dfd_iter, &exists, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
target_dfd = -1; /* Transferred */
|
/* Note early return */
|
||||||
|
if (!exists)
|
||||||
|
return TRUE;
|
||||||
|
|
||||||
while (TRUE)
|
while (TRUE)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -25,31 +25,6 @@
|
||||||
|
|
||||||
#include "ostree-sysroot-private.h"
|
#include "ostree-sysroot-private.h"
|
||||||
|
|
||||||
/* Like glnx_dirfd_iterator_init_at(), but if %ENOENT, then set
|
|
||||||
* @out_exists to %FALSE, and return successfully.
|
|
||||||
*/
|
|
||||||
static gboolean
|
|
||||||
dfd_iter_init_allow_noent (int dfd,
|
|
||||||
const char *path,
|
|
||||||
GLnxDirFdIterator *dfd_iter,
|
|
||||||
gboolean *out_exists,
|
|
||||||
GError **error)
|
|
||||||
{
|
|
||||||
glnx_fd_close int fd = glnx_opendirat_with_errno (dfd, path, TRUE);
|
|
||||||
if (fd < 0)
|
|
||||||
{
|
|
||||||
if (errno != ENOENT)
|
|
||||||
return glnx_throw_errno (error);
|
|
||||||
*out_exists = FALSE;
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
if (!glnx_dirfd_iterator_init_take_fd (fd, dfd_iter, error))
|
|
||||||
return FALSE;
|
|
||||||
fd = -1;
|
|
||||||
*out_exists = TRUE;
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* @deploydir_dfd: Directory FD for ostree/deploy
|
/* @deploydir_dfd: Directory FD for ostree/deploy
|
||||||
* @osname: Target osname
|
* @osname: Target osname
|
||||||
* @inout_deployments: All deployments in this subdir will be appended to this array
|
* @inout_deployments: All deployments in this subdir will be appended to this array
|
||||||
|
|
@ -64,7 +39,7 @@ _ostree_sysroot_list_deployment_dirs_for_os (int deploydir_dfd,
|
||||||
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
||||||
gboolean exists;
|
gboolean exists;
|
||||||
const char *osdeploy_path = glnx_strjoina (osname, "/deploy");
|
const char *osdeploy_path = glnx_strjoina (osname, "/deploy");
|
||||||
if (!dfd_iter_init_allow_noent (deploydir_dfd, osdeploy_path, &dfd_iter, &exists, error))
|
if (!ot_dfd_iter_init_allow_noent (deploydir_dfd, osdeploy_path, &dfd_iter, &exists, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
if (!exists)
|
if (!exists)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
@ -106,7 +81,7 @@ list_all_deployment_directories (OstreeSysroot *self,
|
||||||
|
|
||||||
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
||||||
gboolean exists;
|
gboolean exists;
|
||||||
if (!dfd_iter_init_allow_noent (self->sysroot_fd, "ostree/deploy", &dfd_iter, &exists, error))
|
if (!ot_dfd_iter_init_allow_noent (self->sysroot_fd, "ostree/deploy", &dfd_iter, &exists, error))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
if (!exists)
|
if (!exists)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
|
||||||
|
|
@ -421,23 +421,17 @@ _ostree_sysroot_read_boot_loader_configs (OstreeSysroot *self,
|
||||||
g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
|
g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
|
||||||
|
|
||||||
g_autofree char *entries_path = g_strdup_printf ("boot/loader.%d/entries", bootversion);
|
g_autofree char *entries_path = g_strdup_printf ("boot/loader.%d/entries", bootversion);
|
||||||
/* Temporary owned by iterator */
|
gboolean entries_exists;
|
||||||
int fd = glnx_opendirat_with_errno (self->sysroot_fd, entries_path, TRUE);
|
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
||||||
if (fd == -1)
|
if (!ot_dfd_iter_init_allow_noent (self->sysroot_fd, entries_path,
|
||||||
{
|
&dfd_iter, &entries_exists, error))
|
||||||
if (errno != ENOENT)
|
return FALSE;
|
||||||
return glnx_throw_errno (error);
|
if (!entries_exists)
|
||||||
else
|
|
||||||
{
|
{
|
||||||
/* Note early return */
|
/* Note early return */
|
||||||
*out_loader_configs = g_steal_pointer (&ret_loader_configs);
|
*out_loader_configs = g_steal_pointer (&ret_loader_configs);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
g_auto(GLnxDirFdIterator) dfd_iter = { 0, };
|
|
||||||
if (!glnx_dirfd_iterator_init_take_fd (fd, &dfd_iter, error))
|
|
||||||
return FALSE;
|
|
||||||
|
|
||||||
while (TRUE)
|
while (TRUE)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -172,6 +172,31 @@ ot_openat_ignore_enoent (int dfd,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Like glnx_dirfd_iterator_init_at(), but if %ENOENT, then set
|
||||||
|
* @out_exists to %FALSE, and return successfully.
|
||||||
|
*/
|
||||||
|
gboolean
|
||||||
|
ot_dfd_iter_init_allow_noent (int dfd,
|
||||||
|
const char *path,
|
||||||
|
GLnxDirFdIterator *dfd_iter,
|
||||||
|
gboolean *out_exists,
|
||||||
|
GError **error)
|
||||||
|
{
|
||||||
|
glnx_fd_close int fd = glnx_opendirat_with_errno (dfd, path, TRUE);
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
if (errno != ENOENT)
|
||||||
|
return glnx_throw_errno_prefix (error, "opendirat");
|
||||||
|
*out_exists = FALSE;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
if (!glnx_dirfd_iterator_init_take_fd (fd, dfd_iter, error))
|
||||||
|
return FALSE;
|
||||||
|
fd = -1;
|
||||||
|
*out_exists = TRUE;
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
GBytes *
|
GBytes *
|
||||||
ot_file_mapat_bytes (int dfd,
|
ot_file_mapat_bytes (int dfd,
|
||||||
const char *path,
|
const char *path,
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "ot-unix-utils.h"
|
#include "ot-unix-utils.h"
|
||||||
|
#include "libglnx.h"
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
|
@ -53,6 +54,12 @@ gboolean ot_openat_ignore_enoent (int dfd,
|
||||||
int *out_fd,
|
int *out_fd,
|
||||||
GError **error);
|
GError **error);
|
||||||
|
|
||||||
|
gboolean ot_dfd_iter_init_allow_noent (int dfd,
|
||||||
|
const char *path,
|
||||||
|
GLnxDirFdIterator *dfd_iter,
|
||||||
|
gboolean *out_exists,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
GBytes *ot_file_mapat_bytes (int dfd,
|
GBytes *ot_file_mapat_bytes (int dfd,
|
||||||
const char *path,
|
const char *path,
|
||||||
GError **error);
|
GError **error);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue