From 06b00bced8807fe8c16f2abdfad956f59e053ca8 Mon Sep 17 00:00:00 2001
From: Javier Martinez Canillas <javierm@redhat.com>
Date: Thu, 16 Apr 2020 18:53:03 +0200
Subject: [PATCH 203/220] grub-switch-to-blscfg: Use install to copy GRUB
 binary, modules and config

By default the cp command truncates the destination before copying from the
source, so if interrupted it can lead to a file that's half written.

This behavior can be modified using the --remove-destination option, but is
usually a better choice to use the install tool for this. So let's do that.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
---
 util/grub-switch-to-blscfg.in | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/util/grub-switch-to-blscfg.in b/util/grub-switch-to-blscfg.in
index 4bbed8e..3333a62 100644
--- a/util/grub-switch-to-blscfg.in
+++ b/util/grub-switch-to-blscfg.in
@@ -273,7 +273,7 @@ copy_bls() {
 if test -f /run/ostree-booted && test -d /sys/firmware/efi/efivars && \
    ! ${grub_editenv} - list | grep -q blsdir; then
     grub_binary="$(find /usr/lib/ostree-boot/efi/EFI/${EFIDIR}/ -name grub*.efi)"
-    cp ${grub_binary} ${grubdir} || exit 1
+    install -m 700 ${grub_binary} ${grubdir} || exit 1
     # Create a hidden file to indicate that grub2 now has BLS support.
     touch /boot/grub2/.grub2-blscfg-supported
 fi
@@ -307,13 +307,13 @@ if [ "${GENERATE}" -eq 1 ] ; then
 
     if [ -n "${mod_dir}" ]; then
 	for mod in blscfg increment; do
-	    cp ${prefix}/lib/grub/${mod_dir}/${mod}.mod ${grubdir}/$mod_dir/ || exit 1
+	    install -m 700 ${prefix}/lib/grub/${mod_dir}/${mod}.mod ${grubdir}/$mod_dir/ || exit 1
 	done
     fi
 
     cp -af "${GRUB_CONFIG_FILE}" "${GRUB_CONFIG_FILE}${backupsuffix}"
     if ! grub2-mkconfig -o "${GRUB_CONFIG_FILE}" ; then
-        cp -af "${GRUB_CONFIG_FILE}${backupsuffix}" "${GRUB_CONFIG_FILE}"
+        install -m 700 "${GRUB_CONFIG_FILE}${backupsuffix}" "${GRUB_CONFIG_FILE}"
         sed -i"${backupsuffix}" \
             -e 's,^GRUB_ENABLE_BLSCFG=.*,GRUB_ENABLE_BLSCFG=false,' \
             "${etcdefaultgrub}"
-- 
1.8.3.1

