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,8 +52,6 @@ symlink_at_replace (const char *oldpath,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
int res;
/* Possibly in the future generate a temporary random name here,
* would need to move "generate a temporary name" code into
* libglnx or glib?
@ -64,22 +62,14 @@ symlink_at_replace (const char *oldpath,
(void) unlinkat (parent_dfd, temppath, 0);
/* Create the temp link */
do
res = symlinkat (oldpath, parent_dfd, temppath);
while (G_UNLIKELY (res == -1 && errno == EINTR));
if (res == -1)
{
glnx_set_error_from_errno (error);
goto out;
}
if (TEMP_FAILURE_RETRY (symlinkat (oldpath, parent_dfd, temppath)) < 0)
return glnx_throw_errno_prefix (error, "symlinkat");
/* Rename it into place */
if (!glnx_renameat (parent_dfd, temppath, parent_dfd, newpath, error))
goto out;
return FALSE;
ret = TRUE;
out:
return ret;
return TRUE;
}
static GLnxFileCopyFlags
@ -104,26 +94,17 @@ hardlink_or_copy_at (int src_dfd,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
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,
sysroot_flags_to_copy_flags (0, flags),
cancellable, error);
}
else
{
glnx_set_error_from_errno (error);
goto out;
}
return glnx_throw_errno_prefix (error, "linkat");
}
ret = TRUE;
out:
return ret;
return TRUE;
}
static gboolean
@ -135,8 +116,6 @@ dirfd_copy_attributes_and_xattrs (int src_parent_dfd,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
struct stat src_stbuf;
g_autoptr(GVariant) xattrs = NULL;
/* 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,
&xattrs, cancellable, error))
goto out;
return FALSE;
if (!glnx_fd_set_all_xattrs (dest_dfd, xattrs,
cancellable, error))
goto out;
return FALSE;
}
if (fstat (src_dfd, &src_stbuf) != 0)
{
glnx_set_error_from_errno (error);
goto out;
}
struct stat src_stbuf;
if (!glnx_fstat (src_dfd, &src_stbuf, error))
return FALSE;
if (fchown (dest_dfd, src_stbuf.st_uid, src_stbuf.st_gid) != 0)
{
glnx_set_error_from_errno (error);
goto out;
}
return glnx_throw_errno_prefix (error, "fchown");
if (fchmod (dest_dfd, src_stbuf.st_mode) != 0)
{
glnx_set_error_from_errno (error);
goto out;
}
return glnx_throw_errno_prefix (error, "fchmod");
ret = TRUE;
out:
return ret;
return TRUE;
}
static gboolean
@ -242,7 +211,6 @@ ensure_directory_from_template (int orig_etc_fd,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
glnx_fd_close int src_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');
if (!glnx_opendirat (modified_etc_fd, path, TRUE, &src_dfd, error))
goto out;
return FALSE;
/* Create with mode 0700, we'll fchmod/fchown later */
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,
parent_path, NULL, flags, cancellable, error))
goto out;
return FALSE;
/* Loop */
goto again;
@ -280,29 +248,19 @@ ensure_directory_from_template (int orig_etc_fd,
}
}
else
{
glnx_set_error_from_errno (error);
g_prefix_error (error, "mkdirat: ");
goto out;
}
return glnx_throw_errno_prefix (error, "mkdirat");
}
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,
flags, cancellable, error))
goto out;
return FALSE;
ret = TRUE;
if (out_dfd)
{
g_assert (target_dfd != -1);
*out_dfd = target_dfd;
target_dfd = -1;
}
out:
return ret;
*out_dfd = glnx_steal_fd (&target_dfd);
return TRUE;
}
/**
@ -321,80 +279,62 @@ copy_modified_config_file (int orig_etc_fd,
GCancellable *cancellable,
GError **error)
{
gboolean ret = FALSE;
struct stat modified_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;
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)
{
g_autofree char *parent = g_path_get_dirname (path);
if (!ensure_directory_from_template (orig_etc_fd, modified_etc_fd, new_etc_fd,
parent, &dest_parent_dfd, flags, cancellable, error))
goto out;
return FALSE;
}
else
{
dest_parent_dfd = dup (new_etc_fd);
if (dest_parent_dfd == -1)
{
glnx_set_error_from_errno (error);
goto out;
}
return glnx_throw_errno_prefix (error, "dup");
}
g_assert (dest_parent_dfd != -1);
if (fstatat (new_etc_fd, path, &new_stbuf, AT_SYMLINK_NOFOLLOW) < 0)
{
if (errno == ENOENT)
;
else
{
glnx_set_error_from_errno (error);
goto out;
}
if (errno != ENOENT)
return glnx_throw_errno_prefix (error, "fstatat");
}
else if (S_ISDIR(new_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",
path);
goto out;
path), FALSE;
}
else
{
/* Do nothing here - we assume that we've already
* recursively copied the parent directory.
*/
ret = TRUE;
goto out;
return TRUE;
}
}
else
{
if (unlinkat (new_etc_fd, path, 0) < 0)
{
glnx_set_error_from_errno (error);
goto out;
}
if (!glnx_unlinkat (new_etc_fd, path, 0, error))
return FALSE;
}
if (S_ISDIR (modified_stbuf.st_mode))
{
if (!copy_dir_recurse (modified_etc_fd, new_etc_fd, path, flags,
cancellable, error))
goto out;
return FALSE;
}
else if (S_ISLNK (modified_stbuf.st_mode) || S_ISREG (modified_stbuf.st_mode))
{
@ -402,19 +342,16 @@ copy_modified_config_file (int orig_etc_fd,
new_etc_fd, path,
sysroot_flags_to_copy_flags (GLNX_FILE_COPY_OVERWRITE, flags),
cancellable, error))
goto out;
return FALSE;
}
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'",
path);
goto out;
}
ret = TRUE;
out:
return ret;
return TRUE;
}
/*