summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>2008-12-15 00:15:01 (EST)
committer ticktock35 <ticktock35@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>2008-12-15 00:15:01 (EST)
commitf53b702fecb08d1b8ee090bc48c12faeb5d4b49c (patch)
treea77309a60879772d0c729a0655e58b3a9facd72d
parent371254054431baca18545c4e02c9e588e7d95a66 (diff)
opkg: implement opkg_find_package()
git-svn-id: http://opkg.googlecode.com/svn/trunk@99 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358
-rw-r--r--configure.ac2
-rw-r--r--libopkg/opkg.c113
-rw-r--r--libopkg/opkg.h2
-rw-r--r--tests/libopkg_test.c60
4 files changed, 142 insertions, 35 deletions
diff --git a/configure.ac b/configure.ac
index aae0e63..e9b264d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,6 +1,6 @@
# Process this file with autoconf to produce a configure script
AC_INIT(libopkg/libopkg.c)
-AM_INIT_AUTOMAKE([opkg], [0.1.2])
+AM_INIT_AUTOMAKE([opkg], [0.1.3])
AM_CONFIG_HEADER(libopkg/config.h)
AC_CANONICAL_HOST
diff --git a/libopkg/opkg.c b/libopkg/opkg.c
index 630b243..9f63812 100644
--- a/libopkg/opkg.c
+++ b/libopkg/opkg.c
@@ -45,10 +45,32 @@ struct _opkg_t
__FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
#define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data);
-#define OLD_PKG_TO_NEW(pkg) opkg_package_new_with_values (pkg->name, pkg->version, pkg->architecture, pkg->description, pkg->tags, pkg->url, (pkg->size ? atoi (pkg->size) : 0), (pkg->state_status == SS_INSTALLED));
+#define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0
/** Private Functions ***/
+static opkg_package_t*
+old_pkg_to_new (pkg_t *old)
+{
+ opkg_package_t *new;
+
+ new = opkg_package_new ();
+
+#define sstrdup(x) (x) ? strdup (x) : NULL;
+
+ new->name = sstrdup (old->name);
+ new->version = pkg_version_str_alloc (old);
+ new->architecture = sstrdup (old->architecture);
+ new->repository = sstrdup (old->src->name);
+ new->description = sstrdup (old->description);
+ new->tags = sstrdup (old->tags);
+ new->url = sstrdup (old->url);
+
+ new->size = (old->size) ? atoi (old->size) : 0;
+ new->installed = (old->state_status == SS_INSTALLED);
+
+ return new;
+}
static int
opkg_configure_packages(opkg_conf_t *conf, char *pkg_name)
@@ -143,28 +165,6 @@ opkg_package_new ()
return p;
}
-opkg_package_t *
-opkg_package_new_with_values (const char *name, const char *version,
- const char *arch, const char *desc, const char *tags, const char *url, int size, int installed)
-{
- opkg_package_t *package;
- package = opkg_package_new ();
-
-#define sstrdup(x) (x) ? strdup (x) : NULL;
-
- package->name = sstrdup (name);
- package->version = sstrdup (version);
- package->architecture = sstrdup (arch);
- package->description = sstrdup (desc);
- package->tags = sstrdup (tags);
- package->url = sstrdup (url);
-
- package->size = size;
- package->installed = (installed != 0);
-
- return package;
-}
-
void
opkg_package_free (opkg_package_t *p)
{
@@ -376,7 +376,7 @@ opkg_install_package (opkg_t *opkg, const char *package_name, opkg_progress_call
return 1;
}
pdata.action = OPKG_INSTALL;
- pdata.package = OLD_PKG_TO_NEW (new);
+ pdata.package = old_pkg_to_new (new);
progress (pdata, 0);
@@ -441,7 +441,7 @@ opkg_remove_package (opkg_t *opkg, const char *package_name, opkg_progress_callb
}
pdata.action = OPKG_REMOVE;
- pdata.package = OLD_PKG_TO_NEW (pkg);
+ pdata.package = old_pkg_to_new (pkg);
progress (pdata, 0);
@@ -515,7 +515,7 @@ opkg_upgrade_package (opkg_t *opkg, const char *package_name, opkg_progress_call
}
pdata.action = OPKG_INSTALL;
- pdata.package = OLD_PKG_TO_NEW (pkg);
+ pdata.package = old_pkg_to_new (pkg);
progress (pdata, 0);
opkg_upgrade_pkg (opkg->conf, pkg);
@@ -550,7 +550,7 @@ opkg_upgrade_all (opkg_t *opkg, opkg_progress_callback_t progress_callback, void
{
pkg = installed->pkgs[i];
- pdata.package = OLD_PKG_TO_NEW (pkg);
+ pdata.package = old_pkg_to_new (pkg);
progress (pdata, 99 * i / installed->len);
opkg_package_free (pdata.package);
@@ -759,7 +759,7 @@ opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_d
pkg = all->pkgs[i];
- package = OLD_PKG_TO_NEW (pkg);
+ package = old_pkg_to_new (pkg);
callback (opkg, package, user_data);
}
@@ -800,7 +800,7 @@ opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, v
if (cmp < 0)
{
- package = OLD_PKG_TO_NEW (new);
+ package = old_pkg_to_new (new);
callback (opkg, package, user_data);
}
}
@@ -810,3 +810,58 @@ opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, v
return 0;
}
+opkg_package_t*
+opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo)
+{
+ pkg_vec_t *all;
+ opkg_package_t *package = NULL;
+ int i;
+#define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0
+
+ opkg_assert (opkg);
+
+ all = pkg_vec_alloc ();
+ pkg_hash_fetch_available (&opkg->conf->pkg_hash, all);
+ for (i = 0; i < all->len; i++)
+ {
+ pkg_t *pkg;
+ char *pkgv;
+
+ pkg = all->pkgs[i];
+
+ /* check name */
+ if (sstrcmp (pkg->name, name))
+ continue;
+
+ /* check version */
+ pkgv = pkg_version_str_alloc (pkg);
+ if (sstrcmp (pkgv, ver))
+ {
+ free (pkgv);
+ continue;
+ }
+ free (pkgv);
+
+ /* check architecture */
+ if (arch)
+ {
+ if (sstrcmp (pkg->architecture, arch))
+ continue;
+ }
+
+ /* check repository */
+ if (repo)
+ {
+ if (sstrcmp (pkg->src->name, repo))
+ continue;
+ }
+
+ /* match found */
+ package = old_pkg_to_new (pkg);
+ break;
+ }
+
+ pkg_vec_free (all);
+
+ return package;
+}
diff --git a/libopkg/opkg.h b/libopkg/opkg.h
index c352331..b51aeda 100644
--- a/libopkg/opkg.h
+++ b/libopkg/opkg.h
@@ -50,7 +50,6 @@ struct _opkg_progress_data_t
};
opkg_package_t* opkg_package_new ();
-opkg_package_t* opkg_package_new_with_values (const char *name, const char *version, const char *arch, const char *desc, const char *tags, const char *url, int size, int installed);
void opkg_package_free (opkg_package_t *package);
opkg_t* opkg_new ();
@@ -67,3 +66,4 @@ int opkg_update_package_lists (opkg_t *opkg, opkg_progress_callback_t callback,
int opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data);
int opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data);
+opkg_package_t* opkg_find_package (opkg_t *opkg, const char *name, const char *version, const char *architecture, const char *repository);
diff --git a/tests/libopkg_test.c b/tests/libopkg_test.c
index 8c64b5e..6288bd4 100644
--- a/tests/libopkg_test.c
+++ b/tests/libopkg_test.c
@@ -2,6 +2,8 @@
#include <stdlib.h>
#include <stdio.h>
+opkg_package_t *find_pkg = NULL;
+
void
progress_callback (opkg_t *opkg, const opkg_progress_data_t *progress, void *data)
{
@@ -23,7 +25,13 @@ package_list_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
printf ("\rPackage count: %d Installed, %d Total Available", install_count, total_count);
fflush (stdout);
- opkg_package_free (pkg);
+ if (!find_pkg)
+ {
+ /* store the first package to print out later */
+ find_pkg = pkg;
+ }
+ else
+ opkg_package_free (pkg);
}
void
@@ -32,10 +40,36 @@ package_list_upgradable_callback (opkg_t *opkg, opkg_package_t *pkg, void *data)
printf ("%s - %s\n", pkg->name, pkg->version);
}
+void
+print_package (opkg_package_t *pkg)
+{
+ printf (
+ "Name: %s\n"
+ "Version: %s\n"
+ "Repository: %s\n"
+ "Architecture: %s\n"
+ "Description: %s\n"
+ "Tags: %s\n"
+ "URL: %s\n"
+ "Size: %d\n"
+ "Installed: %s\n",
+ pkg->name,
+ pkg->version,
+ pkg->repository,
+ pkg->architecture,
+ pkg->description,
+ pkg->tags,
+ pkg->url,
+ pkg->size,
+ (pkg->installed ? "True" : "False")
+ );
+}
+
int
main (int argc, char **argv)
{
opkg_t *opkg;
+ opkg_package_t *pkg;
int err;
opkg = opkg_new ();
@@ -47,6 +81,25 @@ main (int argc, char **argv)
err = opkg_update_package_lists (opkg, progress_callback, "Updating...");
printf ("\nopkg_update_package_lists returned %d\n", err);
+ opkg_list_packages (opkg, package_list_callback, NULL);
+ printf ("\n");
+
+ if (find_pkg)
+ {
+ printf ("Finding package \"%s\"\n", find_pkg->name);
+ pkg = opkg_find_package (opkg, find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->repository);
+ if (pkg)
+ {
+ print_package (pkg);
+ opkg_package_free (find_pkg);
+ opkg_package_free (pkg);
+ }
+ else
+ printf ("Package \"%s\" not found!\n", find_pkg->name);
+ }
+ else
+ printf ("No package available to test find_package.\n");
+
err = opkg_install_package (opkg, "aspell", progress_callback, "Installing...");
printf ("\nopkg_install_package returned %d\n", err);
@@ -62,8 +115,7 @@ main (int argc, char **argv)
err = opkg_upgrade_all (opkg, progress_callback, "Upgrading all...");
printf ("\nopkg_upgrade_all returned %d\n", err);
- opkg_list_packages (opkg, package_list_callback, NULL);
- printf ("\n");
-
opkg_free (opkg);
+
+ return 0;
}