Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 2 Jun 2007 22:42:45 GMT
From:      Andrew Turner <andrew@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 120803 for review
Message-ID:  <200706022242.l52MgjvP017471@repoman.freebsd.org>

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

Change 120803 by andrew@andrew_hermies on 2007/06/02 22:42:01

	Bump WARNS up to 6
	Add get_base_dirs that takes a space seperated string of base directories and returns an array of strings of base directories. It doesn't handle directories with spaces in them as it will split them.
	Read the base_dirs value from the config file

Affected files ...

.. //depot/projects/soc2007/andrew-update/backend/Makefile#3 edit
.. //depot/projects/soc2007/andrew-update/backend/facund-be.c#4 edit
.. //depot/projects/soc2007/andrew-update/backend/freebsd-config-control.conf#1 add

Differences ...

==== //depot/projects/soc2007/andrew-update/backend/Makefile#3 (text+ko) ====

@@ -4,4 +4,6 @@
 
 MAN=
 
+WARNS=	6
+
 .include <bsd.prog.mk>

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

@@ -32,6 +32,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 
+#include <assert.h>
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -49,8 +50,9 @@
 #define DEFAULT_CONFIG_FILE	"/etc/freebsd-update-control.conf"
 #define UPDATE_DATA_DIR		"/var/db/freebsd-update"
 
-static int has_update(const char *);
-static void* look_for_updates(void *);
+static int	  has_update(const char *);
+static void	 *look_for_updates(void *);
+static char	**get_base_dirs(char *);
 
 /*
  * Looks for updates on the system with a root of basedir
@@ -160,11 +162,72 @@
 	return NULL;
 }
 
+/*
+ * Takes in a string of space seperated directories and returns
+ * a NULL terminated array of pointers to each directory
+ */
+char **
+get_base_dirs(char *str)
+{
+	char *ptr, **ret;
+	int dir_count;
+
+	/* An empty string will contain no directories */
+	if (str == NULL || str[0] == '\0')
+		return NULL;
+
+	/* Count the number of dirs to read */
+	dir_count = 0;
+	ptr = str;
+	while (ptr != NULL) {
+		/* Skip leading spaces */
+		while (ptr[0] == ' ')
+			*ptr++;
+
+		ptr = strchr(ptr, ' ');
+		dir_count++;
+	}
+
+	/*
+	 * There must be at least one directory utherwise
+	 * the empty string check would have returned
+	 */
+	assert(dir_count > 0);
+
+	/* create an array to hold pointers to the dir names */
+	ret = calloc((dir_count + 1) * sizeof(char *), 1);
+
+
+	/* Set the point the ret array to the directories */
+	dir_count = 0;
+	ptr = str;
+	while (ptr != NULL) {
+		/* Skip leading spaces */
+		while (ptr[0] == ' ')
+			*ptr++;
+
+		/* Point to the directory name */
+		ret[dir_count] = ptr;
+		ptr = strchr(ptr, ' ');
+
+		/* Make the previous directory null terminated */
+		if (ptr != NULL) {
+			ptr[0] = '\0';
+			*ptr++;
+		}
+
+		dir_count++;
+	}
+	ret[dir_count] = NULL;
+
+	return ret;
+}
+
 int
 main(int argc __unused, char *argv[] __unused)
 {
-	const char *base_dirs[] = { "/", NULL };
 	const char *config_file;
+	char *basedirs_string, **base_dirs;
 	int config_fd;
 	properties config_data;
 	char ch;
@@ -186,6 +249,8 @@
 	argc -= optind;
 	argv += optind;
 
+	basedirs_string = NULL;
+
 	/* Read in the config file */
 	config_fd = open(config_file, O_RDONLY);
 	if (config_fd == -1 && errno != ENOENT) {
@@ -197,11 +262,28 @@
 		if (config_data == NULL) {
 			errx(1, "Could not read the config file");
 		}
+
+		basedirs_string = strdup(property_find(config_data,
+		    "base_dirs"));
+		if (basedirs_string == NULL) {
+			errx(1, "Malloc failed");
+		}
+
 		properties_free(config_data);
 	}
 
+	/* Read in the base dirs */
+	base_dirs = get_base_dirs(basedirs_string);
+	if (base_dirs == NULL) {
+		errx(1, "No base dirs were given, set base_dirs in %s",
+		    config_file);
+	}
+
 	look_for_updates(base_dirs);
 
+	if (base_dirs != NULL)
+		free(base_dirs);
+	free(basedirs_string);
 	return 0;
 }
 



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