makepkg: reorder source handling functions
[pacman-ng.git] / scripts / pkgdelta.sh.in
blob3d80261ff0f511ebc2aff43cbf0ecb239d25901b
1 #!/bin/bash
3 # pkgdelta - create delta files for use with pacman and repo-add
4 # @configure_input@
6 # Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # bash options
23 set -o errexit
25 # gettext initialization
26 export TEXTDOMAIN='pacman-scripts'
27 export TEXTDOMAINDIR='@localedir@'
29 declare -r myver='@PACKAGE_VERSION@'
31 QUIET=0
33 # minimal of package before deltas are generated (bytes)
34 min_pkg_size=$((1024*1024))
36 # percent of new package above which the delta will be discarded
37 max_delta_size=70
40 # ensure we have a sane umask set
41 umask 0022
43 m4_include(library/parseopts.sh)
44 m4_include(library/output_format.sh)
46 # print usage instructions
47 usage() {
48 printf "pkgdelta (pacman) %s\n\n" "$myver"
49 printf -- "$(gettext "Usage: pkgdelta [options] <package1> <package2>\n")"
50 printf -- "$(gettext "\
51 pkgdelta will create a delta file between two packages.\n\
52 This delta file can then be added to a database using repo-add.\n\n")"
53 printf -- "$(gettext "Example: pkgdelta pacman-3.0.0.pkg.tar.gz pacman-3.0.1.pkg.tar.gz")\n"
54 echo
55 printf -- "$(gettext "Options:\n")"
56 printf -- "$(gettext " -q, --quiet minimize output\n")"
57 printf -- "$(gettext " --min-pkg-size minimum package size before deltas are generated\n")"
58 printf -- "$(gettext " --max-delta-size percent of new package above which the delta will be discarded\n")"
61 version() {
62 printf "pkgdelta (pacman) %s\n\n" "$myver"
63 printf -- "$(gettext "\
64 Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>.\n\n\
65 This is free software; see the source for copying conditions.\n\
66 There is NO WARRANTY, to the extent permitted by law.\n")"
69 m4_include(library/human_to_size.sh)
71 isnumeric() {
72 [[ $1 != *[!0-9]* ]]
75 read_pkginfo()
77 pkgname= pkgver= arch=
78 local OLDIFS=$IFS
79 # IFS (field separator) is only the newline character
80 IFS="
82 local line var val
83 for line in $(bsdtar -xOqf "$1" .PKGINFO 2>/dev/null |
84 grep -v "^#" | sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
85 eval "$line"
86 if [[ -n $pkgname && -n $pkgver && -n $arch ]]; then
87 IFS=$OLDIFS
88 return 0
90 done
91 IFS=$OLDIFS
92 error "$(gettext "Invalid package file '%s'.")" "$1"
93 return 1
96 # $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize
97 create_xdelta()
99 local oldfile=$1
100 local newfile=$2
101 local \
102 oldname oldver oldarch \
103 newname newver newarch \
104 deltafile
106 read_pkginfo "$oldfile" || return 1
107 oldname="$pkgname"
108 oldver="$pkgver"
109 oldarch="$arch"
110 read_pkginfo "$newfile" || return 1
111 newname="$pkgname"
112 newver="$pkgver"
113 newarch="$arch"
115 pkgsize="$(@SIZECMD@ -L "$newfile")"
117 if ((pkgsize < min_pkg_size)); then
118 msg "$(gettext "Skipping delta creation for small package: %s - size %s")" "$newname" "$pkgsize"
119 return 0
122 if [[ $oldname != "$newname" ]]; then
123 error "$(gettext "The package names don't match : '%s' and '%s'")" "$oldname" "$newname"
124 return 1
127 if [[ $oldarch != "$newarch" ]]; then
128 error "$(gettext "The package architectures don't match : '%s' and '%s'")" "$oldarch" "$newarch"
129 return 1
132 if [[ $oldver == "$newver" ]]; then
133 error "$(gettext "Both packages have the same version : '%s'")" "$newver"
134 return 1
137 msg "$(gettext "Generating delta from version %s to version %s")" "$oldver" "$newver"
138 deltafile=$(dirname "$newfile")/$pkgname-${oldver}_to_${newver}-$arch.delta
139 local ret=0
141 xdelta3 -q -f -s "$oldfile" "$newfile" "$deltafile" || ret=$?
142 if (( ret )); then
143 error "$(gettext "Delta could not be created.")"
144 return 1
147 deltasize="$(@SIZECMD@ -L "$deltafile")"
149 if ((max_delta_size * pkgsize / 100 < deltasize)); then
150 msg "$(gettext "Delta package larger than maximum size. Removing.")"
151 rm -f "$deltafile"
152 return 0
155 msg "$(gettext "Generated delta : '%s'")" "$deltafile"
156 (( QUIET )) && echo "$deltafile"
158 return 0
161 OPT_SHORT='hqV'
162 OPT_LONG=('help' 'quiet' 'max-delta-size:' 'min-pkg-size:' 'version')
163 if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
164 exit 1
166 set -- "${OPTRET[@]}"
167 unset OPT_SHORT OPT_LONG OPTRET
169 # parse arguments
170 while :; do
171 case $1 in
172 -h|--help)
173 usage
174 exit 0 ;;
175 -V|--version)
176 version
177 exit 0 ;;
178 -q|--quiet)
179 QUIET=1;;
180 --min-pkg-size)
181 if ! min_pkg_size=$(human_to_size "$2"); then
182 echo "invalid argument '$2' for option -- '$1'"
183 exit 1
185 shift ;;
186 --max-delta-size)
187 arg=$(awk -v val="$2" 'BEGIN { print val * 100 }')
188 if ! isnumeric "$arg" || (( arg > 200 )); then
189 echo "invalid argument '$2' for option -- '$1'"
190 exit 1
192 max_delta_size=$arg
193 shift ;;
195 shift
196 break 2 ;;
197 esac
198 shift
199 done
201 if (( $# != 2 )); then
202 usage
203 exit 1
206 for i in "$@"; do
207 if [[ ! -f $i ]]; then
208 error "$(gettext "File '%s' does not exist")" "$i"
209 exit 1
211 done
213 if ! type xdelta3 &>/dev/null; then
214 error "$(gettext "Cannot find the xdelta3 binary! Is xdelta3 installed?")"
215 exit 1
218 create_xdelta "${args[@]}"
220 # vim: set ts=2 sw=2 noet: