PERFORCE change 123473 for review
Andrew Turner
andrew at FreeBSD.org
Sat Jul 14 07:45:30 UTC 2007
http://perforce.freebsd.org/chv.cgi?CH=123473
Change 123473 by andrew at andrew_hermies on 2007/07/14 07:45:24
Implement the list_updates handler returning fake data for now
Affected files ...
.. //depot/projects/soc2007/andrew-update/backend/facund-be.c#12 edit
Differences ...
==== //depot/projects/soc2007/andrew-update/backend/facund-be.c#12 (text+ko) ====
@@ -285,34 +285,164 @@
return resp;
}
-static struct facund_response *
-facund_call_list_updates(const char *id __unused, struct facund_object *obj __unused)
+static const char **
+get_dir_list(const struct facund_object *obj)
{
+ const char **dirs;
const struct facund_object *cur;
- unsigned int pos;
+ size_t items, pos;
+ assert(obj != NULL);
- if (obj == NULL) {
- /* TODO: Don't use magic numbers */
- return facund_response_new(id, 1, "No data sent", NULL);
- }
+ switch(facund_object_get_type(obj)) {
+ case FACUND_STRING:
+ dirs = malloc(2 * sizeof(char *));
+ if (dirs == NULL)
+ return NULL;
- switch (facund_object_get_type(obj)) {
- case FACUND_STRING:
- facund_object_print(obj);
+ dirs[0] = facund_object_get_string(
+ __DECONST(struct facund_object *, obj));
+ dirs[1] = NULL;
break;
case FACUND_ARRAY:
+ items = facund_object_array_size(obj);
+ if (items == 0)
+ return NULL;
+
+ dirs = malloc((items + 1) * sizeof(char *));
+ if (dirs == NULL)
+ return NULL;
+
for (pos = 0;
(cur = facund_object_get_array_item(obj, pos)) != NULL;
- pos++) {
- facund_object_print(__DECONST(struct facund_object *, cur));
+ pos++) {
+ dirs[pos] = facund_object_get_string(cur);
}
+ dirs[pos] = NULL;
+ assert(pos == items);
+
break;
default:
+ return NULL;
+ }
+ return dirs;
+}
+
+static struct facund_response *
+facund_call_list_updates(const char *id, struct facund_object *obj)
+{
+ const struct facund_object *cur, *area_objs[2];
+ struct facund_object *args;
+ const char **base_dirs, *areas[2];
+ int get_base, get_ports;
+ 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);
+ }
+
+ if (facund_object_get_type(obj) != FACUND_ARRAY) {
return facund_response_new(id, 1, "Bad data sent", NULL);
- break;
+ }
+
+ for (pos = 0; (cur = facund_object_get_array_item(obj, pos)) != NULL;
+ pos++) {
+ 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);
+ }
+ break;
+ case 1:
+ /* Read in the directories to fet updates for */
+ base_dirs = 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);
+
+ return facund_response_new(id, 1, "Too many arguments",
+ NULL);
+ }
+ }
+ if (pos != 2) {
+ if (base_dirs != NULL)
+ free(base_dirs);
+ return facund_response_new(id, 1,
+ "Not enough arguments", 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\n", __func__);
- return NULL;
+
+ printf("STUB: %s (base: %s, ports: %s)\n", __func__,
+ (get_base ? "yes" : "no"), (get_ports ? "yes" : "no"));
+ free(base_dirs);
+ return facund_response_new(id, RESP_GOOD, "Success", args);
}
static struct facund_response *
@@ -344,7 +474,7 @@
}
int
-main(int argc __unused, char *argv[] __unused)
+main(int argc, char *argv[])
{
pthread_t update_thread, comms_thread;
struct facund_conn *conn;
More information about the p4-projects
mailing list