brewco/rdconfig.c

changeset 464
4a624c071ca9
parent 455
f84501d8dd87
child 465
3aac3276689d
--- a/brewco/rdconfig.c	Thu Dec 17 22:45:54 2015 +0100
+++ b/brewco/rdconfig.c	Sun Dec 20 20:37:40 2015 +0100
@@ -27,6 +27,8 @@
 
 int		debug = TRUE;
 sys_config	Config;			/* System configuration		*/
+extern char	*etcpath;
+
 
 #define MY_ENCODING "utf-8"
 
@@ -604,13 +606,7 @@
     /*
      * Now write the XML configuration
      */
-    if (getenv((char *)"USER") == NULL) {
-	mypath = xstrcpy((char *)"/root");
-    } else {
-	mypath = xstrcpy(getenv((char *)"HOME"));
-    }
-    mypath = xstrcat(mypath, (char *)"/.brewco/etc/");
-    mkdirs(mypath, 0755);
+    mypath = xstrcpy(etcpath);
     mypath = xstrcat(mypath, (char *)"brewco.xml");
 
     if (debug)
@@ -1271,26 +1267,47 @@
 
 int rdconfig(void) 
 {
-    int		ival, rc = 0;
-    char	*mypath;
+    int		i, ival, rc = 0;
+    char	*mypath, *fpath, *tpath;
     xmlDocPtr	doc;
     xmlNodePtr	cur;
     xmlChar	*key;
 
     killconfig();
-    syslog(LOG_NOTICE, "HOME='%s' USER='%s' LOGNAME='%s'", MBSE_SS(getenv((char *)"HOME")), MBSE_SS(getenv((char *)"USER")), MBSE_SS(getenv((char *)"LOGNAME")));
 
     /*
      * Search config file
      */
-    if (getenv((char *)"USER") == NULL) {
-	mypath = xstrcpy((char *)"/root");
-    } else {
-    	mypath = xstrcpy(getenv((char *)"HOME"));
+    mypath = xstrcpy(etcpath);
+    mypath = xstrcat(mypath, (char *)"brewco.xml");
+
+    /*
+     * Make upto 10 backups of the configuration.
+     */
+    fpath = calloc(1024, sizeof(char));
+    tpath = calloc(1024, sizeof(char));
+
+    for (i = 9; i > 0; i--) {
+	snprintf(tpath, 1023, "%s.%d", mypath, i+1);
+	snprintf(fpath, 1023, "%s.%d", mypath, i);
+	if (file_exist(fpath, R_OK) == 0) {
+	    unlink(tpath);
+	    if (file_cp(fpath, tpath)) {
+		syslog(LOG_NOTICE, "rdconfig file_cp(%s, %s): %s", fpath, tpath, strerror(errno));
+	    }
+	}
     }
-    mypath = xstrcat(mypath, (char *)"/.brewco/etc/");
-    mkdirs(mypath, 0755);
-    mypath = xstrcat(mypath, (char *)"brewco.xml");
+    snprintf(tpath, 1023, "%s.1", mypath);
+    snprintf(fpath, 1023, "%s", mypath);
+    if (file_exist(fpath, R_OK) == 0) {
+    	unlink(tpath);
+    	if (file_cp(fpath, tpath)) {
+	    syslog(LOG_NOTICE, "rdconfig file_cp(%s, %s): %s", fpath, tpath, strerror(errno));
+    	}
+    }
+    free(fpath);
+    free(tpath);
+
     if ((doc = xmlParseFile(mypath)) == NULL) {
 	/*
 	 * No config file, create a fresh one

mercurial