Fix file importing code to not generate some duplicate directories
authorDan Winship <danw@gnome.org>
Wed, 20 Feb 2008 17:37:37 +0000 (12:37 -0500)
committerDan Winship <danw@gnome.org>
Wed, 20 Feb 2008 17:42:42 +0000 (12:42 -0500)
build_file_tree() depends on the fact that the contents of a directory
are sorted immediately after it in the file list, but '.' sorts before '/'
lexicographically, so you'd actually end up with, eg:

    /etc/yum
    /etc/yum.conf
    /etc/yum/pluginconf.d

which would cause a second entry for /etc/yum to be added to the file
list.

For now I've fixed this by make compare_filenames() do a special strcmp
by hand, manually sorting '/' before anything else.

razor.c

diff --git a/razor.c b/razor.c
index b5fbf88..b491004 100644 (file)
--- a/razor.c
+++ b/razor.c
@@ -530,8 +530,29 @@ compare_filenames(const void *p1, const void *p2, void *data)
 {
        const struct import_entry *e1 = p1;
        const struct import_entry *e2 = p2;
-
-       return strcmp(e1->name, e2->name);
+       const char *n1 = e1->name;
+       const char *n2 = e2->name;
+
+       /* Need to make sure that the contents of a directory
+        * are sorted immediately after it. So "foo/bar" has to
+        * sort before "foo.conf"
+        *
+        * FIXME: this is about 60% slower than strcmp
+        */
+       while (*n1 && *n2) {
+               if (*n1 < *n2)
+                       return *n2 == '/' ? 1 : -1;
+               else if (*n1 > *n2)
+                       return *n1 == '/' ? -1 : 1;
+               n1++;
+               n2++;
+       }
+       if (*n1)
+               return 1;
+       else if (*n2)
+               return -1;
+       else
+               return 0;
 }
 
 static void