|
|
c401cc |
From 7cfdcd004ec9c29b2d3ff288c41de24c82c58125 Mon Sep 17 00:00:00 2001
|
|
|
c401cc |
Message-Id: <7cfdcd004ec9c29b2d3ff288c41de24c82c58125.1386348946.git.jdenemar@redhat.com>
|
|
|
c401cc |
From: "Daniel P. Berrange" <berrange@redhat.com>
|
|
|
c401cc |
Date: Mon, 2 Dec 2013 13:39:09 +0000
|
|
|
c401cc |
Subject: [PATCH] Introduce standard methods for sorting strings with qsort
|
|
|
c401cc |
|
|
|
c401cc |
For
|
|
|
c401cc |
|
|
|
c401cc |
https://bugzilla.redhat.com/show_bug.cgi?id=1035403
|
|
|
c401cc |
|
|
|
c401cc |
Add virStringSortCompare and virStringSortRevCompare as
|
|
|
c401cc |
standard functions to use with qsort.
|
|
|
c401cc |
|
|
|
c401cc |
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
|
|
|
c401cc |
(cherry picked from commit c60a2713d69e2cdbeac8bbdd85f49acdbae47600)
|
|
|
c401cc |
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
|
|
|
c401cc |
---
|
|
|
c401cc |
src/libvirt_private.syms | 2 ++
|
|
|
c401cc |
src/lxc/lxc_container.c | 14 ++------------
|
|
|
c401cc |
src/util/virstring.c | 29 +++++++++++++++++++++++++++++
|
|
|
c401cc |
src/util/virstring.h | 3 +++
|
|
|
c401cc |
tests/virstringtest.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
c401cc |
5 files changed, 82 insertions(+), 12 deletions(-)
|
|
|
c401cc |
|
|
|
c401cc |
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
|
|
|
c401cc |
index 0421c5b..6821352 100644
|
|
|
c401cc |
--- a/src/libvirt_private.syms
|
|
|
c401cc |
+++ b/src/libvirt_private.syms
|
|
|
c401cc |
@@ -1928,6 +1928,8 @@ virStringArrayHasString;
|
|
|
c401cc |
virStringFreeList;
|
|
|
c401cc |
virStringJoin;
|
|
|
c401cc |
virStringListLength;
|
|
|
c401cc |
+virStringSortCompare;
|
|
|
c401cc |
+virStringSortRevCompare;
|
|
|
c401cc |
virStringSplit;
|
|
|
c401cc |
virStrncpy;
|
|
|
c401cc |
virStrndup;
|
|
|
c401cc |
diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c
|
|
|
c401cc |
index 01ed93e..c2cbfb4 100644
|
|
|
c401cc |
--- a/src/lxc/lxc_container.c
|
|
|
c401cc |
+++ b/src/lxc/lxc_container.c
|
|
|
c401cc |
@@ -483,16 +483,6 @@ error_out:
|
|
|
c401cc |
/*_syscall2(int, pivot_root, char *, newroot, const char *, oldroot)*/
|
|
|
c401cc |
extern int pivot_root(const char * new_root,const char * put_old);
|
|
|
c401cc |
|
|
|
c401cc |
-static int lxcContainerChildMountSort(const void *a, const void *b)
|
|
|
c401cc |
-{
|
|
|
c401cc |
- const char **sa = (const char**)a;
|
|
|
c401cc |
- const char **sb = (const char**)b;
|
|
|
c401cc |
-
|
|
|
c401cc |
- /* Deliberately reversed args - we need to unmount deepest
|
|
|
c401cc |
- children first */
|
|
|
c401cc |
- return strcmp(*sb, *sa);
|
|
|
c401cc |
-}
|
|
|
c401cc |
-
|
|
|
c401cc |
#ifndef MS_REC
|
|
|
c401cc |
# define MS_REC 16384
|
|
|
c401cc |
#endif
|
|
|
c401cc |
@@ -544,7 +534,7 @@ static int lxcContainerGetSubtree(const char *prefix,
|
|
|
c401cc |
|
|
|
c401cc |
if (mounts)
|
|
|
c401cc |
qsort(mounts, nmounts, sizeof(mounts[0]),
|
|
|
c401cc |
- lxcContainerChildMountSort);
|
|
|
c401cc |
+ virStringSortRevCompare);
|
|
|
c401cc |
|
|
|
c401cc |
ret = 0;
|
|
|
c401cc |
cleanup:
|
|
|
c401cc |
@@ -822,7 +812,7 @@ static int lxcContainerSetReadOnly(void)
|
|
|
c401cc |
|
|
|
c401cc |
if (mounts)
|
|
|
c401cc |
qsort(mounts, nmounts, sizeof(mounts[0]),
|
|
|
c401cc |
- lxcContainerChildMountSort);
|
|
|
c401cc |
+ virStringSortRevCompare);
|
|
|
c401cc |
|
|
|
c401cc |
for (i = 0; i < nmounts; i++) {
|
|
|
c401cc |
VIR_DEBUG("Bind readonly %s", mounts[i]);
|
|
|
c401cc |
diff --git a/src/util/virstring.c b/src/util/virstring.c
|
|
|
c401cc |
index d11db5c..8d0ca70 100644
|
|
|
c401cc |
--- a/src/util/virstring.c
|
|
|
c401cc |
+++ b/src/util/virstring.c
|
|
|
c401cc |
@@ -616,3 +616,32 @@ size_t virStringListLength(char **strings)
|
|
|
c401cc |
|
|
|
c401cc |
return i;
|
|
|
c401cc |
}
|
|
|
c401cc |
+
|
|
|
c401cc |
+
|
|
|
c401cc |
+/**
|
|
|
c401cc |
+ * virStringSortCompare:
|
|
|
c401cc |
+ *
|
|
|
c401cc |
+ * A comparator function for sorting strings in
|
|
|
c401cc |
+ * normal order with qsort().
|
|
|
c401cc |
+ */
|
|
|
c401cc |
+int virStringSortCompare(const void *a, const void *b)
|
|
|
c401cc |
+{
|
|
|
c401cc |
+ const char **sa = (const char**)a;
|
|
|
c401cc |
+ const char **sb = (const char**)b;
|
|
|
c401cc |
+
|
|
|
c401cc |
+ return strcmp(*sa, *sb);
|
|
|
c401cc |
+}
|
|
|
c401cc |
+
|
|
|
c401cc |
+/**
|
|
|
c401cc |
+ * virStringSortRevCompare:
|
|
|
c401cc |
+ *
|
|
|
c401cc |
+ * A comparator function for sorting strings in
|
|
|
c401cc |
+ * reverse order with qsort().
|
|
|
c401cc |
+ */
|
|
|
c401cc |
+int virStringSortRevCompare(const void *a, const void *b)
|
|
|
c401cc |
+{
|
|
|
c401cc |
+ const char **sa = (const char**)a;
|
|
|
c401cc |
+ const char **sb = (const char**)b;
|
|
|
c401cc |
+
|
|
|
c401cc |
+ return strcmp(*sb, *sa);
|
|
|
c401cc |
+}
|
|
|
c401cc |
diff --git a/src/util/virstring.h b/src/util/virstring.h
|
|
|
c401cc |
index b390150..13a6e5a 100644
|
|
|
c401cc |
--- a/src/util/virstring.h
|
|
|
c401cc |
+++ b/src/util/virstring.h
|
|
|
c401cc |
@@ -223,4 +223,7 @@ size_t virStringListLength(char **strings);
|
|
|
c401cc |
virAsprintfInternal(false, 0, NULL, NULL, 0, \
|
|
|
c401cc |
strp, __VA_ARGS__)
|
|
|
c401cc |
|
|
|
c401cc |
+int virStringSortCompare(const void *a, const void *b);
|
|
|
c401cc |
+int virStringSortRevCompare(const void *a, const void *b);
|
|
|
c401cc |
+
|
|
|
c401cc |
#endif /* __VIR_STRING_H__ */
|
|
|
c401cc |
diff --git a/tests/virstringtest.c b/tests/virstringtest.c
|
|
|
c401cc |
index 30803d4..b4ce679 100644
|
|
|
c401cc |
--- a/tests/virstringtest.c
|
|
|
c401cc |
+++ b/tests/virstringtest.c
|
|
|
c401cc |
@@ -231,6 +231,49 @@ cleanup:
|
|
|
c401cc |
return ret;
|
|
|
c401cc |
}
|
|
|
c401cc |
|
|
|
c401cc |
+
|
|
|
c401cc |
+static int
|
|
|
c401cc |
+testStringSortCompare(const void *opaque ATTRIBUTE_UNUSED)
|
|
|
c401cc |
+{
|
|
|
c401cc |
+ const char *randlist[] = {
|
|
|
c401cc |
+ "tasty", "astro", "goat", "chicken", "turducken",
|
|
|
c401cc |
+ };
|
|
|
c401cc |
+ const char *randrlist[] = {
|
|
|
c401cc |
+ "tasty", "astro", "goat", "chicken", "turducken",
|
|
|
c401cc |
+ };
|
|
|
c401cc |
+ const char *sortlist[] = {
|
|
|
c401cc |
+ "astro", "chicken", "goat", "tasty", "turducken",
|
|
|
c401cc |
+ };
|
|
|
c401cc |
+ const char *sortrlist[] = {
|
|
|
c401cc |
+ "turducken", "tasty", "goat", "chicken", "astro",
|
|
|
c401cc |
+ };
|
|
|
c401cc |
+ int ret = -1;
|
|
|
c401cc |
+ size_t i;
|
|
|
c401cc |
+
|
|
|
c401cc |
+ qsort(randlist, ARRAY_CARDINALITY(randlist), sizeof(randlist[0]),
|
|
|
c401cc |
+ virStringSortCompare);
|
|
|
c401cc |
+ qsort(randrlist, ARRAY_CARDINALITY(randrlist), sizeof(randrlist[0]),
|
|
|
c401cc |
+ virStringSortRevCompare);
|
|
|
c401cc |
+
|
|
|
c401cc |
+ for (i = 0; i < ARRAY_CARDINALITY(randlist); i++) {
|
|
|
c401cc |
+ if (STRNEQ(randlist[i], sortlist[i])) {
|
|
|
c401cc |
+ fprintf(stderr, "sortlist[%zu] '%s' != randlist[%zu] '%s'\n",
|
|
|
c401cc |
+ i, sortlist[i], i, randlist[i]);
|
|
|
c401cc |
+ goto cleanup;
|
|
|
c401cc |
+ }
|
|
|
c401cc |
+ if (STRNEQ(randrlist[i], sortrlist[i])) {
|
|
|
c401cc |
+ fprintf(stderr, "sortrlist[%zu] '%s' != randrlist[%zu] '%s'\n",
|
|
|
c401cc |
+ i, sortrlist[i], i, randrlist[i]);
|
|
|
c401cc |
+ goto cleanup;
|
|
|
c401cc |
+ }
|
|
|
c401cc |
+ }
|
|
|
c401cc |
+
|
|
|
c401cc |
+ ret = 0;
|
|
|
c401cc |
+ cleanup:
|
|
|
c401cc |
+ return ret;
|
|
|
c401cc |
+}
|
|
|
c401cc |
+
|
|
|
c401cc |
+
|
|
|
c401cc |
static int
|
|
|
c401cc |
mymain(void)
|
|
|
c401cc |
{
|
|
|
c401cc |
@@ -282,6 +325,9 @@ mymain(void)
|
|
|
c401cc |
if (virtTestRun("strdup", 1, testStrndupNegative, NULL) < 0)
|
|
|
c401cc |
ret = -1;
|
|
|
c401cc |
|
|
|
c401cc |
+ if (virtTestRun("virStringSortCompare", 1, testStringSortCompare, NULL) < 0)
|
|
|
c401cc |
+ ret = -1;
|
|
|
c401cc |
+
|
|
|
c401cc |
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
|
|
c401cc |
}
|
|
|
c401cc |
|
|
|
c401cc |
--
|
|
|
c401cc |
1.8.4.5
|
|
|
c401cc |
|