chiark / gitweb /
Print that we are updating for every rsync
[ypp-sc-tools.db-test.git] / pctb / common.c
index 7fa5b44bc6b393a922ee8ea8db120b7fec377205..2c2a085a30e9fdaa7ded64ffa7b10c82f31208ac 100644 (file)
 
 #include "common.h"
 
-#define dbassert(x)                                                    \
-  ((x) ? (void)0 :                                                     \
-     fatal("Error in database.\n"                                      \
-          " Requirement not met: %s:%d: %s", __FILE__,__LINE__, #x))
-
-void fgetsline(FILE *f, char *lbuf, size_t lbufsz) {
-  errno=0;
-  char *s= fgets(lbuf,lbufsz,f);
-  sysassert(!ferror(f));
-  dbassert(!feof(f));
-  assert(s);
-  int l= strlen(lbuf);
-  dbassert(l>0);  dbassert(lbuf[--l]='\n');
-  lbuf[l]= 0;
-}
-
 void *mmalloc(size_t sz) {
   void *r;
   if (!sz) return 0;
@@ -55,3 +39,112 @@ void *mrealloc(void *p, size_t sz) {
   sysassert( r= realloc(p,sz) );
   return r;
 }
+
+
+FILE *dbfile;
+static const char *basepath; /* as passed in by caller */
+static pid_t dbzcat;
+
+int dbfile_gzopen(const char *basepath_spec) {
+  assert(!dbfile);
+
+  basepath= basepath_spec;
+  //uncomppath= masprintf("%s (uncompressed)", basepath);
+
+  char *zpath= masprintf("%s.gz", basepath);
+  int zfd= open(zpath, O_RDONLY);
+  free(zpath);
+
+  if (zfd<0) { sysassert(errno==ENOENT); return 0; }
+
+  int pipefds[2];
+  sysassert(! pipe(pipefds) );
+
+  sysassert( (dbzcat=fork()) != -1 );
+  if (!dbzcat) {
+    sysassert( dup2(zfd,0)==0 );
+    sysassert( dup2(pipefds[1],1)==1 );
+    sysassert(! close(zfd) );
+    sysassert(! close(pipefds[0]) );
+    sysassert(! close(pipefds[1]) );
+    execlp("zcat","zcat",(char*)0);
+    sysassert(!"execlp zcat");
+  }
+  sysassert(! close(zfd) );
+  sysassert(! close(pipefds[1]) );
+  sysassert( dbfile= fdopen(pipefds[0], "r") );
+
+  return 1;
+}  
+
+int dbfile_open(const char *tpath) {
+  assert(!dbfile);
+
+  basepath= tpath;
+
+  dbzcat= -1;
+  dbfile= fopen(tpath,"r");
+  if (!dbfile) { sysassert(errno==ENOENT); return 0; }
+  return 1;
+}  
+
+void dbfile_close(void) {
+  if (!dbfile) return;
+
+  sysassert(!ferror(dbfile));
+  sysassert(!fclose(dbfile));
+
+  if (dbzcat != -1) {
+    char *zcatstr= masprintf("zcat %s.gz", basepath);
+    waitpid_check_exitstatus(dbzcat,zcatstr,1);
+    free(zcatstr);
+    dbzcat= -1;
+  }
+
+  dbfile= 0;
+}
+
+#define dbassertgl(x) ((x) ? (void)0 : dbfile_assertfail(file,line,#x))
+
+void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line) {
+  errno=0;
+  char *s= fgets(lbuf,lbufsz,dbfile);
+  sysassert(!ferror(dbfile));
+  dbassertgl(!feof(dbfile));
+  assert(s);
+  int l= strlen(lbuf);
+  dbassertgl(l>0);  dbassertgl(lbuf[--l]=='\n');
+  lbuf[l]= 0;
+}
+
+int dbfile_vscanf(const char *fmt, va_list al) {
+  int r= vfscanf(dbfile,fmt,al);
+  sysassert(!ferror(dbfile));
+  return r;
+}
+
+int dbfile_scanf(const char *fmt, ...) {
+  va_list al;
+  va_start(al,fmt);
+  int r= dbfile_vscanf(fmt,al);
+  va_end(al);
+  return r;
+}
+
+void dbfile_assertfail(const char *file, int line, const char *m) {
+  if (dbzcat)
+    fatal("Error in dictionary file %s.gz:\n"
+         " Requirement not met at %s:%d:\n"
+         " %s",
+         basepath, file,line, m);
+  else if (dbfile)
+    fatal("Error in dictionary file %s at byte %ld:\n"
+         " Requirement not met at %s:%d:\n"
+         " %s",
+         basepath,(long)ftell(dbfile), file,line, m);
+  else
+    fatal("Semantic error in dictionaries:\n"
+         " Requirement not met at %s:%d:\n"
+         " %s",
+         file,line, m);
+}