3

I want to write a script to check the version of a opkg package before installing it.

I've done a lot of research and found a few answer on dpkg system but none on opkg system.

I've tried opkg info, opkg status, opkg list but they all seem to work on installed packages, but not with .opk files.

I can somehow get the information by opkg install file.opk --noaction but it's very slow and the output could be hard to parse reliably because the test installation could fail for all different reasons.

And opkg compare-versions only compares strings so no solution.


Update:

I thought more about this.

The opk package is basically a .tar.gz file and version number is stored in one of the text files in it (in the CONTROL/control file).

So to get the version info from the package, it's inevitable to decompress it, so it has to take some time.

Although a quick solution is to put the version number as part of the file name of the package then parse the name, but file name can be very easily altered.

My plan is to use a two-pass approach: 1) include version number as part of the package name and parse the package name to get a "not-very-trustable" version number; 2) if it looks good then do a "dry run" installation to get the real version number as is in the CONTROL/control file. If it's also good then proceed to real installation.

One of the problems with this approach is that, it effectively doubled the amount of time to do a real installation: before a real installation can run a dry run is run first, which takes equal amount of time.

Update 2:

The above update is not entirely true because opk is an ar archive that has two files in it debian-binary control.tar.gz data.tar.gz, and version info is in control.tar.gz.

So it's feasible to quickly extract control.tar.gz from opk then extract control from control.tar.gz, then get version number from there.

  • 1
    The opkg info command doesn't provide info about installed package but from the index. After an opkg update, if a newer version is available on the repo, the opkg info version will display the newer version, even if the package isn't yet installed. The Status field gives you the information about the package installation. – didil Sep 27 '17 at 13:19

1 Answers1

1
#!/bin/sh

# command that checks opk version number
#set -e

VERSION=3
version ()
{
  echo
  echo "`basename $1` version $VERSION"
  echo "command that checks opk version number"
  echo

  exit 0
}

usage ()
{
  echo "
Usage: `basename $1` <options> [ files for install partition ]

Mandatory options:
  --opk_file         file name (e.g ./my_package.opk)

Optional options:
  --version             Print version. 
"
  exit 1
}


# Process command line...
while [ $# -gt 0 ]; do
  case $1 in
    --help | -h)
      usage $0
      ;;
    --opk_file) shift; opk_file=$1; shift; ;;
    --version) version $0;;
    *) copy="$copy $1"; shift; ;;
  esac
done

test -z $opk_file && usage $0
###########################
end parsing command line
###########################
ar -x $opk_file control.tar.gz    #extract control.tar.gz from .opk
tar -zxvf control.tar.gz ./control > /dev/null 2>&1
# extract control file from control.tar.gz, silent stderr/stdout
cat control |grep Version | sed -e "s/Version:\s*\(\d*\)\D*/\1/"
# grep Version keyword and remove it
rm control
rm control.tar.gz
# clean up

Usage:

opkversion.sh --opk_file ./mypackage.opk

Sample output:

9999

Adjust sed command to change the filtering policy of the command (version number of a opk package can be very complicated).