]> pd.if.org Git - zpackage/commitdiff
add pkgfile to set file metadata
authorNathan Wagner <nw@hydaspes.if.org>
Sun, 21 Oct 2018 01:51:54 +0000 (01:51 +0000)
committerNathan Wagner <nw@hydaspes.if.org>
Sat, 3 Nov 2018 12:39:52 +0000 (12:39 +0000)
Makefile
zpm-pkgfile [new file with mode: 0755]

index 1fb6e6ce1e90316493d80d7cea8d16392ac4e469..d3e7622f4b4a8ae5bd6720391341797e9b3689cd 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@ ZPKGBIN=zpm-addfile zpm-extract zpm-init zpm-vercmp zpm-stat zpm-hash \
 
 SCRIPTS=zpm zpm-install zpm-merge zpm-list zpm-preserve zpm-test zpm-log \
        zpm-contents zpm-uninstall zpm-pathmod zpm-rmpackage zpm-newpackage \
-       zpm-pkg zpm-add zpm-setscript
+       zpm-pkg zpm-add zpm-setscript zpm-pkgfile
 COMPILED=$(ZPKGBIN)
 PROGRAMS=$(SCRIPTS) $(COMPILED)
 
diff --git a/zpm-pkgfile b/zpm-pkgfile
new file mode 100755 (executable)
index 0000000..ede02af
--- /dev/null
@@ -0,0 +1,126 @@
+#!/bin/sh
+
+# edit package file metadata
+
+#zpm pkg -s packager=foo zpm
+
+# zpm pkg -f <pkgfile> zpm file/path configuration=1
+# zpm pkg zpm packager
+
+warn() {
+       printf '%s:' "$0" 1>&2
+       printf ' %s' $* 1>&2
+       printf '\n' 1>&2
+       rv=1
+}
+
+die() {
+       printf '%s:' "$0" 1>&2
+       printf ' %s' $* 1>&2
+       printf '\n' 1>&2
+       exit 1
+}
+
+pkgfile=${ZPMDB:-/var/lib/zpm/db.zpm}
+
+while getopts f: opt; do
+       case $opt in
+               f) pkgfile="$OPTARG" ;;
+       esac
+done
+shift $((OPTIND - 1))
+
+if [ ! -f $pkgfile ]; then
+       echo cannot find $pkgfile
+       exit 1
+fi
+
+(
+rv=0
+
+{
+printf "begin;\n"
+while [ $# -gt 0 ]; do
+       item=$1
+       shift
+       if [ -z "$pkgid" ]; then
+               item=":${item#:}"
+       elif [ -z "$path" ]; then
+               item="/${item#/}"
+       fi
+
+       show=0
+       case "$item" in
+               :*)
+                       pkgid=$(zpm findpkg -f $pkgfile "${item#:}")
+                       if [ -z "$pkgid" ]; then
+                               warn "can't find pkgid ${item#:} in $pkgfile"
+                               break;
+                       fi
+                       continue
+                       ;;
+               /*)
+                       path="$item"
+                       continue
+                       ;;
+               *=*)
+                       field=${item%%=*}
+                       value=${item#*=}
+                       ;;
+               *=)
+                       field=${item%%=*}
+                       value=
+                       ;;
+               *)
+               field=$item
+               show=1
+               ;;
+       esac
+
+       if [ -z "$path" ]; then
+               warn "no package file path specified"
+               break;
+       fi
+
+       vfield=$(zpm quote "$field")
+
+       hasfield=$(zpm shell $pkgfile "select name from pragma_table_info('packagefiles_pkgid') where name = '$vfield';")
+       if [ -z "$hasfield" ]; then
+               warn "$field is not a valid packagefile field"
+               break
+       fi
+
+       field=$(zpm quote -i "$field")
+       vpath=$(zpm quote "$path")
+       vpkg=$(zpm quote "$pkgid")
+
+       haspath=$(zpm shell $pkgfile "select 1 from packagefiles_pkgid where pkgid = '$vpkg' and path = '$vpath';")
+       if [ "$haspath" != 1 ]; then
+               warn "$path not in $pkgid"
+               break
+       fi
+
+       if [ $show -eq 1 ]; then
+               printf "select %s from packagefiles_pkgid where pkgid = '%s' and path = '%s';\n" "$field" "$pkgid" "$vpath"
+               continue
+       fi
+
+       if [ -z "$value" ]; then
+               value=NULL
+       else
+               value=$(zpm quote -q "$value")
+       fi
+
+       printf "update packagefiles_pkgid set %s = %s where pkgid = '$pkgid' and path = '%s';\n" $field $value "$vpath"
+done
+
+if [ $rv -eq 0 ]; then
+       printf "commit;\n"
+else
+       printf "rollback;\n"
+fi
+
+#} | cat
+} | zpm shell $pkgfile
+exit $rv
+)