[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#799443: [PATCH initramfs-tools 4/5] hook-functions: Preserve symlinks when copying files



When the source for copy_file is a symlink, copy both the symlink and
the file it points to.  Resolve the symlink fully rather than trying
to replicate a potentially complex chain of symlinks.

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
---
 hook-functions | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/hook-functions b/hook-functions
index 6940fb7..022ef27 100644
--- a/hook-functions
+++ b/hook-functions
@@ -115,7 +115,7 @@ manual_add_modules()
 # If the target exists, we leave it and return 1.
 # On any other error, we return >1.
 copy_file() {
-	local type src target
+	local type src target link_target
 
 	type="${1}"
 	src="${2}"
@@ -124,16 +124,29 @@ copy_file() {
 	[ -f "${src}" ] || return 2
 
 	if [ -d "${DESTDIR}/${target}" ]; then
-		# check if already copied
-		[ -e "${DESTDIR}/$target/${src##*/}" ] && return 1
-	else
-		[ -e "${DESTDIR}/$target" ] && return 1
-		#FIXME: inst_dir
-		mkdir -p "${DESTDIR}/${target%/*}"
+		target="${target}/${src##*/}"
 	fi
 
+	# check if already copied
+	[ -e "${DESTDIR}/${target}" ] && return 1
+
+	#FIXME: inst_dir
+	mkdir -p "${DESTDIR}/${target%/*}"
+
 	[ "${verbose}" = "y" ] && echo "Adding ${type} ${src}"
-	cp -pL "${src}" "${DESTDIR}/${target}" || return $(($? + 1))
+
+	if [ -h "${src}" ]; then
+		# We don't need to replicate a chain of links completely;
+		# just link directly to the ultimate target.  Create a
+		# relative link so it always points to the right place.
+		link_target="$(readlink -f "${src}")" || return $(($? + 1))
+		ln -rs "${DESTDIR}/${link_target}" "${DESTDIR}/${target}"
+		src="${link_target}"
+		target="${link_target}"
+		[ "${verbose}" = "y" ] && echo "(link to ${src})"
+	fi
+
+	cp -pP "${src}" "${DESTDIR}/${target}" || return $(($? + 1))
 }
 
 # $1 = executable to copy to ramdisk, with library dependencies

Attachment: signature.asc
Description: Digital signature


Reply to: