[SCM] Samba Shared Repository - branch v3-6-test updated

Volker Lendecke vlendec at samba.org
Thu Sep 23 13:20:31 MDT 2010


The branch, v3-6-test has been updated
       via  95d3591 tdb: add restore
      from  4ffe968 Thank goodness for code reviews. Volker caught - this should be lp_posix_pathnames() not lp_unix_extensions().

http://gitweb.samba.org/?p=samba.git;a=shortlog;h=v3-6-test


- Log -----------------------------------------------------------------
commit 95d3591dee6fe6fb2c63da7695bf7bd1b3accdf1
Author: Volker Lendecke <vl at samba.org>
Date:   Sat Sep 18 10:56:10 2010 +0400

    tdb: add restore
    
    Based on an idea by Simon McVittie, largely rewritten

-----------------------------------------------------------------------

Summary of changes:
 lib/tdb/config.mk                                  |    9 +
 .../tdb/manpages/tdbrestore.8.xml                  |   31 ++--
 lib/tdb/tdb.mk                                     |    7 +-
 lib/tdb/tools/tdbrestore.c                         |  226 ++++++++++++++++++++
 lib/tdb/wscript                                    |    4 +
 source3/Makefile.in                                |    9 +
 source3/configure.in                               |    2 +
 7 files changed, 273 insertions(+), 15 deletions(-)
 copy docs-xml/manpages-3/tdbdump.8.xml => lib/tdb/manpages/tdbrestore.8.xml (56%)
 create mode 100644 lib/tdb/tools/tdbrestore.c


Changeset truncated at 500 lines:

diff --git a/lib/tdb/config.mk b/lib/tdb/config.mk
index b2e322c..22caada 100644
--- a/lib/tdb/config.mk
+++ b/lib/tdb/config.mk
@@ -35,6 +35,15 @@ PRIVATE_DEPENDENCIES = \
 tdbtorture_OBJ_FILES = $(tdbsrcdir)/tools/tdbtorture.o
 
 ################################################
+# Start BINARY tdbrestore
+[BINARY::tdbrestore]
+INSTALLDIR = BINDIR
+PRIVATE_DEPENDENCIES = \
+		LIBTDB
+# End BINARY tdbrestore
+################################################
+
+################################################
 # Start BINARY tdbdump
 [BINARY::tdbdump]
 INSTALLDIR = BINDIR
diff --git a/docs-xml/manpages-3/tdbdump.8.xml b/lib/tdb/manpages/tdbrestore.8.xml
similarity index 56%
copy from docs-xml/manpages-3/tdbdump.8.xml
copy to lib/tdb/manpages/tdbrestore.8.xml
index 90465e5..64c0ba2 100644
--- a/docs-xml/manpages-3/tdbdump.8.xml
+++ b/lib/tdb/manpages/tdbrestore.8.xml
@@ -1,9 +1,9 @@
 <?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
-<refentry id="tdbdump.8">
+<refentry id="tdbrestore.8">
 
 <refmeta>
-	<refentrytitle>tdbdump</refentrytitle>
+	<refentrytitle>tdbrestore</refentrytitle>
 	<manvolnum>8</manvolnum>
 	<refmiscinfo class="source">Samba</refmiscinfo>
 	<refmiscinfo class="manual">System Administration tools</refmiscinfo>
@@ -12,14 +12,14 @@
 
 
 <refnamediv>
-	<refname>tdbdump</refname>
-	<refpurpose>tool for printing the contents of a TDB file</refpurpose>
+	<refname>tdbrestore</refname>
+	<refpurpose>tool for creating a TDB file out of a tdbdump output</refpurpose>
 </refnamediv>
 
 <refsynopsisdiv>
 	<cmdsynopsis>
-		<command>tdbdump</command>
-		<arg choice="req">filename</arg>
+		<command>tdbrestore</command>
+		<arg choice="req">tdbfilename</arg>
 	</cmdsynopsis>
 </refsynopsisdiv>
 
@@ -29,13 +29,15 @@
 	<para>This tool is part of the <citerefentry><refentrytitle>samba</refentrytitle>
 	<manvolnum>1</manvolnum></citerefentry> suite.</para>
 
-	<para><command>tdbdump</command> is a very simple utility that 'dumps' the 
-		contents of a TDB (Trivial DataBase) file to standard output in a 
-		human-readable format.
+	<para><command>tdbrestore</command> is a very simple utility that 'restores' the
+		contents of dump file into TDB (Trivial DataBase) file. The dump file is obtained from the tdbdump
+		command.
 	</para>
 
