summaryrefslogtreecommitdiff
path: root/source3/tdb/tdbtool.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>1999-12-21 03:04:37 +0000
committerAndrew Tridgell <tridge@samba.org>1999-12-21 03:04:37 +0000
commitc2c42b8694be0b2ce271613153cca71dc8e83a9f (patch)
tree8c068082fa8b9010c61f06c7285f05ffe1a3d8f9 /source3/tdb/tdbtool.c
parente004340f715743e16f599bd0be3e9dad4c53bf2d (diff)
downloadsamba-c2c42b8694be0b2ce271613153cca71dc8e83a9f.tar.gz
samba-c2c42b8694be0b2ce271613153cca71dc8e83a9f.tar.bz2
samba-c2c42b8694be0b2ce271613153cca71dc8e83a9f.zip
first pass at the database code for Samba. This also includes a test
suite and a very simple tool for manuipulating the databases. the main code is in tdb/tdb.c and includes both mmap and file based IO. All databases auto-expand and allow multiple simultaneous writers. the next step is using this new capability in lots of places in Samba where we have existing ad-hoc databases (This used to be commit c89d29cc5e3b6d568928acace01144059f1668b3)
Diffstat (limited to 'source3/tdb/tdbtool.c')
-rw-r--r--source3/tdb/tdbtool.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/source3/tdb/tdbtool.c b/source3/tdb/tdbtool.c
new file mode 100644
index 0000000000..766ce0ff47
--- /dev/null
+++ b/source3/tdb/tdbtool.c
@@ -0,0 +1,206 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <readline/readline.h>
+#include <readline/history.h>
+#include "tdb.h"
+
+/* a tdb tool for manipulating a tdb database */
+
+static TDB_CONTEXT *tdb;
+
+static void help(void)
+{
+ printf("
+tdbtool:
+ create dbname : create a database
+ open dbname : open an existing database
+ erase : erase the database
+ dump : dump the database as strings
+ insert key data : insert a record
+ store key data : store a record (replace)
+ show key : show a record by key
+ delete key : delete a record by key
+");
+}
+
+static void terror(char *why)
+{
+ printf("%s\n", why);
+}
+
+static void create_tdb(void)
+{
+ char *tok = strtok(NULL, " ");
+ if (!tok) {
+ help();
+ return;
+ }
+ if (tdb) tdb_close(tdb);
+ tdb = tdb_open(tok, 0, O_RDWR | O_CREAT | O_TRUNC, 0600);
+}
+
+static void open_tdb(void)
+{
+ char *tok = strtok(NULL, " ");
+ if (!tok) {
+ help();
+ return;
+ }
+ if (tdb) tdb_close(tdb);
+ tdb = tdb_open(tok, 0, O_RDWR, 0600);
+}
+
+static void insert_tdb(void)
+{
+ char *k = strtok(NULL, " ");
+ char *d = strtok(NULL, " ");
+ TDB_DATA key, dbuf;
+
+ if (!k || !d) {
+ help();
+ return;
+ }
+
+ key.dptr = k;
+ key.dsize = strlen(k);
+ dbuf.dptr = d;
+ dbuf.dsize = strlen(d);
+
+ if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
+ terror("insert failed");
+ }
+}
+
+static void store_tdb(void)
+{
+ char *k = strtok(NULL, " ");
+ char *d = strtok(NULL, " ");
+ TDB_DATA key, dbuf;
+
+ if (!k || !d) {
+ help();
+ return;
+ }
+
+ key.dptr = k;
+ key.dsize = strlen(k);
+ dbuf.dptr = d;
+ dbuf.dsize = strlen(d);
+
+ if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
+ terror("store failed");
+ }
+}
+
+static void show_tdb(void)
+{
+ char *k = strtok(NULL, " ");
+ TDB_DATA key, dbuf;
+
+ if (!k) {
+ help();
+ return;
+ }
+
+ key.dptr = k;
+ key.dsize = strlen(k);
+
+ dbuf = tdb_fetch(tdb, key);
+ if (!dbuf.dptr) terror("fetch failed");
+ printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr);
+}
+
+static void delete_tdb(void)
+{
+ char *k = strtok(NULL, " ");
+ TDB_DATA key;
+
+ if (!k) {
+ help();
+ return;
+ }
+
+ key.dptr = k;
+ key.dsize = strlen(k);
+
+ if (tdb_delete(tdb, key) != 0) {
+ terror("delete failed");
+ }
+}
+
+static int print_rec(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf)
+{
+ printf("%*.*s : %*.*s\n",
+ (int)key.dsize, (int)key.dsize, key.dptr,
+ (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr);
+ return 0;
+}
+
+static int total_bytes;
+
+static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf)
+{
+ total_bytes += dbuf.dsize;
+ return 0;
+}
+
+static void info_tdb(void)
+{
+ int count;
+ total_bytes = 0;
+ count = tdb_traverse(tdb, traverse_fn);
+ printf("%d records totalling %d bytes\n", count, total_bytes);
+}
+
+int main(int argc, char *argv[])
+{
+ char *line;
+ char *tok;
+
+ while ((line=readline("tdb> "))) {
+ tok = strtok(line," ");
+ if (strcmp(tok,"create") == 0) {
+ create_tdb();
+ continue;
+ } else if (strcmp(tok,"open") == 0) {
+ open_tdb();
+ continue;
+ }
+
+ /* all the rest require a open database */
+ if (!tdb) {
+ terror("database not open");
+ help();
+ continue;
+ }
+
+ if (strcmp(tok,"insert") == 0) {
+ insert_tdb();
+ } else if (strcmp(tok,"store") == 0) {
+ store_tdb();
+ } else if (strcmp(tok,"show") == 0) {
+ show_tdb();
+ } else if (strcmp(tok,"erase") == 0) {
+ tdb_traverse(tdb, tdb_delete);
+ } else if (strcmp(tok,"delete") == 0) {
+ delete_tdb();
+ } else if (strcmp(tok,"dump") == 0) {
+ tdb_traverse(tdb, print_rec);
+ } else if (strcmp(tok,"info") == 0) {
+ info_tdb();
+ } else {
+ help();
+ }
+ }
+
+ if (tdb) tdb_close(tdb);
+
+ return 0;
+}