diff cdb.c @ 16:70f4df5a7283

add cdb_write, log basic mmap outcomes
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Thu, 27 Feb 2025 18:26:29 +0000
parents 0e21568dec72
children d9c11148df3b
line wrap: on
line diff
--- a/cdb.c	Thu Feb 27 18:24:24 2025 +0000
+++ b/cdb.c	Thu Feb 27 18:26:29 2025 +0000
@@ -3,16 +3,21 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
+#include <stdio.h>
 #include "readwrite.h"
+#include "strerr.h"
 #include "error.h"
 #include "seek.h"
 #include "byte.h"
 #include "alloc.h"
+#include "buffer.h"
 #include "cdb.h"
 
 
 /*static Cdb c;*/
 
+static char _buf[32];
+
 Cdb* cdb_new(void)
 {
   return (Cdb *)(alloc(sizeof(Cdb)));
@@ -58,7 +63,11 @@
 
   if (fstat(fd,&st) == 0)
     if (st.st_size <= 0xffffffff) {
+      sprintf(_buf,"%'u",st.st_size);
+      strerr_warn3("cdb_init ","fd size: ",_buf,0);
       x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
+      sprintf(_buf,"%x",x);
+      strerr_warn3("cdb_init ","mmap addr: ",_buf,0);
       if (x + 1) {
 	c->size = st.st_size;
 	c->map = x;
@@ -66,6 +75,17 @@
     }
 }
 
+void cdb_init_nomap(Cdb *c,int fd)
+{
+  struct stat st;
+  char *x;
+
+  cdb_clear(c);
+  cdb_findstart(c);
+  c->fd = fd;
+}
+
+
 int cdb_read(Cdb *c,char *buf,unsigned int len,uint32 pos)
 {
   if (c->map) {
@@ -159,3 +179,25 @@
   cdb_findstart(c);
   return cdb_findnext(c,key,len);
 }
+
+char wbuf[1024];
+
+int cdb_write(Cdb *c,int fd)
+{
+  uint32 pos;
+  uint32 len;
+  int r;
+
+  pos = cdb_datapos(c);
+  len = cdb_datalen(c);
+
+  while (len > 0) {
+    r = sizeof wbuf;
+    if (r > len) r = len;
+    if (cdb_read(c,wbuf,r,pos) == -1) return -1;
+    if (buffer_put(buffer_1small,wbuf,r) == -1) return -2;
+    pos += r;
+    len -= r;
+  }
+  if (buffer_flush(buffer_1small) == -1) return -2;
+}