4 #include <string.h> /* memcpy() */
5 #include <stdlib.h> /* malloc() and friends */
7 /* tdb.h needs mode_t defined externally, and it is defined in one of these
8 (which are the ones required for open() */
16 /* Local operations names are prepended with an 'x' so they don't collide with
17 * tdb real functions */
19 int xtdb_set(struct db_conn
*db
, const unsigned char *key
, size_t ksize
,
20 unsigned char *val
, size_t vsize
);
21 int xtdb_get(struct db_conn
*db
, const unsigned char *key
, size_t ksize
,
22 unsigned char *val
, size_t *vsize
);
23 int xtdb_del(struct db_conn
*db
, const unsigned char *key
, size_t ksize
);
24 int xtdb_close(struct db_conn
*db
);
27 struct db_conn
*xtdb_open(const char *name
, int flags
)
32 tdb_db
= tdb_open(name
, 0, 0, O_CREAT
| O_RDWR
, 0640);
36 db
= malloc(sizeof(struct db_conn
));
46 db
->close
= xtdb_close
;
51 int xtdb_close(struct db_conn
*db
)
55 rv
= tdb_close(db
->conn
);
60 int xtdb_set(struct db_conn
*db
, const unsigned char *key
, size_t ksize
,
61 unsigned char *val
, size_t vsize
)
65 /* we can't maintain "const"ness here because tdb's prototypes; the
66 * same applies to get and del, so we just cast */
67 k
.dptr
= (unsigned char *) key
;
69 v
.dptr
= (unsigned char *) val
;
72 return tdb_store(db
->conn
, k
, v
, TDB_REPLACE
) == 0;
75 int xtdb_get(struct db_conn
*db
, const unsigned char *key
, size_t ksize
,
76 unsigned char *val
, size_t *vsize
)
80 k
.dptr
= (unsigned char *) key
;
83 v
= tdb_fetch(db
->conn
, k
);
91 memcpy(val
, v
.dptr
, v
.dsize
);
96 int xtdb_del(struct db_conn
*db
, const unsigned char *key
, size_t ksize
)
100 k
.dptr
= (unsigned char *) key
;
103 return tdb_delete(db
->conn
, k
) == 0;
108 #include <stddef.h> /* NULL */
110 struct db_conn
*xtdb_open(const char *name
, int flags
)