deploy: Port some functions to new style

There are a number of simple ports here.  Prep for further work
in `/etc` merge.

I also stripped trailing whitespace globally.

Closes: #996
Approved by: jlebon
This commit is contained in:
Colin Walters 2017-07-06 17:04:13 -04:00 committed by Atomic Bot
parent 7fa534ac17
commit d2a05e5a09
1 changed files with 62 additions and 125 deletions

View File

@ -52,34 +52,24 @@ symlink_at_replace (const char *oldpath,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
int res;
/* Possibly in the future generate a temporary random name here, /* Possibly in the future generate a temporary random name here,
* would need to move "generate a temporary name" code into * would need to move "generate a temporary name" code into
* libglnx or glib? * libglnx or glib?
*/ */
g_autofree char *temppath = g_strconcat (newpath, ".tmp", NULL); g_autofree char *temppath = g_strconcat (newpath, ".tmp", NULL);
/* Clean up any stale temporary links */ /* Clean up any stale temporary links */
(void) unlinkat (parent_dfd, temppath, 0); (void) unlinkat (parent_dfd, temppath, 0);
/* Create the temp link */ /* Create the temp link */
do if (TEMP_FAILURE_RETRY (symlinkat (oldpath, parent_dfd, temppath)) < 0)
res = symlinkat (oldpath, parent_dfd, temppath); return glnx_throw_errno_prefix (error, "symlinkat");
while (G_UNLIKELY (res == -1 && errno == EINTR));
if (res == -1)
{
glnx_set_error_from_errno (error);
goto out;
}
/* Rename it into place */ /* Rename it into place */
if (!glnx_renameat (parent_dfd, temppath, parent_dfd, newpath, error)) if (!glnx_renameat (parent_dfd, temppath, parent_dfd, newpath, error))
goto out; return FALSE;
ret = TRUE; return TRUE;
out:
return ret;
} }
static GLnxFileCopyFlags static GLnxFileCopyFlags
@ -104,26 +94,17 @@ hardlink_or_copy_at (int src_dfd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
if (linkat (src_dfd, src_subpath, dest_dfd, dest_subpath, 0) != 0) if (linkat (src_dfd, src_subpath, dest_dfd, dest_subpath, 0) != 0)
{ {
if (errno == EMLINK || errno == EXDEV) if (G_IN_SET (errno, EMLINK, EXDEV))
{ return glnx_file_copy_at (src_dfd, src_subpath, NULL, dest_dfd, dest_subpath,
return glnx_file_copy_at (src_dfd, src_subpath, NULL, dest_dfd, dest_subpath, sysroot_flags_to_copy_flags (0, flags),
sysroot_flags_to_copy_flags (0, flags), cancellable, error);
cancellable, error);
}
else else
{ return glnx_throw_errno_prefix (error, "linkat");
glnx_set_error_from_errno (error);
goto out;
}
} }
ret = TRUE; return TRUE;
out:
return ret;
} }
static gboolean static gboolean
@ -135,8 +116,6 @@ dirfd_copy_attributes_and_xattrs (int src_parent_dfd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
struct stat src_stbuf;
g_autoptr(GVariant) xattrs = NULL; g_autoptr(GVariant) xattrs = NULL;
/* Clone all xattrs first, so we get the SELinux security context /* Clone all xattrs first, so we get the SELinux security context
@ -147,31 +126,21 @@ dirfd_copy_attributes_and_xattrs (int src_parent_dfd,
{ {
if (!glnx_dfd_name_get_all_xattrs (src_parent_dfd, src_name, if (!glnx_dfd_name_get_all_xattrs (src_parent_dfd, src_name,
&xattrs, cancellable, error)) &xattrs, cancellable, error))
goto out; return FALSE;
if (!glnx_fd_set_all_xattrs (dest_dfd, xattrs, if (!glnx_fd_set_all_xattrs (dest_dfd, xattrs,
cancellable, error)) cancellable, error))
goto out; return FALSE;
} }
if (fstat (src_dfd, &src_stbuf) != 0) struct stat src_stbuf;
{ if (!glnx_fstat (src_dfd, &src_stbuf, error))
glnx_set_error_from_errno (error); return FALSE;
goto out;
}
if (fchown (dest_dfd, src_stbuf.st_uid, src_stbuf.st_gid) != 0) if (fchown (dest_dfd, src_stbuf.st_uid, src_stbuf.st_gid) != 0)
{ return glnx_throw_errno_prefix (error, "fchown");
glnx_set_error_from_errno (error);
goto out;
}
if (fchmod (dest_dfd, src_stbuf.st_mode) != 0) if (fchmod (dest_dfd, src_stbuf.st_mode) != 0)
{ return glnx_throw_errno_prefix (error, "fchmod");
glnx_set_error_from_errno (error);
goto out;
}
ret = TRUE; return TRUE;
out:
return ret;
} }
static gboolean static gboolean
@ -199,7 +168,7 @@ copy_dir_recurse (int src_parent_dfd,
if (!dirfd_copy_attributes_and_xattrs (src_parent_dfd, name, src_dfd_iter.fd, dest_dfd, if (!dirfd_copy_attributes_and_xattrs (src_parent_dfd, name, src_dfd_iter.fd, dest_dfd,
flags, cancellable, error)) flags, cancellable, error))
return FALSE; return FALSE;
while (TRUE) while (TRUE)
{ {
struct stat child_stbuf; struct stat child_stbuf;
@ -242,7 +211,6 @@ ensure_directory_from_template (int orig_etc_fd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
glnx_fd_close int src_dfd = -1; glnx_fd_close int src_dfd = -1;
glnx_fd_close int target_dfd = -1; glnx_fd_close int target_dfd = -1;
@ -250,7 +218,7 @@ ensure_directory_from_template (int orig_etc_fd,
g_assert (*path != '/' && *path != '\0'); g_assert (*path != '/' && *path != '\0');
if (!glnx_opendirat (modified_etc_fd, path, TRUE, &src_dfd, error)) if (!glnx_opendirat (modified_etc_fd, path, TRUE, &src_dfd, error))
goto out; return FALSE;
/* Create with mode 0700, we'll fchmod/fchown later */ /* Create with mode 0700, we'll fchmod/fchown later */
again: again:
@ -268,7 +236,7 @@ ensure_directory_from_template (int orig_etc_fd,
{ {
if (!ensure_directory_from_template (orig_etc_fd, modified_etc_fd, new_etc_fd, if (!ensure_directory_from_template (orig_etc_fd, modified_etc_fd, new_etc_fd,
parent_path, NULL, flags, cancellable, error)) parent_path, NULL, flags, cancellable, error))
goto out; return FALSE;
/* Loop */ /* Loop */
goto again; goto again;
@ -280,29 +248,19 @@ ensure_directory_from_template (int orig_etc_fd,
} }
} }
else else
{ return glnx_throw_errno_prefix (error, "mkdirat");
glnx_set_error_from_errno (error);
g_prefix_error (error, "mkdirat: ");
goto out;
}
} }
if (!glnx_opendirat (new_etc_fd, path, TRUE, &target_dfd, error)) if (!glnx_opendirat (new_etc_fd, path, TRUE, &target_dfd, error))
goto out; return FALSE;
if (!dirfd_copy_attributes_and_xattrs (modified_etc_fd, path, src_dfd, target_dfd, if (!dirfd_copy_attributes_and_xattrs (modified_etc_fd, path, src_dfd, target_dfd,
flags, cancellable, error)) flags, cancellable, error))
goto out; return FALSE;
ret = TRUE;
if (out_dfd) if (out_dfd)
{ *out_dfd = glnx_steal_fd (&target_dfd);
g_assert (target_dfd != -1); return TRUE;
*out_dfd = target_dfd;
target_dfd = -1;
}
out:
return ret;
} }
/** /**
@ -321,100 +279,79 @@ copy_modified_config_file (int orig_etc_fd,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
gboolean ret = FALSE;
struct stat modified_stbuf; struct stat modified_stbuf;
struct stat new_stbuf; struct stat new_stbuf;
if (!glnx_fstatat (modified_etc_fd, path, &modified_stbuf, AT_SYMLINK_NOFOLLOW, error))
return glnx_prefix_error (error, "Reading modified config file");
glnx_fd_close int dest_parent_dfd = -1; glnx_fd_close int dest_parent_dfd = -1;
if (fstatat (modified_etc_fd, path, &modified_stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
glnx_set_error_from_errno (error);
g_prefix_error (error, "Failed to read modified config file '%s': ", path);
goto out;
}
if (strchr (path, '/') != NULL) if (strchr (path, '/') != NULL)
{ {
g_autofree char *parent = g_path_get_dirname (path); g_autofree char *parent = g_path_get_dirname (path);
if (!ensure_directory_from_template (orig_etc_fd, modified_etc_fd, new_etc_fd, if (!ensure_directory_from_template (orig_etc_fd, modified_etc_fd, new_etc_fd,
parent, &dest_parent_dfd, flags, cancellable, error)) parent, &dest_parent_dfd, flags, cancellable, error))
goto out; return FALSE;
} }
else else
{ {
dest_parent_dfd = dup (new_etc_fd); dest_parent_dfd = dup (new_etc_fd);
if (dest_parent_dfd == -1) if (dest_parent_dfd == -1)
{ return glnx_throw_errno_prefix (error, "dup");
glnx_set_error_from_errno (error);
goto out;
}
} }
g_assert (dest_parent_dfd != -1); g_assert (dest_parent_dfd != -1);
if (fstatat (new_etc_fd, path, &new_stbuf, AT_SYMLINK_NOFOLLOW) < 0) if (fstatat (new_etc_fd, path, &new_stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{ {
if (errno == ENOENT) if (errno != ENOENT)
; return glnx_throw_errno_prefix (error, "fstatat");
else
{
glnx_set_error_from_errno (error);
goto out;
}
} }
else if (S_ISDIR(new_stbuf.st_mode)) else if (S_ISDIR(new_stbuf.st_mode))
{ {
if (!S_ISDIR(modified_stbuf.st_mode)) if (!S_ISDIR(modified_stbuf.st_mode))
{ {
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, return g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND,
"Modified config file newly defaults to directory '%s', cannot merge", "Modified config file newly defaults to directory '%s', cannot merge",
path); path), FALSE;
goto out;
} }
else else
{ {
/* Do nothing here - we assume that we've already /* Do nothing here - we assume that we've already
* recursively copied the parent directory. * recursively copied the parent directory.
*/ */
ret = TRUE; return TRUE;
goto out;
} }
} }
else else
{ {
if (unlinkat (new_etc_fd, path, 0) < 0) if (!glnx_unlinkat (new_etc_fd, path, 0, error))
{ return FALSE;
glnx_set_error_from_errno (error);
goto out;
}
} }
if (S_ISDIR (modified_stbuf.st_mode)) if (S_ISDIR (modified_stbuf.st_mode))
{ {
if (!copy_dir_recurse (modified_etc_fd, new_etc_fd, path, flags, if (!copy_dir_recurse (modified_etc_fd, new_etc_fd, path, flags,
cancellable, error)) cancellable, error))
goto out; return FALSE;
} }
else if (S_ISLNK (modified_stbuf.st_mode) || S_ISREG (modified_stbuf.st_mode)) else if (S_ISLNK (modified_stbuf.st_mode) || S_ISREG (modified_stbuf.st_mode))
{ {
if (!glnx_file_copy_at (modified_etc_fd, path, &modified_stbuf, if (!glnx_file_copy_at (modified_etc_fd, path, &modified_stbuf,
new_etc_fd, path, new_etc_fd, path,
sysroot_flags_to_copy_flags (GLNX_FILE_COPY_OVERWRITE, flags), sysroot_flags_to_copy_flags (GLNX_FILE_COPY_OVERWRITE, flags),
cancellable, error)) cancellable, error))
goto out; return FALSE;
} }
else else
{ {
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, return glnx_throw (error,
"Unsupported non-regular/non-symlink file in /etc '%s'", "Unsupported non-regular/non-symlink file in /etc '%s'",
path); path);
goto out;
} }
ret = TRUE; return TRUE;
out:
return ret;
} }
/* /*
@ -575,7 +512,7 @@ checkout_deployment_tree (OstreeSysroot *sysroot,
if (!glnx_opendirat (osdeploy_dfd, checkout_target_name, TRUE, &ret_fd, error)) if (!glnx_opendirat (osdeploy_dfd, checkout_target_name, TRUE, &ret_fd, error))
goto out; goto out;
ret = TRUE; ret = TRUE;
*out_deployment_dfd = ret_fd; *out_deployment_dfd = ret_fd;
out: out:
@ -652,7 +589,7 @@ relabel_recursively (OstreeSysroot *sysroot,
cancellable, error); cancellable, error);
if (!direnum) if (!direnum)
goto out; goto out;
while (TRUE) while (TRUE)
{ {
GFileInfo *file_info; GFileInfo *file_info;
@ -706,7 +643,7 @@ selinux_relabel_dir (OstreeSysroot *sysroot,
cancellable, error); cancellable, error);
if (!root_info) if (!root_info)
goto out; goto out;
g_ptr_array_add (path_parts, (char*)prefix); g_ptr_array_add (path_parts, (char*)prefix);
if (!relabel_recursively (sysroot, sepolicy, dir, root_info, path_parts, if (!relabel_recursively (sysroot, sepolicy, dir, root_info, path_parts,
cancellable, error)) cancellable, error))
@ -959,7 +896,7 @@ get_kernel_from_tree (int deployment_dfd,
if (!glnx_dirfd_iterator_next_dent (&dfditer, &dent, cancellable, error)) if (!glnx_dirfd_iterator_next_dent (&dfditer, &dent, cancellable, error))
goto out; goto out;
if (dent == NULL) if (dent == NULL)
break; break;
@ -983,7 +920,7 @@ get_kernel_from_tree (int deployment_dfd,
ret_initramfs_name = g_strdup (dent->d_name); ret_initramfs_name = g_strdup (dent->d_name);
} }
} }
if (ret_kernel_name != NULL && ret_initramfs_name != NULL) if (ret_kernel_name != NULL && ret_initramfs_name != NULL)
break; break;
} }
@ -1451,7 +1388,7 @@ static GHashTable *
assign_bootserials (GPtrArray *deployments) assign_bootserials (GPtrArray *deployments)
{ {
guint i; guint i;
GHashTable *ret = GHashTable *ret =
g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL); g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
for (i = 0; i < deployments->len; i++) for (i = 0; i < deployments->len; i++)
@ -1488,7 +1425,7 @@ deployment_bootconfigs_equal (OstreeDeployment *a,
__attribute__((cleanup(_ostree_kernel_args_cleanup))) OstreeKernelArgs *b_kargs = NULL; __attribute__((cleanup(_ostree_kernel_args_cleanup))) OstreeKernelArgs *b_kargs = NULL;
g_autofree char *a_boot_options_without_ostree = NULL; g_autofree char *a_boot_options_without_ostree = NULL;
g_autofree char *b_boot_options_without_ostree = NULL; g_autofree char *b_boot_options_without_ostree = NULL;
/* We checksum the kernel arguments *except* ostree= */ /* We checksum the kernel arguments *except* ostree= */
a_kargs = _ostree_kernel_args_from_string (a_boot_options); a_kargs = _ostree_kernel_args_from_string (a_boot_options);
_ostree_kernel_args_replace (a_kargs, "ostree"); _ostree_kernel_args_replace (a_kargs, "ostree");
@ -1662,10 +1599,10 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self,
{ {
OstreeDeployment *deployment = new_deployments->pdata[i]; OstreeDeployment *deployment = new_deployments->pdata[i];
g_autoptr(GFile) deployment_root = NULL; g_autoptr(GFile) deployment_root = NULL;
if (deployment == self->booted_deployment) if (deployment == self->booted_deployment)
found_booted_deployment = TRUE; found_booted_deployment = TRUE;
deployment_root = ostree_sysroot_get_deployment_directory (self, deployment); deployment_root = ostree_sysroot_get_deployment_directory (self, deployment);
if (!g_file_query_exists (deployment_root, NULL)) if (!g_file_query_exists (deployment_root, NULL))
{ {
@ -1693,7 +1630,7 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self,
g_prefix_error (error, "Creating new current bootlinks: "); g_prefix_error (error, "Creating new current bootlinks: ");
goto out; goto out;
} }
if (!full_system_sync (self, cancellable, error)) if (!full_system_sync (self, cancellable, error))
{ {
g_prefix_error (error, "Full sync: "); g_prefix_error (error, "Full sync: ");
@ -1707,7 +1644,7 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self,
g_prefix_error (error, "Swapping current bootlinks: "); g_prefix_error (error, "Swapping current bootlinks: ");
goto out; goto out;
} }
bootloader_is_atomic = TRUE; bootloader_is_atomic = TRUE;
} }
else else
@ -1741,7 +1678,7 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self,
if (!glnx_shutil_mkdir_p_at (self->sysroot_fd, new_loader_entries_dir, 0755, if (!glnx_shutil_mkdir_p_at (self->sysroot_fd, new_loader_entries_dir, 0755,
cancellable, error)) cancellable, error))
goto out; goto out;
/* Need the repo to try and extract the versions for deployments. /* Need the repo to try and extract the versions for deployments.
* But this is a "nice-to-have" for the bootloader UI, so failure * But this is a "nice-to-have" for the bootloader UI, so failure
* here is not fatal to the whole operation. We just gracefully * here is not fatal to the whole operation. We just gracefully
@ -1819,7 +1756,7 @@ ostree_sysroot_write_deployments_with_options (OstreeSysroot *self,
g_prefix_error (error, "Full sync: "); g_prefix_error (error, "Full sync: ");
goto out; goto out;
} }
if (!swap_bootloader (self, self->bootversion, new_bootversion, if (!swap_bootloader (self, self->bootversion, new_bootversion,
cancellable, error)) cancellable, error))
{ {