3 # Copyright (c) 2010 Advanced Computing Technologies LLC
4 # Written by: John H. Baldwin <jhb@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # This is a tool to manage updating files that are not updated as part
31 # of 'make installworld' such as files in /etc. Unlike other tools,
32 # this one is specifically tailored to assisting with mass upgrades.
33 # To that end it does not require user intervention while running.
35 # Theory of operation:
37 # The most reliable way to update changes to files that have local
38 # modifications is to perform a three-way merge between the original
39 # unmodified file, the new version of the file, and the modified file.
40 # This requires having all three versions of the file available when
41 # performing an update.
43 # To that end, etcupdate uses a strategy where the current unmodified
44 # tree is kept in WORKDIR/current and the previous unmodified tree is
45 # kept in WORKDIR/old. When performing a merge, a new tree is built
46 # if needed and then the changes are merged into DESTDIR. Any files
47 # with unresolved conflicts after the merge are left in a tree rooted
48 # at WORKDIR/conflicts.
50 # To provide extra flexibility, etcupdate can also build tarballs of
51 # root trees that can later be used. It can also use a tarball as the
52 # source of a new tree instead of building it from /usr/src.
54 # Global settings. These can be adjusted by config files and in some
55 # cases by command line options.
58 # - automatable conflict resolution
59 # - a 'revert' command to make a file "stock"
64 usage: etcupdate [-nBF] [-d workdir] [-r | -s source | -t tarball] [-A patterns]
65 [-D destdir] [-I patterns] [-L logfile] [-M options]
66 etcupdate build [-B] [-d workdir] [-s source] [-L logfile] [-M options]
68 etcupdate diff [-d workdir] [-D destdir] [-I patterns] [-L logfile]
69 etcupdate extract [-B] [-d workdir] [-s source | -t tarball] [-L logfile]
71 etcupdate resolve [-d workdir] [-D destdir] [-L logfile]
72 etcupdate status [-d workdir] [-D destdir]
77 # Used to write a message prepended with '>>>' to the logfile.
83 # Used for assertion conditions that should never happen.
90 # Used to write a warning message. These are saved to the WARNINGS
91 # file with " " prepended.
94 echo -n " " >> $WARNINGS
95 echo "$@" >> $WARNINGS
98 # Output a horizontal rule using the passed-in character. Matches the
99 # length used for Index lines in CVS and SVN diffs.
107 # Output a text description of a specified file's type.
109 # $1 - file pathname.
112 stat -f "%HT" $1 | tr "[:upper:]" "[:lower:]"
115 # Returns true (0) if a file exists
117 # $1 - file pathname.
123 # Returns true (0) if a file should be ignored, false otherwise.
131 for pattern in $IGNORE_FILES; do
141 # Ignore /.cshrc and /.profile if they are hardlinked to the
142 # same file in /root. This ensures we only compare those
143 # files once in that case.
146 if [ ${DESTDIR}$1 -ef ${DESTDIR}/root$1 ]; then
157 # Returns true (0) if the new version of a file should always be
158 # installed rather than attempting to do a merge.
166 for pattern in $ALWAYS_INSTALL; do
181 # $1 - directory to store new tree in
186 make="make $MAKE_OPTIONS"
188 log "Building tree at $1 with $make"
189 mkdir -p $1/usr/obj >&3 2>&1
190 (cd $SRCDIR; $make DESTDIR=$1 distrib-dirs) >&3 2>&1 || return 1
192 if ! [ -n "$nobuild" ]; then
194 MAKEOBJDIRPREFIX=$1/usr/obj $make _obj SUBDIR_OVERRIDE=etc &&
195 MAKEOBJDIRPREFIX=$1/usr/obj $make everything SUBDIR_OVERRIDE=etc &&
196 MAKEOBJDIRPREFIX=$1/usr/obj $make DESTDIR=$1 distribution) \
199 (cd $SRCDIR; $make DESTDIR=$1 distribution) >&3 2>&1 || return 1
201 chflags -R noschg $1 >&3 2>&1 || return 1
202 rm -rf $1/usr/obj >&3 2>&1 || return 1
204 # Purge auto-generated files. Only the source files need to
205 # be updated after which these files are regenerated.
206 rm -f $1/etc/*.db $1/etc/passwd >&3 2>&1 || return 1
208 # Remove empty files. These just clutter the output of 'diff'.
209 find $1 -type f -size 0 -delete >&3 2>&1 || return 1
211 # Trim empty directories.
212 find -d $1 -type d -empty -delete >&3 2>&1 || return 1
216 # Generate a new NEWTREE tree. If tarball is set, then the tree is
217 # extracted from the tarball. Otherwise the tree is built from a
221 # If we have a tarball, extract that into the new directory.
222 if [ -n "$tarball" ]; then
223 if ! (mkdir -p $NEWTREE && tar xf $tarball -C $NEWTREE) \
225 echo "Failed to extract new tree."
230 if ! build_tree $NEWTREE; then
231 echo "Failed to build new tree."
238 # Forcefully remove a tree. Returns true (0) if the operation succeeds.
246 chflags -R noschg $1 >&3 2>&1
252 # Return values for compare()
260 # Compare two files/directories/symlinks. Note that this does not
261 # recurse into subdirectories. Instead, if two nodes are both
262 # directories, they are assumed to be equivalent.
264 # Returns true (0) if the nodes are identical. If only one of the two
265 # nodes are present, return one of the COMPARE_ONLY* constants. If
266 # the nodes are different, return one of the COMPARE_DIFF* constants
267 # to indicate the type of difference.
275 # If the first node doesn't exist, then check for the second
276 # node. Note that -e will fail for a symbolic link that
277 # points to a missing target.
280 return $COMPARE_ONLYSECOND
282 return $COMPARE_EQUAL
284 elif ! exists $2; then
285 return $COMPARE_ONLYFIRST
288 # If the two nodes are different file types fail.
289 first=`stat -f "%Hp" $1`
290 second=`stat -f "%Hp" $2`
291 if [ "$first" != "$second" ]; then
292 return $COMPARE_DIFFTYPE
295 # If both are symlinks, compare the link values.
299 if [ "$first" = "$second" ]; then
300 return $COMPARE_EQUAL
302 return $COMPARE_DIFFLINKS
306 # If both are files, compare the file contents.
308 if cmp -s $1 $2; then
309 return $COMPARE_EQUAL
311 return $COMPARE_DIFFFILES
315 # As long as the two nodes are the same type of file, consider
317 return $COMPARE_EQUAL
320 # Returns true (0) if the only difference between two regular files is a
321 # change in the FreeBSD ID string.
323 # $1 - path of first file
324 # $2 - path of second file
328 diff -qI '\$FreeBSD.*\$' $1 $2 >/dev/null 2>&1
331 # This is a wrapper around compare that will return COMPARE_EQUAL if
332 # the only difference between two regular files is a change in the
333 # FreeBSD ID string. It only makes this adjustment if the -F flag has
345 if [ -n "$FREEBSD_ID" -a "$cmp" -eq $COMPARE_DIFFFILES ] && \
346 fbsdid_only $1 $2; then
347 return $COMPARE_EQUAL
353 # Returns true (0) if a directory is empty.
355 # $1 - pathname of the directory to check
364 # Returns true (0) if one directories contents are a subset of the
365 # other. This will recurse to handle subdirectories and compares
366 # individual files in the trees. Its purpose is to quiet spurious
367 # directory warnings for dryrun invocations.
369 # $1 - first directory (sub)
370 # $2 - second directory (super)
375 if ! [ -d $1 -a -d $2 ]; then
379 # Ignore files that are present in the second directory but not
382 for file in $contents; do
383 if ! compare $1/$file $2/$file; then
387 if [ -d $1/$file ]; then
388 if ! dir_subset $1/$file $2/$file; then
396 # Returns true (0) if a directory in the destination tree is empty.
397 # If this is a dryrun, then this returns true as long as the contents
398 # of the directory are a subset of the contents in the old tree
399 # (meaning that the directory would be empty in a non-dryrun when this
400 # was invoked) to quiet spurious warnings.
402 # $1 - pathname of the directory to check relative to DESTDIR.
406 if [ -n "$dryrun" ]; then
407 dir_subset $DESTDIR/$1 $OLDTREE/$1
411 empty_dir $DESTDIR/$1
414 # Output a diff of two directory entries with the same relative name
415 # in different trees. Note that as with compare(), this does not
416 # recurse into subdirectories. If the nodes are identical, nothing is
422 # $4 - label for first tree
423 # $5 - label for second tree
426 local first second file old new diffargs
428 if [ -n "$FREEBSD_ID" ]; then
429 diffargs="-I \\\$FreeBSD.*\\\$"
434 compare_fbsdid $1/$3 $2/$3
449 first=`file_type $1/$3`
450 second=`file_type $2/$3`
452 echo "Node changed from a $first to a $second: $3"
456 first=`readlink $1/$file`
457 second=`readlink $2/$file`
459 echo "Link changed: $file"
468 diff -u $diffargs -L "$3 ($4)" $1/$3 -L "$3 ($5)" $2/$3
473 # Create missing parent directories of a node in a target tree
474 # preserving the owner, group, and permissions from a specified
479 # $3 - pathname of the node (relative to both trees)
486 # Nothing to do if the parent directory exists. This also
487 # catches the degenerate cases when the path is just a simple
489 if [ -d ${2}$dir ]; then
493 # If non-directory file exists with the desired directory
495 if exists ${2}$dir; then
496 # If this is a dryrun and we are installing the
497 # directory in the DESTDIR and the file in the DESTDIR
498 # matches the file in the old tree, then fake success
499 # to quiet spurious warnings.
500 if [ -n "$dryrun" -a "$2" = "$DESTDIR" ]; then
501 if compare $OLDTREE/$dir $DESTDIR/$dir; then
506 args=`file_type ${2}$dir`
507 warn "Directory mismatch: ${2}$dir ($args)"
511 # Ensure the parent directory of the directory is present
513 if ! install_dirs $1 "$2" $dir; then
517 # Format attributes from template directory as install(1)
519 args=`stat -f "-o %Su -g %Sg -m %0Mp%0Lp" $1/$dir`
521 log "install -d $args ${2}$dir"
522 if [ -z "$dryrun" ]; then
523 install -d $args ${2}$dir >&3 2>&1
528 # Perform post-install fixups for a file. This largely consists of
529 # regenerating any files that depend on the newly installed file.
531 # $1 - pathname of the updated file (relative to DESTDIR)
536 # Grr, newaliases only works for an empty DESTDIR.
537 if [ -z "$DESTDIR" ]; then
539 if [ -z "$dryrun" ]; then
547 log "cap_mkdb ${DESTDIR}$1"
548 if [ -z "$dryrun" ]; then
549 cap_mkdb ${DESTDIR}$1 >&3 2>&1
553 log "pwd_mkdb -p -d $DESTDIR/etc ${DESTDIR}$1"
554 if [ -z "$dryrun" ]; then
555 pwd_mkdb -p -d $DESTDIR/etc ${DESTDIR}$1 \
560 # /etc/rc.d/motd hardcodes the /etc/motd path.
561 # Don't warn about non-empty DESTDIR's since this
562 # change is only cosmetic anyway.
563 if [ -z "$DESTDIR" ]; then
564 log "sh /etc/rc.d/motd start"
565 if [ -z "$dryrun" ]; then
566 sh /etc/rc.d/motd start >&3 2>&1
573 # Install the "new" version of a file. Returns true if it succeeds
574 # and false otherwise.
576 # $1 - pathname of the file to install (relative to DESTDIR)
580 if ! install_dirs $NEWTREE "$DESTDIR" $1; then
583 log "cp -Rp ${NEWTREE}$1 ${DESTDIR}$1"
584 if [ -z "$dryrun" ]; then
585 cp -Rp ${NEWTREE}$1 ${DESTDIR}$1 >&3 2>&1
591 # Install the "resolved" version of a file. Returns true if it succeeds
592 # and false otherwise.
594 # $1 - pathname of the file to install (relative to DESTDIR)
598 # This should always be present since the file is already
599 # there (it caused a conflict). However, it doesn't hurt to
601 if ! install_dirs $NEWTREE "$DESTDIR" $1; then
605 log "cp -Rp ${CONFLICTS}$1 ${DESTDIR}$1"
606 cp -Rp ${CONFLICTS}$1 ${DESTDIR}$1 >&3 2>&1
611 # Generate a conflict file when a "new" file conflicts with an
612 # existing file in DESTDIR.
614 # $1 - pathname of the file that conflicts (relative to DESTDIR)
618 if [ -n "$dryrun" ]; then
622 install_dirs $NEWTREE $CONFLICTS $1
623 diff --changed-group-format='<<<<<<< (local)
626 ' $DESTDIR/$1 $NEWTREE/$1 > $CONFLICTS/$1
629 # Remove the "old" version of a file.
631 # $1 - pathname of the old file to remove (relative to DESTDIR)
634 log "rm -f ${DESTDIR}$1"
635 if [ -z "$dryrun" ]; then
636 rm -f ${DESTDIR}$1 >&3 2>&1
641 # Update a file that has no local modifications.
643 # $1 - pathname of the file to update (relative to DESTDIR)
648 # If the old file is a directory, then remove it with rmdir
649 # (this should only happen if the file has changed its type
650 # from a directory to a non-directory). If the directory
651 # isn't empty, then fail. This will be reported as a warning
653 if [ -d $DESTDIR/$1 ]; then
654 if empty_destdir $1; then
655 log "rmdir ${DESTDIR}$1"
656 if [ -z "$dryrun" ]; then
657 rmdir ${DESTDIR}$1 >&3 2>&1
663 # If both the old and new files are regular files, leave the
664 # existing file. This avoids breaking hard links for /.cshrc
665 # and /.profile. Otherwise, explicitly remove the old file.
666 elif ! [ -f ${DESTDIR}$1 -a -f ${NEWTREE}$1 ]; then
667 log "rm -f ${DESTDIR}$1"
668 if [ -z "$dryrun" ]; then
669 rm -f ${DESTDIR}$1 >&3 2>&1
673 # If the new file is a directory, note that the old file has
674 # been removed, but don't do anything else for now. The
675 # directory will be installed if needed when new files within
676 # that directory are installed.
677 if [ -d $NEWTREE/$1 ]; then
678 if empty_dir $NEWTREE/$1; then
683 elif install_new $1; then
689 # Update the FreeBSD ID string in a locally modified file to match the
690 # FreeBSD ID string from the "new" version of the file.
692 # $1 - pathname of the file to update (relative to DESTDIR)
697 # If the FreeBSD ID string is removed from the local file,
698 # there is nothing to do. In this case, treat the file as
699 # updated. Otherwise, if either file has more than one
700 # FreeBSD ID string, just punt and let the user handle the
702 new=`grep -c '\$FreeBSD.*\$' ${NEWTREE}$1`
703 dest=`grep -c '\$FreeBSD.*\$' ${DESTDIR}$1`
704 if [ "$dest" -eq 0 ]; then
707 if [ "$dest" -ne 1 -o "$dest" -ne 1 ]; then
711 # If the FreeBSD ID string in the new file matches the FreeBSD ID
712 # string in the local file, there is nothing to do.
713 new=`grep '\$FreeBSD.*\$' ${NEWTREE}$1`
714 dest=`grep '\$FreeBSD.*\$' ${DESTDIR}$1`
715 if [ "$new" = "$dest" ]; then
719 # Build the new file in three passes. First, copy all the
720 # lines preceding the FreeBSD ID string from the local version
721 # of the file. Second, append the FreeBSD ID string line from
722 # the new version. Finally, append all the lines after the
723 # FreeBSD ID string from the local version of the file.
724 file=`mktemp $WORKDIR/etcupdate-XXXXXXX`
725 awk '/\$FreeBSD.*\$/ { exit } { print }' ${DESTDIR}$1 >> $file
726 awk '/\$FreeBSD.*\$/ { print }' ${NEWTREE}$1 >> $file
727 awk '/\$FreeBSD.*\$/ { ok = 1; next } { if (ok) print }' \
728 ${DESTDIR}$1 >> $file
730 # As an extra sanity check, fail the attempt if the updated
731 # version of the file has any differences aside from the
733 if ! fbsdid_only ${DESTDIR}$1 $file; then
738 log "cp $file ${DESTDIR}$1"
739 if [ -z "$dryrun" ]; then
740 cp $file ${DESTDIR}$1 >&3 2>&1
748 # Attempt to update a file that has local modifications. This routine
749 # only handles regular files. If the 3-way merge succeeds without
750 # conflicts, the updated file is installed. If the merge fails, the
751 # merged version with conflict markers is left in the CONFLICTS tree.
753 # $1 - pathname of the file to merge (relative to DESTDIR)
758 # Try the merge to see if there is a conflict.
759 merge -q -p ${DESTDIR}$1 ${OLDTREE}$1 ${NEWTREE}$1 >/dev/null 2>&3
763 # No conflicts, so just redo the merge to the
765 log "merge ${DESTDIR}$1 ${OLDTREE}$1 ${NEWTREE}$1"
766 if [ -z "$dryrun" ]; then
767 merge ${DESTDIR}$1 ${OLDTREE}$1 ${NEWTREE}$1
773 # Conflicts, save a version with conflict markers in
774 # the conflicts directory.
775 if [ -z "$dryrun" ]; then
776 install_dirs $NEWTREE $CONFLICTS $1
777 log "cp -Rp ${DESTDIR}$1 ${CONFLICTS}$1"
778 cp -Rp ${DESTDIR}$1 ${CONFLICTS}$1 >&3 2>&1
779 merge -A -q -L "yours" -L "original" -L "new" \
780 ${CONFLICTS}$1 ${OLDTREE}$1 ${NEWTREE}$1
785 panic "merge failed with status $res"
790 # Returns true if a file contains conflict markers from a merge conflict.
792 # $1 - pathname of the file to resolve (relative to DESTDIR)
796 egrep -q '^(<{7}|\|{7}|={7}|>{7}) ' $CONFLICTS/$1
799 # Attempt to resolve a conflict. The user is prompted to choose an
800 # action for each conflict. If the user edits the file, they are
801 # prompted again for an action. The process is very similar to
802 # resolving conflicts after an update or merge with Perforce or
803 # Subversion. The prompts are modelled on a subset of the available
804 # commands for resolving conflicts with Subversion.
806 # $1 - pathname of the file to resolve (relative to DESTDIR)
811 echo "Resolving conflict in '$1':"
814 # Only display the resolved command if the file
815 # doesn't contain any conflicts.
816 echo -n "Select: (p) postpone, (df) diff-full, (e) edit,"
817 if ! has_conflicts $1; then
818 echo -n " (r) resolved,"
821 echo -n " (h) help for more options: "
825 diff -u ${DESTDIR}$1 ${CONFLICTS}$1
828 $EDITOR ${CONFLICTS}$1
832 (p) postpone - ignore this conflict for now
833 (df) diff-full - show all changes made to merged file
834 (e) edit - change merged file in an editor
835 (r) resolved - accept merged version of file
836 (mf) mine-full - accept local version of entire file (ignore new changes)
837 (tf) theirs-full - accept new version of entire file (lose local changes)
838 (h) help - show this list
842 # For mine-full, just delete the
843 # merged file and leave the local
844 # version of the file as-is.
852 # If the merged file has conflict
853 # markers, require confirmation.
854 if has_conflicts $1; then
855 echo "File '$1' still has conflicts," \
856 "are you sure? (y/n) "
858 if [ "$junk" != "y" ]; then
863 if ! install_resolved $1; then
864 panic "Unable to install merged" \
871 # For theirs-full, install the new
872 # version of the file over top of the
874 if ! install_new $1; then
875 panic "Unable to install new" \
882 echo "Invalid command."
888 # Handle a file that has been removed from the new tree. If the file
889 # does not exist in DESTDIR, then there is nothing to do. If the file
890 # exists in DESTDIR and is identical to the old version, remove it
891 # from DESTDIR. Otherwise, whine about the conflict but leave the
892 # file in DESTDIR. To handle directories, this uses two passes. The
893 # first pass handles all non-directory files. The second pass handles
894 # just directories and removes them if they are empty.
896 # If -F is specified, and the only difference in the file in DESTDIR
897 # is a change in the FreeBSD ID string, then remove the file.
899 # $1 - pathname of the file (relative to DESTDIR)
900 handle_removed_file()
905 if ignore $file; then
906 log "IGNORE: removed file $file"
910 compare_fbsdid $DESTDIR/$file $OLDTREE/$file
913 if ! [ -d $DESTDIR/$file ]; then
918 panic "Removed file now missing"
921 # Already removed, nothing to do.
923 $COMPARE_DIFFTYPE|$COMPARE_DIFFLINKS|$COMPARE_DIFFFILES)
924 dest=`file_type $DESTDIR/$file`
925 warn "Modified $dest remains: $file"
930 # Handle a directory that has been removed from the new tree. Only
931 # remove the directory if it is empty.
933 # $1 - pathname of the directory (relative to DESTDIR)
934 handle_removed_directory()
940 log "IGNORE: removed dir $dir"
944 if [ -d $DESTDIR/$dir -a -d $OLDTREE/$dir ]; then
945 if empty_destdir $dir; then
946 log "rmdir ${DESTDIR}$dir"
947 if [ -z "$dryrun" ]; then
948 rmdir ${DESTDIR}$dir >/dev/null 2>&1
952 warn "Non-empty directory remains: $dir"
957 # Handle a file that exists in both the old and new trees. If the
958 # file has not changed in the old and new trees, there is nothing to
959 # do. If the file in the destination directory matches the new file,
960 # there is nothing to do. If the file in the destination directory
961 # matches the old file, then the new file should be installed.
962 # Everything else becomes some sort of conflict with more detailed
965 # $1 - pathname of the file (relative to DESTDIR)
966 handle_modified_file()
968 local cmp dest file new newdestcmp old
971 if ignore $file; then
972 log "IGNORE: modified file $file"
976 compare $OLDTREE/$file $NEWTREE/$file
978 if [ $cmp -eq $COMPARE_EQUAL ]; then
982 if [ $cmp -eq $COMPARE_ONLYFIRST -o $cmp -eq $COMPARE_ONLYSECOND ]; then
983 panic "Changed file now missing"
986 compare $NEWTREE/$file $DESTDIR/$file
988 if [ $newdestcmp -eq $COMPARE_EQUAL ]; then
992 # If the only change in the new file versus the destination
993 # file is a change in the FreeBSD ID string and -F is
994 # specified, just install the new file.
995 if [ -n "$FREEBSD_ID" -a $newdestcmp -eq $COMPARE_DIFFFILES ] && \
996 fbsdid_only $NEWTREE/$file $DESTDIR/$file; then
997 if update_unmodified $file; then
1000 panic "Updating FreeBSD ID string failed"
1004 # If the local file is the same as the old file, install the
1005 # new file. If -F is specified and the only local change is
1006 # in the FreeBSD ID string, then install the new file as well.
1007 if compare_fbsdid $OLDTREE/$file $DESTDIR/$file; then
1008 if update_unmodified $file; then
1013 # If the only change in the new file versus the old file is a
1014 # change in the FreeBSD ID string and -F is specified, just
1015 # update the FreeBSD ID string in the local file.
1016 if [ -n "$FREEBSD_ID" -a $cmp -eq $COMPARE_DIFFFILES ] && \
1017 fbsdid_only $OLDTREE/$file $NEWTREE/$file; then
1018 if update_freebsdid $file; then
1023 # If the file was removed from the dest tree, just whine.
1024 if [ $newdestcmp -eq $COMPARE_ONLYFIRST ]; then
1025 # If the removed file matches an ALWAYS_INSTALL glob,
1026 # then just install the new version of the file.
1027 if always_install $file; then
1028 log "ALWAYS: adding $file"
1029 if ! [ -d $NEWTREE/$file ]; then
1030 if install_new $file; then
1039 old=`file_type $OLDTREE/$file`
1040 new=`file_type $NEWTREE/$file`
1041 warn "Remove mismatch: $file ($old became $new)"
1044 old=`readlink $OLDTREE/$file`
1045 new=`readlink $NEWTREE/$file`
1047 "Removed link changed: $file (\"$old\" became \"$new\")"
1050 warn "Removed file changed: $file"
1056 # Treat the file as unmodified and force install of the new
1057 # file if it matches an ALWAYS_INSTALL glob. If the update
1058 # attempt fails, then fall through to the normal case so a
1059 # warning is generated.
1060 if always_install $file; then
1061 log "ALWAYS: updating $file"
1062 if update_unmodified $file; then
1067 # If the file changed types between the old and new trees but
1068 # the files in the new and dest tree are both of the same
1069 # type, treat it like an added file just comparing the new and
1071 if [ $cmp -eq $COMPARE_DIFFTYPE ]; then
1074 new=`readlink $NEWTREE/$file`
1075 dest=`readlink $DESTDIR/$file`
1077 "New link conflict: $file (\"$new\" vs \"$dest\")"
1087 # If the file has not changed types between the old
1088 # and new trees, but it is a different type in
1089 # DESTDIR, then just warn.
1090 if [ $newdestcmp -eq $COMPARE_DIFFTYPE ]; then
1091 new=`file_type $NEWTREE/$file`
1092 dest=`file_type $DESTDIR/$file`
1093 warn "Modified mismatch: $file ($new vs $dest)"
1100 old=`file_type $OLDTREE/$file`
1101 new=`file_type $NEWTREE/$file`
1102 dest=`file_type $DESTDIR/$file`
1103 warn "Modified $dest changed: $file ($old became $new)"
1106 old=`readlink $OLDTREE/$file`
1107 new=`readlink $NEWTREE/$file`
1109 "Modified link changed: $file (\"$old\" became \"$new\")"
1117 # Handle a file that has been added in the new tree. If the file does
1118 # not exist in DESTDIR, simply copy the file into DESTDIR. If the
1119 # file exists in the DESTDIR and is identical to the new version, do
1120 # nothing. Otherwise, generate a diff of the two versions of the file
1121 # and mark it as a conflict.
1123 # $1 - pathname of the file (relative to DESTDIR)
1126 local cmp dest file new
1129 if ignore $file; then
1130 log "IGNORE: added file $file"
1134 compare $DESTDIR/$file $NEWTREE/$file
1141 panic "Added file now missing"
1143 $COMPARE_ONLYSECOND)
1144 # Ignore new directories. They will be
1145 # created as needed when non-directory nodes
1147 if ! [ -d $NEWTREE/$file ]; then
1148 if install_new $file; then
1157 # Treat the file as unmodified and force install of the new
1158 # file if it matches an ALWAYS_INSTALL glob. If the update
1159 # attempt fails, then fall through to the normal case so a
1160 # warning is generated.
1161 if always_install $file; then
1162 log "ALWAYS: updating $file"
1163 if update_unmodified $file; then
1170 new=`file_type $NEWTREE/$file`
1171 dest=`file_type $DESTDIR/$file`
1172 warn "New file mismatch: $file ($new vs $dest)"
1175 new=`readlink $NEWTREE/$file`
1176 dest=`readlink $DESTDIR/$file`
1177 warn "New link conflict: $file (\"$new\" vs \"$dest\")"
1180 # If the only change in the new file versus
1181 # the destination file is a change in the
1182 # FreeBSD ID string and -F is specified, just
1183 # install the new file.
1184 if [ -n "$FREEBSD_ID" ] && \
1185 fbsdid_only $NEWTREE/$file $DESTDIR/$file; then
1186 if update_unmodified $file; then
1190 "Updating FreeBSD ID string failed"
1200 # Main routines for each command
1202 # Build a new tree and save it in a tarball.
1207 if [ $# -ne 1 ]; then
1208 echo "Missing required tarball."
1213 log "build command: $1"
1215 # Create a temporary directory to hold the tree
1216 dir=`mktemp -d $WORKDIR/etcupdate-XXXXXXX`
1217 if [ $? -ne 0 ]; then
1218 echo "Unable to create temporary directory."
1221 if ! build_tree $dir; then
1222 echo "Failed to build tree."
1226 if ! tar cfj $1 -C $dir . >&3 2>&1; then
1227 echo "Failed to create tarball."
1234 # Output a diff comparing the tree at DESTDIR to the current
1235 # unmodified tree. Note that this diff does not include files that
1236 # are present in DESTDIR but not in the unmodified tree.
1241 if [ $# -ne 0 ]; then
1245 # Requires an unmodified tree to diff against.
1246 if ! [ -d $NEWTREE ]; then
1247 echo "Reference tree to diff against unavailable."
1251 # Unfortunately, diff alone does not quite provide the right
1252 # level of options that we want, so improvise.
1253 for file in `(cd $NEWTREE; find .) | sed -e 's/^\.//'`; do
1254 if ignore $file; then
1258 diffnode $NEWTREE "$DESTDIR" $file "stock" "local"
1262 # Just extract a new tree into NEWTREE either by building a tree or
1263 # extracting a tarball. This can be used to bootstrap updates by
1264 # initializing the current "stock" tree to match the currently
1267 # Unlike 'update', this command does not rotate or preserve an
1268 # existing NEWTREE, it just replaces any existing tree.
1272 if [ $# -ne 0 ]; then
1276 log "extract command: tarball=$tarball"
1278 if [ -d $NEWTREE ]; then
1279 if ! remove_tree $NEWTREE; then
1280 echo "Unable to remove current tree."
1288 # Resolve conflicts left from an earlier merge.
1293 if [ $# -ne 0 ]; then
1297 if ! [ -d $CONFLICTS ]; then
1301 conflicts=`(cd $CONFLICTS; find . ! -type d) | sed -e 's/^\.//'`
1302 for file in $conflicts; do
1303 resolve_conflict $file
1306 if [ -n "$NEWALIAS_WARN" ]; then
1307 warn "Needs update: /etc/mail/aliases.db" \
1308 "(requires manual update via newaliases(1))"
1311 echo " Needs update: /etc/mail/aliases.db" \
1312 "(requires manual update via newaliases(1))"
1316 # Report a summary of the previous merge. Specifically, list any
1317 # remaining conflicts followed by any warnings from the previous
1322 if [ $# -ne 0 ]; then
1326 if [ -d $CONFLICTS ]; then
1327 (cd $CONFLICTS; find . ! -type d) | sed -e 's/^\./ C /'
1329 if [ -s $WARNINGS ]; then
1335 # Perform an actual merge. The new tree can either already exist (if
1336 # rerunning a merge), be extracted from a tarball, or generated from a
1342 if [ $# -ne 0 ]; then
1346 log "update command: rerun=$rerun tarball=$tarball"
1348 if [ `id -u` -ne 0 ]; then
1349 echo "Must be root to update a tree."
1353 # Enforce a sane umask
1356 # XXX: Should existing conflicts be ignored and removed during
1359 # Trim the conflicts tree. Whine if there is anything left.
1360 if [ -e $CONFLICTS ]; then
1361 find -d $CONFLICTS -type d -empty -delete >&3 2>&1
1362 rmdir $CONFLICTS >&3 2>&1
1364 if [ -d $CONFLICTS ]; then
1365 echo "Conflicts remain from previous update, aborting."
1369 if [ -z "$rerun" ]; then
1370 # For a dryrun that is not a rerun, do not rotate the existing
1371 # stock tree. Instead, extract a tree to a temporary directory
1372 # and use that for the comparison.
1373 if [ -n "$dryrun" ]; then
1374 dir=`mktemp -d $WORKDIR/etcupdate-XXXXXXX`
1375 if [ $? -ne 0 ]; then
1376 echo "Unable to create temporary directory."
1382 # Rotate the existing stock tree to the old tree.
1383 elif [ -d $NEWTREE ]; then
1384 # First, delete the previous old tree if it exists.
1385 if ! remove_tree $OLDTREE; then
1386 echo "Unable to remove old tree."
1390 # Move the current stock tree.
1391 if ! mv $NEWTREE $OLDTREE >&3 2>&1; then
1392 echo "Unable to rename current stock tree."
1397 if ! [ -d $OLDTREE ]; then
1399 No previous tree to compare against, a sane comparison is not possible.
1401 log "No previous tree to compare against."
1402 if [ -n "$dir" ]; then
1408 # Populate the new tree.
1412 # Build lists of nodes in the old and new trees.
1413 (cd $OLDTREE; find .) | sed -e 's/^\.//' | sort > $WORKDIR/old.files
1414 (cd $NEWTREE; find .) | sed -e 's/^\.//' | sort > $WORKDIR/new.files
1416 # Split the files up into three groups using comm.
1417 comm -23 $WORKDIR/old.files $WORKDIR/new.files > $WORKDIR/removed.files
1418 comm -13 $WORKDIR/old.files $WORKDIR/new.files > $WORKDIR/added.files
1419 comm -12 $WORKDIR/old.files $WORKDIR/new.files > $WORKDIR/both.files
1421 # Initialize conflicts and warnings handling.
1425 # The order for the following sections is important. In the
1426 # odd case that a directory is converted into a file, the
1427 # existing subfiles need to be removed if possible before the
1428 # file is converted. Similarly, in the case that a file is
1429 # converted into a directory, the file needs to be converted
1430 # into a directory if possible before the new files are added.
1432 # First, handle removed files.
1433 for file in `cat $WORKDIR/removed.files`; do
1434 handle_removed_file $file
1437 # For the directory pass, reverse sort the list to effect a
1438 # depth-first traversal. This is needed to ensure that if a
1439 # directory with subdirectories is removed, the entire
1440 # directory is removed if there are no local modifications.
1441 for file in `sort -r $WORKDIR/removed.files`; do
1442 handle_removed_directory $file
1445 # Second, handle files that exist in both the old and new
1447 for file in `cat $WORKDIR/both.files`; do
1448 handle_modified_file $file
1451 # Finally, handle newly added files.
1452 for file in `cat $WORKDIR/added.files`; do
1453 handle_added_file $file
1456 if [ -n "$NEWALIAS_WARN" ]; then
1457 warn "Needs update: /etc/mail/aliases.db" \
1458 "(requires manual update via newaliases(1))"
1461 if [ -s $WARNINGS ]; then
1466 if [ -n "$dir" ]; then
1467 if [ -z "$dryrun" -o -n "$rerun" ]; then
1468 panic "Should not have a temporary directory"
1475 # Determine which command we are executing. A command may be
1476 # specified as the first word. If one is not specified then 'update'
1477 # is assumed as the default command.
1479 if [ $# -gt 0 ]; then
1481 build|diff|extract|status|resolve)
1486 # If first arg is an option, assume the
1495 # Set default variable values.
1497 # The path to the source tree used to build trees.
1500 # The destination directory where the modified files live.
1503 # Ignore changes in the FreeBSD ID string.
1506 # Files that should always have the new version of the file installed.
1509 # Files to ignore and never update during a merge.
1512 # Flags to pass to 'make' when building a tree.
1515 # Include a config file if it exists. Note that command line options
1516 # override any settings in the config file. More details are in the
1517 # manual, but in general the following variables can be set:
1527 if [ -r /etc/etcupdate.conf ]; then
1528 . /etc/etcupdate.conf
1531 # Parse command line options
1538 while getopts "d:nrs:t:A:BD:FI:L:M:" option; do
1556 # To allow this option to be specified
1557 # multiple times, accumulate command-line
1558 # specified patterns in an 'always' variable
1559 # and use that to overwrite ALWAYS_INSTALL
1560 # after parsing all options. Need to be
1561 # careful here with globbing expansion.
1563 always="$always $OPTARG"
1576 # To allow this option to be specified
1577 # multiple times, accumulate command-line
1578 # specified patterns in an 'ignore' variable
1579 # and use that to overwrite IGNORE_FILES after
1580 # parsing all options. Need to be careful
1581 # here with globbing expansion.
1583 ignore="$ignore $OPTARG"
1590 MAKE_OPTIONS="$OPTARG"
1598 shift $((OPTIND - 1))
1600 # Allow -A command line options to override ALWAYS_INSTALL set from
1603 if [ -n "$always" ]; then
1604 ALWAYS_INSTALL="$always"
1607 # Allow -I command line options to override IGNORE_FILES set from the
1609 if [ -n "$ignore" ]; then
1610 IGNORE_FILES="$ignore"
1614 # Where the "old" and "new" trees are stored.
1615 WORKDIR=${WORKDIR:-$DESTDIR/var/db/etcupdate}
1617 # Log file for verbose output from program that are run. The log file
1618 # is opened on fd '3'.
1619 LOGFILE=${LOGFILE:-$WORKDIR/log}
1621 # The path of the "old" tree
1622 OLDTREE=$WORKDIR/old
1624 # The path of the "new" tree
1625 NEWTREE=$WORKDIR/current
1627 # The path of the "conflicts" tree where files with merge conflicts are saved.
1628 CONFLICTS=$WORKDIR/conflicts
1630 # The path of the "warnings" file that accumulates warning notes from an update.
1631 WARNINGS=$WORKDIR/warnings
1633 # Use $EDITOR for resolving conflicts. If it is not set, default to vi.
1634 EDITOR=${EDITOR:-/usr/bin/vi}
1636 # Handle command-specific argument processing such as complaining
1637 # about unsupported options. Since the configuration file is always
1638 # included, do not complain about extra command line arguments that
1639 # may have been set via the config file rather than the command line.
1642 if [ -n "$rerun" -a -n "$tarball" ]; then
1643 echo "Only one of -r or -t can be specified."
1648 build|diff|resolve|status)
1649 if [ -n "$dryrun" -o -n "$rerun" -o -n "$tarball" ]; then
1654 if [ -n "$dryrun" -o -n "$rerun" ]; then
1660 # Open the log file. Don't truncate it if doing a minor operation so
1661 # that a minor operation doesn't lose log info from a major operation.
1662 if ! mkdir -p $WORKDIR 2>/dev/null; then
1663 echo "Failed to create work directory $WORKDIR"
1667 diff|resolve|status)