On ���, 2002-02-02 at 14:55, Borsenkow Andrej wrote:
> This patch returns the highest package version installed instead of the
> first one found (as was the case before). It avoids always suggesting
> update to already installed version. It works at least with
> kernel-source (the only one test case I have).  Comments are welcome.

I comment myself - I got comparison wrong :-) This one is better:

--- utils.c.orig        Fri Dec  7 21:35:29 2001
+++ utils.c     Sun Feb  3 13:58:45 2002
@@ -252,6 +252,21 @@
   return strcmp(pack->name, name);
 }
 
+gint compare_rpm_version(int old_epoch, gchar *old_ver, gchar *old_rel, int 
+new_epoch, gchar *new_ver, gchar *new_rel)
+{
+  int compare;
+
+  compare = new_epoch >= 0 ? new_epoch - old_epoch : 0;
+  if (compare == 0) {
+    compare = rpmvercmp(new_ver, old_ver);
+    if (compare == 0) {
+      compare = rpmvercmp(new_rel, old_rel);
+    }
+  }
+
+  return compare;
+}
+
 /**
 * \brief Check if a package is currently installed, and return its version
 *        and release
@@ -269,20 +284,31 @@
 {
   gboolean  ret_val = FALSE;
   GSList   *package;
+  int       cur_epoch = 0;
+  gchar    *cur_ver = NULL;
+  gchar    *cur_rel = NULL;
 
+  *epoch = 0;
   *release = NULL;
   *version = NULL;
   
-  package = g_slist_find_custom(installed_packages, name,
-                                (GCompareFunc)is_installed_compare_func);
-  
-  if (package)
+  for (package = installed_packages; package; package = g_slist_next(package)) 
+    if (is_installed_compare_func(package->data, name) == 0) {
+      installed_list_package *pack = package->data;
+      if (cur_ver == NULL ||
+         compare_rpm_version (cur_epoch, cur_ver, cur_rel,
+                              pack->epoch, pack->version, pack->release) > 0) {
+       cur_epoch = pack->epoch;
+       cur_ver   = pack->version;
+       cur_rel   = pack->release;
+      }
+    }
+  if (cur_ver)
   {
-    installed_list_package *pack = package->data;
-    ret_val = TRUE;
-    *release = g_strdup(pack->release);
-    *version = g_strdup(pack->version);
-    *epoch = pack->epoch;
+    ret_val  = TRUE;
+    *release = g_strdup(cur_rel);
+    *version = g_strdup(cur_ver);
+    *epoch   = cur_epoch;
   }
   return ret_val;
 }



Reply via email to