[pacman-dev] bacman

Carlo Bersani carlocci at gmail.com
Fri May 30 20:27:41 EDT 2008


Il Friday 30 May 2008 15:28:18 Allan McRae ha scritto:
> Here is my latest adjustment to the bacman script including all changes
> mentioned so far in the thread.
>
> Remaining issues are permissions:
> 1) Group information is lost
> 2) Directory permissions are always 755
> 3) Sometimes bails when user does not have permission to read file. I
> added a catch for this but it could probably be fixed with the above.
>
Here might be a patch to fix 2)
I don't know how to remove the warning about stripping the leading /, though.

I was thinking that it would be nice to just create the tar archive with the 
files and then gzip it, instead of using the temporary directory which needs 
fakeroot -u in order to preserve ownerships. Maybe tomorrow.

--- bacman	2008-05-31 01:20:12.000000000 +0200
+++ bacman.new	2008-05-31 01:35:06.000000000 +0200
@@ -117,10 +117,8 @@
     case $current in
         %FILES%)
             ret=0
-            if [ -d "/$i" ]; then
-                mkdir "$i" || ret=$?
-            elif [ -f "/$i" ]; then
-                cp -dp "/$i" "$i" || ret=$?
+            if [ -e "/$i" ]; then
+               bsdtar -cnf - "/$i" 2> /dev/null || ret=$? | bsdtar -xpf -
             else
                 echo "/$i" is missing: this might result in a broken package
             fi




Here's the complete script:
#!/bin/bash
#
#   bacman: recreate a package from a running system
#   This script rebuilds an already installed package using metadata
#   stored into the pacman database and system files
#
#   (c) 2008 - locci <carlocci_at_gmail_dot_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/>.
#

readonly progname="bacman"
readonly progver="0.1.2"

#
# User Friendliness
#
function usage(){
    echo "This program recreates a package using pacman's db and system 
files"
    echo "Usage:   $progname <installed package name>"
    echo "Example: $progname kernel26"
}

if [ $# -ne 1 ] ; then
    usage
    exit 1
fi

if [ "$1" = "--help" -o "$1" = "-h" ] ; then
    usage
    exit 0
fi

if [ "$1" = "--version" -o "$1" = "-v" ]; then
    echo "$progname version $version"
    echo "Copyright (C) 2008 locci"
    exit 0
fi

#
# Setting environmental variables
#
if [ ! -r /etc/pacman.conf ]; then
    echo Error: unable to read /etc/pacman.conf
    exit 1
fi

eval $(awk '/DBPath/ {print $1$2$3}' /etc/pacman.conf)
pac_db="${DBPath:-/var/lib/pacman/}/local"

if [ ! -r /etc/makepkg.conf ]; then
    echo Error: unable to read /etc/makepkg.conf
    exit 1
fi

source "/etc/makepkg.conf"
if [ -r ~/.makepkg.conf ]; then
    source ~/.makepkg.conf
fi

pkg_arch=${CARCH:-'unknown'}
pkg_dest="${PKGDEST:-$PWD}"
pkg_pkger=${PACKAGER:-'Unknown Packager'}

pkg_name="$1"
pkg_dir="$(echo $pac_db/$pkg_name-[0-9]*)"
pkg_namver="${pkg_dir##*/}"

#
# Checks everything is in place
#
if [ ! -d "$pac_db" ] ; then
    echo Error: pacman database directory ${pac_db} not found
    exit 1
fi

if [ ! -d "$pkg_dir" ] ; then
    echo Error: package ${pkg_name} not found in pacman database
    exit 1
fi

#
# Begin
#
echo Package: ${pkg_namver}
work_dir=$(mktemp -d -p /tmp)
cd $work_dir || exit 1

#
# File copying
#
echo Copying package files...

cat "$pkg_dir"/files |
while read i; do
    if [ -z "$i" ] ; then
        continue
    fi

    if [[ "$i" =~ %[A-Z]*% ]] ; then
        current=$i
        continue
    fi

    case $current in
        %FILES%)
            ret=0
            if [ -e "/$i" ]; then
                bsdtar -cnf - "/$i" 2> /dev/null || ret=$? | bsdtar -xpf -
            else
                echo "/$i" is missing: this might result in a broken package
            fi

            if [ $ret -ne 0 ]; then
                echo Error: unable to create /$i
                exit 1
            fi
        ;;
    esac
done

ret=$?
if [ $ret -ne 0 ]; then
    rm -rf $work_dir
    exit 1
fi

pkg_size=$(du -sb | awk '{print $1}')

if [ -f "$pkg_dir/install" ] ; then
    cp "$pkg_dir/install" "$work_dir/.INSTALL"
fi
if  [ -f $pkg_dir/changelog ] ; then
    cp "$pkg_dir/changelog" "$work_dir/.CHANGELOG"
fi

#
# .PKGINFO stuff
#
echo Generating .PKGINFO metadata...
echo "# Generated by $progname $progver"    > .PKGINFO
echo "# $(LC_ALL=C date)"            >> .PKGINFO
echo "#"                    >> .PKGINFO

cat "$pkg_dir"/{desc,files,depends} |
while read i; do
    if [[ -z "$i" ]]; then
        continue;
    fi

    if [[ "$i" =~ %[A-Z]*% ]] ; then
        current=$i
        continue
    fi

    case "$current" in
        # desc
        %NAME%)
            echo "pkgname = $i"    >> .PKGINFO
        ;;
        %VERSION%)
            echo "pkgver = $i"    >> .PKGINFO
        ;;
        %DESC%)
            echo "pkgdesc = $i"    >> .PKGINFO
        ;;
        %URL%)
            echo "url = $i"    >> .PKGINFO
        ;;
        %LICENSE%)
            echo "license = $i"    >> .PKGINFO
        ;;
        %ARCH%)
            echo "arch = $i"    >> .PKGINFO
        ;;
        %BUILDDATE%)
            echo "builddate = $(date -u "+%s")"    >> .PKGINFO
        ;;
        %PACKAGER%)
            echo "packager = $pkg_pkger"        >> .PKGINFO
        ;;
        %SIZE%)
            echo "size = $pkg_size"        >> .PKGINFO
        ;;
        %GROUPS%)
            echo "group = $i"    >> .PKGINFO
        ;;
        %REPLACES%)
            echo "replaces = $i"    >> .PKGINFO
        ;;
        %FORCE%)
            echo "force = true" >> .PKGINFO
        ;;

        # files
        %BACKUP%)
            # strip the md5sum after the tab
            echo "backup = ${i%%    *}"   >> .PKGINFO
        ;;

        # depends
        %DEPENDS%)
            echo "depend = $i"   >> .PKGINFO
        ;;
        %OPTDEPENDS%)
            echo "optdepend = $i" >> .PKGINFO
        ;;
        %CONFLICTS%)
            echo "conflict = $i" >> .PKGINFO
        ;;
        %PROVIDES%)
            echo "provides = $i"  >> .PKGINFO
        ;;
    esac
done

#
# Generate the package
#
echo Generating the package...

ret=0
bsdtar -czf "$pkg_dest/$pkg_namver-$pkg_arch.tar.gz" $(ls -A) || ret=$?
if [ $ret -ne 0 ]; then
    echo Error: unable to write package to $pkg_dest
    echo Maybe the disk is full or you do not have write access
    exit 1
fi

rm -rf $work_dir

echo Done

exit 0

# vim: set ts=2 sw=2 noet:






More information about the pacman-dev mailing list