Added file copy. Move home directory detection to the main program function. During opening of the main configuration file, backups are made, 10 revisions in total.

Sun, 20 Dec 2015 20:37:40 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 20 Dec 2015 20:37:40 +0100
changeset 464
4a624c071ca9
parent 463
a1da58215b65
child 465
3aac3276689d

Added file copy. Move home directory detection to the main program function. During opening of the main configuration file, backups are made, 10 revisions in total.

brewco/brewco.c file | annotate | diff | comparison | revisions
brewco/brewco.h file | annotate | diff | comparison | revisions
brewco/rdconfig.c file | annotate | diff | comparison | revisions
brewco/rdrecipes.c file | annotate | diff | comparison | revisions
brewco/rdsession.c file | annotate | diff | comparison | revisions
brewco/util.c file | annotate | diff | comparison | revisions
brewco/util.h file | annotate | diff | comparison | revisions
--- a/brewco/brewco.c	Thu Dec 17 22:45:54 2015 +0100
+++ b/brewco/brewco.c	Sun Dec 20 20:37:40 2015 +0100
@@ -46,6 +46,9 @@
 #ifdef USE_SIMULATOR
 extern int		SIM_cooler;
 #endif
+char			*etcpath = NULL;
+char			*varpath = NULL;
+
 
 
 #ifndef HAVE_WIRINGPI_H
