[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Patch to anna for choosing retriever



I "volunteered" (:p) to hack in some support in anna for choosing which
retriever to use. Seeing as my sister too is called Anna, I felt
qualified enough for this ;) Attached is a patch. Seeing as I don't
trust myself with proofreading and testing my own code, I'd be happy if
someone could look it over.


Regards,
Martin
diff -u -r1.2 Makefile
--- Makefile	28 Mar 2002 19:30:35 -0000	1.2
+++ Makefile	25 Aug 2002 23:02:23 -0000
@@ -1,6 +1,7 @@
 CFLAGS=-Wall -g -D_GNU_SOURCE
 OBJS=$(subst .c,.o,$(wildcard *.c))
 BIN=anna
+LIBS=-ldebconf
 
 ifdef DEBUG
 CFLAGS:=$(CFLAGS) -DDODEBUG
diff -u -r1.7 anna.h
--- anna.h	21 Dec 2000 22:28:53 -0000	1.7
+++ anna.h	25 Aug 2002 23:02:23 -0000
@@ -3,5 +3,9 @@
 #define DOWNLOAD_DIR "/var/cache/anna"
 #define DPKG_UNPACK_COMMAND "udpkg --unpack"
 
+#define ANNA_RETRIEVER	"anna/retriever"
+#define RETRIEVER_DIR	"/usr/lib/debian-installer/retriever"
+#define RETRIEVER_DIR_LEN	35	/* ick */
+
 int get_package (struct package_t *package, char *dest);
 struct package_t *get_packages (void);
diff -u -r1.11 retriever.c
--- retriever.c	25 Aug 2002 20:14:09 -0000	1.11
+++ retriever.c	25 Aug 2002 23:02:23 -0000
@@ -2,6 +2,10 @@
  * Retriever interface code.
  */
 
+#include <cdebconf/debconfclient.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -9,10 +13,63 @@
 #include "anna.h"
 
 /* Returns the filename of the retriever to use. */
-/* TODO: handle more than one, and don't hard-code. */
+/* TODO: error handling */
 char *chosen_retriever (void) {
-        
-	return "/usr/lib/debian-installer/retriever/cdrom-retriever";
+	static char *retriever = NULL;
+
+	if (retriever == NULL) {
+		DIR *retrdir;
+		struct dirent *d_ent;
+		struct debconfclient *debconf;
+		int retstr_size = 1, fname_len;
+		char *ret_choices, *ret_default = NULL;
+		char *fname;
+		struct stat st;
+
+		/* Find out which retrievers are available */
+		retrdir = opendir(RETRIEVER_DIR);
+		ret_choices = malloc(1);
+		ret_choices[0] = '\0';
+		while ((d_ent = readdir(retrdir)) != NULL) {
+			fname_len = strlen(d_ent->d_name);
+			fname = (char *)malloc(RETRIEVER_DIR_LEN + 1 + fname_len);
+			strcpy(fname, RETRIEVER_DIR "/");
+			strcat(fname, d_ent->d_name);
+			stat(fname, &st);
+			free(fname);
+			if (S_ISREG(st.st_mode)) {
+				/* Should we check for x flag too? */
+				retstr_size += fname_len + 2;
+				ret_choices = realloc(ret_choices, retstr_size);
+				strcat(ret_choices, d_ent->d_name);
+				strcat(ret_choices, ", ");
+				/* Pick the first one to be default :) */
+				if (ret_default == NULL)
+					ret_default = strdup(d_ent->d_name);
+			}
+		}
+		closedir(retrdir);
+		if (retstr_size >= 3)
+			ret_choices[retstr_size-3] = '\0';
+
+		debconf = debconfclient_new();
+		debconf->command(debconf, "TITLE", "Choose Retriever", NULL);
+		debconf->command(debconf, "SET", ANNA_RETRIEVER, ret_default, NULL);
+		debconf->command(debconf, "SUBST", ANNA_RETRIEVER, "RETRIEVER", ret_choices, NULL);
+		debconf->command(debconf, "SUBST", ANNA_RETRIEVER, "DEFAULT", ret_default, NULL);
+		debconf->command(debconf, "INPUT medium", ANNA_RETRIEVER, NULL);
+		debconf->command(debconf, "GO", NULL);
+		debconf->command(debconf, "GET", ANNA_RETRIEVER, NULL);
+		retriever = malloc(RETRIEVER_DIR_LEN + 1 + strlen(debconf->value));
+		strcpy(retriever, RETRIEVER_DIR "/");
+		strcat(retriever, debconf->value);
+		debconfclient_delete(debconf);
+
+		free(ret_choices);
+		free(ret_default);
+	}
+
+	return retriever;
 }
 
 /* Ask the chosen retriever to download a particular package to to dest. */
--- /dev/null   2002-07-08 21:14:29.000000000 +0200
+++ debian/templates    2002-08-25 23:59:23.000000000 +0200
@@ -0,0 +1,7 @@
+Template: anna/retriever
+Type: select
+Choices: ${RETRIEVER}
+Default: ${DEFAULT}
+Description: Choose the retriever to use.
+ The retriever is responsible for fetching the packages to be
+ installed.

Attachment: signature.asc
Description: Detta =?ISO-8859-1?Q?=E4r?= en digitalt signerad meddelandedel


Reply to: