base-files: fix postinstall uci-defaults removal
[openwrt/openwrt.git] / package / base-files / files / lib / functions.sh
index 67f4a046e595c0a0a862cf739c521e6a2afd73a1..50a7b408698cbf5141d65f1f4eaf851c16d84876 100755 (executable)
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Copyright (C) 2006-2013 OpenWrt.org
+# Copyright (C) 2006-2014 OpenWrt.org
 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
 # Copyright (C) 2010 Vertical Communications
 
@@ -57,16 +57,16 @@ config () {
        export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
        name="${name:-cfg$CONFIG_NUM_SECTIONS}"
        append CONFIG_SECTIONS "$name"
-       [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
        export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
-       export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
+       config_set "$CONFIG_SECTION" "TYPE" "${cfgtype}"
+       [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
 }
 
 option () {
        local varname="$1"; shift
        local value="$*"
 
-       export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
+       config_set "$CONFIG_SECTION" "${varname}" "${value}"
        [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
 }
 
@@ -81,7 +81,7 @@ list() {
        config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
        config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
        append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
-       list_cb "$varname" "$*"
+       [ -n "$NO_CALLBACK" ] || list_cb "$varname" "$*"
 }
 
 config_unset() {
@@ -113,11 +113,8 @@ config_set() {
        local section="$1"
        local option="$2"
        local value="$3"
-       local old_section="$CONFIG_SECTION"
 
-       CONFIG_SECTION="$section"
-       option "$option" "$value"
-       CONFIG_SECTION="$old_section"
+       export ${NO_EXPORT:+-n} "CONFIG_${section}_${option}=${value}"
 }
 
 config_foreach() {
@@ -153,55 +150,111 @@ config_list_foreach() {
        done
 }
 
-insert_modules() {
-       [ -d /etc/modules.d ] && {
-               cd /etc/modules.d
-               sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
-       }
-}
-
 default_prerm() {
-       local name
-       name=$(echo $(basename $1) | cut -d. -f1)
-       [ -f /usr/lib/opkg/info/${name}.prerm-pkg ] && . /usr/lib/opkg/info/${name}.prerm-pkg
-       for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
-               $i disable
-               $i stop
+       local root="${IPKG_INSTROOT}"
+       local pkgname="$(basename ${1%.*})"
+       local ret=0
+
+       if [ -f "$root/usr/lib/opkg/info/${pkgname}.prerm-pkg" ]; then
+               ( . "$root/usr/lib/opkg/info/${pkgname}.prerm-pkg" )
+               ret=$?
+       fi
+
+       local shell="$(which bash)"
+       for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
+               if [ -n "$root" ]; then
+                       ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" disable
+               else
+                       if [ "$PKG_UPGRADE" != "1" ]; then
+                               "$i" disable
+                       fi
+                       "$i" stop
+               fi
        done
+
+       return $ret
+}
+
+add_group_and_user() {
+       local pkgname="$1"
+       local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
+
+       if [ -n "$rusers" ]; then
+               local tuple oIFS="$IFS"
+               for tuple in $rusers; do
+                       local uid gid uname gname
+
+                       IFS=":"
+                       set -- $tuple; uname="$1"; gname="$2"
+                       IFS="="
+                       set -- $uname; uname="$1"; uid="$2"
+                       set -- $gname; gname="$1"; gid="$2"
+                       IFS="$oIFS"
+
+                       if [ -n "$gname" ] && [ -n "$gid" ]; then
+                               group_exists "$gname" || group_add "$gname" "$gid"
+                       elif [ -n "$gname" ]; then
+                               gid="$(group_add_next "$gname")"
+                       fi
+
+                       if [ -n "$uname" ]; then
+                               user_exists "$uname" || user_add "$uname" "$uid" "$gid"
+                       fi
+
+                       if [ -n "$uname" ] && [ -n "$gname" ]; then
+                               group_add_user "$gname" "$uname"
+                       fi
+
+                       unset uid gid uname gname
+               done
+       fi
 }
 
 default_postinst() {
-       local name rusers
-       name=$(echo $(basename $1) | cut -d. -f1)
-       [ -f ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.postinst-pkg ] && . ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.postinst-pkg
-       rusers=$(grep "Require-User:" ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.control)
-       [ -n "$rusers" ] && {
-               local user group
-               for a in $(echo $rusers | sed "s/Require-User://g"); do
-                       user=""
-                       group=""
-                       for b in $(echo $a | sed "s/:/ /g"); do
-                               [ -z "$user" ] && {
-                                       user=$b
-                                       continue
-                               }
-                               [ -z "$group" ] && {
-                                       group=$b
-                                       group_add_next $b
-                                       gid=$?
-                                       user_exists $user || user_add $user "" $gid
-                                       continue
-                               }
-                               group_add_next $b
-                               group_add_user $b $user
-                       done
+       local root="${IPKG_INSTROOT}"
+       local pkgname="$(basename ${1%.*})"
+       local ret=0
+
+       add_group_and_user "${pkgname}"
+
+       if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then
+               ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" )
+               ret=$?
+       fi
+
+       if [ -d "$root/rootfs-overlay" ]; then
+               cp -R $root/rootfs-overlay/. $root/
+               rm -fR $root/rootfs-overlay/
+       fi
+
+       if [ -z "$root" ] && grep -q -s "^/etc/modules.d/" "/usr/lib/opkg/info/${pkgname}.list"; then
+               kmodloader
+       fi
+
+       if [ -z "$root" ] && grep -q -s "^/etc/uci-defaults/" "/usr/lib/opkg/info/${pkgname}.list"; then
+               . /lib/functions/system.sh
+               [ -d /tmp/.uci ] || mkdir -p /tmp/.uci
+               for i in $(grep -s "^/etc/uci-defaults/" "/usr/lib/opkg/info/${pkgname}.list"); do
+                       ( [ -f "$i" ] && cd "$(dirname $i)" && . "$i" ) && rm -f "$i"
                done
-       }
-       [ -n "${IPKG_INSTROOT}" -o "$PKG_UPGRADE" = "1" ] || for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
-               $i enable
-               $i start
+               uci commit
+       fi
+
+       [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null
+
+       local shell="$(which bash)"
+       for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
+               if [ -n "$root" ]; then
+                       ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
+               else
+                       if [ "$PKG_UPGRADE" != "1" ]; then
+                               "$i" enable
+                       fi
+                       "$i" start
+               fi
        done
-       return 0
+
+       return $ret
 }
 
 include() {
@@ -234,9 +287,7 @@ group_add() {
        [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
        [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
        echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
-       rc=$?
        [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
-       return $rc
 }
 
 group_exists() {
@@ -246,14 +297,17 @@ group_exists() {
 group_add_next() {
        local gid gids
        gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
-       [ -n "$gid" ] && return $gid
+       if [ -n "$gid" ]; then
+               echo $gid
+               return
+       fi
        gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
-       gid=100
-       while [ -n "$(echo $gids | grep $gid)" ] ; do
+       gid=65536
+       while [ -n "$(echo "$gids" | grep "^$gid$")" ] ; do
                gid=$((gid + 1))
        done
        group_add $1 $gid
-       return $gid
+       echo $gid
 }
 
 group_add_user() {
@@ -276,8 +330,8 @@ user_add() {
        local rc
        [ -z "$uid" ] && {
                uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
-               uid=100
-               while [ -n "$(echo $uids | grep $uid)" ] ; do
+               uid=65536
+               while [ -n "$(echo "$uids" | grep "^$uid$")" ] ; do
                        uid=$((uid + 1))
                done
        }
@@ -286,13 +340,15 @@ user_add() {
        [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
        echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
        echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
-       rc=$?
        [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
-       return $rc
 }
 
 user_exists() {
        grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
 }
 
+board_name() {
+       [ -e /tmp/sysinfo/board_name ] && cat /tmp/sysinfo/board_name || echo "generic"
+}
+
 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh