OSDN Git Service

Version 3.2.0
[portsreinstall/current.git] / lib / libpkgsys.sh
index b2bc37e..2d2029f 100644 (file)
@@ -48,16 +48,49 @@ pkgsys_pkgchksum_file ()
        echo CHECKSUM.MD5
 }
 
+# ============= Get the origin of the currently used pkg(8) =============
+pkgsys_portsmgmt_pkg ()
+{
+       local origin
+       if [ ! -e "${DBDIR}/PKG_ORIGIN" ]
+       then
+               origin=`pkgsys_sysvar PKG_ORIGIN` || :
+               [ -n "$origin" -a -d "${PORTSDIR}/$origin" ] || origin=ports-mgmt/pkg
+               echo "$origin" > ${DBDIR}/PKG_ORIGIN
+       fi
+       cat "${DBDIR}/PKG_ORIGIN"
+}
+
+# ============= Get the origin of the currently used dialog4ports(1) =============
+pkgsys_portsmgmt_dialog4ports ()
+{
+       echo "$origin"
+       local origin
+       if [ ! -e "${DBDIR}/DIALOGPORT" ]
+       then
+               origin=`pkgsys_sysvar DIALOGPORT` || :
+               [ -n "$origin" -a -d "${PORTSDIR}/$origin" ] || origin=ports-mgmt/dialog4ports
+               echo "$origin" > ${DBDIR}/DIALOGPORT
+       fi
+       cat "${DBDIR}/DIALOGPORT"
+}
+
 # ============= Check whether a port is indispensable for the standard function of the ports/packages system =============
 pkgsys_is_pkgtool ()
 {
        case $1 in
-       ports-mgmt/pkg|ports-mgmt/dialog4ports)
+       ports-mgmt/pkg|ports-mgmt/pkg-devel|ports-mgmt/dialog4ports)
                ;;
        *)      return 1;;
        esac
 }
 
