--- trunk/mage/usr/lib/mage/mage4.functions.sh 2011/12/27 10:00:34 1548 +++ trunk/mage/usr/lib/mage/mage4.functions.sh 2013/08/14 14:07:00 2167 @@ -38,12 +38,114 @@ return 0 } +mchecksum() +{ + local i + local rundir + local file + local method + local cmd + local retval + + # very basic getops + for i in $* + do + case $1 in + --rundir|-r) shift; rundir="$1" ;; + --file|-f) shift; file="$1" ;; + --method|-m) shift; method="$1" ;; + esac + shift + done + + # sanity checks + [[ -z ${rundir} ]] && die "mchecksum(): rundir missing" + [[ -z ${file} ]] && die "mchecksum(): file missing" + [[ -z ${method} ]] && die "mchecksum(): method missing" + + case ${method} in + md5) cmd="md5sum" ;; + sha256) cmd="sha256sum" ;; + *) die "mchecksum(): unknown method '${method}'" ;; + esac + + if [[ -d ${rundir} ]] + then + pushd ${rundir} &> /dev/null + # be verbose here + ${cmd} -c ${file} #&> /dev/null + retval="$?" + popd &> /dev/null + else + retval=1 + fi + + return "${retval}" +} + +mcheckemptydir() +{ + local dir="$1" + local retval=1 + + if [[ ! -d ${dir} ]] + then + echo "mcheckemptydir(): '${dir}' is not a directory!" + retval=3 + else + shopt -s nullglob dotglob + files=( ${dir}/* ) + (( ${#files[*]} )) || retval=0 + shopt -u nullglob dotglob + fi + + return ${retval} +} + +unpack_package() +{ + local magefile="$1" + local pkg + local pkgtype + local tar_opts + + pkg="$(get_value_from_magefile PKGNAME ${magefile}).${PKGSUFFIX}" + pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})" + + xtitle "[ Unpacking ${pkg} ]" + + # abort on virtual pkg + if [[ ${pkgtype} = virtual ]] + then + echo -ne " ${COLBLUE}---${COLDEFAULT}" + echo " !unpack virtual ${pkg/.${PKGSUFFIX}/} ... " + continue + fi + + # abort on sources pkg + if [[ ${pkgtype} = sources ]] + then + echo -ne " ${COLBLUE}---${COLDEFAULT}" + echo " !unpack sources ${pkg/.${PKGSUFFIX}/} ... " + continue + fi + + # busybox? + if need_busybox_support tar + then + tar_opts="xjf" + else + tar_opts="xjmf" + fi + + echo -e " ${COLBLUE}***${COLDEFAULT} unpacking ${pkg} ... " + tar ${tar_opts} ${PKGDIR}/${pkg} -C ${BUILDDIR} || die "Unpacking package ${pkg}" +} + unpack_packages() { local list="$@" local magefile - local pkg - local pkgtype local count_current local count_total local tar_opts @@ -56,38 +158,8 @@ for magefile in ${list} do - pkg="$(get_value_from_magefile PKGNAME ${magefile}).${PKGSUFFIX}" - pkgtype="$(get_value_from_magefile PKGTYPE ${magefile})" - + unpack_package "${magefile}" (( count_current++ )) - xtitle "[ (${count_current}/${count_total}) Unpacking ${pkg} ]" - - # abort on virtual pkg - if [[ ${pkgtype} = virtual ]] - then - echo -ne " ${COLBLUE}---${COLDEFAULT}" - echo " !unpack virtual (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... " - continue - fi - - # abort on sources pkg - if [[ ${pkgtype} = sources ]] - then - echo -ne " ${COLBLUE}---${COLDEFAULT}" - echo " !unpack sources (${count_current}/${count_total}): ${pkg/.${PKGSUFFIX}/} ... " - continue - fi - - # busybox? - if need_busybox_support tar - then - tar_opts="xjf" - else - tar_opts="xjmf" - fi - - echo -e " ${COLBLUE}***${COLDEFAULT} unpacking (${count_current}/${count_total}): ${pkg} ... " - tar ${tar_opts} ${PKGDIR}/${pkg} -C ${BUILDDIR} || die "Unpacking package ${pkg}" done # add a crlf for a better view @@ -107,8 +179,9 @@ mtime=$(stat -c %Y "${reference}") touch \ --no-create \ + --no-dereference \ --time=mtime \ - --reference "${reference}" \ + --reference="${reference}" \ "${pathto}" echo "${mtime}" @@ -162,7 +235,7 @@ while read pathto posix user group do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t>>> DIR: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t>>> DIR: ${MROOT}${pathto}" # monitors /etc/env.d -> env-rebuild [[ ${pathto} = /etc/env.d ]] && export MAGE_ENV_REBUILD=true @@ -238,7 +311,7 @@ case ${retval} in # file is not protected - (over)write it 0|3) - [[ ${VERBOSE} = on ]] && echo -e "\t>>> FILE: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t>>> FILE: ${MROOT}${pathto}" install -m "${posix}" -o "${user}" -g "${group}" \ ${BUILDDIR}/${pkgname}/binfiles/"${pathto}" \ "${MROOT}${pathto}" @@ -256,7 +329,7 @@ # file is protected, write backup file 2) - if [[ ${VERBOSE} = on ]] + if mqueryfeature "verbose" then echo -en "${COLRED}" echo -n "! prot " @@ -287,7 +360,7 @@ # file is protected but ignored, delete the update/do nothing 4) - if [[ ${VERBOSE} = on ]] + if mqueryfeature "verbose" then echo -en "${COLRED}" echo -n "! ignr " @@ -346,17 +419,17 @@ while read pathto posix link mtime do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t>>> LINK: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t>>> LINK: ${MROOT}${pathto}" ln -snf "${link}" "${MROOT}${pathto}" -# # fix mtime and db -# fix_descriptor ${pkgname}/.symlinks \ -# "${pathto}" \ -# "${posix}" \ -# "${link}" \ -# "$(fix_mtime "${BUILDDIR}/${pkgname}"/.mtime \ -# "${MROOT}${pathto}")" + # fix mtime and db + fix_descriptor ${pkgname}/.symlinks \ + "${pathto}" \ + "${posix}" \ + "${link}" \ + "$(fix_mtime "${BUILDDIR}/${pkgname}"/.mtime \ + "${MROOT}${pathto}")" done < ${BUILDDIR}/${pkgname}/.symlinks @@ -396,7 +469,7 @@ while read pathto posix major minor user group do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t>>> PIPE: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t>>> PIPE: ${MROOT}${pathto}" mknod -m "${posix}" "${MROOT}${pathto}" # make it optional atm !! @@ -440,7 +513,7 @@ while read pathto posix major minor user group do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t>>> CHAR: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t>>> CHAR: ${MROOT}${pathto}" mknod -m ${posix} "${MROOT}${pathto}" b "${major}" "${minor}" @@ -484,7 +557,7 @@ while read pathto posix user group do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t>>> FIFO: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t>>> FIFO: ${MROOT}${pathto}" mkfifo -m "${posix}" "${MROOT}${pathto}" chown "${user}:${group}" "${MROOT}${pathto}" @@ -743,20 +816,9 @@ mtime="$(stat -c %Y ${MROOT}${INSTALLDB}/${pfull}/.mtime)" - # if $pathto is a symlink than compare linked binary - if [ -L "${MROOT}${pathto}" ] - then - # readlink -f resolves full path of linked file - x="$(readlink -f "${MROOT}${pathto}")" - - # abort if target does not exists - # we keep safe here, theoretically the link can removed - [ ! -e "${x}" ] && return 1 - - x=$(stat -c %Y "${x}") - else - x=$(stat -c %Y "${MROOT}${pathto}") - fi + # no extra handlink for symlinks anymore as fix_mtime + # uses --no-dereference, compare directly + x=$(stat -c %Y "${MROOT}${pathto}") [[ ${mtime} = ${x} ]] && return 0 @@ -818,7 +880,7 @@ [ -z "${pathto}" ] && continue if [ ! -L "${MROOT}${pathto}" ] then - [[ ${VERBOSE} = on ]] && \ + mqueryfeature "verbose" && \ echo -e "${COLRED}! exist${COLDEFAULT} === LINK: ${MROOT}${pathto}" continue fi @@ -830,12 +892,12 @@ # 1=keep me # case ${retval} in 0) - [[ ${VERBOSE} = on ]] && echo -e "\t<<< LINK: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t<<< LINK: ${MROOT}${pathto}" rm "${MROOT}${pathto}" ;; 1) - [[ ${VERBOSE} = on ]] && \ + mqueryfeature "verbose" && \ echo -e "${COLRED}! mtime${COLDEFAULT} === LINK: ${MROOT}${pathto}" ;; esac @@ -902,7 +964,7 @@ if [ ! -e "${MROOT}${pathto}" ] then - [[ ${VERBOSE} = on ]] && \ + mqueryfeature "verbose" && \ echo -e "${COLRED}! exist${COLDEFAULT} === FILE: ${MROOT}${pathto}" continue fi @@ -928,13 +990,13 @@ case ${retval} in # file is not protected - delete it 0|3) - [[ ${VERBOSE} = on ]] && echo -e "\t<<< FILE: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t<<< FILE: ${MROOT}${pathto}" rm "${MROOT}${pathto}" ;; # file is protected, do not delete 2) - if [[ ${VERBOSE} = on ]] + if mqueryfeature "verbose" then echo -en "${COLRED}" echo -n "! prot " @@ -945,7 +1007,7 @@ # file is protected but ignored, delete the update/do nothing 4) - if [[ ${VERBOSE} = on ]] + if mqueryfeature "verbose" then echo -en "${COLRED}" echo -n "! ignr " @@ -957,7 +1019,7 @@ esac ;; 1) - [[ ${VERBOSE} = on ]] && \ + mqueryfeature "verbose" && \ echo -e "${COLRED}! mtime${COLDEFAULT} === FILE: ${MROOT}${pathto}" ;; esac @@ -1019,7 +1081,7 @@ do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t<<< PIPE: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t<<< PIPE: ${MROOT}${pathto}" rm "${MROOT}${pathto}" done < ${MROOT}${INSTALLDB}/${pfull}/.pipes @@ -1079,7 +1141,7 @@ do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t<<< CHAR: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t<<< CHAR: ${MROOT}${pathto}" rm "${MROOT}${pathto}" done < ${MROOT}${INSTALLDB}/${pfull}/.char @@ -1141,7 +1203,7 @@ do [ -z "${pathto}" ] && continue - [[ ${VERBOSE} = on ]] && echo -e "\t<<< FIFO: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t<<< FIFO: ${MROOT}${pathto}" rm "${MROOT}${pathto}" done < ${MROOT}${INSTALLDB}/${pfull}/.fifo @@ -1202,7 +1264,7 @@ if [ ! -d "${MROOT}${pathto}" ] then - [[ ${VERBOSE} = on ]] && \ + mqueryfeature "verbose" && \ echo -e "${COLRED}! exist${COLDEFAULT} === DIR: ${MROOT}${pathto}" continue fi @@ -1210,7 +1272,7 @@ # exclude .keep directories if [ -f "${MROOT}${pathto}/.keep" ] then - [[ ${VERBOSE} = on ]] && \ + mqueryfeature "verbose" && \ echo -e "${COLRED}! .keep${COLDEFAULT} === DIR: ${MROOT}${pathto}" continue fi @@ -1223,9 +1285,9 @@ if rmdir "${MROOT}${pathto}" &> /dev/null then - [[ ${VERBOSE} = on ]] && echo -e "\t<<< DIR: ${MROOT}${pathto}" + mqueryfeature "verbose" && echo -e "\t<<< DIR: ${MROOT}${pathto}" else - [[ ${VERBOSE} = on ]] && \ + mqueryfeature "verbose" && \ echo -e "${COLRED}! empty${COLDEFAULT} === DIR: ${MROOT}${pathto}" fi done @@ -1283,9 +1345,111 @@ done } +# convertmirrors [uri] +convertmirrors() +{ + local uri="$1" + local scheme + local mirror + local mirrors + local addon + local real_uri + local output + + # needs + [[ -z ${MIRRORS} ]] && die "convertmirrors(): no mirrors defined!" + [[ -z ${SOURCEFORGE_MIRRORS} ]] && die "convertmirrors(): no sourceforge mirrors defined!" + [[ -z ${GNU_MIRRORS} ]] && die "convertmirrors(): no gnu mirrors defined!" + [[ -z ${GNOME_MIRRORS} ]] && die "convertmirrors(): no gnome mirrors defined!" + [[ -z ${KDE_MIRRORS} ]] && die "convertmirrors(): no kde mirrors defined!" + + # check known uri schemes + case ${uri} in + http://*|https://*|ftp://*|ftps://*) mirrors="" ;; + mirror://*) mirrors="${MIRRORS}"; scheme="mirror://"; addon="/sources" ;; + package://*) mirrors="${MIRRORS}"; scheme="package://"; addon="/${PACKAGES_SERVER_PATH}" ;; + gnu://*) mirrors="${GNU_MIRRORS}"; scheme="gnu://" ;; + sourceforge://*) mirrors="${SOURCEFORGE_MIRRORS}"; scheme="sourceforge://" ;; + gnome://*) mirrors="${GNOME_MIRRORS}"; scheme="gnome://" ;; + kde://*) mirrors="${KDE_MIRRORS}"; scheme="kde://" ;; + *) die "convertmirror(): unsupported uri scheme in '${uri}'!" ;; + esac + + if [[ ! -z ${mirrors} ]] + then + for mirror in ${mirrors} + do + # add a whitespace to the output + [[ -z ${output} ]] || output+=" " + output+="${mirror}${addon}/${uri/${scheme}/}" + done + else + output="${uri}" + fi + + echo "${output}" +} + +mdownload() +{ + local i + local uri + local real_uris + local mirror + local outputfile + local outputdir + local retval + local wget_opts + + # very basic getops + for i in $* + do + case $1 in + --uri|-u) shift; uri="$1" ;; + --dir|-d) shift; outputdir="$1" ;; + esac + shift + done + + # sanity checks; abort if not given + [[ -z ${uri} ]] && die "mdownload(): no uri given!" + [[ -z ${outputdir} ]] && die "mdownload(): no dir given!" + + # convert mirrored uris to the real ones + real_uris="$(convertmirrors ${uri})" + + # verbose or not + mqueryfeature "!verbose" && wget_opts+=" --quiet" + + # filter wget options if busybox was found + wget_opts+=" $(busybox_filter_wget_options ${WGET_FETCH_OPTIONS})" + + # create outputdir + [[ ! -d ${outputdir} ]] && install -d "${outputdir}" + + for mirror in ${real_uris} + do + # get the name of the output file + outputfile="${mirror##*/}" + + wget ${wget_opts} --output-document="${outputdir}/${outputfile}" "${mirror}" + retval="$?" + if [[ ${retval} = 0 ]] + then + break + else + continue + fi + done + + # return wget retval + return "${retval}" +} + # fetch_packages /path/to/mage/file1 /path/to/mage/file2 fetch_packages() { + local i local list="$@" local pkg local mirr @@ -1339,27 +1503,12 @@ continue fi - for mirr in ${MIRRORS} - do - echo -ne " ${COLBLUE}***${COLDEFAULT}" - #echo -e " fetching (${count_current}/${count_total}): ${mirr}/${pkg} ... " - echo -e " fetching (${count_current}/${count_total}): ${pkg} ... " - [[ ${VERBOSE} = off ]] && opt="--quiet" - wget \ - ${wget_opts} \ - --directory-prefix=${PKGDIR} \ - ${opt} ${mirr}/${PACKAGES_SERVER_PATH}/${pkg} - if [[ $? = 0 ]] - then - break - else - continue - fi - done - + echo -ne " ${COLBLUE}***${COLDEFAULT}" + echo -e " fetching (${count_current}/${count_total}): ${pkg} ... " + mdownload --uri "package://${pkg}" --dir "${PKGDIR}" || die "Could not download ${pkg}" if [ ! -f ${PKGDIR}/${pkg} ] then - die "Could not download ${pkg}" + die "Package '${pkg}' after download not found in '${PKGDIR}'" fi done @@ -1414,12 +1563,16 @@ for mirr in ${MIRRORS} do - # path without distribution - mymirr="${mirr%/*}" + # path without distribution + # (only for stable|testing|unstable and not DISTROTAG) + case ${mirr##*/} in + stable|testing|unstable) mymirr="${mirr%/*}";; + *) mymirr="${mirr}";; + esac echo -ne "${COLBLUE} --- ${COLDEFAULT}" echo "fetching latest md5 from ${mymirr} ..." - [[ ${VERBOSE} = off ]] && opt="--quiet" + mqueryfeature "!verbose" && opt="--quiet" wget \ ${wget_opts} \ --directory-prefix=${temp} \ @@ -1448,14 +1601,23 @@ else echo -ne "${COLBLUE} --- ${COLDEFAULT}" echo -n "checking md5sum... " - ( cd ${temp}; md5sum -c ${latest_md5} ) || die "md5 for ${latest_tarball} failed" + mchecksum --rundir "${temp}" --file "${latest_md5}" --method md5 || die "md5 for ${latest_tarball} failed" fi if [[ -d ${MAGEDIR} ]] then echo -ne "${COLBLUE} --- ${COLDEFAULT}" echo "cleaning old mage-tree ${MAGEDIR}..." - rm -rf ${MAGEDIR} + # honor mountpoints and empty dirs + if mountpoint -q ${MAGEDIR} + then + if ! mcheckemptydir ${MAGEDIR} + then + find ${MAGEDIR} -mindepth 1 -maxdepth 1 | xargs --no-run-if-empty rm -r + fi + else + rm -rf ${MAGEDIR} + fi fi if need_busybox_support tar @@ -1514,20 +1676,23 @@ } -# cuts full pathnames or versionized names down to basename -choppkgname() -{ - #we want this only if full name was used - if [ -n "$(echo ${MAGENAME}|fgrep .mage)" ] - then - #cuts ARCH and PBUILD - #ARCH comes from ${MAGERC} - MAGENAME=$(echo ${MAGENAME} |sed -e "s:-${ARCH}-r*.::g") +# unused? +# +# # cuts full pathnames or versionized names down to basename +# choppkgname() +# { +# #we want this only if full name was used +# if [ -n "$(echo ${MAGENAME}|fgrep .mage)" ] +# then +# #cuts ARCH and PBUILD +# #ARCH comes from ${MAGERC} +# MAGENAME=$(echo ${MAGENAME} |sed -e "s:-${ARCH}$(print_distrotag)-r*.::g") +# +# #cuts version number +# MAGENAME=$(basename ${MAGENAME%-*} .mage) +# fi +# } - #cuts version number - MAGENAME=$(basename ${MAGENAME%-*} .mage) - fi -} # get_categorie $PNAME, returns CATEGORIE # $1=pname @@ -1606,7 +1771,7 @@ then HIGHEST_MAGEFILE=${magefile} #for debug only - [[ ${MAGEDEBUG} = on ]] && echo "HIGHEST_MAGEFILE=${HIGHEST_MAGEFILE}" + mqueryfeature "debug" && echo "HIGHEST_MAGEFILE=${HIGHEST_MAGEFILE}" fi done @@ -1713,18 +1878,36 @@ local filename="${file##*/}" local count local output + local oldprotected local i + local x - declare -i count=0 + # hack; do not honor a global set IFS like '§' + local IFS + + count=0 # check if there are already protected files - for oldpretected in $(find ${dirname} -iname "._cfg????_${filename}" | + for oldprotected in $(find ${dirname} -iname "._cfg????_${filename}" | sed -e "s:\(^.*/\)\(._cfg*_\)\(/.*$\):\1\2\3\%\2\%\3:" | sort -t'%' -k3 -k2 | cut -f1 -d'%') do - count=$(echo ${oldpretected} | cut -d_ -f2 | sed -e "s:cfg::") + count="$(echo ${oldprotected} | sed 's:.*\/._cfg\(.*\)_.*:\1:')" done - (( count ++ )) + + # convert 0001 -> 1; 0120 -> 120 etc + # use bash internal base functions to this task + x="$((10#${count}))" + for (( i=0; i