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, 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?
@ -64,22 +62,14 @@ symlink_at_replace (const char *oldpath,
(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
@ -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,80 +279,62 @@ 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))
{ {
@ -402,19 +342,16 @@ copy_modified_config_file (int orig_etc_fd,
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;
} }
/* /*