@@ -564,6 +567,7 @@
 int main(int argc, char *argv[])
 {
     int		rc = 0, c, i;
+    char	*homepath = NULL;
 
     while (1) {
 	int option_index = 0;
@@ -590,6 +594,22 @@
     if (debug)
 	fprintf(stdout, "mbsePi-apps brewco v%s starting\n", VERSION);
 
+    /*
+     * Set home directory path
+     */
+    if (getenv((char *)"USER") == NULL) {
+	homepath = xstrcpy((char *)"/root");
+    } else {
+	homepath = xstrcpy(getenv((char *)"HOME"));
+    }
+    varpath = xstrcpy(homepath);
+    varpath = xstrcat(varpath, (char *)"/.brewco/var/");
+    etcpath = xstrcpy(homepath);
+    etcpath = xstrcat(etcpath, (char *)"/.brewco/etc/");
+    mkdirs(varpath, 0755);
+    mkdirs(etcpath, 0755);
+
+fprintf(stdout, "home: %s etc=%s var=%s\n", homepath, etcpath, varpath);
     if (rdconfig()) {
 	fprintf(stderr, "Error reading configuration\n");
 	syslog(LOG_NOTICE, "Error reading configuration: halted");
--- a/brewco/brewco.h	Thu Dec 17 22:45:54 2015 +0100
+++ b/brewco/brewco.h	Sun Dec 20 20:37:40 2015 +0100
@@ -18,6 +18,7 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <time.h>
+#include <utime.h>
 #include <fcntl.h>
 #include <syslog.h>
 #include <unistd.h>
@@ -204,15 +205,6 @@
     int			duration;		/* Step duration		*/
 } mash_step;
 
-// mash-in	20	80	noskip
-// fitasi	25	55	canskip
-// glucanasi	35	50	canskip
-// proteasi	45	60	canskip
-// B-amilasi	50	70	canskip
-// A-amilasi 1	60	76	canskip
-// A-amilasi 2	60	76	noskip
-// mash-out	75	80	noskip
-
 typedef struct _hop_stand {
     char		*name;			/* Hop stand name		*/
     int			min;			/* Minimum temperature		*/
--- 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
--- a/brewco/rdrecipes.c	Thu Dec 17 22:45:54 2015 +0100
+++ b/brewco/rdrecipes.c	Sun Dec 20 20:37:40 2015 +0100
@@ -26,6 +26,7 @@
 #include "xutil.h"
 
 extern int	debug;
+extern char	*etcpath;
 
 a_recipe	*recipes = NULL;
 
@@ -288,13 +289,7 @@
     /*
      * Now write the XML recipes
      */
-    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 *)"recipes.xml");
 
     if (debug)
@@ -571,18 +566,10 @@
     xmlNodePtr	cur;
     a_recipe	*recipe;
 
-    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 = xstrcat(mypath, (char *)"/.brewco/etc/");
-    mkdirs(mypath, 0755);
+    mypath = xstrcpy(etcpath);
     mypath = xstrcat(mypath, (char *)"recipes.xml");
 
     /*
--- a/brewco/rdsession.c	Thu Dec 17 22:45:54 2015 +0100
+++ b/brewco/rdsession.c	Sun Dec 20 20:37:40 2015 +0100
@@ -26,6 +26,7 @@
 #include "xutil.h"
 
 extern int	debug;
+extern char	*etcpath;
 
 
 const char      BREWSTEP[14][10] = { "NA", "INIT", "WAITSTART", "PREMASH", "MASHING", "IODINE", "REMOVE", "PREBOIL", "BOIL", "BOILDONE", "COOLING", "WHIRLPOOL", "CLEANUP", "DONE" };
@@ -138,13 +139,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/var/");
-    mkdirs(mypath, 0755);
+    mypath = xstrcpy(etcpath);
     mypath = xstrcat(mypath, (char *)"brewing.xml");
 
     if (debug)
@@ -191,18 +186,10 @@
     xmlNodePtr	cur;
     xmlChar	*key;
 
-    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 = xstrcat(mypath, (char *)"/.brewco/var/");
-    mkdirs(mypath, 0755);
+    mypath = xstrcpy(etcpath);
     mypath = xstrcat(mypath, (char *)"brewing.xml");
 
     /*
--- a/brewco/util.c	Thu Dec 17 22:45:54 2015 +0100
+++ b/brewco/util.c	Sun Dec 20 20:37:40 2015 +0100
@@ -76,6 +76,69 @@
 
 
 
+/*
+ * Buffered filecopy, filetime is preserved.
+ */
+int file_cp(char *from, char *to)
+{
+    char		*line;
+    FILE		*stfrom, *stto;
+    int			dummy, bread;
+    static int		error;
+    struct stat 	sb;
+    struct utimbuf 	ut;
+
+    stfrom = fopen(from, "r");
+    if (stfrom == NULL)
+	return errno;
+
+    stto = fopen(to, "w");
+    if (stto == NULL) {
+	error = errno;
+	fclose(stfrom);
+	return error;
+    }
+
+    line = malloc(16384);
+    do {
+	bread = fread(line, 1, 16384, stfrom);
+	dummy = fwrite(line, 1, bread, stto);
+	if (bread != dummy) {
+	    error = errno;
+	    fclose(stfrom);
+	    fclose(stto);
+	    unlink(to);
+	    free(line);
+	    return error;
+	}
+    } while (bread != 0);
+    free(line);
+    fclose(stfrom);
+    if (fclose(stto) != 0) {
+	error = errno;
+	unlink(to);
+	return error;
+    }
+
+    /*
+     * copy successfull, now copy file- and modification-time
+     */
+    if (stat(from, &sb) == 0) {
+	ut.actime = mktime(localtime(&sb.st_atime));
+	ut.modtime = mktime(localtime(&sb.st_mtime));
+	if (utime(to, &ut) != 0) {
+	    error = errno;
+	    unlink(to);
+	    return error;
+	}
+	chmod(to, sb.st_mode);
+    }
+
+    return 0;
+}
+
+
+
 long millis(void)
 {
     struct timespec	now;
--- a/brewco/util.h	Thu Dec 17 22:45:54 2015 +0100
+++ b/brewco/util.h	Sun Dec 20 20:37:40 2015 +0100
@@ -4,6 +4,7 @@
 
 int  mkdirs(char *, mode_t);
 int  file_exist(char *, int);
+int  file_cp(char *, char *);
 long millis(void);
 
 

mercurial