ver: only compare the given packages, show version

make apk_version_compare() take strings rather than blobs
add apk_pkgversion_compare(), a wrapper that takes packages
cute-signatures
Natanael Copa 2009-06-20 12:38:07 +02:00
parent cb074581f0
commit 4bbed2d648
7 changed files with 71 additions and 37 deletions

View File

@ -94,6 +94,8 @@ int apk_pkg_run_script(struct apk_package *pkg, int root_fd,
struct apk_package *apk_pkg_parse_index_entry(struct apk_database *db, apk_blob_t entry);
int apk_pkg_write_index_entry(struct apk_package *pkg, struct apk_ostream *os);
int apk_pkg_version_compare(struct apk_package *a, struct apk_package *b);
struct apk_dependency apk_dep_from_str(struct apk_database *db,
char *str);
struct apk_dependency apk_dep_from_pkg(struct apk_database *db,

View File

@ -21,6 +21,6 @@
const char *apk_version_op_string(int result_mask);
int apk_version_result_mask(const char *str);
int apk_version_validate(apk_blob_t ver);
int apk_version_compare(apk_blob_t a, apk_blob_t b);
int apk_version_compare(const char *str1, const char *str2);
#endif

View File

@ -164,8 +164,8 @@ static int fetch_main(void *ctx, int argc, char **argv)
for (j = 0; j < dep.name->pkgs->num; j++)
if (pkg == NULL ||
apk_version_compare(APK_BLOB_STR(dep.name->pkgs->item[j]->version),
APK_BLOB_STR(pkg->version))
apk_pkg_version_compare(dep.name->pkgs->item[j],
pkg)
== APK_VERSION_GREATER)
pkg = dep.name->pkgs->item[j];

View File

@ -664,6 +664,11 @@ int apk_pkg_write_index_entry(struct apk_package *info,
return n;
}
int apk_pkg_version_compare(struct apk_package *a, struct apk_package *b)
{
return apk_version_compare(a->version, b->version);
}
struct apk_dependency apk_dep_from_str(struct apk_database *db,
char *str)
{

View File

@ -191,8 +191,7 @@ int apk_state_lock_dependency(struct apk_state *state,
return -1;
}
if (apk_version_compare(APK_BLOB_STR(pkg->version),
APK_BLOB_STR(dep->version))
if (apk_version_compare(pkg->version, dep->version)
& dep->result_mask)
return 0;
@ -203,8 +202,7 @@ int apk_state_lock_dependency(struct apk_state *state,
c = ns_to_choices(state->name[name->id]);
i = 0;
while (i < c->num) {
if (apk_version_compare(APK_BLOB_STR(c->pkgs[i]->version),
APK_BLOB_STR(dep->version))
if (apk_version_compare(c->pkgs[i]->version, dep->version)
& dep->result_mask) {
i++;
continue;
@ -235,8 +233,7 @@ int apk_state_lock_dependency(struct apk_state *state,
installed = pkg;
if (latest == NULL ||
apk_version_compare(APK_BLOB_STR(pkg->version),
APK_BLOB_STR(latest->version)) == APK_VERSION_GREATER)
apk_pkg_version_compare(pkg, latest) == APK_VERSION_GREATER)
latest = pkg;
}
@ -301,8 +298,7 @@ static int call_if_dependency_broke(struct apk_state *state,
dep->result_mask == APK_DEPMASK_CONFLICT)
continue;
if (dep_pkg != NULL &&
(apk_version_compare(APK_BLOB_STR(dep_pkg->version),
APK_BLOB_STR(dep->version))
(apk_version_compare(dep_pkg->version, dep->version)
& dep->result_mask))
continue;
return cb(state, pkg);
@ -466,8 +462,7 @@ static void apk_print_change(struct apk_database *db,
name->name,
oldpkg->version);
} else {
r = apk_version_compare(APK_BLOB_STR(newpkg->version),
APK_BLOB_STR(oldpkg->version));
r = apk_pkg_version_compare(newpkg, oldpkg);
switch (r) {
case APK_VERSION_LESS:
msg = "Downgrading";
@ -579,8 +574,7 @@ static int cmp_downgrade(struct apk_change *change)
{
if (change->newpkg == NULL || change->oldpkg == NULL)
return 0;
if (apk_version_compare(APK_BLOB_STR(change->newpkg->version),
APK_BLOB_STR(change->oldpkg->version))
if (apk_pkg_version_compare(change->newpkg, change->oldpkg)
& APK_VERSION_LESS)
return 1;
return 0;
@ -590,8 +584,7 @@ static int cmp_upgrade(struct apk_change *change)
{
if (change->newpkg == NULL || change->oldpkg == NULL)
return 0;
if (apk_version_compare(APK_BLOB_STR(change->newpkg->version),
APK_BLOB_STR(change->oldpkg->version))
if (apk_pkg_version_compare(change->newpkg, change->oldpkg)
& APK_VERSION_GREATER)
return 1;
return 0;

View File

@ -40,7 +40,7 @@ static int ver_test(int argc, char **argv)
if (argc != 2)
return 1;
r = apk_version_compare(APK_BLOB_STR(argv[0]), APK_BLOB_STR(argv[1]));
r = apk_version_compare(argv[0], argv[1]);
printf("%c\n", res2char(r));
return 0;
}
@ -74,13 +74,35 @@ static int ver_parse(void *ctx, int opt, int optindex, const char *optarg)
return 0;
}
static void ver_print_package_status(struct apk_package *pkg)
{
struct apk_name *name;
struct apk_package *latest, *tmp;
int i, r;
name = pkg->name;
latest = pkg;
for (i = 0; i < name->pkgs->num; i++) {
tmp = name->pkgs->item[i];
if (tmp->name != name ||
apk_pkg_get_state(tmp) == APK_PKG_INSTALLED)
continue;
r = apk_pkg_version_compare(tmp, latest);
if (r == APK_VERSION_GREATER)
latest = tmp;
}
r = apk_pkg_version_compare(pkg, latest);
printf("%s-%-40s%s %s\n", name->name, pkg->version, apk_version_op_string(r), latest->version);
}
static int ver_main(void *ctx, int argc, char **argv)
{
struct ver_ctx *ictx = (struct ver_ctx *) ctx;
struct apk_database db;
struct apk_package *pkg;
struct apk_name *name;
struct apk_package *pkg, *upg, *tmp;
int i, r;
int i, j, ret = 0;
if (ictx->action != NULL)
return ictx->action(argc, argv);
@ -88,23 +110,31 @@ static int ver_main(void *ctx, int argc, char **argv)
if (apk_db_open(&db, apk_root, APK_OPENF_READ) < 0)
return -1;
list_for_each_entry(pkg, &db.installed.packages, installed_pkgs_list) {
name = pkg->name;
upg = pkg;
for (i = 0; i < name->pkgs->num; i++) {
tmp = name->pkgs->item[i];
if (tmp->name != name)
continue;
r = apk_version_compare(APK_BLOB_STR(tmp->version),
APK_BLOB_STR(upg->version));
if (r == APK_VERSION_GREATER)
upg = tmp;
if (argc == 0) {
list_for_each_entry(pkg, &db.installed.packages,
installed_pkgs_list) {
ver_print_package_status(pkg);
}
printf("%-40s%c\n", name->name, pkg != upg ? '<' : '=');
goto ver_exit;
}
apk_db_close(&db);
return 0;
for (i = 0; i < argc; i++) {
name = apk_db_query_name(&db, APK_BLOB_STR(argv[i]));
if (name == NULL) {
apk_error("Not found: %s", name);
ret = 1;
goto ver_exit;
}
for (j = 0; j < name->pkgs->num; j++) {
struct apk_package *pkg = name->pkgs->item[j];
if (apk_pkg_get_state(pkg) == APK_PKG_INSTALLED)
ver_print_package_status(pkg);
}
}
ver_exit:
apk_db_close(&db);
return ret;
}
static struct option ver_options[] = {

View File

@ -174,16 +174,20 @@ int apk_version_validate(apk_blob_t ver)
return t == TOKEN_END;
}
int apk_version_compare(apk_blob_t a, apk_blob_t b)
int apk_version_compare(const char *str1, const char *str2)
{
int at = TOKEN_DIGIT, bt = TOKEN_DIGIT;
int av = 0, bv = 0;
apk_blob_t a, b;
if (APK_BLOB_IS_NULL(a) || APK_BLOB_IS_NULL(b)) {
if (APK_BLOB_IS_NULL(a) && APK_BLOB_IS_NULL(b))
if (str1 == NULL || str2 == NULL) {
if (str1 == NULL && str2 == NULL)
return APK_VERSION_EQUAL;
return APK_VERSION_EQUAL | APK_VERSION_GREATER | APK_VERSION_LESS;
}
a = APK_BLOB_STR(str1);
b = APK_BLOB_STR(str2);
while (at == bt && at != TOKEN_END && at != TOKEN_INVALID && av == bv) {
av = get_token(&at, &a);