Changeset 4418


Ignore:
Timestamp:
05/09/08 12:52:43 (5 years ago)
Author:
thomas
Message:

opkg: implement opkg_find_package()

Location:
trunk/src/target/opkg
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/target/opkg/configure.ac

    r4416 r4418  
    11# Process this file with autoconf to produce a configure script 
    22AC_INIT(libopkg/libopkg.c) 
    3 AM_INIT_AUTOMAKE([opkg], [0.1.2]) 
     3AM_INIT_AUTOMAKE([opkg], [0.1.3]) 
    44AM_CONFIG_HEADER(libopkg/config.h) 
    55 
  • trunk/src/target/opkg/libopkg/opkg.c

    r4416 r4418  
    4646 
    4747#define progress(d, p) d.percentage = p; if (progress_callback) progress_callback (opkg, &d, user_data); 
    48 #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)); 
     48#define SSTRCMP(x,y) (x && y) ? strcmp (x, y) : 0 
    4949 
    5050/** Private Functions ***/ 
    5151 
     52static opkg_package_t* 
     53old_pkg_to_new (pkg_t *old) 
     54{ 
     55  opkg_package_t *new; 
     56 
     57  new = opkg_package_new (); 
     58 
     59#define sstrdup(x) (x) ? strdup (x) : NULL; 
     60 
     61  new->name = sstrdup (old->name); 
     62  new->version = pkg_version_str_alloc (old); 
     63  new->architecture = sstrdup (old->architecture); 
     64  new->repository = sstrdup (old->src->name); 
     65  new->description = sstrdup (old->description); 
     66  new->tags = sstrdup (old->tags); 
     67  new->url = sstrdup (old->url); 
     68 
     69  new->size = (old->size) ? atoi (old->size) : 0; 
     70  new->installed = (old->state_status == SS_INSTALLED); 
     71 
     72  return new; 
     73} 
    5274 
    5375static int 
     
    142164 
    143165  return p; 
    144 } 
    145  
    146 opkg_package_t * 
    147 opkg_package_new_with_values (const char *name, const char *version, 
    148     const char *arch, const char *desc, const char *tags, const char *url, int size, int installed) 
    149 { 
    150   opkg_package_t *package; 
    151   package = opkg_package_new (); 
    152  
    153 #define sstrdup(x) (x) ? strdup (x) : NULL; 
    154  
    155   package->name = sstrdup (name); 
    156   package->version = sstrdup (version); 
    157   package->architecture = sstrdup (arch); 
    158   package->description = sstrdup (desc); 
    159   package->tags = sstrdup (tags); 
    160   package->url = sstrdup (url); 
    161  
    162   package->size = size; 
    163   package->installed = (installed != 0); 
    164  
    165   return package; 
    166166} 
    167167 
     
    377377  } 
    378378  pdata.action = OPKG_INSTALL; 
    379   pdata.package = OLD_PKG_TO_NEW (new); 
     379  pdata.package = old_pkg_to_new (new); 
    380380 
    381381  progress (pdata, 0); 
     
    442442 
    443443  pdata.action = OPKG_REMOVE; 
    444   pdata.package = OLD_PKG_TO_NEW (pkg); 
     444  pdata.package = old_pkg_to_new (pkg); 
    445445  progress (pdata, 0); 
    446446 
     
    516516 
    517517  pdata.action = OPKG_INSTALL; 
    518   pdata.package = OLD_PKG_TO_NEW (pkg); 
     518  pdata.package = old_pkg_to_new (pkg); 
    519519  progress (pdata, 0); 
    520520 
     
    551551    pkg = installed->pkgs[i]; 
    552552 
    553     pdata.package = OLD_PKG_TO_NEW (pkg); 
     553    pdata.package = old_pkg_to_new (pkg); 
    554554    progress (pdata, 99 * i / installed->len); 
    555555    opkg_package_free (pdata.package); 
     
    760760    pkg = all->pkgs[i]; 
    761761 
    762     package = OLD_PKG_TO_NEW (pkg); 
     762    package = old_pkg_to_new (pkg); 
    763763    callback (opkg, package, user_data); 
    764764  } 
     
    801801    if (cmp < 0) 
    802802    { 
    803       package = OLD_PKG_TO_NEW (new); 
     803      package = old_pkg_to_new (new); 
    804804      callback (opkg, package, user_data); 
    805805    } 
     
    811811} 
    812812 
     813opkg_package_t* 
     814opkg_find_package (opkg_t *opkg, const char *name, const char *ver, const char *arch, const char *repo) 
     815{ 
     816  pkg_vec_t *all; 
     817  opkg_package_t *package = NULL; 
     818  int i; 
     819#define sstrcmp(x,y) (x && y) ? strcmp (x, y) : 0 
     820 
     821  opkg_assert (opkg); 
     822 
     823  all = pkg_vec_alloc (); 
     824  pkg_hash_fetch_available (&opkg->conf->pkg_hash, all); 
     825  for (i = 0; i < all->len; i++) 
     826  { 
     827    pkg_t *pkg; 
     828    char *pkgv; 
     829 
     830    pkg = all->pkgs[i]; 
     831 
     832    /* check name */ 
     833    if (sstrcmp (pkg->name, name)) 
     834      continue; 
     835     
     836    /* check version */ 
     837    pkgv = pkg_version_str_alloc (pkg); 
     838    if (sstrcmp (pkgv, ver)) 
     839    { 
     840      free (pkgv); 
     841      continue; 
     842    } 
     843    free (pkgv); 
     844 
     845    /* check architecture */ 
     846    if (arch) 
     847    { 
     848      if (sstrcmp (pkg->architecture, arch)) 
     849        continue; 
     850    } 
     851 
     852    /* check repository */ 
     853    if (repo) 
     854    { 
     855      if (sstrcmp (pkg->src->name, repo)) 
     856          continue; 
     857    } 
     858 
     859    /* match found */ 
     860    package = old_pkg_to_new (pkg); 
     861    break; 
     862  } 
     863 
     864  pkg_vec_free (all); 
     865 
     866  return package; 
     867} 
  • trunk/src/target/opkg/libopkg/opkg.h

    r4416 r4418  
    5151 
    5252opkg_package_t* opkg_package_new (); 
    53 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); 
    5453void opkg_package_free (opkg_package_t *package); 
    5554 
     
    6867int opkg_list_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data); 
    6968int opkg_list_upgradable_packages (opkg_t *opkg, opkg_package_callback_t callback, void *user_data); 
     69opkg_package_t* opkg_find_package (opkg_t *opkg, const char *name, const char *version, const char *architecture, const char *repository); 
  • trunk/src/target/opkg/tests/libopkg_test.c

    r4415 r4418  
    22#include <stdlib.h> 
    33#include <stdio.h> 
     4 
     5opkg_package_t *find_pkg = NULL; 
    46 
    57void 
     
    2426  fflush (stdout); 
    2527 
    26   opkg_package_free (pkg); 
     28  if (!find_pkg) 
     29  { 
     30    /* store the first package to print out later */ 
     31    find_pkg = pkg; 
     32  } 
     33  else 
     34    opkg_package_free (pkg); 
    2735} 
    2836 
     
    3341} 
    3442 
     43void 
     44print_package (opkg_package_t *pkg) 
     45{ 
     46  printf ( 
     47      "Name:         %s\n" 
     48      "Version:      %s\n" 
     49      "Repository:   %s\n" 
     50      "Architecture: %s\n" 
     51      "Description:  %s\n" 
     52      "Tags:         %s\n" 
     53      "URL:          %s\n" 
     54      "Size:         %d\n" 
     55      "Installed:    %s\n", 
     56      pkg->name, 
     57      pkg->version, 
     58      pkg->repository, 
     59      pkg->architecture, 
     60      pkg->description, 
     61      pkg->tags, 
     62      pkg->url, 
     63      pkg->size, 
     64      (pkg->installed ? "True" : "False") 
     65      ); 
     66} 
     67 
    3568int 
    3669main (int argc, char **argv) 
    3770{ 
    3871  opkg_t *opkg; 
     72  opkg_package_t *pkg; 
    3973  int err; 
    4074   
     
    4781  err = opkg_update_package_lists (opkg, progress_callback, "Updating..."); 
    4882  printf ("\nopkg_update_package_lists returned %d\n", err); 
     83 
     84  opkg_list_packages (opkg, package_list_callback, NULL); 
     85  printf ("\n"); 
     86 
     87  if (find_pkg) 
     88  { 
     89    printf ("Finding package \"%s\"\n", find_pkg->name); 
     90    pkg = opkg_find_package (opkg, find_pkg->name, find_pkg->version, find_pkg->architecture, find_pkg->repository); 
     91    if (pkg) 
     92    { 
     93      print_package (pkg); 
     94      opkg_package_free (find_pkg); 
     95      opkg_package_free (pkg); 
     96    } 
     97    else 
     98      printf ("Package \"%s\" not found!\n", find_pkg->name); 
     99  } 
     100  else 
     101    printf ("No package available to test find_package.\n"); 
    49102 
    50103  err = opkg_install_package (opkg, "aspell", progress_callback, "Installing..."); 
     
    63116  printf ("\nopkg_upgrade_all returned %d\n", err); 
    64117 
    65   opkg_list_packages (opkg, package_list_callback, NULL); 
    66   printf ("\n"); 
     118  opkg_free (opkg); 
    67119 
    68   opkg_free (opkg); 
     120  return 0; 
    69121} 
Note: See TracChangeset for help on using the changeset viewer.