3 # repo-add - add a package to a given repo database file
4 # repo-remove - remove a package entry from a given repo database file
7 # Copyright (c) 2006-2012 Pacman Development Team <pacman-dev@archlinux.org>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 # gettext initialization
25 export TEXTDOMAIN
='pacman-scripts'
26 export TEXTDOMAINDIR
='@localedir@'
28 declare -r myver
='@PACKAGE_VERSION@'
29 declare -r confdir
='@sysconfdir@'
40 # ensure we have a sane umask set
43 m4_include
(library
/output_format.sh
)
45 # print usage instructions
48 printf -- "%s (pacman) %s\n\n" "$cmd" "$myver"
49 if [[ $cmd == "repo-add" ]] ; then
50 printf -- "$(gettext "Usage
: repo-add
[options
] <path-to-db
> <package|delta
> ...
\n")"
52 printf -- "$(gettext "\
53 repo-add will update a package database by reading a package
file.
\n\
54 Multiple packages to add can be specified on the
command line.
\n")"
56 printf -- "$(gettext "Options
:\n")"
57 printf -- "$(gettext " -d, --delta generate and add delta
for package update
\n")"
58 printf -- "$(gettext " -f, --files update database
's file list\n")"
59 elif [[ $cmd == "repo-remove" ]] ; then
60 printf -- "$(gettext "Usage: repo-remove [options] <path-to-db> <packagename|delta> ...\n")"
62 printf -- "$(gettext "\
63 repo-remove will update a package database by removing the package name\n\
64 specified on the command line from the given repo database. Multiple\n\
65 packages to remove can be specified on the command line.\n")"
67 printf -- "$(gettext "Options:\n")"
69 printf -- "$(gettext "Please move along, there is nothing to see here.\n")"
72 printf -- "$(gettext " -q, --quiet minimize output\n")"
73 printf -- "$(gettext " -s, --sign sign database with GnuPG after update\n")"
74 printf -- "$(gettext " -k, --key <key> use the specified key to sign the database\n")"
75 printf -- "$(gettext " -v, --verify verify database's signature before update
\n")"
76 printf -- "$(gettext "\n\
77 See
%s
(8) for more details and descriptions of the available options.
\n")" $cmd
79 if [[ $cmd == "repo-add" ]] ; then
80 printf -- "$(gettext "Example
: repo-add
/path
/to
/repo.db.
tar.gz pacman-3.0
.0-1-i686.pkg.
tar.gz
\n")"
81 elif [[ $cmd == "repo-remove" ]] ; then
82 printf -- "$(gettext "Example
: repo-remove
/path
/to
/repo.db.
tar.gz kernel26
\n")"
88 printf "%s (pacman) %s\n\n" "$cmd" "$myver"
89 printf -- "$(gettext "\
90 Copyright
(c
) 2006-2012 Pacman Development Team
<pacman-dev@archlinux.org
>\n\n\
91 This is free software
; see the
source for copying conditions.
\n\
92 There is NO WARRANTY
, to the extent permitted by law.
\n")"
95 # format a metadata entry
102 printf '%%%s%%\n' "$field"
111 for pkgentry
in $tmpdir/tree
/$pkgname*; do
113 if [[ ${name%-*-*} = $pkgname ]]; then
121 # Get the package name from the delta filename
122 get_delta_pkgname
() {
129 # write a delta entry
130 # arg1 - path to delta file
133 pkgname
="$(get_delta_pkgname $deltafile)"
135 pkgentry
=$
(find_pkgentry
$pkgname)
136 if [[ -z $pkgentry ]]; then
137 error
"$(gettext "No database entry
for package
'%s'.
")" "$pkgname"
140 deltas
="$pkgentry/deltas"
141 if [[ ! -f $deltas ]]; then
142 echo -e "%DELTAS%" >$deltas
144 # get md5sum and compressed size of package
145 md5sum="$(openssl dgst -md5 "$deltafile")"
146 md5sum="${md5sum##* }"
147 csize
=$
(@SIZECMD@
-L "$deltafile")
149 oldfile
=$
(xdelta3 printhdr
$deltafile |
grep "XDELTA filename (source)" |
sed 's/.*: *//')
150 newfile
=$
(xdelta3 printhdr
$deltafile |
grep "XDELTA filename (output)" |
sed 's/.*: *//')
152 if grep -q "$oldfile.*$newfile" $deltas; then
153 sed -i.backup
"/$oldfile.*$newfile/d" $deltas && rm -f $deltas.backup
155 msg2
"$(gettext "Adding
'deltas' entry
: %s
-> %s
")" "$oldfile" "$newfile"
156 echo ${deltafile##*/} $md5sum $csize $oldfile $newfile >> $deltas
159 } # end db_write_delta
161 # remove a delta entry
162 # arg1 - path to delta file
165 filename
=${deltafile##*/}
166 pkgname
="$(get_delta_pkgname $deltafile)"
168 pkgentry
=$
(find_pkgentry
$pkgname)
169 if [[ -z $pkgentry ]]; then
172 deltas
="$pkgentry/deltas"
173 if [[ ! -f $deltas ]]; then
176 if grep -q "$filename" $deltas; then
177 sed -i.backup
"/$filename/d" $deltas && rm -f $deltas.backup
178 msg2
"$(gettext "Removing existing entry
'%s'...
")" "$filename"
179 # empty deltas file contains only "%DELTAS%"
180 if (( $
(wc -l < "$deltas") == 1 )); then
181 msg2
"$(gettext "Removing empty deltas
file ...
")"
188 } # end db_remove_delta
191 if ! type -p gpg
>/dev
/null
; then
192 error
"$(gettext "Cannot
find the gpg binary
! Is GnuPG installed?
")"
193 exit 1 # $E_MISSING_PROGRAM
197 # sign the package database once repackaged
199 (( ! SIGN
)) && return
202 msg
"$(gettext "Signing database...
")"
205 if [[ -n $GPGKEY ]]; then
206 SIGNWITHKEY
="-u ${GPGKEY}"
208 gpg
--detach-sign --use-agent ${SIGNWITHKEY} "$dbfile" &>/dev
/null || ret
=$?
211 msg2
"$(gettext "Created signature
file %s.
")" "${dbfile##*/.tmp.}.sig"
213 warning
"$(gettext "Failed to sign package database.
")"
217 # verify the existing package database signature
219 (( ! VERIFY
)) && return
222 msg
"$(gettext "Verifying database signature...
")"
224 if [[ ! -f $dbfile.sig
]]; then
225 warning
"$(gettext "No existing signature found
, skipping verification.
")"
228 gpg
--verify "$dbfile.sig" || ret
=$?
230 msg2
"$(gettext "Database signature
file verified.
")"
232 error
"$(gettext "Database signature was NOT valid
!")"
237 verify_repo_extension
() {
241 *.@
(db|files
).
tar.gz
) TAR_OPT
="z" ;;
242 *.@
(db|files
).
tar.bz2
) TAR_OPT
="j" ;;
243 *.@
(db|files
).
tar.xz
) TAR_OPT
="J" ;;
244 *.@
(db|files
).
tar.Z
) TAR_OPT
="Z" ;;
245 *.@
(db|files
).
tar) TAR_OPT
="" ;;
246 *) error
"$(gettext "'%s' does not have a valid archive extension.
")" \
251 printf '%s' "$TAR_OPT"
254 # write an entry to the pacman database
255 # arg1 - path to package
257 # blank out all variables
259 local -a _groups _licenses _replaces _depends _conflicts _provides _optdepends
260 local pkgname pkgver pkgdesc csize size url arch builddate packager \
261 md5sum sha256sum pgpsig pgpsigsize
263 # read info from the zipped package
265 while read -r line
; do
266 [[ ${line:0:1} = '#' ]] && continue
267 IFS
=' =' read -r var val
< <(printf '%s\n' "$line")
269 # normalize whitespace with an extglob
270 declare "$var=${val//+([[:space:]])/ }"
272 group
) _groups
+=("$group") ;;
273 license
) _licenses
+=("$license") ;;
274 replaces
) _replaces
+=("$replaces") ;;
275 depend
) _depends
+=("$depend") ;;
276 conflict
) _conflicts
+=("$conflict") ;;
277 provides
) _provides
+=("$provides") ;;
278 optdepend
) _optdepends
+=("$optdepend") ;;
280 done< <(bsdtar
-xOqf "$pkgfile" .PKGINFO
)
282 # ensure $pkgname and $pkgver variables were found
283 if [[ -z $pkgname ||
-z $pkgver ]]; then
284 error
"$(gettext "Invalid package
file '%s'.
")" "$pkgfile"
288 if [[ -d $tmpdir/tree
/$pkgname-$pkgver ]]; then
289 warning
"$(gettext "An entry
for '%s' already existed
")" "$pkgname-$pkgver"
292 pkgentry
=$
(find_pkgentry
$pkgname)
293 if [[ -n $pkgentry ]]; then
294 local oldfilename
=$
(grep -A1 FILENAME
$pkgentry/desc |
tail -n1)
295 local oldfile
="$(dirname $1)/$oldfilename"
300 # compute base64'd PGP signature
301 if [[ -f "$pkgfile.sig" ]]; then
302 pgpsigsize
=$
(@SIZECMD@
-L "$pkgfile.sig")
303 if (( pgpsigsize
> 16384 )); then
304 error
"$(gettext "Invalid package signature
file '%s'.
")" "$pkgfile.sig"
307 msg2
"$(gettext "Adding package signature...
")"
308 pgpsig
=$
(openssl base64
-in "$pkgfile.sig" |
tr -d '\n')
311 csize
=$
(@SIZECMD@
-L "$pkgfile")
314 msg2
"$(gettext "Computing checksums...
")"
315 md5sum="$(openssl dgst -md5 "$pkgfile")"
316 md5sum="${md5sum##* }"
317 sha256sum
="$(openssl dgst -sha256 "$pkgfile")"
318 sha256sum
="${sha256sum##* }"
320 # remove an existing entry if it exists, ignore failures
321 db_remove_entry
"$pkgname"
323 # create package directory
324 pushd "$tmpdir/tree" >/dev
/null
325 mkdir
"$pkgname-$pkgver"
326 pushd "$pkgname-$pkgver" >/dev
/null
328 # restore an eventual deltas file
329 [[ -f ..
/$pkgname.deltas
]] && mv "../$pkgname.deltas" deltas
332 msg2
"$(gettext "Creating
'%s' db entry...
")" 'desc'
334 format_entry
"FILENAME" "${1##*/}"
335 format_entry
"NAME" "$pkgname"
336 format_entry
"BASE" "$pkgbase"
337 format_entry
"VERSION" "$pkgver"
338 format_entry
"DESC" "$pkgdesc"
339 format_entry
"GROUPS" "${_groups[@]}"
340 format_entry
"CSIZE" "$csize"
341 format_entry
"ISIZE" "$size"
344 format_entry
"MD5SUM" "$md5sum"
345 format_entry
"SHA256SUM" "$sha256sum"
348 format_entry
"PGPSIG" "$pgpsig"
350 format_entry
"URL" "$url"
351 format_entry
"LICENSE" "${_licenses[@]}"
352 format_entry
"ARCH" "$arch"
353 format_entry
"BUILDDATE" "$builddate"
354 format_entry
"PACKAGER" "$packager"
355 format_entry
"REPLACES" "${_replaces[@]}"
358 # create depends entry
359 msg2
"$(gettext "Creating
'%s' db entry...
")" 'depends'
361 format_entry
"DEPENDS" "${_depends[@]}"
362 format_entry
"CONFLICTS" "${_conflicts[@]}"
363 format_entry
"PROVIDES" "${_provides[@]}"
364 format_entry
"OPTDEPENDS" "${_optdepends[@]}"
370 # create files file if wanted
371 if (( WITHFILES
)); then
372 msg2
"$(gettext "Creating
'%s' db entry...
")" 'files'
373 local files_path
="$tmpdir/tree/$pkgname-$pkgver/files"
374 echo "%FILES%" >$files_path
375 bsdtar
--exclude='^.*' -tf "$pkgfile" >>$files_path
378 # create a delta file
380 if [[ -n $oldfilename ]]; then
381 if [[ -f $oldfile ]]; then
382 delta
=$
(pkgdelta
-q $oldfile $1)
383 if [[ -f $delta ]]; then
384 db_write_delta
$delta
387 warning
"$(gettext "Old package
file not found
: %s
")" "$oldfilename"
393 } # end db_write_entry
395 # remove existing entries from the DB
396 # arg1 - package name
400 local pkgentry
=$
(find_pkgentry
$pkgname)
401 while [[ -n $pkgentry ]]; do
403 if [[ -f $pkgentry/deltas
]]; then
404 mv "$pkgentry/deltas" "$tmpdir/tree/$pkgname.deltas"
406 msg2
"$(gettext "Removing existing entry
'%s'...
")" \
409 pkgentry
=$
(find_pkgentry
$pkgname)
412 } # end db_remove_entry
415 case $
(( RANDOM
% 2 )) in
416 0) printf '%s\n' "H4sIAL3qBE4CAyWLwQ3AMAgD/0xh5UPzYiFUMgjq7LUJsk7yIQNAQTAikFUDnqkr" \
417 "OQFOUm0Wd9pHCi13ONjBpVdqcWx+EdXVX4vXvGv5cgztB9+fJxZ7AAAA"
420 1) printf '%s\n' "H4sIAJVWBU4CA21RMQ7DIBDbeYWrDgQJ7rZ+IA/IB05l69alcx5fc0ASVXUk4jOO" \
421 "7yAAUWtorygwJ4hlMii0YkJKKRKGvsMsiykl1SalvrMD1gUXyXRkGZPx5OPft81K" \
422 "tNAiAjyGjYO47h1JjizPkJrCWbK/4C+uLkT7bzpGc7CT9bmOzNSW5WLSO5vexjmH" \
423 "ZL9JFFZeAa0a2+lKjL2anpYfV+0Zx9LJ+/MC8nRayuDlSNy2rfAPibOzsiWHL0jL" \
426 esac | openssl base64
-d |
gzip -d
432 # ensure the path to the DB exists; $LOCKFILE is always an absolute path
433 repodir
=${LOCKFILE%/*}/
435 if [[ ! -d "$repodir" ]]; then
436 error
"$(gettext "%s does not exist or is not a directory.
")" "$repodir"
441 if ( set -o noclobber
; echo "$$" > "$LOCKFILE") 2> /dev
/null
; then
444 error
"$(gettext "Failed to acquire lockfile
: %s.
")" "$LOCKFILE"
445 [[ -f $LOCKFILE ]] && error
"$(gettext "Held by process
%s
")" "$(cat $LOCKFILE)"
449 if [[ -f $REPO_DB_FILE ]]; then
450 # there are two situations we can have here- a DB with some entries,
451 # or a DB with no contents at all.
452 if ! bsdtar
-tqf "$REPO_DB_FILE" '*/desc' >/dev
/null
2>&1; then
454 if [[ -n $
(bsdtar
-tqf "$REPO_DB_FILE" '*' 2>/dev
/null
) ]]; then
455 error
"$(gettext "Repository
file '%s' is not a proper pacman database.
")" "$REPO_DB_FILE"
459 verify_signature
"$REPO_DB_FILE"
460 msg
"$(gettext "Extracting database to a temporary location...
")"
461 bsdtar
-xf "$REPO_DB_FILE" -C "$tmpdir/tree"
465 error
"$(gettext "Repository
file '%s' was not found.
")" "$REPO_DB_FILE"
469 # check if the file can be created (write permission, directory existence, etc)
470 if ! touch "$REPO_DB_FILE"; then
471 error
"$(gettext "Repository
file '%s' could not be created.
")" "$REPO_DB_FILE"
474 rm -f "$REPO_DB_FILE"
481 if [[ ! -f $1 ]]; then
482 error
"$(gettext "File
'%s' not found.
")" "$1"
486 if [[ ${1##*.} == "delta" ]]; then
488 msg
"$(gettext "Adding delta
'%s'")" "$deltafile"
489 if ! type xdelta3
&>/dev
/null
; then
490 error
"$(gettext "Cannot
find the xdelta3 binary
! Is xdelta3 installed?
")"
493 if db_write_delta
"$deltafile"; then
501 if ! bsdtar
-tqf "$pkgfile" .PKGINFO
>/dev
/null
2>&1; then
502 error
"$(gettext "'%s' is not a package
file, skipping
")" "$pkgfile"
506 msg
"$(gettext "Adding package
'%s'")" "$pkgfile"
508 db_write_entry
"$pkgfile"
512 if [[ ${1##*.} == "delta" ]]; then
514 msg
"$(gettext "Searching
for delta
'%s'...
")" "$deltafile"
515 if db_remove_delta
"$deltafile"; then
518 error
"$(gettext "Delta matching
'%s' not found.
")" "$deltafile"
524 msg
"$(gettext "Searching
for package
'%s'...
")" "$pkgname"
526 if db_remove_entry
"$pkgname"; then
527 rm -f "$tmpdir/tree/$pkgname.deltas"
530 error
"$(gettext "Package matching
'%s' not found.
")" "$pkgname"
536 # unhook all traps to avoid race conditions
537 trap '' EXIT TERM HUP QUIT INT ERR
545 local exit_code
=${1:-$?}
547 # unhook all traps to avoid race conditions
548 trap '' EXIT TERM HUP QUIT INT ERR
550 [[ -d $tmpdir ]] && rm -rf "$tmpdir"
551 (( CLEAN_LOCK
)) && [[ -f $LOCKFILE ]] && rm -f "$LOCKFILE"
558 # determine whether we have gettext; make it a no-op if we do not
559 if ! type gettext &>/dev
/null
; then
566 -h|
--help) usage
; exit 0;;
567 -V|
--version) version
; exit 0;;
570 # figure out what program we are
572 if [[ $cmd == "repo-elephant" ]]; then
577 if [[ $cmd != "repo-add" && $cmd != "repo-remove" ]]; then
578 error
"$(gettext "Invalid
command name
'%s' specified.
")" "$cmd"
582 tmpdir
=$
(mktemp
-d "${TMPDIR:-/tmp}/repo-tools.XXXXXXXXXX") ||
(\
583 error
"$(gettext "Cannot create temp directory
for database building.
")"; \
588 for signal
in TERM HUP QUIT
; do
589 trap "trap_exit \"$(gettext "%s signal caught. Exiting...
")\" \"$signal\"" "$signal"
591 trap 'trap_exit "$(gettext "Aborted by user! Exiting...")"' INT
592 trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR
599 -q|
--quiet) QUIET
=1;;
600 -d|
--delta) DELTA
=1;;
601 -f|
--files) WITHFILES
=1;;
605 if ! gpg
--list-key ${GPGKEY} &>/dev
/null
; then
606 if [[ ! -z $GPGKEY ]]; then
607 error
"$(gettext "The key
${GPGKEY} does not exist
in your keyring.
")"
609 error
"$(gettext "There is no key
in your keyring.
")"
618 if ! gpg
--list-key ${GPGKEY} &>/dev
/null
; then
619 error
"$(gettext "The key
${GPGKEY} does not exist
in your keyring.
")"
634 REPO_DB_FILE
=${args[0]}
635 if [[ -z $REPO_DB_FILE ]]; then
640 if [[ $REPO_DB_FILE == /* ]]; then
641 LOCKFILE
=$REPO_DB_FILE.lck
643 LOCKFILE
=$PWD/$REPO_DB_FILE.lck
646 verify_repo_extension
"$REPO_DB_FILE" >/dev
/null
649 for arg
in "${args[@]:1}"; do
651 repo-add
) add
"$arg" ;;
652 repo-remove
) remove
"$arg" ;;
656 # if at least one operation was a success, re-zip database
657 if (( success
)); then
658 msg
"$(gettext "Creating updated database
file '%s'")" "$REPO_DB_FILE"
660 TAR_OPT
=$
(verify_repo_extension
"$REPO_DB_FILE")
661 # $LOCKFILE is already guaranteed to be absolute so this is safe
662 dirname=${LOCKFILE%/*}
663 filename
=${REPO_DB_FILE##*/}
664 # this ensures we create it on the same filesystem, making moves atomic
665 tempname
="$dirname/.tmp.$filename"
667 pushd "$tmpdir/tree" >/dev
/null
668 if ( shopt -s nullglob
; files
=(*); (( ${#files[*]} )) ); then
669 bsdtar
-c${TAR_OPT}f
"$tempname" *
671 # we have no packages remaining? zip up some emptyness
672 warning
"$(gettext "No packages remain
, creating empty database.
")"
673 bsdtar
-c${TAR_OPT}f
"$tempname" -T /dev
/null
677 create_signature
"$tempname"
679 # hardlink or move the previous version of the database and signature to .old
680 # extension as a backup measure
681 if [[ -f $REPO_DB_FILE ]]; then
682 ln -f "$REPO_DB_FILE" "$REPO_DB_FILE.old" 2>/dev
/null || \
683 mv -f "$REPO_DB_FILE" "$REPO_DB_FILE.old"
685 if [[ -f $REPO_DB_FILE.sig
]]; then
686 ln -f "$REPO_DB_FILE.sig" "$REPO_DB_FILE.old.sig" 2>/dev
/null || \
687 mv -f "$REPO_DB_FILE.sig" "$REPO_DB_FILE.old.sig"
689 rm -f "$REPO_DB_FILE.old.sig"
692 # rotate the newly-created database and signature into place
693 mv "$tempname" "$REPO_DB_FILE"
694 if [[ -f $tempname.sig
]]; then
695 mv "$tempname.sig" "$REPO_DB_FILE.sig"
698 dblink
="${REPO_DB_FILE%.tar*}"
699 rm -f "$dblink" "$dblink.sig"
700 ln -s "$filename" "$dblink" 2>/dev
/null || \
701 ln "$filename" "$dblink" 2>/dev
/null || \
702 cp "$REPO_DB_FILE" "$dblink"
703 if [[ -f "$REPO_DB_FILE.sig" ]]; then
704 ln -s "$filename.sig" "$dblink.sig" 2>/dev
/null || \
705 ln "$filename.sig" "$dblink.sig" 2>/dev
/null || \
706 cp "$REPO_DB_FILE.sig" "$dblink.sig"
709 msg
"$(gettext "No packages modified
, nothing to
do.
")"
714 # vim: set ts=2 sw=2 noet: