Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 21 Jul 2007 10:52:31 GMT
From:      Andrew Turner <andrew@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 123837 for review
Message-ID:  <200707211052.l6LAqV4H052487@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=123837

Change 123837 by andrew@andrew_hermies on 2007/07/21 10:52:07

	Move the argument decoding for list_updates and list_installed to seperate functions
	Return dummy data for list_installed
	In look_for_updates free dir_fd when exiting

Affected files ...

.. //depot/projects/soc2007/andrew-update/backend/facund-be.c#16 edit

Differences ...

==== //depot/projects/soc2007/andrew-update/backend/facund-be.c#16 (text+ko) ====

@@ -63,6 +63,12 @@
 static char	**get_base_dirs(char *);
 static void	 *do_communication(void *);
 
+static struct facund_response * facund_get_update_types(const char *,
+    const struct facund_object *, int *, int *);
+static const char **facund_get_dir_list(const struct facund_object *);
+static struct facund_response *facund_read_type_directory(const char *,
+    const struct facund_object *, const char ***, int *, int *);
+
 static struct facund_response *facund_call_ping(const char *,
     struct facund_object *);
 static struct facund_response *facund_call_list_updates(const char *,
@@ -248,6 +254,8 @@
 			sleep(default_check_period);
 		}
 	}
+
+	free(dir_fd);
 	return NULL;
 }
 
@@ -361,8 +369,58 @@
 	return resp;
 }
 
+/*
+ * Takes either a facund array or a facund string and sets base
+ * or ports to true if they are contained in the object
+ */
+static struct facund_response *
+facund_get_update_types(const char *id, const struct facund_object *obj,
+    int *base, int *ports)
+{
+	const struct facund_object *area_objs[2];
+	const char *areas[2];
+	enum facund_type type;
+
+	assert(base != NULL);
+	assert(ports != NULL);
+
+	type = facund_object_get_type(obj);
+	if (type == FACUND_ARRAY) {
+		if (facund_object_array_size(obj) != 2) {
+			return facund_response_new(id, 1,
+			    "Wrong number of arguments", NULL);
+		}
+		area_objs[0] = facund_object_get_array_item(obj, 0);
+		area_objs[1] = facund_object_get_array_item(obj, 1);
+
+		areas[0] = facund_object_get_string(area_objs[0]);
+		areas[1] = facund_object_get_string(area_objs[1]);
+
+		if (strcmp(areas[0], "base") == 0 || strcmp(areas[1], "base"))
+			*base = 1;
+
+		if (strcmp(areas[0], "ports") == 0 || strcmp(areas[1], "ports"))
+			*ports = 1;
+
+	} else if (type == FACUND_STRING) {
+		areas[0] = facund_object_get_string(obj);
+		if (strcmp(areas[0], "base") == 0) {
+			*base = 1;
+		} else if (strcmp(areas[0], "ports") == 0) {
+			*ports = 1;
+		}
+	} else {
+		return facund_response_new(id, 1, "Incorrect data type", NULL);
+	}
+	return NULL;
+}
+
+/*
+ * Takes a either facund array of string objects or a single string
+ * object and returns a C array of C strings of the objects
+ */
 static const char **
-get_dir_list(const struct facund_object *obj)
+facund_get_dir_list(const struct facund_object *obj)
 {
 	const char **dirs;
 	const struct facund_object *cur;
@@ -404,22 +462,18 @@
 }
 
 static struct facund_response *