+# ============= Check whether a port is indispensable for package operations =============
+pkgsys_is_necessary_pkgtool ()
+{
+       [ x"$WITH_PKGNG" = x'yes' -a \( x"$1" = x'ports-mgmt/pkg' -o x"$1" = x'ports-mgmt/pkg-devel' \) ]
+}
+
 # ============= Check whether the dialog for selecting port options is dialog4ports =============
 pkgsys_is_dialog4ports_used ()
 {
@@ -332,9 +365,20 @@ pkgsys_def_pkgtools ()
                }
                pkg_check_sanity ()
                {
-                       local pkg
+                       local pkg tmp_stdout tmp_stderr
                        pkg=$1
-                       pkg check -s "$pkg" 2> /dev/null
+                       tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
+                       tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
+                       pkg check -s "$pkg" > $tmp_stdout 2> $tmp_stderr || :
+                       grep '^[^:]*: checksum mismatch for ' "$tmp_stderr" | sed 's/^[^:]*: checksum mismatch for //' || :
+                       if grep -q '^pkg: .*: No such file or directory$' "$tmp_stderr"
+                       then
+                               pkg info -ql "$pkg" | while read filepath
+                               do
+                                       [ -e "$filepath" ] || echo "$filepath"
+                               done
+                       fi
+                       :
                }
                pkg_which ()
                {
@@ -353,6 +397,9 @@ pkgsys_def_pkgtools ()
                pkg_delete_f ()
                {
                        pkg delete -fqy "$@"
+                       pkg -N 2> /dev/null || return 0 # If pkg(8) is deinstalled successfully
+                       pkg info -e "$@" || return 0    # Countermeasure for a bug found for pkg-1.3.4 (at least not until 1.2.7_4)
+                       pkg delete -fy "$@"
                }
                pkg_add_tools ()
                {
@@ -476,7 +523,8 @@ pkgsys_def_pkgtools ()
                        PKGNG_AUTODEPS=NO
                        PKGNG_PORTAUDIT_SITE='http=//portaudit.FreeBSD.org/auditfile.tbz'
                        # Load configuration for pkg(1)
-                       pkg_conf=`pkg query %Fp pkg | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'`
+                       pkg_conf=`pkg query %Fp pkg 2> /dev/null | grep '/etc/pkg\.conf\.sample$' | sed 's/\.sample$//'` || :
+                       [ -n "$pkg_conf" ] || pkg_conf=${MYPREFIX}/etc/pkg.conf
                        grep -v -e '^[[:space:]]*#' -e '^[[:space:]]*$' "$pkg_conf" 2> /dev/null \
                                | grep -e '^[[:space:]]*[A-Z0-9_]*[[:space:]]*:[[:space:]]*.*' \
                                | while read srcline
@@ -491,7 +539,7 @@ pkgsys_def_pkgtools ()
                pkg_rescue_tools ()
                {
                        local packagepath checksumpath pkgname is_successful
-                       packagepath=`pkgsys_get_backup_pkg ports-mgmt/pkg` && \
+                       packagepath=`pkgsys_get_backup_pkg \`pkgsys_portsmgmt_pkg\`` && \
                                pkg_add_tools "$packagepath" && return
                        pkg_is_tool_available && return
                        message_echo "WARNING: WITH_PKGNG is set, but pkgng is still missing. It is installed now." >&2
@@ -508,10 +556,10 @@ pkgsys_def_pkgtools ()
                                unset WITH_PKGNG
                                unset WITHOUT_PKGNG
                                
-                               message_echo "INFO: Attempting deinstallation of ports-mgmt/pkg to make sure."
-                               env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/ports-mgmt/pkg" deinstall || :
-                               message_echo "INFO: Attempting (re)installation by ports-mgmt/pkg."
-                               env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/ports-mgmt/pkg" reinstall clean
+                               message_echo "INFO: Attempting deinstallation of pkg(8) to make sure."
+                               env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/`pkgsys_portsmgmt_pkg`" deinstall || :
+                               message_echo "INFO: Attempting (re)installation by pkg(8)."
+                               env __MAKE_CONF="${TMPDIR}/make.conf" make -C "${PORTSDIR}/`pkgsys_portsmgmt_pkg`" reinstall clean
                        ) && {
                                pkg2ng || :
                                pkg_is_tool_available
@@ -591,9 +639,13 @@ pkgsys_def_pkgtools ()
                }
                pkg_check_sanity ()
                {
-                       local pkg
+                       local pkg tmp_stdout tmp_stderr
                        pkg=$1
-                       pkg_info -qg "$pkg" 2> /dev/null
+                       tmp_stdout=${TMPDIR}/pkgng:pkg_check_sanity:stdout
+                       tmp_stderr=${TMPDIR}/pkgng:pkg_check_sanity:stderr
+                       pkg_info -qg "$pkg" > $tmp_stdout 2> $tmp_stderr || :
+                       grep ' fails the original MD5 checksum$' "$tmp_stdout" | sed 's/ fails the original MD5 checksum$//' || :
+                       grep "^pkg_info: .* doesn't exist$" "$tmp_stderr" | sed -E "s/^pkg_info: (.*) doesn't exist$/\1/" || :
                }
                pkg_which ()
                {
@@ -811,7 +863,7 @@ pkgsys_eval_ports_glob ()
 # ============= Create a back-up package archive =============
 pkgsys_create_backup_pkg ()
 {
-       local pkgname dstdir origin backup_pkg_old origin_regexp pkgname_ptn backup_pkg pkgpath
+       local pkgname dstdir origin backup_pkg_old pkgname_ptn backup_pkg dbpath pkgpath origin_regexp
        pkgname=$1
        dstdir=$2
        rm -rf "${TMPDIR}"/package.tmp
@@ -834,6 +886,9 @@ pkgsys_create_backup_pkg ()
                message_echo "WARNING: Failed to create backup package for $pkgname." >&2
                return 1
        fi
+       dbpath=${DBDIR}/backup/$origin
+       [ -d "$dbpath" ] || mkdir -p "$dbpath"
+       pkg_info_qL > $dbpath/previously_installed_files
        [ -d "$dstdir" ] || mkdir -p "$dstdir"
        mv "${TMPDIR}/package.tmp/$backup_pkg" "$dstdir"
        pkgpath=$dstdir/$backup_pkg
@@ -873,7 +928,7 @@ pkgsys_delete_backup_pkg ()
 # ============= Get an existing package archive path for a port origin =============
 pkgsys_get_backup_pkg ()
 {
-       local origin origin_regexp
+       local origin tmpnewest origin_regexp
        origin=$1
        tmpnewest=${TMPDIR}/pkgsys_get_backup_pkg::newest
        origin_regexp=`str_escape_regexp "$origin"`
@@ -889,6 +944,27 @@ pkgsys_get_backup_pkg ()
        cat "$tmpnewest" 2> /dev/null
 }
 
+# ============= Get a file list to be restored by the current backup package for a port origin =============
+pkgsys_get_restored_files_by_backup_pkg ()
+{
+       local origin
+       origin=$1
+       cat "${DBDIR}/backup/$origin/previously_installed_files" 2> /dev/null || :
+}
+
+# ============= Check whether any file match restored files by the current backup package for a port origin =============
+pkgsys_chk_match_to_restored_files_by_backup_pkg ()
+{
+       local origin
+       origin=$1
+       pattern_file=$2
+       while read pattern
+       do 
+               grep -qE "$pattern" "${DBDIR}/backup/$origin/previously_installed_files" && return
+       done < $pattern_file
+       false
+}
+
 # ============= Get a package name from a package archive file name =============
 pkgsys_pkgarc_to_pkgname ()
 {
@@ -996,7 +1072,10 @@ pkgsys_get_conflicting_installed_pkgs ()
        mode=$1
        origin=$2
        tmp_conflicts=${TMPDIR}/pkgsys_get_conflicting_installed_pkgs::conflicts
-       pkg_info_Eg `pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin"` > $tmp_conflicts || :
+       pkgsys_get_conflicting_pkgs_patterns "$mode" "$origin" | while read pkg_pattern
+       do
+               pkg_info_Eg "$pkg_pattern" || :
+       done > $tmp_conflicts
        cat "$tmp_conflicts"
        [ `wc -l < $tmp_conflicts` -gt 0 ]
 }      
@@ -1029,15 +1108,13 @@ pkgsys_sanitychk_pkgcontents ()
        _is_reinstall_encouraged=no
        while [ $iline -le $nlines ]
        do
-               src=`sed -n ${iline}p "$tmp_sanity"`
+               filename=`sed -n ${iline}p "$tmp_sanity"`
                iline=$(($iline+1))
-               filename=`echo "$src" | cut -d ' ' -f 1`
-               icol=2
-               until [ -e "$filename" -o "$filename" = "$src" ]
-               do
-                       filename="$filename "`echo "$src" | cut -d ' ' -f $icol`
-               done
-               [ -e "$filename" ] || continue
+               if [ ! -e "$filename" ]
+               then
+                       _is_reinstall_encouraged=yes
+                       break
+               fi
                if expr "$filename" : '.*/include/.*' > /dev/null
                then
                        _is_reinstall_encouraged=yes