3 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 # Copyright (c) 2010-2013 Hudson River Trading LLC
6 # Written by: John H. Baldwin <jhb@FreeBSD.org>
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
18 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 # This is a tool to manage updating files that are not updated as part
33 # of 'make installworld' such as files in /etc. Unlike other tools,
34 # this one is specifically tailored to assisting with mass upgrades.
35 # To that end it does not require user intervention while running.
37 # Theory of operation:
39 # The most reliable way to update changes to files that have local
40 # modifications is to perform a three-way merge between the original
41 # unmodified file, the new version of the file, and the modified file.
42 # This requires having all three versions of the file available when
43 # performing an update.
45 # To that end, etcupdate uses a strategy where the current unmodified
46 # tree is kept in WORKDIR/current and the previous unmodified tree is
47 # kept in WORKDIR/old. When performing a merge, a new tree is built
48 # if needed and then the changes are merged into DESTDIR. Any files
49 # with unresolved conflicts after the merge are left in a tree rooted
50 # at WORKDIR/conflicts.
52 # To provide extra flexibility, etcupdate can also build tarballs of
53 # root trees that can later be used. It can also use a tarball as the
54 # source of a new tree instead of building it from /usr/src.
56 # Global settings. These can be adjusted by config files and in some
57 # cases by command line options.
60 # - automatable conflict resolution
61 # - a 'revert' command to make a file "stock"
66 usage: etcupdate [-npBF] [-d workdir] [-r | -s source | -t tarball]
67 [-A patterns] [-D destdir] [-I patterns] [-L logfile]
69 etcupdate build [-B] [-d workdir] [-s source] [-L logfile] [-M options]
71 etcupdate diff [-d workdir] [-D destdir] [-I patterns] [-L logfile]
72 etcupdate extract [-B] [-d workdir] [-s source | -t tarball] [-L logfile]
74 etcupdate resolve [-p] [-d workdir] [-D destdir] [-L logfile]
75 etcupdate status [-d workdir] [-D destdir]
80 # Used to write a message prepended with '>>>' to the logfile.
86 # Used for assertion conditions that should never happen.
93 # Used to write a warning message. These are saved to the WARNINGS
94 # file with " " prepended.
97 echo -n " " >> $WARNINGS
98 echo "$@" >> $WARNINGS
101 # Output a horizontal rule using the passed-in character. Matches the
102 # length used for Index lines in CVS and SVN diffs.
110 # Output a text description of a specified file's type.
112 # $1 - file pathname.
115 stat -f "%HT" $1 | tr "[:upper:]" "[:lower:]"
118 # Returns true (0) if a file exists
120 # $1 - file pathname.
126 # Returns true (0) if a file should be ignored, false otherwise.
134 for pattern in $IGNORE_FILES; do
144 # Ignore /.cshrc and /.profile if they are hardlinked to the
145 # same file in /root. This ensures we only compare those
146 # files once in that case.
149 if [ ${DESTDIR}$1 -ef ${DESTDIR}/root$1 ]; then
160 # Returns true (0) if the new version of a file should always be
161 # installed rather than attempting to do a merge.
169 for pattern in $ALWAYS_INSTALL; do
184 # $1 - directory to store new tree in
187 local destdir dir file make
189 make="make $MAKE_OPTIONS -DNO_FILEMON"
191 log "Building tree at $1 with $make"
192 mkdir -p $1/usr/obj >&3 2>&1
193 destdir=`realpath $1`
195 if [ -n "$preworld" ]; then
196 # Build a limited tree that only contains files that are
197 # crucial to installworld.
198 for file in $PREWORLD_FILES; do
199 name=$(basename $file)
200 mkdir -p $1/etc >&3 2>&1 || return 1
201 cp -p $SRCDIR/$file $1/etc/$name || return 1
203 elif ! [ -n "$nobuild" ]; then
204 (cd $SRCDIR; $make DESTDIR=$destdir distrib-dirs &&
205 MAKEOBJDIRPREFIX=$destdir/usr/obj $make _obj SUBDIR_OVERRIDE=etc &&
206 MAKEOBJDIRPREFIX=$destdir/usr/obj $make everything SUBDIR_OVERRIDE=etc &&
207 MAKEOBJDIRPREFIX=$destdir/usr/obj $make DESTDIR=$destdir distribution) \
210 (cd $SRCDIR; $make DESTDIR=$destdir distrib-dirs &&
211 $make DESTDIR=$destdir distribution) >&3 2>&1 || return 1
213 chflags -R noschg $1 >&3 2>&1 || return 1
214 rm -rf $1/usr/obj >&3 2>&1 || return 1
216 # Purge auto-generated files. Only the source files need to
217 # be updated after which these files are regenerated.
218 rm -f $1/etc/*.db $1/etc/passwd $1/var/db/services.db >&3 2>&1 || \
221 # Remove empty files. These just clutter the output of 'diff'.
222 find $1 -type f -size 0 -delete >&3 2>&1 || return 1
224 # Trim empty directories.
225 find -d $1 -type d -empty -delete >&3 2>&1 || return 1
229 # Generate a new NEWTREE tree. If tarball is set, then the tree is
230 # extracted from the tarball. Otherwise the tree is built from a
236 # If we have a tarball, extract that into the new directory.
237 if [ -n "$tarball" ]; then
239 if [ -n "$preworld" ]; then
240 files="$PREWORLD_FILES"
242 if ! (mkdir -p $NEWTREE && tar xf $tarball -C $NEWTREE $files) \
244 echo "Failed to extract new tree."
249 if ! build_tree $NEWTREE; then
250 echo "Failed to build new tree."
257 # Forcefully remove a tree. Returns true (0) if the operation succeeds.
265 chflags -R noschg $1 >&3 2>&1
271 # Return values for compare()
279 # Compare two files/directories/symlinks. Note that this does not
280 # recurse into subdirectories. Instead, if two nodes are both
281 # directories, they are assumed to be equivalent.
283 # Returns true (0) if the nodes are identical. If only one of the two
284 # nodes are present, return one of the COMPARE_ONLY* constants. If
285 # the nodes are different, return one of the COMPARE_DIFF* constants
286 # to indicate the type of difference.
294 # If the first node doesn't exist, then check for the second
295 # node. Note that -e will fail for a symbolic link that
296 # points to a missing target.
299 return $COMPARE_ONLYSECOND
301 return $COMPARE_EQUAL
303 elif ! exists $2; then
304 return $COMPARE_ONLYFIRST
307 # If the two nodes are different file types fail.
308 first=`stat -f "%Hp" $1`
309 second=`stat -f "%Hp" $2`
310 if [ "$first" != "$second" ]; then
311 return $COMPARE_DIFFTYPE
314 # If both are symlinks, compare the link values.
318 if [ "$first" = "$second" ]; then
319 return $COMPARE_EQUAL
321 return $COMPARE_DIFFLINKS
325 # If both are files, compare the file contents.
327 if cmp -s $1 $2; then
328 return $COMPARE_EQUAL
330 return $COMPARE_DIFFFILES
334 # As long as the two nodes are the same type of file, consider
336 return $COMPARE_EQUAL
339 # Returns true (0) if the only difference between two regular files is a
340 # change in the FreeBSD ID string.
342 # $1 - path of first file
343 # $2 - path of second file
347 diff -qI '\$FreeBSD.*\$' $1 $2 >/dev/null 2>&1
350 # This is a wrapper around compare that will return COMPARE_EQUAL if
351 # the only difference between two regular files is a change in the
352 # FreeBSD ID string. It only makes this adjustment if the -F flag has
364 if [ -n "$FREEBSD_ID" -a "$cmp" -eq $COMPARE_DIFFFILES ] && \
365 fbsdid_only $1 $2; then
366 return $COMPARE_EQUAL
372 # Returns true (0) if a directory is empty.
374 # $1 - pathname of the directory to check
383 # Returns true (0) if one directories contents are a subset of the
384 # other. This will recurse to handle subdirectories and compares
385 # individual files in the trees. Its purpose is to quiet spurious
386 # directory warnings for dryrun invocations.
388 # $1 - first directory (sub)
389 # $2 - second directory (super)
394 if ! [ -d $1 -a -d $2 ]; then
398 # Ignore files that are present in the second directory but not
401 for file in $contents; do
402 if ! compare $1/$file $2/$file; then
406 if [ -d $1/$file ]; then
407 if ! dir_subset $1/$file $2/$file; then
415 # Returns true (0) if a directory in the destination tree is empty.
416 # If this is a dryrun, then this returns true as long as the contents
417 # of the directory are a subset of the contents in the old tree
418 # (meaning that the directory would be empty in a non-dryrun when this
419 # was invoked) to quiet spurious warnings.
421 # $1 - pathname of the directory to check relative to DESTDIR.
425 if [ -n "$dryrun" ]; then
426 dir_subset $DESTDIR/$1 $OLDTREE/$1
430 empty_dir $DESTDIR/$1
433 # Output a diff of two directory entries with the same relative name
434 # in different trees. Note that as with compare(), this does not
435 # recurse into subdirectories. If the nodes are identical, nothing is
441 # $4 - label for first tree
442 # $5 - label for second tree
445 local first second file old new diffargs
447 if [ -n "$FREEBSD_ID" ]; then
448 diffargs="-I \\\$FreeBSD.*\\\$"
453 compare_fbsdid $1/$3 $2/$3
468 first=`file_type $1/$3`
469 second=`file_type $2/$3`
471 echo "Node changed from a $first to a $second: $3"
475 first=`readlink $1/$file`
476 second=`readlink $2/$file`
478 echo "Link changed: $file"
487 diff -u $diffargs -L "$3 ($4)" $1/$3 -L "$3 ($5)" $2/$3
492 # Run one-off commands after an update has completed. These commands
493 # are not tied to a specific file, so they cannot be handled by
494 # post_install_file().
499 # None of these commands should be run for a pre-world update.
500 if [ -n "$preworld" ]; then
504 # If /etc/localtime exists and is not a symlink and /var/db/zoneinfo
505 # exists, run tzsetup -r to refresh /etc/localtime.
506 if [ -f ${DESTDIR}/etc/localtime -a \
507 ! -L ${DESTDIR}/etc/localtime ]; then
508 if [ -f ${DESTDIR}/var/db/zoneinfo ]; then
509 if [ -n "${DESTDIR}" ]; then
514 log "tzsetup -r ${args}"
515 if [ -z "$dryrun" ]; then
516 tzsetup -r ${args} >&3 2>&1
519 warn "Needs update: /etc/localtime (required" \
520 "manual update via tzsetup(8))"
525 # Create missing parent directories of a node in a target tree
526 # preserving the owner, group, and permissions from a specified
531 # $3 - pathname of the node (relative to both trees)
538 # Nothing to do if the parent directory exists. This also
539 # catches the degenerate cases when the path is just a simple
541 if [ -d ${2}$dir ]; then
545 # If non-directory file exists with the desired directory
547 if exists ${2}$dir; then
548 # If this is a dryrun and we are installing the
549 # directory in the DESTDIR and the file in the DESTDIR
550 # matches the file in the old tree, then fake success
551 # to quiet spurious warnings.
552 if [ -n "$dryrun" -a "$2" = "$DESTDIR" ]; then
553 if compare $OLDTREE/$dir $DESTDIR/$dir; then
558 args=`file_type ${2}$dir`
559 warn "Directory mismatch: ${2}$dir ($args)"
563 # Ensure the parent directory of the directory is present
565 if ! install_dirs $1 "$2" $dir; then
569 # Format attributes from template directory as install(1)
571 args=`stat -f "-o %Su -g %Sg -m %0Mp%0Lp" $1/$dir`
573 log "install -d $args ${2}$dir"
574 if [ -z "$dryrun" ]; then
575 install -d $args ${2}$dir >&3 2>&1
580 # Perform post-install fixups for a file. This largely consists of
581 # regenerating any files that depend on the newly installed file.
583 # $1 - pathname of the updated file (relative to DESTDIR)
588 # Grr, newaliases only works for an empty DESTDIR.
589 if [ -z "$DESTDIR" ]; then
591 if [ -z "$dryrun" ]; then
598 /usr/share/certs/trusted/*)
599 /usr/share/certs/blacklisted/*)
601 if [ -z "$dryrun" ]; then
602 env DESTDIR=${DESTDIR} certctl rehash >&3 2>&1
606 log "cap_mkdb ${DESTDIR}$1"
607 if [ -z "$dryrun" ]; then
608 cap_mkdb ${DESTDIR}$1 >&3 2>&1
612 log "pwd_mkdb -p -d $DESTDIR/etc ${DESTDIR}$1"
613 if [ -z "$dryrun" ]; then
614 pwd_mkdb -p -d $DESTDIR/etc ${DESTDIR}$1 \
619 # /etc/rc.d/motd hardcodes the /etc/motd path.
620 # Don't warn about non-empty DESTDIR's since this
621 # change is only cosmetic anyway.
622 if [ -z "$DESTDIR" ]; then
623 log "sh /etc/rc.d/motd start"
624 if [ -z "$dryrun" ]; then
625 sh /etc/rc.d/motd start >&3 2>&1
630 log "services_mkdb -q -o $DESTDIR/var/db/services.db" \
632 if [ -z "$dryrun" ]; then
633 services_mkdb -q -o $DESTDIR/var/db/services.db \
634 ${DESTDIR}$1 >&3 2>&1
640 # Install the "new" version of a file. Returns true if it succeeds
641 # and false otherwise.
643 # $1 - pathname of the file to install (relative to DESTDIR)
647 if ! install_dirs $NEWTREE "$DESTDIR" $1; then
650 log "cp -Rp ${NEWTREE}$1 ${DESTDIR}$1"
651 if [ -z "$dryrun" ]; then
652 cp -Rp ${NEWTREE}$1 ${DESTDIR}$1 >&3 2>&1
658 # Install the "resolved" version of a file. Returns true if it succeeds
659 # and false otherwise.
661 # $1 - pathname of the file to install (relative to DESTDIR)
665 # This should always be present since the file is already
666 # there (it caused a conflict). However, it doesn't hurt to
668 if ! install_dirs $NEWTREE "$DESTDIR" $1; then
672 log "cp -Rp ${CONFLICTS}$1 ${DESTDIR}$1"
673 cp -Rp ${CONFLICTS}$1 ${DESTDIR}$1 >&3 2>&1
678 # Generate a conflict file when a "new" file conflicts with an
679 # existing file in DESTDIR.
681 # $1 - pathname of the file that conflicts (relative to DESTDIR)
685 if [ -n "$dryrun" ]; then
689 install_dirs $NEWTREE $CONFLICTS $1
690 diff --changed-group-format='<<<<<<< (local)
693 ' $DESTDIR/$1 $NEWTREE/$1 > $CONFLICTS/$1
696 # Remove the "old" version of a file.
698 # $1 - pathname of the old file to remove (relative to DESTDIR)
701 log "rm -f ${DESTDIR}$1"
702 if [ -z "$dryrun" ]; then
703 rm -f ${DESTDIR}$1 >&3 2>&1
708 # Update a file that has no local modifications.
710 # $1 - pathname of the file to update (relative to DESTDIR)
715 # If the old file is a directory, then remove it with rmdir
716 # (this should only happen if the file has changed its type
717 # from a directory to a non-directory). If the directory
718 # isn't empty, then fail. This will be reported as a warning
720 if [ -d $DESTDIR/$1 ]; then
721 if empty_destdir $1; then
722 log "rmdir ${DESTDIR}$1"
723 if [ -z "$dryrun" ]; then
724 rmdir ${DESTDIR}$1 >&3 2>&1
730 # If both the old and new files are regular files, leave the
731 # existing file. This avoids breaking hard links for /.cshrc
732 # and /.profile. Otherwise, explicitly remove the old file.
733 elif ! [ -f ${DESTDIR}$1 -a -f ${NEWTREE}$1 ]; then
734 log "rm -f ${DESTDIR}$1"
735 if [ -z "$dryrun" ]; then
736 rm -f ${DESTDIR}$1 >&3 2>&1
740 # If the new file is a directory, note that the old file has
741 # been removed, but don't do anything else for now. The
742 # directory will be installed if needed when new files within
743 # that directory are installed.
744 if [ -d $NEWTREE/$1 ]; then
745 if empty_dir $NEWTREE/$1; then
750 elif install_new $1; then
756 # Update the FreeBSD ID string in a locally modified file to match the
757 # FreeBSD ID string from the "new" version of the file.
759 # $1 - pathname of the file to update (relative to DESTDIR)
764 # If the FreeBSD ID string is removed from the local file,
765 # there is nothing to do. In this case, treat the file as
766 # updated. Otherwise, if either file has more than one
767 # FreeBSD ID string, just punt and let the user handle the
769 new=`grep -c '\$FreeBSD.*\$' ${NEWTREE}$1`
770 dest=`grep -c '\$FreeBSD.*\$' ${DESTDIR}$1`
771 if [ "$dest" -eq 0 ]; then
774 if [ "$dest" -ne 1 -o "$dest" -ne 1 ]; then
778 # If the FreeBSD ID string in the new file matches the FreeBSD ID
779 # string in the local file, there is nothing to do.
780 new=`grep '\$FreeBSD.*\$' ${NEWTREE}$1`
781 dest=`grep '\$FreeBSD.*\$' ${DESTDIR}$1`
782 if [ "$new" = "$dest" ]; then
786 # Build the new file in three passes. First, copy all the
787 # lines preceding the FreeBSD ID string from the local version
788 # of the file. Second, append the FreeBSD ID string line from
789 # the new version. Finally, append all the lines after the
790 # FreeBSD ID string from the local version of the file.
791 file=`mktemp $WORKDIR/etcupdate-XXXXXXX`
792 awk '/\$FreeBSD.*\$/ { exit } { print }' ${DESTDIR}$1 >> $file
793 awk '/\$FreeBSD.*\$/ { print }' ${NEWTREE}$1 >> $file
794 awk '/\$FreeBSD.*\$/ { ok = 1; next } { if (ok) print }' \
795 ${DESTDIR}$1 >> $file
797 # As an extra sanity check, fail the attempt if the updated
798 # version of the file has any differences aside from the
800 if ! fbsdid_only ${DESTDIR}$1 $file; then
805 log "cp $file ${DESTDIR}$1"
806 if [ -z "$dryrun" ]; then
807 cp $file ${DESTDIR}$1 >&3 2>&1
815 # Attempt to update a file that has local modifications. This routine
816 # only handles regular files. If the 3-way merge succeeds without
817 # conflicts, the updated file is installed. If the merge fails, the
818 # merged version with conflict markers is left in the CONFLICTS tree.
820 # $1 - pathname of the file to merge (relative to DESTDIR)
825 # Try the merge to see if there is a conflict.
826 diff3 -E -m ${DESTDIR}$1 ${OLDTREE}$1 ${NEWTREE}$1 > /dev/null 2>&3
830 # No conflicts, so just redo the merge to the
832 log "diff3 -E -m ${DESTDIR}$1 ${OLDTREE}$1 ${NEWTREE}$1"
833 if [ -z "$dryrun" ]; then
834 temp=$(mktemp -t etcupdate)
835 diff3 -E -m ${DESTDIR}$1 ${OLDTREE}$1 ${NEWTREE}$1 > ${temp}
836 # Use "cat >" to preserve metadata.
837 cat ${temp} > ${DESTDIR}$1
844 # Conflicts, save a version with conflict markers in
845 # the conflicts directory.
846 if [ -z "$dryrun" ]; then
847 install_dirs $NEWTREE $CONFLICTS $1
848 log "diff3 -m -A ${DESTDIR}$1 ${CONFLICTS}$1"
849 diff3 -m -A -L "yours" -L "original" -L "new" \
850 ${DESTDIR}$1 ${OLDTREE}$1 ${NEWTREE}$1 > \
856 panic "merge failed with status $res"
861 # Returns true if a file contains conflict markers from a merge conflict.
863 # $1 - pathname of the file to resolve (relative to DESTDIR)
867 egrep -q '^(<{7}|\|{7}|={7}|>{7}) ' $CONFLICTS/$1
870 # Attempt to resolve a conflict. The user is prompted to choose an
871 # action for each conflict. If the user edits the file, they are
872 # prompted again for an action. The process is very similar to
873 # resolving conflicts after an update or merge with Perforce or
874 # Subversion. The prompts are modelled on a subset of the available
875 # commands for resolving conflicts with Subversion.
877 # $1 - pathname of the file to resolve (relative to DESTDIR)
882 echo "Resolving conflict in '$1':"
885 # Only display the resolved command if the file
886 # doesn't contain any conflicts.
887 echo -n "Select: (p) postpone, (df) diff-full, (e) edit,"
888 if ! has_conflicts $1; then
889 echo -n " (r) resolved,"
892 echo -n " (h) help for more options: "
896 diff -u ${DESTDIR}$1 ${CONFLICTS}$1
899 $EDITOR ${CONFLICTS}$1
903 (p) postpone - ignore this conflict for now
904 (df) diff-full - show all changes made to merged file
905 (e) edit - change merged file in an editor
906 (r) resolved - accept merged version of file
907 (mf) mine-full - accept local version of entire file (ignore new changes)
908 (tf) theirs-full - accept new version of entire file (lose local changes)
909 (h) help - show this list
913 # For mine-full, just delete the
914 # merged file and leave the local
915 # version of the file as-is.
923 # If the merged file has conflict
924 # markers, require confirmation.
925 if has_conflicts $1; then
926 echo "File '$1' still has conflicts," \
927 "are you sure? (y/n) "
929 if [ "$junk" != "y" ]; then
934 if ! install_resolved $1; then
935 panic "Unable to install merged" \
942 # For theirs-full, install the new
943 # version of the file over top of the
945 if ! install_new $1; then
946 panic "Unable to install new" \
953 echo "Invalid command."
959 # Handle a file that has been removed from the new tree. If the file
960 # does not exist in DESTDIR, then there is nothing to do. If the file
961 # exists in DESTDIR and is identical to the old version, remove it
962 # from DESTDIR. Otherwise, whine about the conflict but leave the
963 # file in DESTDIR. To handle directories, this uses two passes. The
964 # first pass handles all non-directory files. The second pass handles
965 # just directories and removes them if they are empty.
967 # If -F is specified, and the only difference in the file in DESTDIR
968 # is a change in the FreeBSD ID string, then remove the file.
970 # $1 - pathname of the file (relative to DESTDIR)
971 handle_removed_file()
976 if ignore $file; then
977 log "IGNORE: removed file $file"
981 compare_fbsdid $DESTDIR/$file $OLDTREE/$file
984 if ! [ -d $DESTDIR/$file ]; then
989 panic "Removed file now missing"
992 # Already removed, nothing to do.
994 $COMPARE_DIFFTYPE|$COMPARE_DIFFLINKS|$COMPARE_DIFFFILES)
995 dest=`file_type $DESTDIR/$file`
996 warn "Modified $dest remains: $file"
1001 # Handle a directory that has been removed from the new tree. Only
1002 # remove the directory if it is empty.
1004 # $1 - pathname of the directory (relative to DESTDIR)
1005 handle_removed_directory()
1010 if ignore $dir; then
1011 log "IGNORE: removed dir $dir"
1015 if [ -d $DESTDIR/$dir -a -d $OLDTREE/$dir ]; then
1016 if empty_destdir $dir; then
1017 log "rmdir ${DESTDIR}$dir"
1018 if [ -z "$dryrun" ]; then
1019 rmdir ${DESTDIR}$dir >/dev/null 2>&1
1023 warn "Non-empty directory remains: $dir"
1028 # Handle a file that exists in both the old and new trees. If the
1029 # file has not changed in the old and new trees, there is nothing to
1030 # do. If the file in the destination directory matches the new file,
1031 # there is nothing to do. If the file in the destination directory
1032 # matches the old file, then the new file should be installed.
1033 # Everything else becomes some sort of conflict with more detailed
1036 # $1 - pathname of the file (relative to DESTDIR)
1037 handle_modified_file()
1039 local cmp dest file new newdestcmp old
1042 if ignore $file; then
1043 log "IGNORE: modified file $file"
1047 compare $OLDTREE/$file $NEWTREE/$file
1049 if [ $cmp -eq $COMPARE_EQUAL ]; then
1053 if [ $cmp -eq $COMPARE_ONLYFIRST -o $cmp -eq $COMPARE_ONLYSECOND ]; then
1054 panic "Changed file now missing"
1057 compare $NEWTREE/$file $DESTDIR/$file
1059 if [ $newdestcmp -eq $COMPARE_EQUAL ]; then
1063 # If the only change in the new file versus the destination
1064 # file is a change in the FreeBSD ID string and -F is
1065 # specified, just install the new file.
1066 if [ -n "$FREEBSD_ID" -a $newdestcmp -eq $COMPARE_DIFFFILES ] && \
1067 fbsdid_only $NEWTREE/$file $DESTDIR/$file; then
1068 if update_unmodified $file; then
1071 panic "Updating FreeBSD ID string failed"
1075 # If the local file is the same as the old file, install the
1076 # new file. If -F is specified and the only local change is
1077 # in the FreeBSD ID string, then install the new file as well.
1078 if compare_fbsdid $OLDTREE/$file $DESTDIR/$file; then
1079 if update_unmodified $file; then
1084 # If the file was removed from the dest tree, just whine.
1085 if [ $newdestcmp -eq $COMPARE_ONLYFIRST ]; then
1086 # If the removed file matches an ALWAYS_INSTALL glob,
1087 # then just install the new version of the file.
1088 if always_install $file; then
1089 log "ALWAYS: adding $file"
1090 if ! [ -d $NEWTREE/$file ]; then
1091 if install_new $file; then
1098 # If the only change in the new file versus the old
1099 # file is a change in the FreeBSD ID string and -F is
1100 # specified, don't warn.
1101 if [ -n "$FREEBSD_ID" -a $cmp -eq $COMPARE_DIFFFILES ] && \
1102 fbsdid_only $OLDTREE/$file $NEWTREE/$file; then
1108 old=`file_type $OLDTREE/$file`
1109 new=`file_type $NEWTREE/$file`
1110 warn "Remove mismatch: $file ($old became $new)"
1113 old=`readlink $OLDTREE/$file`
1114 new=`readlink $NEWTREE/$file`
1116 "Removed link changed: $file (\"$old\" became \"$new\")"
1119 warn "Removed file changed: $file"
1125 # Treat the file as unmodified and force install of the new
1126 # file if it matches an ALWAYS_INSTALL glob. If the update
1127 # attempt fails, then fall through to the normal case so a
1128 # warning is generated.
1129 if always_install $file; then
1130 log "ALWAYS: updating $file"
1131 if update_unmodified $file; then
1136 # If the only change in the new file versus the old file is a
1137 # change in the FreeBSD ID string and -F is specified, just
1138 # update the FreeBSD ID string in the local file.
1139 if [ -n "$FREEBSD_ID" -a $cmp -eq $COMPARE_DIFFFILES ] && \
1140 fbsdid_only $OLDTREE/$file $NEWTREE/$file; then
1141 if update_freebsdid $file; then
1146 # If the file changed types between the old and new trees but
1147 # the files in the new and dest tree are both of the same
1148 # type, treat it like an added file just comparing the new and
1150 if [ $cmp -eq $COMPARE_DIFFTYPE ]; then
1153 new=`readlink $NEWTREE/$file`
1154 dest=`readlink $DESTDIR/$file`
1156 "New link conflict: $file (\"$new\" vs \"$dest\")"
1166 # If the file has not changed types between the old
1167 # and new trees, but it is a different type in
1168 # DESTDIR, then just warn.
1169 if [ $newdestcmp -eq $COMPARE_DIFFTYPE ]; then
1170 new=`file_type $NEWTREE/$file`
1171 dest=`file_type $DESTDIR/$file`
1172 warn "Modified mismatch: $file ($new vs $dest)"
1179 old=`file_type $OLDTREE/$file`
1180 new=`file_type $NEWTREE/$file`
1181 dest=`file_type $DESTDIR/$file`
1182 warn "Modified $dest changed: $file ($old became $new)"
1185 old=`readlink $OLDTREE/$file`
1186 new=`readlink $NEWTREE/$file`
1188 "Modified link changed: $file (\"$old\" became \"$new\")"
1196 # Handle a file that has been added in the new tree. If the file does
1197 # not exist in DESTDIR, simply copy the file into DESTDIR. If the
1198 # file exists in the DESTDIR and is identical to the new version, do
1199 # nothing. Otherwise, generate a diff of the two versions of the file
1200 # and mark it as a conflict.
1202 # $1 - pathname of the file (relative to DESTDIR)
1205 local cmp dest file new
1208 if ignore $file; then
1209 log "IGNORE: added file $file"
1213 compare $DESTDIR/$file $NEWTREE/$file
1220 panic "Added file now missing"
1222 $COMPARE_ONLYSECOND)
1223 # Ignore new directories. They will be
1224 # created as needed when non-directory nodes
1226 if ! [ -d $NEWTREE/$file ]; then
1227 if install_new $file; then
1236 # Treat the file as unmodified and force install of the new
1237 # file if it matches an ALWAYS_INSTALL glob. If the update
1238 # attempt fails, then fall through to the normal case so a
1239 # warning is generated.
1240 if always_install $file; then
1241 log "ALWAYS: updating $file"
1242 if update_unmodified $file; then
1249 new=`file_type $NEWTREE/$file`
1250 dest=`file_type $DESTDIR/$file`
1251 warn "New file mismatch: $file ($new vs $dest)"
1254 new=`readlink $NEWTREE/$file`
1255 dest=`readlink $DESTDIR/$file`
1256 warn "New link conflict: $file (\"$new\" vs \"$dest\")"
1259 # If the only change in the new file versus
1260 # the destination file is a change in the
1261 # FreeBSD ID string and -F is specified, just
1262 # install the new file.
1263 if [ -n "$FREEBSD_ID" ] && \
1264 fbsdid_only $NEWTREE/$file $DESTDIR/$file; then
1265 if update_unmodified $file; then
1269 "Updating FreeBSD ID string failed"
1279 # Main routines for each command
1281 # Build a new tree and save it in a tarball.
1286 if [ $# -ne 1 ]; then
1287 echo "Missing required tarball."
1292 log "build command: $1"
1294 # Create a temporary directory to hold the tree
1295 dir=`mktemp -d $WORKDIR/etcupdate-XXXXXXX`
1296 if [ $? -ne 0 ]; then
1297 echo "Unable to create temporary directory."
1300 if ! build_tree $dir; then
1301 echo "Failed to build tree."
1305 if ! tar cfj $1 -C $dir . >&3 2>&1; then
1306 echo "Failed to create tarball."
1313 # Output a diff comparing the tree at DESTDIR to the current
1314 # unmodified tree. Note that this diff does not include files that
1315 # are present in DESTDIR but not in the unmodified tree.
1320 if [ $# -ne 0 ]; then
1324 # Requires an unmodified tree to diff against.
1325 if ! [ -d $NEWTREE ]; then
1326 echo "Reference tree to diff against unavailable."
1330 # Unfortunately, diff alone does not quite provide the right
1331 # level of options that we want, so improvise.
1332 for file in `(cd $NEWTREE; find .) | sed -e 's/^\.//'`; do
1333 if ignore $file; then
1337 diffnode $NEWTREE "$DESTDIR" $file "stock" "local"
1341 # Just extract a new tree into NEWTREE either by building a tree or
1342 # extracting a tarball. This can be used to bootstrap updates by
1343 # initializing the current "stock" tree to match the currently
1346 # Unlike 'update', this command does not rotate or preserve an
1347 # existing NEWTREE, it just replaces any existing tree.
1351 if [ $# -ne 0 ]; then
1355 log "extract command: tarball=$tarball"
1357 if [ -d $NEWTREE ]; then
1358 if ! remove_tree $NEWTREE; then
1359 echo "Unable to remove current tree."
1367 # Resolve conflicts left from an earlier merge.
1372 if [ $# -ne 0 ]; then
1376 if ! [ -d $CONFLICTS ]; then
1380 if ! [ -d $NEWTREE ]; then
1381 echo "The current tree is not present to resolve conflicts."
1385 conflicts=`(cd $CONFLICTS; find . ! -type d) | sed -e 's/^\.//'`
1386 for file in $conflicts; do
1387 resolve_conflict $file
1390 if [ -n "$NEWALIAS_WARN" ]; then
1391 warn "Needs update: /etc/mail/aliases.db" \
1392 "(requires manual update via newaliases(1))"
1395 echo " Needs update: /etc/mail/aliases.db" \
1396 "(requires manual update via newaliases(1))"
1400 # Report a summary of the previous merge. Specifically, list any
1401 # remaining conflicts followed by any warnings from the previous
1406 if [ $# -ne 0 ]; then
1410 if [ -d $CONFLICTS ]; then
1411 (cd $CONFLICTS; find . ! -type d) | sed -e 's/^\./ C /'
1413 if [ -s $WARNINGS ]; then
1419 # Perform an actual merge. The new tree can either already exist (if
1420 # rerunning a merge), be extracted from a tarball, or generated from a
1426 if [ $# -ne 0 ]; then
1430 log "update command: rerun=$rerun tarball=$tarball preworld=$preworld"
1432 if [ `id -u` -ne 0 ]; then
1433 echo "Must be root to update a tree."
1437 # Enforce a sane umask
1440 # XXX: Should existing conflicts be ignored and removed during
1443 # Trim the conflicts tree. Whine if there is anything left.
1444 if [ -e $CONFLICTS ]; then
1445 find -d $CONFLICTS -type d -empty -delete >&3 2>&1
1446 rmdir $CONFLICTS >&3 2>&1
1448 if [ -d $CONFLICTS ]; then
1449 echo "Conflicts remain from previous update, aborting."
1453 if [ -z "$rerun" ]; then
1454 # For a dryrun that is not a rerun, do not rotate the existing
1455 # stock tree. Instead, extract a tree to a temporary directory
1456 # and use that for the comparison.
1457 if [ -n "$dryrun" ]; then
1458 dir=`mktemp -d $WORKDIR/etcupdate-XXXXXXX`
1459 if [ $? -ne 0 ]; then
1460 echo "Unable to create temporary directory."
1464 # A pre-world dryrun has already set OLDTREE to
1465 # point to the current stock tree.
1466 if [ -z "$preworld" ]; then
1471 # For a pre-world update, blow away any pre-existing
1473 elif [ -n "$preworld" ]; then
1474 if ! remove_tree $NEWTREE; then
1475 echo "Unable to remove pre-world tree."
1479 # Rotate the existing stock tree to the old tree.
1480 elif [ -d $NEWTREE ]; then
1481 # First, delete the previous old tree if it exists.
1482 if ! remove_tree $OLDTREE; then
1483 echo "Unable to remove old tree."
1487 # Move the current stock tree.
1488 if ! mv $NEWTREE $OLDTREE >&3 2>&1; then
1489 echo "Unable to rename current stock tree."
1494 if ! [ -d $OLDTREE ]; then
1496 No previous tree to compare against, a sane comparison is not possible.
1498 log "No previous tree to compare against."
1499 if [ -n "$dir" ]; then
1505 # Populate the new tree.
1509 # Build lists of nodes in the old and new trees.
1510 (cd $OLDTREE; find .) | sed -e 's/^\.//' | sort > $WORKDIR/old.files
1511 (cd $NEWTREE; find .) | sed -e 's/^\.//' | sort > $WORKDIR/new.files
1513 # Split the files up into three groups using comm.
1514 comm -23 $WORKDIR/old.files $WORKDIR/new.files > $WORKDIR/removed.files
1515 comm -13 $WORKDIR/old.files $WORKDIR/new.files > $WORKDIR/added.files
1516 comm -12 $WORKDIR/old.files $WORKDIR/new.files > $WORKDIR/both.files
1518 # Initialize conflicts and warnings handling.
1522 # Ignore removed files for the pre-world case. A pre-world
1523 # update uses a stripped-down tree.
1524 if [ -n "$preworld" ]; then
1525 > $WORKDIR/removed.files
1528 # The order for the following sections is important. In the
1529 # odd case that a directory is converted into a file, the
1530 # existing subfiles need to be removed if possible before the
1531 # file is converted. Similarly, in the case that a file is
1532 # converted into a directory, the file needs to be converted
1533 # into a directory if possible before the new files are added.
1535 # First, handle removed files.
1536 for file in `cat $WORKDIR/removed.files`; do
1537 handle_removed_file $file
1540 # For the directory pass, reverse sort the list to effect a
1541 # depth-first traversal. This is needed to ensure that if a
1542 # directory with subdirectories is removed, the entire
1543 # directory is removed if there are no local modifications.
1544 for file in `sort -r $WORKDIR/removed.files`; do
1545 handle_removed_directory $file
1548 # Second, handle files that exist in both the old and new
1550 for file in `cat $WORKDIR/both.files`; do
1551 handle_modified_file $file
1554 # Finally, handle newly added files.
1555 for file in `cat $WORKDIR/added.files`; do
1556 handle_added_file $file
1559 if [ -n "$NEWALIAS_WARN" ]; then
1560 warn "Needs update: /etc/mail/aliases.db" \
1561 "(requires manual update via newaliases(1))"
1564 # Run any special one-off commands after an update has completed.
1567 if [ -s $WARNINGS ]; then
1572 if [ -n "$dir" ]; then
1573 if [ -z "$dryrun" -o -n "$rerun" ]; then
1574 panic "Should not have a temporary directory"
1581 # Determine which command we are executing. A command may be
1582 # specified as the first word. If one is not specified then 'update'
1583 # is assumed as the default command.
1585 if [ $# -gt 0 ]; then
1587 build|diff|extract|status|resolve)
1592 # If first arg is an option, assume the
1601 # Set default variable values.
1603 # The path to the source tree used to build trees.
1606 # The destination directory where the modified files live.
1609 # Ignore changes in the FreeBSD ID string.
1612 # Files that should always have the new version of the file installed.
1615 # Files to ignore and never update during a merge.
1618 # Flags to pass to 'make' when building a tree.
1621 # Include a config file if it exists. Note that command line options
1622 # override any settings in the config file. More details are in the
1623 # manual, but in general the following variables can be set:
1633 if [ -r /etc/etcupdate.conf ]; then
1634 . /etc/etcupdate.conf
1637 # Parse command line options
1645 while getopts "d:nprs:t:A:BD:FI:L:M:" option; do
1666 # To allow this option to be specified
1667 # multiple times, accumulate command-line
1668 # specified patterns in an 'always' variable
1669 # and use that to overwrite ALWAYS_INSTALL
1670 # after parsing all options. Need to be
1671 # careful here with globbing expansion.
1673 always="$always $OPTARG"
1686 # To allow this option to be specified
1687 # multiple times, accumulate command-line
1688 # specified patterns in an 'ignore' variable
1689 # and use that to overwrite IGNORE_FILES after
1690 # parsing all options. Need to be careful
1691 # here with globbing expansion.
1693 ignore="$ignore $OPTARG"
1700 MAKE_OPTIONS="$OPTARG"
1708 shift $((OPTIND - 1))
1710 # Allow -A command line options to override ALWAYS_INSTALL set from
1713 if [ -n "$always" ]; then
1714 ALWAYS_INSTALL="$always"
1717 # Allow -I command line options to override IGNORE_FILES set from the
1719 if [ -n "$ignore" ]; then
1720 IGNORE_FILES="$ignore"
1724 # Where the "old" and "new" trees are stored.
1725 WORKDIR=${WORKDIR:-$DESTDIR/var/db/etcupdate}
1727 # Log file for verbose output from program that are run. The log file
1728 # is opened on fd '3'.
1729 LOGFILE=${LOGFILE:-$WORKDIR/log}
1731 # The path of the "old" tree
1732 OLDTREE=$WORKDIR/old
1734 # The path of the "new" tree
1735 NEWTREE=$WORKDIR/current
1737 # The path of the "conflicts" tree where files with merge conflicts are saved.
1738 CONFLICTS=$WORKDIR/conflicts
1740 # The path of the "warnings" file that accumulates warning notes from an update.
1741 WARNINGS=$WORKDIR/warnings
1743 # Use $EDITOR for resolving conflicts. If it is not set, default to vi.
1744 EDITOR=${EDITOR:-/usr/bin/vi}
1746 # Files that need to be updated before installworld.
1747 PREWORLD_FILES="etc/master.passwd etc/group"
1749 # Handle command-specific argument processing such as complaining
1750 # about unsupported options. Since the configuration file is always
1751 # included, do not complain about extra command line arguments that
1752 # may have been set via the config file rather than the command line.
1755 if [ -n "$rerun" -a -n "$tarball" ]; then
1756 echo "Only one of -r or -t can be specified."
1760 if [ -n "$rerun" -a -n "$preworld" ]; then
1761 echo "Only one of -p or -r can be specified."
1767 if [ -n "$dryrun" -o -n "$rerun" -o -n "$tarball" -o \
1768 -n "$preworld" ]; then
1773 if [ -n "$dryrun" -o -n "$rerun" -o -n "$tarball" ]; then
1778 if [ -n "$dryrun" -o -n "$rerun" -o -n "$preworld" ]; then
1784 # Pre-world mode uses a different set of trees. It leaves the current
1785 # tree as-is so it is still present for a full etcupdate run after the
1786 # world install is complete. Instead, it installs a few critical files
1787 # into a separate tree.
1788 if [ -n "$preworld" ]; then
1790 NEWTREE=$WORKDIR/preworld
1793 # Open the log file. Don't truncate it if doing a minor operation so
1794 # that a minor operation doesn't lose log info from a major operation.
1795 if ! mkdir -p $WORKDIR 2>/dev/null; then
1796 echo "Failed to create work directory $WORKDIR"
1800 diff|resolve|status)