util_tdb: add function tdb_data_string()
authorGregor Beck <gbeck@sernet.de>
Wed, 20 Mar 2013 08:56:54 +0000 (09:56 +0100)
committerMichael Adam <obnox@samba.org>
Thu, 18 Apr 2013 11:15:11 +0000 (13:15 +0200)
Signed-off-by: Gregor Beck <gbeck@sernet.de>
Reviewed-by: Michael Adam <obnox@samba.org>
Reviewed-by: Stefan Metzmacher <metze@samba.org>
source3/include/util_tdb.h
source3/lib/util_tdb.c

index e350413cf9f05d62dfe6f2ce845b1425fed2b205..c9e9e409c48f7d51e7e33a342747b8475ab0ba08 100644 (file)
@@ -44,4 +44,6 @@ NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err);
 
 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2);
 
+char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d);
+
 #endif /* __TDBUTIL_H__ */
index 8bfc75f18b65631459b1c45e841056e067281618..440c28b98d249140e6434adab7487df3b0bbac65 100644 (file)
@@ -22,6 +22,7 @@
 #include "includes.h"
 #include "system/filesys.h"
 #include "util_tdb.h"
+#include "cbuf.h"
 
 #undef malloc
 #undef realloc
@@ -418,3 +419,35 @@ int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
        }
        return ret;
 }
+
+char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d)
+{
+       int len;
+       char *ret = NULL;
+       cbuf *ost = cbuf_new(mem_ctx);
+
+       if (ost == NULL) {
+               return NULL;
+       }
+
+       len = cbuf_printf(ost, "%d:");
+       if (len == -1) {
+               goto done;
+       }
+
+       if (d.dptr == NULL) {
+               len = cbuf_puts(ost, "<NULL>", -1);
+       } else {
+               len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
+       }
+       if (len == -1) {
+               goto done;
+       }
+
+       cbuf_swapptr(ost, &ret, 0);
+       talloc_steal(mem_ctx, ret);
+
+done:
+       talloc_free(ost);
+       return ret;
+}