-	<para>This tool can be used when debugging problems with TDB files. It is 
-		intended for those who are somewhat familiar with Samba internals.
+	<para>This tool wait on the standard input for the content of the dump and will write the tdb in the tdbfilename
+  parameter.
+	</para>
+	<para>This tool can be used for unpacking the content of tdb as backup mean.
 	</para>
 </refsect1>
 
@@ -53,9 +55,12 @@
 	The original Samba software and related utilities were created by Andrew Tridgell.
 	Samba is now developed by the Samba Team as an Open Source project similar to the way
 	the Linux kernel is developed.
-	</para> 
 
-	<para>The tdbdump man page was written by Jelmer Vernooij.</para>
+        This tool was initially written by Volker Lendecke based on an
+        idea by Simon McVittie.
+	</para>
+
+	<para>The tdbrestore man page was written by Matthieu Patou.</para>
 </refsect1>
 
 </refentry>
diff --git a/lib/tdb/tdb.mk b/lib/tdb/tdb.mk
index 0dcd419..64d7996 100644
--- a/lib/tdb/tdb.mk
+++ b/lib/tdb/tdb.mk
@@ -1,7 +1,7 @@
 dirs::
 	@mkdir -p bin common tools
 
-PROGS = bin/tdbtool$(EXEEXT) bin/tdbdump$(EXEEXT) bin/tdbbackup$(EXEEXT)
+PROGS = bin/tdbtool$(EXEEXT) bin/tdbrestore$(EXEEXT) bin/tdbdump$(EXEEXT) bin/tdbbackup$(EXEEXT)
 PROGS_NOINSTALL = bin/tdbtest$(EXEEXT) bin/tdbtorture$(EXEEXT)
 ALL_PROGS = $(PROGS) $(PROGS_NOINSTALL)
 
@@ -23,6 +23,9 @@ bin/tdbtorture$(EXEEXT): tools/tdbtorture.o $(TDB_LIB)
 bin/tdbdump$(EXEEXT): tools/tdbdump.o $(TDB_LIB)
 	$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbdump tools/tdbdump.o -L. -ltdb $(TDB_DEPS)
 
+bin/tdbrestore$(EXEEXT): tools/tdbrestore.o $(TDB_LIB)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbrestore tools/tdbrestore.o -L. -ltdb $(TDB_DEPS)
+
 bin/tdbbackup$(EXEEXT): tools/tdbbackup.o $(TDB_LIB)
 	$(CC) $(CFLAGS) $(LDFLAGS) -o bin/tdbbackup tools/tdbbackup.o -L. -ltdb $(TDB_DEPS)
 
@@ -53,7 +56,7 @@ tdb.$(SHLIBEXT): libtdb.$(SHLIBEXT) pytdb.o
 install:: installdirs installbin installheaders installlibs \
 		  $(PYTHON_INSTALL_TARGET) installdocs
 
-doc:: manpages/tdbbackup.8 manpages/tdbdump.8 manpages/tdbtool.8
+doc:: manpages/tdbbackup.8 manpages/tdbrestore.8 manpages/tdbdump.8 manpages/tdbtool.8
 
 .SUFFIXES: .8.xml .8
 
diff --git a/lib/tdb/tools/tdbrestore.c b/lib/tdb/tools/tdbrestore.c
new file mode 100644
index 0000000..485c440
--- /dev/null
+++ b/lib/tdb/tools/tdbrestore.c
@@ -0,0 +1,226 @@
+/*
+   tdbrestore -- construct a tdb from tdbdump output.
+   Copyright (C) Volker Lendecke		2010
+   Copyright (C) Simon McVittie			2005
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include <assert.h>
+#include "replace.h"
+#include "system/locale.h"
+#include "system/time.h"
+#include "system/filesys.h"
+#include "system/wait.h"
+#include "tdb.h"
+
+#define debug_fprintf(file, fmt, ...) do {/*nothing*/} while (0)
+
+static int read_linehead(FILE *f)
+{
+	int i, c;
+	int num_bytes;
+	char prefix[128];
+
+	while (1) {
+		c = getc(f);
+		if (c == EOF) {
+			return -1;
+		}
+		if (c == '\(') {
+			break;
+		}
+	}
+	for (i=0; i<sizeof(prefix); i++) {
+		c = getc(f);
+		if (c == EOF) {
+			return -1;
+		}
+		prefix[i] = c;
+		if (c == '"') {
+			break;
+		}
+	}
+	if (i == sizeof(prefix)) {
+		return -1;
+	}
+	prefix[i] = '\0';
+
+	if (sscanf(prefix, "%d) = ", &num_bytes) != 1) {
+		return -1;
+	}
+	return num_bytes;
+}
+
+static int read_hex(void) {
+	int c;
+	c = getchar();
+	if (c == EOF) {
+		fprintf(stderr, "Unexpected EOF in data\n");
+		return -1;
+	} else if (c == '"') {
+		fprintf(stderr, "Unexpected \\\" sequence\n");
+		return -1;
+	} else if ('0' <= c && c <= '9')  {
+		return c - '0';
+	} else if ('A' <= c && c <= 'F')  {
+		return c - 'A' + 10;
+	} else if ('a' <= c && c <= 'f')  {
+		return c - 'a' + 10;
+	} else {
+		fprintf(stderr, "Invalid hex: %c\n", c);
+		return -1;
+	}
+}
+
+static int read_data(FILE *f, TDB_DATA *d, size_t size) {
+	int c, low, high;
+	int i;
+
+	d->dptr = (unsigned char *)malloc(size);
+	if (d->dptr == NULL) {
+		return -1;
+	}
+	d->dsize = size;
+
+	for (i=0; i<size; i++) {
+		c = getc(f);
+		if (c == EOF) {
+			fprintf(stderr, "Unexpected EOF in data\n");
+			return 1;
+		} else if (c == '"') {
+			return 0;
+		} else if (c == '\\') {
+			high = read_hex();
+			if (high < 0) {
+				return -1;
+			}
+			high = high << 4;
+			assert(high == (high & 0xf0));
+			low = read_hex();
+			if (low < 0) {
+				return -1;
+			}
+			assert(low == (low & 0x0f));
+			d->dptr[i] = (low|high);
+		} else {
+			d->dptr[i] = c;
+		}
+	}
+	return 0;
+}
+
+static int swallow(FILE *f, const char *s, int *eof)
+{
+	char line[128];
+
+	if (fgets(line, sizeof(line), f) == NULL) {
+		if (eof != NULL) {
+			*eof = 1;
+		}
+		return -1;
+	}
+	if (strcmp(line, s) != 0) {
+		return -1;
+	}
+	return 0;
+}
+
+static int read_rec(FILE *f, TDB_CONTEXT *tdb, int *eof)
+{
+	int length;
+	TDB_DATA key, data;
+	int ret = -1;
+
+	key.dptr = NULL;
+	data.dptr = NULL;
+
+	if (swallow(f, "{\n", eof) == -1) {
+		goto fail;
+	}
+	length = read_linehead(f);
+	if (length == -1) {
+		goto fail;
+	}
+	if (read_data(f, &key, length) == -1) {
+		goto fail;
+	}
+	if (swallow(f, "\"\n", NULL) == -1) {
+		goto fail;
+	}
+	length = read_linehead(f);
+	if (length == -1) {
+		goto fail;
+	}
+	if (read_data(f, &data, length) == -1) {
+		goto fail;
+	}
+	if ((swallow(f, "\"\n", NULL) == -1)
+	    || (swallow(f, "}\n", NULL) == -1)) {
+		goto fail;
+	}
+	if (tdb_store(tdb, key, data, TDB_INSERT) == -1) {
+		fprintf(stderr, "TDB error: %s\n", tdb_errorstr(tdb));
+		goto fail;
+	}
+
+	ret = 0;
+fail:
+	free(key.dptr);
+	free(data.dptr);
+	return ret;
+}
+
+static int restore_tdb(const char *fname)
+{
+	TDB_CONTEXT *tdb;
+
+	tdb = tdb_open(fname, 0, 0, O_RDWR|O_CREAT|O_EXCL, 0666);
+	if (!tdb) {
+		perror("tdb_open");
+		fprintf(stderr, "Failed to open %s\n", fname);
+		return 1;
+	}
+
+	while (1) {
+		int eof = 0;
+		if (read_rec(stdin, tdb, &eof) == -1) {
+			if (eof) {
+				break;
+			}
+			return 1;
+		}
+	}
+	if (tdb_close(tdb)) {
+		fprintf(stderr, "Error closing tdb\n");
+		return 1;
+	}
+	fprintf(stderr, "EOF\n");
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	char *fname;
+
+	if (argc < 2) {
+		printf("Usage: %s dbname < tdbdump_output\n", argv[0]);
+		exit(1);
+	}
+
+	fname = argv[1];
+
+	return restore_tdb(fname);
+}
diff --git a/lib/tdb/wscript b/lib/tdb/wscript
index d661e41..ff468f2 100644
--- a/lib/tdb/wscript
+++ b/lib/tdb/wscript
@@ -75,6 +75,10 @@ def build(bld):
                          'tdb',
                          install=False)
 
+        bld.SAMBA_BINARY('tdbrestore',
+                         'tools/tdbrestore.c',
+                         'tdb', manpages='manpages/tdbrestore.8')
+
         bld.SAMBA_BINARY('tdbdump',
                          'tools/tdbdump.c',
                          'tdb', manpages='manpages/tdbdump.8')
diff --git a/source3/Makefile.in b/source3/Makefile.in
index e76ab23..35ab705 100644
--- a/source3/Makefile.in
+++ b/source3/Makefile.in
@@ -94,6 +94,7 @@ TALLOCTORT = @TALLOCTORT@
 TDBBACKUP = @TDBBACKUP@
 TDBTOOL = @TDBTOOL@
 TDBDUMP = @TDBDUMP@
+TDBRESTORE = @TDBRESTORE@
 TDBTORTURE = @TDBTORTURE@
 
 INSTALLCMD=@INSTALL@
@@ -215,6 +216,7 @@ BIN_PROGS1 = bin/smbclient at EXEEXT@ bin/net at EXEEXT@ bin/smbspool at EXEEXT@ \
 	bin/smbta-util at EXEEXT@
 BIN_PROGS2 = bin/smbcontrol at EXEEXT@ bin/smbtree at EXEEXT@ $(TDBBACKUP) \
 	bin/nmblookup at EXEEXT@ bin/pdbedit at EXEEXT@ $(TDBDUMP) \
+	$(TDBRESTORE) \
 	$(TDBTOOL)
 BIN_PROGS3 = bin/smbpasswd at EXEEXT@ bin/rpcclient at EXEEXT@ bin/smbcacls at EXEEXT@ \
 	bin/profiles at EXEEXT@ bin/ntlm_auth at EXEEXT@ bin/sharesec at EXEEXT@ \
@@ -1420,6 +1422,9 @@ TDBTOOL_OBJ = @tdbdir@/tools/tdbtool.o $(LIBREPLACE_OBJ) \
 TDBDUMP_OBJ = @tdbdir@/tools/tdbdump.o $(LIBREPLACE_OBJ) \
 	$(SOCKET_WRAPPER_OBJ)
 
+TDBRESTORE_OBJ = @tdbdir@/tools/tdbrestore.o $(LIBREPLACE_OBJ) \
+	$(SOCKET_WRAPPER_OBJ)
+
 TDBTORTURE_OBJ = @tdbdir@/tools/tdbtorture.o $(LIBREPLACE_OBJ) \
 	$(SOCKET_WRAPPER_OBJ)
 
@@ -3024,6 +3029,10 @@ bin/tdbdump at EXEEXT@: $(BINARY_PREREQS) $(TDBDUMP_OBJ) $(LIBTALLOC) $(LIBTDB)
 	@$(CC) -o $@ $(LDFLAGS) $(TDBDUMP_OBJ) $(DYNEXP) \
 		$(LIBS) $(LIBTALLOC_LIBS) $(LIBTDB_LIBS)
 
+bin/tdbrestore at EXEEXT@: $(BINARY_PREREQS) $(TDBRESTORE_OBJ) $(LIBTDB)
+	@echo Linking $@
+	@$(CC) -o $@ $(LDFLAGS) $(TDBRESTORE_OBJ) $(DYNEXP) $(LIBTDB_LIBS) $(LIBREPLACE_LIBS)
+
 bin/tdbtorture at EXEEXT@: $(BINARY_PREREQS) $(TDBTORTURE_OBJ) $(LIBTALLOC) $(LIBTDB)
 	@echo Linking $@
 	@$(CC) -o $@ $(LDFLAGS) $(TDBTORTURE_OBJ) $(DYNEXP) \
diff --git a/source3/configure.in b/source3/configure.in
index 28ca2ef..c86494e 100644
--- a/source3/configure.in
+++ b/source3/configure.in
@@ -2152,6 +2152,8 @@ then
 	AC_SUBST(TDBBACKUP)
 	TDBDUMP="bin/tdbdump\$(EXEEXT)"
 	AC_SUBST(TDBDUMP)
+	TDBRESTORE="bin/tdbrestore\$(EXEEXT)"
+	AC_SUBST(TDBRESTORE)
 	TDBTOOL="bin/tdbtool\$(EXEEXT)"
 	AC_SUBST(TDBTOOL)
 	TDBTORTURE="bin/tdbtorture\$(EXEEXT)"


-- 
Samba Shared Repository


More information about the samba-cvs mailing list