pacman-key: test for content in keyring files
[pacman-ng.git] / scripts / pkgdelta.sh.in
blobae0bfc380f0ddf8cfd537e1f92120b928e4db466
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 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/output_format.sh)
45 # print usage instructions
46 usage() {
47 printf "pkgdelta (pacman) %s\n\n" "$myver"
48 printf -- "$(gettext "Usage: pkgdelta [options] <package1> <package2>\n")"
49 printf -- "$(gettext "\
50 pkgdelta will create a delta file between two packages.\n\
51 This delta file can then be added to a database using repo-add.\n\n")"
52 printf -- "$(gettext "Example: pkgdelta pacman-3.0.0.pkg.tar.gz pacman-3.0.1.pkg.tar.gz")\n"
53 echo
54 printf -- "$(gettext "Options:\n")"
55 printf -- "$(gettext " -q, --quiet minimize output\n")"
56 printf -- "$(gettext " --min-pkg-size minimum package size before deltas are generated (bytes)\n")"
57 printf -- "$(gettext " --max-delta-size percent of package size above which deltas will be discarded\n")"
60 version() {
61 printf "pkgdelta (pacman) %s\n\n" "$myver"
62 printf -- "$(gettext "\
63 Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>.\n\n\
64 This is free software; see the source for copying conditions.\n\
65 There is NO WARRANTY, to the extent permitted by law.\n")"
68 isnumeric() {
69 [[ $1 != *[!0-9]* ]]
72 read_pkginfo()
74 pkgname= pkgver= arch=
75 local OLDIFS=$IFS
76 # IFS (field separator) is only the newline character
77 IFS="
79 local line var val
80 for line in $(bsdtar -xOqf "$1" .PKGINFO 2>/dev/null |
81 grep -v "^#" | sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
82 eval "$line"
83 if [[ -n $pkgname && -n $pkgver && -n $arch ]]; then
84 IFS=$OLDIFS
85 return 0
87 done
88 IFS=$OLDIFS
89 error "$(gettext "Invalid package file '%s'.")" "$1"
90 return 1
93 # $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize
94 create_xdelta()
96 local oldfile=$1
97 local newfile=$2
98 local \
99 oldname oldver oldarch \
100 newname newver newarch \
101 deltafile
103 read_pkginfo "$oldfile" || return 1
104 oldname="$pkgname"
105 oldver="$pkgver"
106 oldarch="$arch"
107 read_pkginfo "$newfile" || return 1
108 newname="$pkgname"
109 newver="$pkgver"
110 newarch="$arch"
112 pkgsize="$(@SIZECMD@ -L "$newfile")"
114 if ((pkgsize < min_pkg_size)); then
115 msg "$(gettext "Skipping delta creation for small package: %s - size %s")" "$newname" "$pkgsize"
116 return 0
119 if [[ $oldname != "$newname" ]]; then
120 error "$(gettext "The package names don't match : '%s' and '%s'")" "$oldname" "$newname"
121 return 1
124 if [[ $oldarch != "$newarch" ]]; then
125 error "$(gettext "The package architectures don't match : '%s' and '%s'")" "$oldarch" "$newarch"
126 return 1
129 if [[ $oldver == "$newver" ]]; then
130 error "$(gettext "Both packages have the same version : '%s'")" "$newver"
131 return 1
134 msg "$(gettext "Generating delta from version %s to version %s")" "$oldver" "$newver"
135 deltafile=$(dirname "$newfile")/$pkgname-${oldver}_to_${newver}-$arch.delta
136 local ret=0
138 xdelta3 -q -f -s "$oldfile" "$newfile" "$deltafile" || ret=$?
139 if (( ret )); then
140 error "$(gettext "Delta could not be created.")"
141 return 1
144 deltasize="$(@SIZECMD@ -L "$deltafile")"
146 if ((max_delta_size * pkgsize / 100 < deltasize)); then
147 msg "$(gettext "Delta package larger than maximum size. Removing.")"
148 rm -f "$deltafile"
149 return 0
152 msg "$(gettext "Generated delta : '%s'")" "$deltafile"
153 (( QUIET )) && echo "$deltafile"
155 return 0
158 declare -a args
160 # parse arguments
161 while (( $# )); do
162 case "$1" in
163 -h|--help) usage; exit 0 ;;
164 -V|--version) version; exit 0 ;;
165 -q|--quiet) QUIET=1;;
166 --min-pkg-size)
167 if ! isnumeric "$2"; then
168 echo "invalid argument '$2' for option -- '$1'"
169 exit 1
171 min_pkg_size=$2
172 shift
174 --max-delta-size)
175 arg=$(echo "$2" | awk '{print $1 * 100}')
176 if ! isnumeric "$arg" || (($arg > 200)); then
177 echo "invalid argument '$2' for option -- '$1'"
178 exit 1
180 max_delta_size=$arg
181 shift
183 --) shift; args+=("$@"); break 2 ;;
184 -*) echo "invalid option -- '$1'"; usage; exit 1 ;;
185 *) args+=("$1");;
186 esac
187 shift
188 done
190 if (( ${#args[@]} != 2 )); then
191 usage
192 exit 1
195 for i in "${args[@]}"; do
196 if [[ ! -f $i ]]; then
197 error "$(gettext "File '%s' does not exist")" "$i"
198 exit 1
200 done
202 if ! type xdelta3 &>/dev/null; then
203 error "$(gettext "Cannot find the xdelta3 binary! Is xdelta3 installed?")"
204 exit 1
207 create_xdelta "${args[@]}"
209 # vim: set ts=2 sw=2 noet: