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

[PATCH] new flat md5sum based scheme for conffile db layout



the pathname of the conffile is transformed into an md5sum, allowing for
a simpler conffile db implementation where conffiles are now found in:

	<admindir>/conffiles{,-new,-old}/<pkg>/<hash>
---
 src/conffiles.c |   51 ++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 38 insertions(+), 13 deletions(-)

diff --git a/src/conffiles.c b/src/conffiles.c
index 96fdbd9..fd9b47d 100644
--- a/src/conffiles.c
+++ b/src/conffiles.c
@@ -11,6 +11,24 @@
 #include <dpkg-db.h>
 #include "main.h"
 
+/* MD5 string length, minus null byte.  mostly here for readability below */
+#define MD5SUM_LEN 32
+
+/* calculate the md5sum of the contents of the null-terminated string input 
+ * putting the result in output, which will be allocated with memory to
+ * store the result.  your job to free it later.
+ */
+static void conff_md5sum_str(const char *input, char **output){
+  buffer_arg arg = { (void*) output };
+  struct buffer_data data = { buffer_write, arg, BUFFER_WRITE_MD5 };
+  data.type|=BUFFER_WRITE_SETUP;
+  data.proc(&data, NULL, 0, "conff_md5sum_str");
+  data.type=BUFFER_WRITE_MD5;
+  data.proc(&data, (void*)input, strlen(input), "conff_md5sum_str");
+  data.type|=BUFFER_WRITE_SHUTDOWN;
+  data.proc(&data, NULL, 0, "conff_md5sum_str");
+}
+
 /* return a pointer to a char* which has the full on-disk location of
  * the file in the "conffile db" registered for the file at path.  pass
  * flags f to indicate whether you want the "new", "old", or "current" version.
@@ -19,42 +37,49 @@
  * ought to be.  your job to free() the result.
  */
 static char* conff_db_path(const char *pkg, const char *path, conff_flag f){
-  char *p = NULL;
+  char *p = NULL, *hash = NULL;
   const char *ext = "";
-  size_t cfgdb_root_sz = 0, p_sz = 0, path_sz = 0;
+  size_t root_sz = 0, path_sz = 0;
   
   /* <admindir>/<conffiles[-ext]>/<pkg> + '\0' */
-  cfgdb_root_sz = strlen(admindir)+1+strlen(CONFFILESDIR)+1+strlen(pkg)+1;
-
-  if(path!=NULL) path_sz = strlen(path);
+  root_sz = strlen(admindir)+1+strlen(CONFFILESDIR)+1+strlen(pkg)+1;
 
   /* in the case of new/old, we add extra space for the [-ext] */
   switch(f){
   case conff_db_new:
     ext=NEWDBEXT;
-    cfgdb_root_sz+=strlen(NEWDBEXT);
+    root_sz+=strlen(NEWDBEXT);
     break;
   case conff_db_old:
     ext=OLDDBEXT;
-    cfgdb_root_sz+=strlen(OLDDBEXT);
+    root_sz+=strlen(OLDDBEXT);
     break;
   case conff_db_cur:
+  	ext = "";
     break;
   default:
     ohshit("conff_db_path called with unsupported flags %x", f);
     break;
   }
 
-  /* add add the path's length, if appropriate */
-  if(path!=NULL) path_sz = strlen(path);
+  path_sz = root_sz;
+  /* and if a conffile is being requested (not just the db root)... */
+  if(path!=NULL) {
+  	/* / + hash */
+  	path_sz+=(1+MD5SUM_LEN);
+  } 
 
-  p = m_malloc(cfgdb_root_sz + path_sz);
+  p = m_malloc(path_sz);
 
   /* this is the path to the conffile db root for pkg */
-  snprintf(p, cfgdb_root_sz, "%s/%s%s/%s", admindir, CONFFILESDIR, ext, pkg);
+  snprintf(p, root_sz, "%s/%s%s/%s", admindir, CONFFILESDIR, ext, pkg);
 
-  /* append the pathname if relevant */
-  if(path_sz) strncat(p, path, path_sz);
+  /* append the pathname's hash if relevant */
+  if(path!=NULL) {
+	conff_md5sum_str(path, &hash);
+  	snprintf(p+root_sz-1, 1+MD5SUM_LEN+1, "/%s", hash );
+	free(hash);
+  }
 
   printf("conff_db_path(%s, %s, %x) = %s\n", pkg, path, f, p);
   return p;
-- 
1.5.4.3


Reply to: