2011-06-30 11:07:54 -04:00
|
|
|
#!/bin/bash
|
2009-02-25 13:26:31 -05:00
|
|
|
#
|
|
|
|
# pkgdelta - create delta files for use with pacman and repo-add
|
|
|
|
# @configure_input@
|
|
|
|
#
|
|
|
|
# Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
# bash options
|
|
|
|
set -o errexit
|
|
|
|
|
|
|
|
# gettext initialization
|
2011-06-23 22:21:51 -04:00
|
|
|
export TEXTDOMAIN='pacman-scripts'
|
2009-02-25 13:26:31 -05:00
|
|
|
export TEXTDOMAINDIR='@localedir@'
|
|
|
|
|
2011-12-06 16:29:33 -05:00
|
|
|
declare -r myver='@PACKAGE_VERSION@'
|
2009-02-25 13:26:31 -05:00
|
|
|
|
|
|
|
QUIET=0
|
2013-03-04 03:29:50 -05:00
|
|
|
USE_COLOR='y'
|
2009-02-25 13:26:31 -05:00
|
|
|
|
2012-04-06 17:37:32 -04:00
|
|
|
# minimal of package before deltas are generated (bytes)
|
|
|
|
min_pkg_size=$((1024*1024))
|
|
|
|
|
|
|
|
# percent of new package above which the delta will be discarded
|
|
|
|
max_delta_size=70
|
|
|
|
|
|
|
|
|
2009-02-25 13:26:31 -05:00
|
|
|
# ensure we have a sane umask set
|
|
|
|
umask 0022
|
|
|
|
|
2012-04-25 22:00:00 -04:00
|
|
|
m4_include(library/parseopts.sh)
|
2011-06-09 06:43:25 -04:00
|
|
|
m4_include(library/output_format.sh)
|
2009-02-25 13:26:31 -05:00
|
|
|
|
|
|
|
# print usage instructions
|
|
|
|
usage() {
|
2013-11-08 02:53:33 -05:00
|
|
|
printf "pkgdelta (pacman) %s\n" "${myver}"
|
|
|
|
echo
|
2012-04-06 14:51:17 -04:00
|
|
|
printf -- "$(gettext "Usage: pkgdelta [options] <package1> <package2>\n")"
|
2013-11-08 02:53:33 -05:00
|
|
|
echo
|
2012-01-23 17:14:25 -05:00
|
|
|
printf -- "$(gettext "\
|
2013-11-08 02:53:33 -05:00
|
|
|
pkgdelta will create a delta file between two packages.\n\
|
|
|
|
This delta file can then be added to a database using repo-add.\n")"
|
|
|
|
echo
|
2012-01-23 17:14:25 -05:00
|
|
|
printf -- "$(gettext "Example: pkgdelta pacman-3.0.0.pkg.tar.gz pacman-3.0.1.pkg.tar.gz")\n"
|
2012-04-06 14:51:17 -04:00
|
|
|
echo
|
|
|
|
printf -- "$(gettext "Options:\n")"
|
2012-05-20 12:27:36 -04:00
|
|
|
printf -- "$(gettext " -q, --quiet minimize output\n")"
|
2013-03-04 03:29:50 -05:00
|
|
|
printf -- "$(gettext " --nocolor remove color from output\n")"
|
2012-04-25 22:28:39 -04:00
|
|
|
printf -- "$(gettext " --min-pkg-size minimum package size before deltas are generated\n")"
|
|
|
|
printf -- "$(gettext " --max-delta-size percent of new package above which the delta will be discarded\n")"
|
2009-02-25 13:26:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
version() {
|
2013-11-08 02:53:33 -05:00
|
|
|
printf "pkgdelta (pacman) %s\n" "$myver"
|
2012-01-23 17:14:25 -05:00
|
|
|
printf -- "$(gettext "\
|
2009-02-25 13:26:31 -05:00
|
|
|
Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>.\n\n\
|
|
|
|
This is free software; see the source for copying conditions.\n\
|
|
|
|
There is NO WARRANTY, to the extent permitted by law.\n")"
|
|
|
|
}
|
|
|
|
|
2012-04-25 22:28:39 -04:00
|
|
|
m4_include(library/human_to_size.sh)
|
|
|
|
|
2012-04-06 17:37:32 -04:00
|
|
|
isnumeric() {
|
|
|
|
[[ $1 != *[!0-9]* ]]
|
|
|
|
}
|
|
|
|
|
2012-11-25 16:00:58 -05:00
|
|
|
read_pkginfo() {
|
2013-03-09 22:45:00 -05:00
|
|
|
unset pkgver pkgname arch
|
2012-11-25 16:00:58 -05:00
|
|
|
while IFS='=' read -r field value; do
|
|
|
|
# skip comments and invalid lines
|
|
|
|
[[ $field = '#'* || -z $value ]] && continue
|
|
|
|
|
|
|
|
# skip lines which aren't fields we care about
|
|
|
|
[[ $field != @(pkgver|pkgname|arch) ]] || continue
|
|
|
|
|
2013-03-09 22:45:00 -05:00
|
|
|
declare -g "${field% }=${value# }"
|
2012-11-25 16:00:58 -05:00
|
|
|
|
|
|
|
[[ $pkgname && $pkgver && $arch ]] && return 0
|
2013-03-09 22:45:00 -05:00
|
|
|
done < <(bsdtar -xOqf "$1" .PKGINFO 2>/dev/null)
|
2012-11-25 16:00:58 -05:00
|
|
|
|
2011-02-28 18:50:23 -05:00
|
|
|
error "$(gettext "Invalid package file '%s'.")" "$1"
|
2009-02-25 13:26:31 -05:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize
|
|
|
|
create_xdelta()
|
|
|
|
{
|
|
|
|
local oldfile=$1
|
|
|
|
local newfile=$2
|
|
|
|
local \
|
|
|
|
oldname oldver oldarch \
|
|
|
|
newname newver newarch \
|
|
|
|
deltafile
|
|
|
|
|
|
|
|
read_pkginfo "$oldfile" || return 1
|
|
|
|
oldname="$pkgname"
|
|
|
|
oldver="$pkgver"
|
|
|
|
oldarch="$arch"
|
|
|
|
read_pkginfo "$newfile" || return 1
|
|
|
|
newname="$pkgname"
|
|
|
|
newver="$pkgver"
|
|
|
|
newarch="$arch"
|
|
|
|
|
2012-04-06 17:37:32 -04:00
|
|
|
pkgsize="$(@SIZECMD@ -L "$newfile")"
|
|
|
|
|
|
|
|
if ((pkgsize < min_pkg_size)); then
|
|
|
|
msg "$(gettext "Skipping delta creation for small package: %s - size %s")" "$newname" "$pkgsize"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
2011-09-28 15:05:03 -04:00
|
|
|
if [[ $oldname != "$newname" ]]; then
|
2009-02-25 13:26:31 -05:00
|
|
|
error "$(gettext "The package names don't match : '%s' and '%s'")" "$oldname" "$newname"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2011-09-28 15:05:03 -04:00
|
|
|
if [[ $oldarch != "$newarch" ]]; then
|
2009-02-25 13:26:31 -05:00
|
|
|
error "$(gettext "The package architectures don't match : '%s' and '%s'")" "$oldarch" "$newarch"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2011-09-28 15:05:03 -04:00
|
|
|
if [[ $oldver == "$newver" ]]; then
|
2009-02-25 13:26:31 -05:00
|
|
|
error "$(gettext "Both packages have the same version : '%s'")" "$newver"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
msg "$(gettext "Generating delta from version %s to version %s")" "$oldver" "$newver"
|
2012-04-07 08:18:28 -04:00
|
|
|
deltafile=$(dirname "$newfile")/$pkgname-${oldver}_to_${newver}-$arch.delta
|
2009-02-25 13:26:31 -05:00
|
|
|
local ret=0
|
|
|
|
|
2014-12-30 10:14:08 -05:00
|
|
|
xdelta3 -q -f -9 -S lzma -s "$oldfile" "$newfile" "$deltafile" || ret=$?
|
2009-11-05 10:55:48 -05:00
|
|
|
if (( ret )); then
|
2009-02-25 13:26:31 -05:00
|
|
|
error "$(gettext "Delta could not be created.")"
|
|
|
|
return 1
|
|
|
|
fi
|
2012-04-06 17:37:32 -04:00
|
|
|
|
|
|
|
deltasize="$(@SIZECMD@ -L "$deltafile")"
|
|
|
|
|
|
|
|
if ((max_delta_size * pkgsize / 100 < deltasize)); then
|
|
|
|
msg "$(gettext "Delta package larger than maximum size. Removing.")"
|
|
|
|
rm -f "$deltafile"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
msg "$(gettext "Generated delta : '%s'")" "$deltafile"
|
|
|
|
(( QUIET )) && echo "$deltafile"
|
|
|
|
|
2009-02-25 13:26:31 -05:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2012-04-25 22:00:00 -04:00
|
|
|
OPT_SHORT='hqV'
|
2013-03-04 03:29:50 -05:00
|
|
|
OPT_LONG=('help' 'quiet' 'max-delta-size:' 'min-pkg-size:' 'nocolor' 'version')
|
2012-04-25 22:00:00 -04:00
|
|
|
if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
set -- "${OPTRET[@]}"
|
|
|
|
unset OPT_SHORT OPT_LONG OPTRET
|
2012-04-06 14:51:17 -04:00
|
|
|
|
|
|
|
# parse arguments
|
2012-04-25 22:00:00 -04:00
|
|
|
while :; do
|
|
|
|
case $1 in
|
|
|
|
-h|--help)
|
|
|
|
usage
|
|
|
|
exit 0 ;;
|
|
|
|
-V|--version)
|
|
|
|
version
|
|
|
|
exit 0 ;;
|
|
|
|
-q|--quiet)
|
|
|
|
QUIET=1;;
|
2013-03-04 03:29:50 -05:00
|
|
|
--nocolor)
|
|
|
|
USE_COLOR='n';;
|
2012-04-06 17:37:32 -04:00
|
|
|
--min-pkg-size)
|
2012-04-25 22:28:39 -04:00
|
|
|
if ! min_pkg_size=$(human_to_size "$2"); then
|
2012-04-06 17:37:32 -04:00
|
|
|
echo "invalid argument '$2' for option -- '$1'"
|
|
|
|
exit 1
|
|
|
|
fi
|
2012-04-25 22:00:00 -04:00
|
|
|
shift ;;
|
2012-04-06 17:37:32 -04:00
|
|
|
--max-delta-size)
|
2012-04-25 22:28:39 -04:00
|
|
|
arg=$(awk -v val="$2" 'BEGIN { print val * 100 }')
|
|
|
|
if ! isnumeric "$arg" || (( arg > 200 )); then
|
2012-04-06 17:37:32 -04:00
|
|
|
echo "invalid argument '$2' for option -- '$1'"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
max_delta_size=$arg
|
2012-04-25 22:00:00 -04:00
|
|
|
shift ;;
|
|
|
|
--)
|
2012-04-06 17:37:32 -04:00
|
|
|
shift
|
2012-04-25 22:00:00 -04:00
|
|
|
break 2 ;;
|
2012-04-06 14:51:17 -04:00
|
|
|
esac
|
|
|
|
shift
|
|
|
|
done
|
|
|
|
|
2013-03-04 03:29:50 -05:00
|
|
|
m4_include(library/term_colors.sh)
|
|
|
|
|
2012-04-25 22:00:00 -04:00
|
|
|
if (( $# != 2 )); then
|
2009-02-25 13:26:31 -05:00
|
|
|
usage
|
2011-07-28 13:08:37 -04:00
|
|
|
exit 1
|
2009-02-25 13:26:31 -05:00
|
|
|
fi
|
|
|
|
|
2012-04-25 22:00:00 -04:00
|
|
|
for i in "$@"; do
|
2012-04-06 14:51:17 -04:00
|
|
|
if [[ ! -f $i ]]; then
|
|
|
|
error "$(gettext "File '%s' does not exist")" "$i"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
done
|
2009-02-25 13:26:31 -05:00
|
|
|
|
2009-11-05 10:55:48 -05:00
|
|
|
if ! type xdelta3 &>/dev/null; then
|
2009-02-25 13:26:31 -05:00
|
|
|
error "$(gettext "Cannot find the xdelta3 binary! Is xdelta3 installed?")"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2012-08-16 22:59:46 -04:00
|
|
|
create_xdelta "$@"
|
2011-06-08 02:44:38 -04:00
|
|
|
|
2014-01-22 18:06:11 -05:00
|
|
|
# vim: set noet:
|