Skip to contentSkip to conversation

Support ordering of records in compare function

SEMANAGE Ivan Gyurdiev 1 participant 1 message Dec 23, 2005 SELinux security module

lore
Share & tools
lore
#0 lore raw

This patch changes the compare functions for user/boolean/iface/seuser (and a fixme for ports), to support ordering of records (by returning -1, 1, or 0, and not just 0 or -1). This is necessary to sort records in a reasonable amount of time, which I need to do to support a join operation.

libsemanage/src/database.h +3 -1
diff -Naurp --exclude-from excludes old/libsemanage/src/database.h new/libsemanage/src/database.h
@@ -37,7 +37,9 @@ typedef struct record_table {
 	void (*key_free) (
 		record_key_t* key);
 
-	/* Return 0 if record can be matched against key, and 1 otherwise */
+	/* Return 0 if the record matches the key, 
+	 * -1 if the key represents a record that should
+	 * be ordered before this record, and 1 if vice-versa */
 	int (*compare) (
 		record_t* rec, 
 		record_key_t* key);
libsemanage/src/seuser_record.c +2 -4
diff -Naurp --exclude-from excludes old/libsemanage/src/seuser_record.c new/libsemanage/src/seuser_record.c
@@ -90,10 +90,8 @@ hidden_def(semanage_seuser_key_free)
 int semanage_seuser_compare(
 	semanage_seuser_t* seuser,
 	semanage_seuser_key_t* key) {
-	
-	if (!strcmp(seuser->name, key->name))
-		return 0;
-	return 1;
+
+	return strcmp(seuser->name, key->name);	
 }
 hidden_def(semanage_seuser_compare)
 
libsepol/src/boolean_record.c +2 -4
diff -Naurp --exclude-from excludes old/libsepol/src/boolean_record.c new/libsepol/src/boolean_record.c
@@ -68,10 +68,8 @@ void sepol_bool_key_free(sepol_bool_key_
 int sepol_bool_compare(
 	sepol_bool_t* boolean,
 	sepol_bool_key_t* key) {
-	
-	if (!strcmp(boolean->name, key->name))
-		return 0;
-	return 1;
+
+	return strcmp(boolean->name, key->name);
 }
 
 /* Name */
libsepol/src/iface_record.c +2 -4
diff -Naurp --exclude-from excludes old/libsepol/src/iface_record.c new/libsepol/src/iface_record.c
@@ -73,10 +73,8 @@ void sepol_iface_key_free(sepol_iface_ke
 int sepol_iface_compare(
 	sepol_iface_t* iface, 
 	sepol_iface_key_t* key) {
-	
-	if (!strcmp(iface->name, key->name)) 
-		return 0;
-	return 1;
+
+	return strcmp(iface->name, key->name);
 }
 
 /* Create */
libsepol/src/port_record.c +2 -0
diff -Naurp --exclude-from excludes old/libsepol/src/port_record.c new/libsepol/src/port_record.c
@@ -84,6 +84,8 @@ int sepol_port_compare(
 	sepol_port_t* port, 
 	sepol_port_key_t* key) {
 
+	/* FIXME: needs to support ordering of ports (-1, 0, 1) */
+
 	if ((port->low <= key->low) && 
 	    (port->high >= key->high) &&
 	    (port->proto == key->proto))
libsepol/src/user_record.c +2 -4
diff -Naurp --exclude-from excludes old/libsepol/src/user_record.c new/libsepol/src/user_record.c
@@ -78,10 +78,8 @@ void sepol_user_key_free(sepol_user_key_
 int sepol_user_compare(
 	sepol_user_t* user,
 	sepol_user_key_t* key) {
-	
-	if (!strcmp(user->name, key->name))
-		return 0;
-	return 1;
+
+	return strcmp(user->name, key->name);
 }
 
 /* Name */