-facund_call_list_updates(const char *id, struct facund_object *obj)
+facund_read_type_directory(const char *id, const struct facund_object *obj,
+    const char ***base_dirs, int *base, int *ports)
 {
-	const struct facund_object *cur, *area_objs[2];
-	struct facund_object *args;
-	const char **base_dirs, *areas[2];
-	int get_base, get_ports;
+	const struct facund_object *cur;
+	struct facund_response *ret;
 	unsigned int pos;
-	enum facund_type type;
 
-	get_base = get_ports = 0;
-	base_dirs = NULL;
-
-	if (obj == NULL) {
-		/* TODO: Don't use magic numbers */
-		return facund_response_new(id, 1, "No data sent", NULL);
-	}
+	assert(id != NULL);
+	assert(obj != NULL);
+	assert(base_dirs != NULL);
+	assert(base != NULL);
+	assert(ports != NULL);
 
 	if (facund_object_get_type(obj) != FACUND_ARRAY) {
 		return facund_response_new(id, 1, "Bad data sent", NULL);
@@ -430,51 +484,20 @@
 		switch (pos) {
 		case 0:
 			/* Read in the type of updates to list */
-			type = facund_object_get_type(cur);
-			if (type == FACUND_ARRAY) {
-				if (facund_object_array_size(cur) != 2) {
-					return facund_response_new(id, 1,
-					    "Wrong number of arguments", NULL);
-				}
-				area_objs[0] =
-				    facund_object_get_array_item(cur, 0);
-				area_objs[1] =
-				    facund_object_get_array_item(cur, 1);
-
-				areas[0] =
-				    facund_object_get_string(area_objs[0]);
-				areas[1] =
-				    facund_object_get_string(area_objs[1]);
-
-				if (strcmp(areas[0], "base") == 0 ||
-				    strcmp(areas[1], "base"))
-					get_base = 1;
-
-				if (strcmp(areas[0], "base") == 0 ||
-				    strcmp(areas[1], "base"))
-					get_ports = 1;
-			} else if (type == FACUND_STRING) {
-				areas[0] = facund_object_get_string(cur);
-				if (strcmp(areas[0], "base") == 0) {
-					get_base = 1;
-				} else if (strcmp(areas[0], "base") == 0) {
-					get_ports = 1;
-				}
-			} else {
-				return facund_response_new(id, 1,
-				    "Incorrect data type", NULL);
-			}
+			ret = facund_get_update_types(id, cur, base, ports);
+			if (ret != NULL)
+				return ret;
 			break;
 		case 1:
-			/* Read in the directories to fet updates for */
-			base_dirs = get_dir_list(cur);
-			if (base_dirs == NULL)
+			/* Read in the directories to get updates for */
+			*base_dirs = facund_get_dir_list(cur);
+			if (*base_dirs == NULL)
 				return facund_response_new(id, 1,
 				    "Malloc failed", NULL);
 			break;
 		default:
-			if (base_dirs != NULL)
-				free(base_dirs);
+			if (*base_dirs != NULL)
+				free(*base_dirs);
 
 			return facund_response_new(id, 1, "Too many arguments",
 			    NULL);
@@ -486,6 +509,37 @@
 		return facund_response_new(id, 1,
 		    "Not enough arguments", NULL);
 	}
+	return NULL;
+}
+
+static struct facund_response *
+facund_call_list_updates(const char *id, struct facund_object *obj)
+{
+	struct facund_response *ret;
+	struct facund_object *args;
+	const char **base_dirs;
+	int get_base, get_ports;
+	unsigned int pos;
+
+	get_base = get_ports = 0;
+	base_dirs = NULL;
+
+	if (obj == NULL) {
+		/* TODO: Don't use magic numbers */
+		return facund_response_new(id, 1, "No data sent", NULL);
+	}
+
+	/* Read in the arguments */
+	ret = facund_read_type_directory(id, obj, &base_dirs, &get_base,
+	    &get_ports);
+	if (ret != NULL) {
+		if (base_dirs != NULL)
+			free(base_dirs);
+		return ret;
+	}
+	/* This should have been assigned as ret is NULL when successful */
+	assert(base_dirs != NULL);
+
 	/*
 	 * If any of these asserts fail there was
 	 * incorrect logic checking arguments
@@ -507,7 +561,7 @@
 		/* Add a list of directories to the array */
 		updates = facund_object_new_array();
 		item = facund_object_new_string();
-		facund_object_set_string(item, "6.2-p1");
+		facund_object_set_string(item, "6.2-p2");
 		facund_object_array_append(updates, item);
 		facund_object_array_append(pair, updates);
 
@@ -517,15 +571,77 @@
 
 	printf("STUB: %s (base: %s, ports: %s)\n", __func__,
 	    (get_base ? "yes" : "no"), (get_ports ? "yes" : "no"));
+	for (pos = 0; base_dirs[pos] != NULL; pos++) {
+		printf("Dir: %s\n", base_dirs[pos]);
+	}
 	free(base_dirs);
 	return facund_response_new(id, RESP_GOOD, "Success", args);
 }
 
 static struct facund_response *
-facund_call_list_installed(const char *id __unused, struct facund_object *obj __unused)
+facund_call_list_installed(const char *id, struct facund_object *obj)
 {
-	printf("STUB: %s\n", __func__);
-	return NULL;
+	struct facund_response *ret;
+	struct facund_object *args;
+	const char **base_dirs;
+	int get_base, get_ports;
+	unsigned int pos;
+
+	get_base = get_ports = 0;
+	base_dirs = NULL;
+
+	if (obj == NULL) {
+		/* TODO: Don't use magic numbers */
+		return facund_response_new(id, 1, "No data sent", NULL);
+	}
+
+	/* Read in the arguments */
+	ret = facund_read_type_directory(id, obj, &base_dirs, &get_base,
+	    &get_ports);
+	if (ret != NULL) {
+		if (base_dirs != NULL)
+			free(base_dirs);
+		return ret;
+	}
+	/* This should have been assigned as ret is NULL when successful */
+	assert(base_dirs != NULL);
+
+	/*
+	 * If any of these asserts fail there was
+	 * incorrect logic checking arguments
+	 */
+	assert(get_ports == 1 || get_base == 1);
+	assert(base_dirs[0] != NULL);
+
+	args = facund_object_new_array();
+	for (pos = 0; base_dirs[pos] != NULL; pos++) {
+		struct facund_object *pair, *item, *updates;
+
+		pair = facund_object_new_array();
+
+		/* Add the directory to the start of the array */
+		item = facund_object_new_string();
+		facund_object_set_string(item, base_dirs[pos]);
+		facund_object_array_append(pair, item);
+
+		/* Add a list of directories to the array */
+		updates = facund_object_new_array();
+		item = facund_object_new_string();
+		facund_object_set_string(item, "6.2-p1");
+		facund_object_array_append(updates, item);
+		facund_object_array_append(pair, updates);
+
+		/* Add the directory on to the end of the arguments to return */
+		facund_object_array_append(args, pair);
+	}
+
+	printf("STUB: %s (base: %s, ports: %s)\n", __func__,
+	    (get_base ? "yes" : "no"), (get_ports ? "yes" : "no"));
+	for (pos = 0; base_dirs[pos] != NULL; pos++) {
+		printf("Dir: %s\n", base_dirs[pos]);
+	}
+	free(base_dirs);
+	return facund_response_new(id, RESP_GOOD, "Success", args);
 }
 
 static struct facund_response *



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200707211052.l6LAqV4H052487>