Support $DPKG_ROOT in ucf and ucfr utilities
Thank you for merging and uploading the $DPKG_ROOT related fixes to the maintainer scripts. Now that ucf can be installed with --force-script-chrootless we can start making the ucf and ucfr utilities aware of $DPKG_ROOT. These changes are invasive. Please review carefully. I appreciate your attention to detail and sincere criticism.
- keep $DPKG_ROOT environment variable
- prefix statedir with $DPKG_ROOT
- use /etc directory from $DPKG_ROOT
- destination passed to ucf must not have the $DPKG_ROOT prefix
- path to configuration file passed to ucfr must not have prefix
- use dpkg-realpath instead of readlink as the former respects $DPKG_ROOT automatically and resolves even absolute symlinks relative to it
- other file arguments like --sum-file still require $DPKG_ROOT prefix if appropriate
This patch prefixes a lot of places with $DPKG_ROOT which makes the patch and the resulting code look quite messy. There is an alternative implementation which requires the caller of ucf (for example the openssh-server postinst maintainer script) to prefix the destination path with $DPKG_ROOT. With that done, ucf only needs to remove $DPKG_ROOT from a few places. The patch would look similar to this:
--- a/ucf
+++ b/ucf
@@ -217,24 +217,24 @@ replace_md5sum () {
if [ "$VERBOSE" ]; then
echo >&2 "grep -v \"${dest_file_bre}\" \"$statedir/hashfile\""
grep -v "${dest_file_bre}" "$statedir/hashfile" >&2 || true
- md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|" >&2
+ md5sum "$orig_new_file" | sed "s|$orig_new_file|${dest_file##"$DPKG_ROOT"}|" >&2
fi
grep -v "${dest_file_bre}" "$statedir/hashfile" > \
"$statedir/hashfile.tmp" || true
- md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|" >> \
+ md5sum "$orig_new_file" | sed "s|$orig_new_file|${dest_file##"$DPKG_ROOT"}|" >> \
"$statedir/hashfile.tmp"
mv -f "$statedir/hashfile.tmp" "$statedir/hashfile"
else
echo "(grep -v \"${dest_file_bre}\" \"$statedir/hashfile\""
- echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|$dest_file|\"; "
+ echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|${dest_file##"$DPKG_ROOT"}|\"; "
echo ") | sort > \"$statedir/hashfile\""
fi
else
if [ "$docmd" = "YES" ]; then
- md5sum "$orig_new_file" | sed "s|$orig_new_file|$dest_file|" > \
+ md5sum "$orig_new_file" | sed "s|$orig_new_file|${dest_file##"$DPKG_ROOT"}|" > \
"$statedir/hashfile"
else
- echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|$dest_file|\" >" \
+ echo " md5sum \"$orig_new_file\" | sed \"s|$orig_new_file|${dest_file##"$DPKG_ROOT"}|\" >" \
"\"$statedir/hashfile\""
fi
fi
@@ -402,7 +402,7 @@ if [ -n "$divert_line" ]; then
dest_file=$(dpkg-divert --truename "$dest_file")
fi
fi
-dest_file_bre="[[:space:]]$(escape_bre "$dest_file")"'$'
+dest_file_bre="[[:space:]]$(escape_bre "${dest_file##"$DPKG_ROOT"}")"'$'
######################################################################
######## #########
@@ -558,7 +558,10 @@ else
old_mdsum_dir=""
fi
-cached_file="$(echo "$dest_file" | tr / :)"
+# Remove $DPKG_ROOT prefix from $dest_file
+# The double quotes make sure that potential shell wildcards in $DPKG_ROOT are
+# not interpreted as such but as a fixed string.
+cached_file="$(echo "${dest_file##"$DPKG_ROOT"}" | tr / :)"
######################################################################
######## #########
######## Debugging dump #########
But this has disadvantages:
- I find the removal of the
$DPKG_ROOTprefix untidy as well - if maintainer scripts do not have to change how they call ucf, then that would make use of $DPKG_ROOT easier
- to also make the output of ucf cleaner, the
$DPKG_ROOTprefix has to be removed from a lot more places than the patch above suggests
The code has been tested for correctness by the salsa CI pipeline associated to this MR: helmutg/dpkg-root-demo!8 (merged)
Since the $DPKG_ROOT environment variable is empty by default, most changes of this diff should not introduce new bugs. The biggest functional change is probably the introduction of dpkg-realpath.
The ucf utility is required by openssh-server which is a useful package to have early in the bootstrapping phase to set up a buildd server. This makes chrootless support interesting to have in ucf.
What do you think?