alpm: alpm_db_get_pkgcache_list => alpm_db_get_pkgcache
[pacman-ng.git] / scripts / pkgdelta.sh.in
blob6bc3f5da8307f05a66327239dc75e21d4c3d1bcc
1 #!@BASH_SHELL@
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 nounset
24 set -o errexit
26 # gettext initialization
27 export TEXTDOMAIN='pacman'
28 export TEXTDOMAINDIR='@localedir@'
30 myver='@PACKAGE_VERSION@'
32 QUIET=0
34 # ensure we have a sane umask set
35 umask 0022
37 msg() {
38 (( QUIET )) && return
39 local mesg=$1; shift
40 printf "==> ${mesg}\n" "$@" >&1
43 warning() {
44 local mesg=$1; shift
45 printf "==> $(gettext "WARNING:") ${mesg}\n" "$@" >&2
48 error() {
49 local mesg=$1; shift
50 printf "==> $(gettext "ERROR:") ${mesg}\n" "$@" >&2
53 # print usage instructions
54 usage() {
55 printf "pkgdelta (pacman) %s\n\n" "$myver"
56 printf "$(gettext "Usage: pkgdelta [-q] <package1> <package2>\n")"
57 printf "$(gettext "\
58 pkgdelta will create a delta file between two packages\n\
59 This delta file can then be added to a database using repo-add.\n\n")"
60 echo "$(gettext "Example: pkgdelta pacman-3.0.0.pkg.tar.gz pacman-3.0.1.pkg.tar.gz")"
63 version() {
64 printf "pkgdelta (pacman) %s\n\n" "$myver"
65 printf "$(gettext "\
66 Copyright (c) 2009 Xavier Chantry <shiningxc@gmail.com>.\n\n\
67 This is free software; see the source for copying conditions.\n\
68 There is NO WARRANTY, to the extent permitted by law.\n")"
71 read_pkginfo()
73 pkgname= pkgver= arch=
74 local OLDIFS=$IFS
75 # IFS (field separator) is only the newline character
76 IFS="
78 local line var val
79 for line in $(bsdtar -xOf "$1" .PKGINFO 2>/dev/null |
80 grep -v "^#" | sed 's|\(\w*\)\s*=\s*\(.*\)|\1="\2"|'); do
81 eval "$line"
82 if [[ -n $pkgname && -n $pkgver && -n $arch ]]; then
83 IFS=$OLDIFS
84 return 0
86 done
87 IFS=$OLDIFS
88 error "$(gettext "Invalid package '%s'")" "$1"
89 return 1
92 # $oldfile $oldmd5 $newfile $newmd5 $deltafile $deltamd5 $deltasize
93 create_xdelta()
95 local oldfile=$1
96 local newfile=$2
97 local \
98 oldname oldver oldarch \
99 newname newver newarch \
100 deltafile
102 read_pkginfo "$oldfile" || return 1
103 oldname="$pkgname"
104 oldver="$pkgver"
105 oldarch="$arch"
106 read_pkginfo "$newfile" || return 1
107 newname="$pkgname"
108 newver="$pkgver"
109 newarch="$arch"
111 if [[ $oldname != $newname ]]; then
112 error "$(gettext "The package names don't match : '%s' and '%s'")" "$oldname" "$newname"
113 return 1
116 if [[ $oldarch != $newarch ]]; then
117 error "$(gettext "The package architectures don't match : '%s' and '%s'")" "$oldarch" "$newarch"
118 return 1
121 if [[ $oldver == $newver ]]; then
122 error "$(gettext "Both packages have the same version : '%s'")" "$newver"
123 return 1
126 msg "$(gettext "Generating delta from version %s to version %s")" "$oldver" "$newver"
127 deltafile="$(dirname $newfile)/$pkgname-${oldver}_to_${newver}-$arch.delta"
128 local ret=0
130 xdelta3 -q -f -s "$oldfile" "$newfile" "$deltafile" || ret=$?
131 if (( ret )); then
132 error "$(gettext "Delta could not be created.")"
133 return 1
134 else
135 msg "$(gettext "Generated delta : '%s'")" "$deltafile"
136 (( QUIET )) && echo "$deltafile"
138 return 0
141 case "$1" in
142 -q|--quiet) QUIET=1; shift ;;
143 esac
145 if (( $# != 2 )); then
146 usage
147 exit 0
150 if [[ ! -f $1 ]]; then
151 error "$(gettext "File '%s' does not exist")" "$1"
152 exit 0
155 if [[ ! -f $2 ]]; then
156 error "$(gettext "File '%s' does not exist")" "$2"
157 exit 0
160 if ! type xdelta3 &>/dev/null; then
161 error "$(gettext "Cannot find the xdelta3 binary! Is xdelta3 installed?")"
162 exit 1
165 create_xdelta "$1" "$2"