mirror of https://github.com/vitalif/e2fsprogs
Add tdb from Samba into libext2fs library.
Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>bitmap-optimize
parent
4aec958561
commit
106ad96dae
|
@ -1,3 +1,8 @@
|
|||
2007-04-04 Theodore Tso <tytso@mit.edu>
|
||||
|
||||
* configure, configure.in: Add checks for mmap(), utime(), and
|
||||
strdup(), as well as sys/mman.h and utime.h.
|
||||
|
||||
2006-11-14 Theodore Tso <tytso@mit.edu>
|
||||
|
||||
* MCONFIG.in (WFLAGS): Add -fstrict-aliasing and
|
||||
|
|
|
@ -12558,7 +12558,9 @@ fi
|
|||
|
||||
|
||||
|
||||
for ac_header in stdlib.h unistd.h stdarg.h stdint.h errno.h malloc.h mntent.h paths.h dirent.h getopt.h setjmp.h signal.h termios.h linux/fd.h linux/major.h sys/disklabel.h sys/ioctl.h sys/mkdev.h sys/prctl.h sys/queue.h sys/sockio.h sys/socket.h sys/sysmacros.h sys/time.h sys/stat.h sys/types.h sys/wait.h sys/resource.h net/if_dl.h netinet/in.h
|
||||
|
||||
|
||||
for ac_header in stdlib.h unistd.h stdarg.h stdint.h errno.h malloc.h mntent.h paths.h dirent.h getopt.h setjmp.h signal.h termios.h linux/fd.h linux/major.h sys/disklabel.h sys/ioctl.h sys/mman.h sys/mkdev.h sys/prctl.h sys/queue.h sys/sockio.h sys/socket.h sys/sysmacros.h sys/time.h sys/stat.h sys/types.h sys/wait.h sys/resource.h net/if_dl.h netinet/in.h utime.h
|
||||
do
|
||||
as_ac_Header=`echo "ac_cv_header_$ac_header" | $as_tr_sh`
|
||||
if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then
|
||||
|
@ -16324,7 +16326,10 @@ fi
|
|||
|
||||
|
||||
|
||||
for ac_func in chflags getrusage llseek lseek64 open64 fstat64 getmntinfo strtoull strcasecmp srandom jrand48 fchown mallinfo fdatasync strnlen strptime sysconf pathconf posix_memalign memalign valloc __secure_getenv prctl
|
||||
|
||||
|
||||
|
||||
for ac_func in chflags getrusage llseek lseek64 open64 fstat64 getmntinfo strtoull strcasecmp srandom jrand48 fchown mallinfo fdatasync strnlen strptime strdup sysconf pathconf posix_memalign memalign valloc __secure_getenv prctl mmap utime
|
||||
do
|
||||
as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh`
|
||||
{ echo "$as_me:$LINENO: checking for $ac_func" >&5
|
||||
|
|
|
@ -569,7 +569,7 @@ if test $cross_compiling = no; then
|
|||
else
|
||||
AC_CHECK_PROGS(BUILD_CC, gcc cc)
|
||||
fi
|
||||
AC_CHECK_HEADERS(stdlib.h unistd.h stdarg.h stdint.h errno.h malloc.h mntent.h paths.h dirent.h getopt.h setjmp.h signal.h termios.h linux/fd.h linux/major.h sys/disklabel.h sys/ioctl.h sys/mkdev.h sys/prctl.h sys/queue.h sys/sockio.h sys/socket.h sys/sysmacros.h sys/time.h sys/stat.h sys/types.h sys/wait.h sys/resource.h net/if_dl.h netinet/in.h)
|
||||
AC_CHECK_HEADERS(stdlib.h unistd.h stdarg.h stdint.h errno.h malloc.h mntent.h paths.h dirent.h getopt.h setjmp.h signal.h termios.h linux/fd.h linux/major.h sys/disklabel.h sys/ioctl.h sys/mman.h sys/mkdev.h sys/prctl.h sys/queue.h sys/sockio.h sys/socket.h sys/sysmacros.h sys/time.h sys/stat.h sys/types.h sys/wait.h sys/resource.h net/if_dl.h netinet/in.h utime.h)
|
||||
AC_CHECK_HEADERS(sys/disk.h sys/mount.h,,,
|
||||
[[
|
||||
#if HAVE_SYS_QUEUE_H
|
||||
|
@ -675,7 +675,7 @@ AC_CHECK_MEMBER(struct sockaddr.sa_len,
|
|||
[#include <sys/types.h>
|
||||
#include <sys/socket.h>])
|
||||
dnl
|
||||
AC_CHECK_FUNCS(chflags getrusage llseek lseek64 open64 fstat64 getmntinfo strtoull strcasecmp srandom jrand48 fchown mallinfo fdatasync strnlen strptime sysconf pathconf posix_memalign memalign valloc __secure_getenv prctl)
|
||||
AC_CHECK_FUNCS(chflags getrusage llseek lseek64 open64 fstat64 getmntinfo strtoull strcasecmp srandom jrand48 fchown mallinfo fdatasync strnlen strptime strdup sysconf pathconf posix_memalign memalign valloc __secure_getenv prctl mmap utime)
|
||||
dnl
|
||||
dnl Check to see if -lsocket is required (solaris) to make something
|
||||
dnl that uses socket() to compile; this is needed for the UUID library
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
2007-04-04 Theodore Tso <tytso@mit.edu>
|
||||
|
||||
* tdb.c, tdb.h, Makefile.in: Add the tdb library from Samba into
|
||||
ext2fs.
|
||||
|
||||
* tst_types.c: Add #include's to avoid compiler warnings.
|
||||
|
||||
2007-04-02 Theodore Tso <tytso@mit.edu>
|
||||
|
|
|
@ -62,6 +62,7 @@ OBJS= $(DEBUGFS_LIB_OBJS) $(RESIZE_LIB_OBJS) $(E2IMAGE_LIB_OBJS) \
|
|||
res_gdt.o \
|
||||
rw_bitmaps.o \
|
||||
swapfs.o \
|
||||
tdb.o \
|
||||
unix_io.o \
|
||||
unlink.o \
|
||||
valid_blk.o \
|
||||
|
@ -122,6 +123,7 @@ SRCS= ext2_err.c \
|
|||
$(srcdir)/rs_bitmap.c \
|
||||
$(srcdir)/rw_bitmaps.c \
|
||||
$(srcdir)/swapfs.c \
|
||||
$(srcdir)/tdb.c \
|
||||
$(srcdir)/test_io.c \
|
||||
$(srcdir)/unix_io.c \
|
||||
$(srcdir)/unlink.c \
|
||||
|
@ -134,7 +136,8 @@ SRCS= ext2_err.c \
|
|||
$(srcdir)/tst_getsize.c \
|
||||
$(srcdir)/tst_iscan.c
|
||||
|
||||
HFILES= bitops.h ext2fs.h ext2_io.h ext2_fs.h ext2_ext_attr.h ext3_extents.h
|
||||
HFILES= bitops.h ext2fs.h ext2_io.h ext2_fs.h ext2_ext_attr.h ext3_extents.h \
|
||||
tdb.h
|
||||
HFILES_IN= ext2_err.h ext2_types.h
|
||||
|
||||
LIBRARY= libext2fs
|
||||
|
@ -224,6 +227,10 @@ tst_types: tst_types.o ext2_types.h
|
|||
@echo " LD $@"
|
||||
@$(CC) -o tst_types tst_types.o
|
||||
|
||||
ext2_tdbtool: tdbtool.o
|
||||
@echo " LD $@"
|
||||
@$(CC) -o ext2_tdbtool tdbtool.o tdb.o
|
||||
|
||||
mkjournal: mkjournal.c $(STATIC_LIBEXT2FS)
|
||||
@echo " LD $@"
|
||||
@$(CC) -o mkjournal $(srcdir)/mkjournal.c -DDEBUG $(STATIC_LIBEXT2FS) $(LIBCOM_ERR) $(ALL_CFLAGS)
|
||||
|
@ -265,7 +272,8 @@ clean::
|
|||
$(RM) -f \#* *.s *.o *.a *~ *.bak core profiled/* checker/* \
|
||||
tst_badblocks tst_iscan ext2_err.et ext2_err.c ext2_err.h \
|
||||
tst_byteswap tst_ismounted tst_getsize tst_sectgetsize \
|
||||
mkjournal ../libext2fs.a ../libext2fs_p.a ../libext2fs_chk.a
|
||||
tst_bitops tst_types ext2_tdbtool mkjournal \
|
||||
../libext2fs.a ../libext2fs_p.a ../libext2fs_chk.a
|
||||
|
||||
mostlyclean:: clean
|
||||
distclean:: clean
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,197 @@
|
|||
#ifndef __TDB_H__
|
||||
#define __TDB_H__
|
||||
|
||||
/*
|
||||
Unix SMB/CIFS implementation.
|
||||
|
||||
trivial database library
|
||||
|
||||
Copyright (C) Andrew Tridgell 1999-2004
|
||||
|
||||
** NOTE! The following LGPL license applies to the tdb
|
||||
** library. This does NOT imply that all of Samba is released
|
||||
** under the LGPL
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library 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
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* flags to tdb_store() */
|
||||
#define TDB_REPLACE 1
|
||||
#define TDB_INSERT 2
|
||||
#define TDB_MODIFY 3
|
||||
|
||||
/* flags for tdb_open() */
|
||||
#define TDB_DEFAULT 0 /* just a readability place holder */
|
||||
#define TDB_CLEAR_IF_FIRST 1
|
||||
#define TDB_INTERNAL 2 /* don't store on disk */
|
||||
#define TDB_NOLOCK 4 /* don't do any locking */
|
||||
#define TDB_NOMMAP 8 /* don't use mmap */
|
||||
#define TDB_CONVERT 16 /* convert endian (internal use) */
|
||||
#define TDB_BIGENDIAN 32 /* header is big-endian (internal use) */
|
||||
#define TDB_NOSYNC 64 /* don't use synchronous transactions */
|
||||
#define TDB_SEQNUM 128 /* maintain a sequence number */
|
||||
|
||||
#define TDB_ERRCODE(code, ret) ((tdb->ecode = (code)), ret)
|
||||
|
||||
/* error codes */
|
||||
enum TDB_ERROR {TDB_SUCCESS=0, TDB_ERR_CORRUPT, TDB_ERR_IO, TDB_ERR_LOCK,
|
||||
TDB_ERR_OOM, TDB_ERR_EXISTS, TDB_ERR_NOLOCK, TDB_ERR_LOCK_TIMEOUT,
|
||||
TDB_ERR_NOEXIST, TDB_ERR_EINVAL, TDB_ERR_RDONLY};
|
||||
|
||||
/* debugging uses one of the following levels */
|
||||
enum tdb_debug_level {TDB_DEBUG_FATAL = 0, TDB_DEBUG_ERROR,
|
||||
TDB_DEBUG_WARNING, TDB_DEBUG_TRACE};
|
||||
|
||||
typedef struct TDB_DATA {
|
||||
unsigned char *dptr;
|
||||
size_t dsize;
|
||||
} TDB_DATA;
|
||||
|
||||
#ifndef PRINTF_ATTRIBUTE
|
||||
#if (__GNUC__ >= 3)
|
||||
/** Use gcc attribute to check printf fns. a1 is the 1-based index of
|
||||
* the parameter containing the format, and a2 the index of the first
|
||||
* argument. Note that some gcc 2.x versions don't handle this
|
||||
* properly **/
|
||||
#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
|
||||
#else
|
||||
#define PRINTF_ATTRIBUTE(a1, a2)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* ext2fs tdb renames */
|
||||
#define tdb_open ext2fs_tdb_open
|
||||
#define tdb_open_ex ext2fs_tdb_open_ex
|
||||
#define tdb_set_max_dead ext2fs_tdb_set_max_dead
|
||||
#define tdb_reopen ext2fs_tdb_reopen
|
||||
#define tdb_reopen_all ext2fs_tdb_reopen_all
|
||||
#define tdb_set_logging_function ext2fs_tdb_set_logging_function
|
||||
#define tdb_error ext2fs_tdb_error
|
||||
#define tdb_errorstr ext2fs_tdb_errorstr
|
||||
#define tdb_fetch ext2fs_tdb_fetch
|
||||
#define tdb_parse_record ext2fs_tdb_parse_record
|
||||
#define tdb_delete ext2fs_tdb_delete
|
||||
#define tdb_store ext2fs_tdb_store
|
||||
#define tdb_append ext2fs_tdb_append
|
||||
#define tdb_close ext2fs_tdb_close
|
||||
#define tdb_firstkey ext2fs_tdb_firstkey
|
||||
#define tdb_nextkey ext2fs_tdb_nextkey
|
||||
#define tdb_traverse ext2fs_tdb_traverse
|
||||
#define tdb_traverse_read ext2fs_tdb_traverse_read
|
||||
#define tdb_exists ext2fs_tdb_exists
|
||||
#define tdb_lockall ext2fs_tdb_lockall
|
||||
#define tdb_unlockall ext2fs_tdb_unlockall
|
||||
#define tdb_lockall_read ext2fs_tdb_lockall_read
|
||||
#define tdb_unlockall_read ext2fs_tdb_unlockall_read
|
||||
#define tdb_name ext2fs_tdb_name
|
||||
#define tdb_fd ext2fs_tdb_fd
|
||||
#define tdb_log_fn ext2fs_tdb_log_fn
|
||||
#define tdb_get_logging_private ext2fs_tdb_get_logging_private
|
||||
#define tdb_transaction_start ext2fs_tdb_transaction_start
|
||||
#define tdb_transaction_commit ext2fs_tdb_transaction_commit
|
||||
#define tdb_transaction_cancel ext2fs_tdb_transaction_cancel
|
||||
#define tdb_transaction_recover ext2fs_tdb_transaction_recover
|
||||
#define tdb_get_seqnum ext2fs_tdb_get_seqnum
|
||||
#define tdb_hash_size ext2fs_tdb_hash_size
|
||||
#define tdb_map_size ext2fs_tdb_map_size
|
||||
#define tdb_get_flags ext2fs_tdb_get_flags
|
||||
#define tdb_chainlock ext2fs_tdb_chainlock
|
||||
#define tdb_chainunlock ext2fs_tdb_chainunlock
|
||||
#define tdb_chainlock_read ext2fs_tdb_chainlock_read
|
||||
#define tdb_chainunlock_read ext2fs_tdb_chainunlock_read
|
||||
#define tdb_dump_all ext2fs_tdb_dump_all
|
||||
#define tdb_printfreelist ext2fs_tdb_printfreelist
|
||||
#define tdb_validate_freelist ext2fs_tdb_validate_freelist
|
||||
|
||||
|
||||
/* this is the context structure that is returned from a db open */
|
||||
typedef struct tdb_context TDB_CONTEXT;
|
||||
|
||||
typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
|
||||
typedef void (*tdb_log_func)(struct tdb_context *, enum tdb_debug_level, const char *, ...) PRINTF_ATTRIBUTE(3, 4);
|
||||
typedef unsigned int (*tdb_hash_func)(TDB_DATA *key);
|
||||
|
||||
struct tdb_logging_context {
|
||||
tdb_log_func log_fn;
|
||||
void *log_private;
|
||||
};
|
||||
|
||||
struct tdb_context *tdb_open(const char *name, int hash_size, int tdb_flags,
|
||||
int open_flags, mode_t mode);
|
||||
struct tdb_context *tdb_open_ex(const char *name, int hash_size, int tdb_flags,
|
||||
int open_flags, mode_t mode,
|
||||
const struct tdb_logging_context *log_ctx,
|
||||
tdb_hash_func hash_fn);
|
||||
void tdb_set_max_dead(struct tdb_context *tdb, int max_dead);
|
||||
|
||||
int tdb_reopen(struct tdb_context *tdb);
|
||||
int tdb_reopen_all(int parent_longlived);
|
||||
void tdb_set_logging_function(struct tdb_context *tdb, const struct tdb_logging_context *log_ctx);
|
||||
enum TDB_ERROR tdb_error(struct tdb_context *tdb);
|
||||
const char *tdb_errorstr(struct tdb_context *tdb);
|
||||
TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
|
||||
int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key,
|
||||
int (*parser)(TDB_DATA key, TDB_DATA data,
|
||||
void *private_data),
|
||||
void *private_data);
|
||||
int tdb_delete(struct tdb_context *tdb, TDB_DATA key);
|
||||
int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag);
|
||||
int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf);
|
||||
int tdb_close(struct tdb_context *tdb);
|
||||
TDB_DATA tdb_firstkey(struct tdb_context *tdb);
|
||||
TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key);
|
||||
int tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *);
|
||||
int tdb_traverse_read(struct tdb_context *tdb, tdb_traverse_func fn, void *);
|
||||
int tdb_exists(struct tdb_context *tdb, TDB_DATA key);
|
||||
int tdb_lockall(struct tdb_context *tdb);
|
||||
int tdb_unlockall(struct tdb_context *tdb);
|
||||
int tdb_lockall_read(struct tdb_context *tdb);
|
||||
int tdb_unlockall_read(struct tdb_context *tdb);
|
||||
const char *tdb_name(struct tdb_context *tdb);
|
||||
int tdb_fd(struct tdb_context *tdb);
|
||||
tdb_log_func tdb_log_fn(struct tdb_context *tdb);
|
||||
void *tdb_get_logging_private(struct tdb_context *tdb);
|
||||
int tdb_transaction_start(struct tdb_context *tdb);
|
||||
int tdb_transaction_commit(struct tdb_context *tdb);
|
||||
int tdb_transaction_cancel(struct tdb_context *tdb);
|
||||
int tdb_transaction_recover(struct tdb_context *tdb);
|
||||
int tdb_get_seqnum(struct tdb_context *tdb);
|
||||
int tdb_hash_size(struct tdb_context *tdb);
|
||||
size_t tdb_map_size(struct tdb_context *tdb);
|
||||
int tdb_get_flags(struct tdb_context *tdb);
|
||||
|
||||
/* Low level locking functions: use with care */
|
||||
int tdb_chainlock(struct tdb_context *tdb, TDB_DATA key);
|
||||
int tdb_chainunlock(struct tdb_context *tdb, TDB_DATA key);
|
||||
int tdb_chainlock_read(struct tdb_context *tdb, TDB_DATA key);
|
||||
int tdb_chainunlock_read(struct tdb_context *tdb, TDB_DATA key);
|
||||
|
||||
/* Debug functions. Not used in production. */
|
||||
void tdb_dump_all(struct tdb_context *tdb);
|
||||
int tdb_printfreelist(struct tdb_context *tdb);
|
||||
int tdb_validate_freelist(struct tdb_context *tdb, int *pnum_entries);
|
||||
|
||||
extern TDB_DATA tdb_null;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* tdb.h */
|
|
@ -0,0 +1,23 @@
|
|||
#!/bin/sh
|
||||
#
|
||||
# This file creates a stand-alone TDB based on a set of sources from
|
||||
# Samba
|
||||
|
||||
#BASE_DIR=/usr/projects/samba/samba-4.0.0tp4/source/lib/tdb
|
||||
BASE_DIR=/usr/projects/samba/tdb
|
||||
|
||||
FILES="error.c lock.c io.c transaction.c freelist.c \
|
||||
freelistcheck.c traverse.c dump.c tdb.c open.c"
|
||||
|
||||
cp $BASE_DIR/common/tdb_private.h tdb.c
|
||||
for i in $FILES; do
|
||||
if [ `tail -n 1 tdb.c | wc -c` -gt 1 ]; then
|
||||
printf "\n" >> tdb.c
|
||||
fi
|
||||
echo "/* file: $i */" >> tdb.c
|
||||
sed -e '1,/#include "tdb_private.h"/d' < $BASE_DIR/common/$i >> tdb.c
|
||||
done
|
||||
|
||||
cp $BASE_DIR/include/tdb.h .
|
||||
cp $BASE_DIR/tools/tdbtool.c .
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
Index: tdb-sa/tdb.c
|
||||
===================================================================
|
||||
--- tdb-sa.orig/tdb.c 2007-04-04 12:16:26.000000000 -0400
|
||||
+++ tdb-sa/tdb.c 2007-04-04 12:16:53.000000000 -0400
|
||||
@@ -1,9 +1,9 @@
|
||||
/*
|
||||
- Unix SMB/CIFS implementation.
|
||||
+ trivial database library - standalone version
|
||||
|
||||
- trivial database library - private includes
|
||||
-
|
||||
- Copyright (C) Andrew Tridgell 2005
|
||||
+ Copyright (C) Andrew Tridgell 1999-2005
|
||||
+ Copyright (C) Jeremy Allison 2000-2006
|
||||
+ Copyright (C) Paul `Rusty' Russell 2000
|
||||
|
||||
** NOTE! The following LGPL license applies to the tdb
|
||||
** library. This does NOT imply that all of Samba is released
|
|
@ -0,0 +1,56 @@
|
|||
Index: tdb/tdb.h
|
||||
===================================================================
|
||||
--- tdb.orig/tdb.h 2007-04-04 20:39:26.000000000 -0400
|
||||
+++ tdb/tdb.h 2007-04-04 20:40:13.000000000 -0400
|
||||
@@ -76,6 +76,51 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
+/* ext2fs tdb renames */
|
||||
+#define tdb_open ext2fs_tdb_open
|
||||
+#define tdb_open_ex ext2fs_tdb_open_ex
|
||||
+#define tdb_set_max_dead ext2fs_tdb_set_max_dead
|
||||
+#define tdb_reopen ext2fs_tdb_reopen
|
||||
+#define tdb_reopen_all ext2fs_tdb_reopen_all
|
||||
+#define tdb_set_logging_function ext2fs_tdb_set_logging_function
|
||||
+#define tdb_error ext2fs_tdb_error
|
||||
+#define tdb_errorstr ext2fs_tdb_errorstr
|
||||
+#define tdb_fetch ext2fs_tdb_fetch
|
||||
+#define tdb_parse_record ext2fs_tdb_parse_record
|
||||
+#define tdb_delete ext2fs_tdb_delete
|
||||
+#define tdb_store ext2fs_tdb_store
|
||||
+#define tdb_append ext2fs_tdb_append
|
||||
+#define tdb_close ext2fs_tdb_close
|
||||
+#define tdb_firstkey ext2fs_tdb_firstkey
|
||||
+#define tdb_nextkey ext2fs_tdb_nextkey
|
||||
+#define tdb_traverse ext2fs_tdb_traverse
|
||||
+#define tdb_traverse_read ext2fs_tdb_traverse_read
|
||||
+#define tdb_exists ext2fs_tdb_exists
|
||||
+#define tdb_lockall ext2fs_tdb_lockall
|
||||
+#define tdb_unlockall ext2fs_tdb_unlockall
|
||||
+#define tdb_lockall_read ext2fs_tdb_lockall_read
|
||||
+#define tdb_unlockall_read ext2fs_tdb_unlockall_read
|
||||
+#define tdb_name ext2fs_tdb_name
|
||||
+#define tdb_fd ext2fs_tdb_fd
|
||||
+#define tdb_log_fn ext2fs_tdb_log_fn
|
||||
+#define tdb_get_logging_private ext2fs_tdb_get_logging_private
|
||||
+#define tdb_transaction_start ext2fs_tdb_transaction_start
|
||||
+#define tdb_transaction_commit ext2fs_tdb_transaction_commit
|
||||
+#define tdb_transaction_cancel ext2fs_tdb_transaction_cancel
|
||||
+#define tdb_transaction_recover ext2fs_tdb_transaction_recover
|
||||
+#define tdb_get_seqnum ext2fs_tdb_get_seqnum
|
||||
+#define tdb_hash_size ext2fs_tdb_hash_size
|
||||
+#define tdb_map_size ext2fs_tdb_map_size
|
||||
+#define tdb_get_flags ext2fs_tdb_get_flags
|
||||
+#define tdb_chainlock ext2fs_tdb_chainlock
|
||||
+#define tdb_chainunlock ext2fs_tdb_chainunlock
|
||||
+#define tdb_chainlock_read ext2fs_tdb_chainlock_read
|
||||
+#define tdb_chainunlock_read ext2fs_tdb_chainunlock_read
|
||||
+#define tdb_dump_all ext2fs_tdb_dump_all
|
||||
+#define tdb_printfreelist ext2fs_tdb_printfreelist
|
||||
+#define tdb_validate_freelist ext2fs_tdb_validate_freelist
|
||||
+
|
||||
+
|
||||
/* this is the context structure that is returned from a db open */
|
||||
typedef struct tdb_context TDB_CONTEXT;
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
Index: tdb/tdb.c
|
||||
===================================================================
|
||||
--- tdb.orig/tdb.c 2007-04-04 20:26:11.000000000 -0400
|
||||
+++ tdb/tdb.c 2007-04-04 20:39:58.000000000 -0400
|
||||
@@ -24,11 +24,78 @@
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
-#include "replace.h"
|
||||
-#include "system/filesys.h"
|
||||
-#include "system/time.h"
|
||||
-#include "system/shmem.h"
|
||||
-#include "system/select.h"
|
||||
+#ifdef CONFIG_STAND_ALONE
|
||||
+#define HAVE_MMAP
|
||||
+#define HAVE_STRDUP
|
||||
+#define HAVE_SYS_MMAN_H
|
||||
+#define HAVE_UTIME_H
|
||||
+#define HAVE_UTIME
|
||||
+#endif
|
||||
+#define _XOPEN_SOURCE 500
|
||||
+
|
||||
+#include <unistd.h>
|
||||
+#include <stdio.h>
|
||||
+#include <stdlib.h>
|
||||
+#include <stdarg.h>
|
||||
+#include <stddef.h>
|
||||
+#include <errno.h>
|
||||
+#include <string.h>
|
||||
+#include <sys/select.h>
|
||||
+#include <sys/time.h>
|
||||
+#include <sys/types.h>
|
||||
+#include <time.h>
|
||||
+#ifdef HAVE_UTIME_H
|
||||
+#include <utime.h>
|
||||
+#endif
|
||||
+#include <sys/stat.h>
|
||||
+#include <sys/file.h>
|
||||
+#include <fcntl.h>
|
||||
+
|
||||
+#ifdef HAVE_SYS_MMAN_H
|
||||
+#include <sys/mman.h>
|
||||
+#endif
|
||||
+
|
||||
+#ifndef MAP_FILE
|
||||
+#define MAP_FILE 0
|
||||
+#endif
|
||||
+
|
||||
+#ifndef MAP_FAILED
|
||||
+#define MAP_FAILED ((void *)-1)
|
||||
+#endif
|
||||
+
|
||||
+#ifndef HAVE_STRDUP
|
||||
+#define strdup rep_strdup
|
||||
+static char *rep_strdup(const char *s)
|
||||
+{
|
||||
+ char *ret;
|
||||
+ int length;
|
||||
+ if (!s)
|
||||
+ return NULL;
|
||||
+
|
||||
+ if (!length)
|
||||
+ length = strlen(s);
|
||||
+
|
||||
+ ret = malloc(length + 1);
|
||||
+ if (ret) {
|
||||
+ strncpy(ret, s, length);
|
||||
+ ret[length] = '\0';
|
||||
+ }
|
||||
+ return ret;
|
||||
+}
|
||||
+#endif
|
||||
+
|
||||
+#ifndef PRINTF_ATTRIBUTE
|
||||
+#if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
|
||||
+/** Use gcc attribute to check printf fns. a1 is the 1-based index of
|
||||
+ * the parameter containing the format, and a2 the index of the first
|
||||
+ * argument. Note that some gcc 2.x versions don't handle this
|
||||
+ * properly **/
|
||||
+#define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
|
||||
+#else
|
||||
+#define PRINTF_ATTRIBUTE(a1, a2)
|
||||
+#endif
|
||||
+#endif
|
||||
+
|
||||
#include "tdb.h"
|
||||
|
||||
#ifndef u32
|
|
@ -0,0 +1,6 @@
|
|||
copyright
|
||||
replace-includes
|
||||
static-prototypes
|
||||
static-functions
|
||||
tdbtool-includes
|
||||
ext2tdb-rename
|
|
@ -0,0 +1,24 @@
|
|||
Index: tdb/tdb.c
|
||||
===================================================================
|
||||
--- tdb.orig/tdb.c 2007-04-04 20:40:06.000000000 -0400
|
||||
+++ tdb/tdb.c 2007-04-04 20:40:08.000000000 -0400
|
||||
@@ -1423,8 +1423,8 @@
|
||||
/*
|
||||
brlock during a transaction - ignore them
|
||||
*/
|
||||
-int transaction_brlock(struct tdb_context *tdb, tdb_off_t offset,
|
||||
- int rw_type, int lck_type, int probe, size_t len)
|
||||
+static int transaction_brlock(struct tdb_context *tdb, tdb_off_t offset,
|
||||
+ int rw_type, int lck_type, int probe, size_t len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -2118,7 +2118,7 @@
|
||||
/* file: freelist.c */
|
||||
|
||||
/* read a freelist record and check for simple errors */
|
||||
-int rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct *rec)
|
||||
+static int rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct *rec)
|
||||
{
|
||||
if (tdb->methods->tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
|
||||
return -1;
|
|
@ -0,0 +1,66 @@
|
|||
Index: tdb/tdb.c
|
||||
===================================================================
|
||||
--- tdb.orig/tdb.c 2007-04-04 20:39:58.000000000 -0400
|
||||
+++ tdb/tdb.c 2007-04-04 20:40:06.000000000 -0400
|
||||
@@ -243,36 +243,36 @@
|
||||
/*
|
||||
internal prototypes
|
||||
*/
|
||||
-int tdb_munmap(struct tdb_context *tdb);
|
||||
-void tdb_mmap(struct tdb_context *tdb);
|
||||
-int tdb_lock(struct tdb_context *tdb, int list, int ltype);
|
||||
-int tdb_unlock(struct tdb_context *tdb, int list, int ltype);
|
||||
-int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, int rw_type, int lck_type, int probe, size_t len);
|
||||
-int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len);
|
||||
-int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
-int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
-int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
-int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
-void *tdb_convert(void *buf, u32 size);
|
||||
-int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
|
||||
-tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec);
|
||||
-int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
-int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
-int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
-int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
-int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
|
||||
-int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
|
||||
-int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec);
|
||||
-unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
|
||||
-int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
|
||||
+static int tdb_munmap(struct tdb_context *tdb);
|
||||
+static void tdb_mmap(struct tdb_context *tdb);
|
||||
+static int tdb_lock(struct tdb_context *tdb, int list, int ltype);
|
||||
+static int tdb_unlock(struct tdb_context *tdb, int list, int ltype);
|
||||
+static int tdb_brlock(struct tdb_context *tdb, tdb_off_t offset, int rw_type, int lck_type, int probe, size_t len);
|
||||
+static int tdb_brlock_upgrade(struct tdb_context *tdb, tdb_off_t offset, size_t len);
|
||||
+static int tdb_write_lock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
+static int tdb_write_unlock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
+static int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
+static int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
+static void *tdb_convert(void *buf, u32 size);
|
||||
+static int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
|
||||
+static tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec);
|
||||
+static int tdb_ofs_read(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
+static int tdb_ofs_write(struct tdb_context *tdb, tdb_off_t offset, tdb_off_t *d);
|
||||
+static int tdb_lock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
+static int tdb_unlock_record(struct tdb_context *tdb, tdb_off_t off);
|
||||
+static int tdb_rec_read(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
|
||||
+static int tdb_rec_write(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec);
|
||||
+static int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct list_struct *rec);
|
||||
+static unsigned char *tdb_alloc_read(struct tdb_context *tdb, tdb_off_t offset, tdb_len_t len);
|
||||
+static int tdb_parse_data(struct tdb_context *tdb, TDB_DATA key,
|
||||
tdb_off_t offset, tdb_len_t len,
|
||||
int (*parser)(TDB_DATA key, TDB_DATA data,
|
||||
void *private_data),
|
||||
void *private_data);
|
||||
-tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash, int locktype,
|
||||
+static tdb_off_t tdb_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, u32 hash, int locktype,
|
||||
struct list_struct *rec);
|
||||
-void tdb_io_init(struct tdb_context *tdb);
|
||||
-int tdb_expand(struct tdb_context *tdb, tdb_off_t size);
|
||||
+static void tdb_io_init(struct tdb_context *tdb);
|
||||
+static int tdb_expand(struct tdb_context *tdb, tdb_off_t size);
|
||||
|
||||
|
||||
/* file: error.c */
|
|
@ -0,0 +1,30 @@
|
|||
Index: tdb/tdbtool.c
|
||||
===================================================================
|
||||
--- tdb.orig/tdbtool.c 2007-04-04 20:39:28.000000000 -0400
|
||||
+++ tdb/tdbtool.c 2007-04-04 20:40:10.000000000 -0400
|
||||
@@ -21,10 +21,21 @@
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
*/
|
||||
|
||||
-#include "replace.h"
|
||||
-#include "system/locale.h"
|
||||
-#include "system/time.h"
|
||||
-#include "system/filesys.h"
|
||||
+#include <errno.h>
|
||||
+#include <stdlib.h>
|
||||
+#include <stdio.h>
|
||||
+#include <fcntl.h>
|
||||
+#include <unistd.h>
|
||||
+#include <string.h>
|
||||
+#include <fcntl.h>
|
||||
+#include <time.h>
|
||||
+#include <sys/mman.h>
|
||||
+#include <sys/stat.h>
|
||||
+#include <sys/time.h>
|
||||
+#include <ctype.h>
|
||||
+#include <signal.h>
|
||||
+#include <stdarg.h>
|
||||
+
|
||||
#include "tdb.h"
|
||||
|
||||
static int do_command(void);
|
|
@ -0,0 +1,620 @@
|
|||
/*
|
||||
Unix SMB/CIFS implementation.
|
||||
Samba database functions
|
||||
Copyright (C) Andrew Tridgell 1999-2000
|
||||
Copyright (C) Paul `Rusty' Russell 2000
|
||||
Copyright (C) Jeremy Allison 2000
|
||||
Copyright (C) Andrew Esh 2001
|
||||
|
||||
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 <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <time.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <ctype.h>
|
||||
#include <signal.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "tdb.h"
|
||||
|
||||
static int do_command(void);
|
||||
const char *cmdname;
|
||||
char *arg1, *arg2;
|
||||
size_t arg1len, arg2len;
|
||||
int bIterate = 0;
|
||||
char *line;
|
||||
TDB_DATA iterate_kbuf;
|
||||
char cmdline[1024];
|
||||
|
||||
enum commands {
|
||||
CMD_CREATE_TDB,
|
||||
CMD_OPEN_TDB,
|
||||
CMD_ERASE,
|
||||
CMD_DUMP,
|
||||
CMD_INSERT,
|
||||
CMD_MOVE,
|
||||
CMD_STORE,
|
||||
CMD_SHOW,
|
||||
CMD_KEYS,
|
||||
CMD_HEXKEYS,
|
||||
CMD_DELETE,
|
||||
CMD_LIST_HASH_FREE,
|
||||
CMD_LIST_FREE,
|
||||
CMD_INFO,
|
||||
CMD_FIRST,
|
||||
CMD_NEXT,
|
||||
CMD_SYSTEM,
|
||||
CMD_QUIT,
|
||||
CMD_HELP
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
enum commands cmd;
|
||||
} COMMAND_TABLE;
|
||||
|
||||
COMMAND_TABLE cmd_table[] = {
|
||||
{"create", CMD_CREATE_TDB},
|
||||
{"open", CMD_OPEN_TDB},
|
||||
{"erase", CMD_ERASE},
|
||||
{"dump", CMD_DUMP},
|
||||
{"insert", CMD_INSERT},
|
||||
{"move", CMD_MOVE},
|
||||
{"store", CMD_STORE},
|
||||
{"show", CMD_SHOW},
|
||||
{"keys", CMD_KEYS},
|
||||
{"hexkeys", CMD_HEXKEYS},
|
||||
{"delete", CMD_DELETE},
|
||||
{"list", CMD_LIST_HASH_FREE},
|
||||
{"free", CMD_LIST_FREE},
|
||||
{"info", CMD_INFO},
|
||||
{"first", CMD_FIRST},
|
||||
{"1", CMD_FIRST},
|
||||
{"next", CMD_NEXT},
|
||||
{"n", CMD_NEXT},
|
||||
{"quit", CMD_QUIT},
|
||||
{"q", CMD_QUIT},
|
||||
{"!", CMD_SYSTEM},
|
||||
{NULL, CMD_HELP}
|
||||
};
|
||||
|
||||
/* a tdb tool for manipulating a tdb database */
|
||||
|
||||
static TDB_CONTEXT *tdb;
|
||||
|
||||
static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
|
||||
static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
|
||||
static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
|
||||
|
||||
static void print_asc(const char *buf,int len)
|
||||
{
|
||||
int i;
|
||||
|
||||
/* We're probably printing ASCII strings so don't try to display
|
||||
the trailing NULL character. */
|
||||
|
||||
if (buf[len - 1] == 0)
|
||||
len--;
|
||||
|
||||
for (i=0;i<len;i++)
|
||||
printf("%c",isprint(buf[i])?buf[i]:'.');
|
||||
}
|
||||
|
||||
static void print_data(const char *buf,int len)
|
||||
{
|
||||
int i=0;
|
||||
if (len<=0) return;
|
||||
printf("[%03X] ",i);
|
||||
for (i=0;i<len;) {
|
||||
printf("%02X ",(int)buf[i]);
|
||||
i++;
|
||||
if (i%8 == 0) printf(" ");
|
||||
if (i%16 == 0) {
|
||||
print_asc(&buf[i-16],8); printf(" ");
|
||||
print_asc(&buf[i-8],8); printf("\n");
|
||||
if (i<len) printf("[%03X] ",i);
|
||||
}
|
||||
}
|
||||
if (i%16) {
|
||||
int n;
|
||||
|
||||
n = 16 - (i%16);
|
||||
printf(" ");
|
||||
if (n>8) printf(" ");
|
||||
while (n--) printf(" ");
|
||||
|
||||
n = i%16;
|
||||
if (n > 8) n = 8;
|
||||
print_asc(&buf[i-(i%16)],n); printf(" ");
|
||||
n = (i%16) - n;
|
||||
if (n>0) print_asc(&buf[i-n],n);
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void help(void)
|
||||
{
|
||||
printf("\n"
|
||||
"tdbtool: \n"
|
||||
" create dbname : create a database\n"
|
||||
" open dbname : open an existing database\n"
|
||||
" erase : erase the database\n"
|
||||
" dump : dump the database as strings\n"
|
||||
" keys : dump the database keys as strings\n"
|
||||
" hexkeys : dump the database keys as hex values\n"
|
||||
" info : print summary info about the database\n"
|
||||
" insert key data : insert a record\n"
|
||||
" move key file : move a record to a destination tdb\n"
|
||||
" store key data : store a record (replace)\n"
|
||||
" show key : show a record by key\n"
|
||||
" delete key : delete a record by key\n"
|
||||
" list : print the database hash table and freelist\n"
|
||||
" free : print the database freelist\n"
|
||||
" ! command : execute system command\n"
|
||||
" 1 | first : print the first record\n"
|
||||
" n | next : print the next record\n"
|
||||
" q | quit : terminate\n"
|
||||
" \\n : repeat 'next' command\n"
|
||||
"\n");
|
||||
}
|
||||
|
||||
static void terror(const char *why)
|
||||
{
|
||||
printf("%s\n", why);
|
||||
}
|
||||
|
||||
static void create_tdb(const char *tdbname)
|
||||
{
|
||||
if (tdb) tdb_close(tdb);
|
||||
tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST,
|
||||
O_RDWR | O_CREAT | O_TRUNC, 0600);
|
||||
if (!tdb) {
|
||||
printf("Could not create %s: %s\n", tdbname, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static void open_tdb(const char *tdbname)
|
||||
{
|
||||
if (tdb) tdb_close(tdb);
|
||||
tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
|
||||
if (!tdb) {
|
||||
printf("Could not open %s: %s\n", tdbname, strerror(errno));
|
||||
}
|
||||
}
|
||||
|
||||
static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
|
||||
{
|
||||
TDB_DATA key, dbuf;
|
||||
|
||||
if ((keyname == NULL) || (keylen == 0)) {
|
||||
terror("need key");
|
||||
return;
|
||||
}
|
||||
|
||||
key.dptr = (unsigned char *)keyname;
|
||||
key.dsize = keylen;
|
||||
dbuf.dptr = (unsigned char *)data;
|
||||
dbuf.dsize = datalen;
|
||||
|
||||
if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
|
||||
terror("insert failed");
|
||||
}
|
||||
}
|
||||
|
||||
static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
|
||||
{
|
||||
TDB_DATA key, dbuf;
|
||||
|
||||
if ((keyname == NULL) || (keylen == 0)) {
|
||||
terror("need key");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((data == NULL) || (datalen == 0)) {
|
||||
terror("need data");
|
||||
return;
|
||||
}
|
||||
|
||||
key.dptr = (unsigned char *)keyname;
|
||||
key.dsize = keylen;
|
||||
dbuf.dptr = (unsigned char *)data;
|
||||
dbuf.dsize = datalen;
|
||||
|
||||
printf("Storing key:\n");
|
||||
print_rec(tdb, key, dbuf, NULL);
|
||||
|
||||
if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
|
||||
terror("store failed");
|
||||
}
|
||||
}
|
||||
|
||||
static void show_tdb(char *keyname, size_t keylen)
|
||||
{
|
||||
TDB_DATA key, dbuf;
|
||||
|
||||
if ((keyname == NULL) || (keylen == 0)) {
|
||||
terror("need key");
|
||||
return;
|
||||
}
|
||||
|
||||
key.dptr = (unsigned char *)keyname;
|
||||
key.dsize = keylen;
|
||||
|
||||
dbuf = tdb_fetch(tdb, key);
|
||||
if (!dbuf.dptr) {
|
||||
terror("fetch failed");
|
||||
return;
|
||||
}
|
||||
|
||||
print_rec(tdb, key, dbuf, NULL);
|
||||
|
||||
free( dbuf.dptr );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static void delete_tdb(char *keyname, size_t keylen)
|
||||
{
|
||||
TDB_DATA key;
|
||||
|
||||
if ((keyname == NULL) || (keylen == 0)) {
|
||||
terror("need key");
|
||||
return;
|
||||
}
|
||||
|
||||
key.dptr = (unsigned char *)keyname;
|
||||
key.dsize = keylen;
|
||||
|
||||
if (tdb_delete(tdb, key) != 0) {
|
||||
terror("delete failed");
|
||||
}
|
||||
}
|
||||
|
||||
static void move_rec(char *keyname, size_t keylen, char* tdbname)
|
||||
{
|
||||
TDB_DATA key, dbuf;
|
||||
TDB_CONTEXT *dst_tdb;
|
||||
|
||||
if ((keyname == NULL) || (keylen == 0)) {
|
||||
terror("need key");
|
||||
return;
|
||||
}
|
||||
|
||||
if ( !tdbname ) {
|
||||
terror("need destination tdb name");
|
||||
return;
|
||||
}
|
||||
|
||||
key.dptr = (unsigned char *)keyname;
|
||||
key.dsize = keylen;
|
||||
|
||||
dbuf = tdb_fetch(tdb, key);
|
||||
if (!dbuf.dptr) {
|
||||
terror("fetch failed");
|
||||
return;
|
||||
}
|
||||
|
||||
print_rec(tdb, key, dbuf, NULL);
|
||||
|
||||
dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
|
||||
if ( !dst_tdb ) {
|
||||
terror("unable to open destination tdb");
|
||||
return;
|
||||
}
|
||||
|
||||
if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
|
||||
terror("failed to move record");
|
||||
}
|
||||
else
|
||||
printf("record moved\n");
|
||||
|
||||
tdb_close( dst_tdb );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
||||
{
|
||||
printf("\nkey %d bytes\n", (int)key.dsize);
|
||||
print_asc((const char *)key.dptr, key.dsize);
|
||||
printf("\ndata %d bytes\n", (int)dbuf.dsize);
|
||||
print_data((const char *)dbuf.dptr, dbuf.dsize);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
||||
{
|
||||
printf("key %d bytes: ", (int)key.dsize);
|
||||
print_asc((const char *)key.dptr, key.dsize);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
||||
{
|
||||
printf("key %d bytes\n", (int)key.dsize);
|
||||
print_data((const char *)key.dptr, key.dsize);
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int total_bytes;
|
||||
|
||||
static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
|
||||
{
|
||||
total_bytes += dbuf.dsize;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void info_tdb(void)
|
||||
{
|
||||
int count;
|
||||
total_bytes = 0;
|
||||
if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
|
||||
printf("Error = %s\n", tdb_errorstr(tdb));
|
||||
else
|
||||
printf("%d records totalling %d bytes\n", count, total_bytes);
|
||||
}
|
||||
|
||||
static char *tdb_getline(const char *prompt)
|
||||
{
|
||||
static char thisline[1024];
|
||||
char *p;
|
||||
fputs(prompt, stdout);
|
||||
thisline[0] = 0;
|
||||
p = fgets(thisline, sizeof(thisline)-1, stdin);
|
||||
if (p) p = strchr(p, '\n');
|
||||
if (p) *p = 0;
|
||||
return p?thisline:NULL;
|
||||
}
|
||||
|
||||
static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
|
||||
void *state)
|
||||
{
|
||||
return tdb_delete(the_tdb, key);
|
||||
}
|
||||
|
||||
static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
|
||||
{
|
||||
TDB_DATA dbuf;
|
||||
*pkey = tdb_firstkey(the_tdb);
|
||||
|
||||
dbuf = tdb_fetch(the_tdb, *pkey);
|
||||
if (!dbuf.dptr) terror("fetch failed");
|
||||
else {
|
||||
print_rec(the_tdb, *pkey, dbuf, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
|
||||
{
|
||||
TDB_DATA dbuf;
|
||||
*pkey = tdb_nextkey(the_tdb, *pkey);
|
||||
|
||||
dbuf = tdb_fetch(the_tdb, *pkey);
|
||||
if (!dbuf.dptr)
|
||||
terror("fetch failed");
|
||||
else
|
||||
print_rec(the_tdb, *pkey, dbuf, NULL);
|
||||
}
|
||||
|
||||
static int do_command(void)
|
||||
{
|
||||
COMMAND_TABLE *ctp = cmd_table;
|
||||
enum commands mycmd = CMD_HELP;
|
||||
int cmd_len;
|
||||
|
||||
if (cmdname && strlen(cmdname) == 0) {
|
||||
mycmd = CMD_NEXT;
|
||||
} else {
|
||||
while (ctp->name) {
|
||||
cmd_len = strlen(ctp->name);
|
||||
if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
|
||||
mycmd = ctp->cmd;
|
||||
break;
|
||||
}
|
||||
ctp++;
|
||||
}
|
||||
}
|
||||
|
||||
switch (mycmd) {
|
||||
case CMD_CREATE_TDB:
|
||||
bIterate = 0;
|
||||
create_tdb(arg1);
|
||||
return 0;
|
||||
case CMD_OPEN_TDB:
|
||||
bIterate = 0;
|
||||
open_tdb(arg1);
|
||||
return 0;
|
||||
case CMD_SYSTEM:
|
||||
/* Shell command */
|
||||
system(arg1);
|
||||
return 0;
|
||||
case CMD_QUIT:
|
||||
return 1;
|
||||
default:
|
||||
/* all the rest require a open database */
|
||||
if (!tdb) {
|
||||
bIterate = 0;
|
||||
terror("database not open");
|
||||
help();
|
||||
return 0;
|
||||
}
|
||||
switch (mycmd) {
|
||||
case CMD_ERASE:
|
||||
bIterate = 0;
|
||||
tdb_traverse(tdb, do_delete_fn, NULL);
|
||||
return 0;
|
||||
case CMD_DUMP:
|
||||
bIterate = 0;
|
||||
tdb_traverse(tdb, print_rec, NULL);
|
||||
return 0;
|
||||
case CMD_INSERT:
|
||||
bIterate = 0;
|
||||
insert_tdb(arg1, arg1len,arg2,arg2len);
|
||||
return 0;
|
||||
case CMD_MOVE:
|
||||
bIterate = 0;
|
||||
move_rec(arg1,arg1len,arg2);
|
||||
return 0;
|
||||
case CMD_STORE:
|
||||
bIterate = 0;
|
||||
store_tdb(arg1,arg1len,arg2,arg2len);
|
||||
return 0;
|
||||
case CMD_SHOW:
|
||||
bIterate = 0;
|
||||
show_tdb(arg1, arg1len);
|
||||
return 0;
|
||||
case CMD_KEYS:
|
||||
tdb_traverse(tdb, print_key, NULL);
|
||||
return 0;
|
||||
case CMD_HEXKEYS:
|
||||
tdb_traverse(tdb, print_hexkey, NULL);
|
||||
return 0;
|
||||
case CMD_DELETE:
|
||||
bIterate = 0;
|
||||
delete_tdb(arg1,arg1len);
|
||||
return 0;
|
||||
case CMD_LIST_HASH_FREE:
|
||||
tdb_dump_all(tdb);
|
||||
return 0;
|
||||
case CMD_LIST_FREE:
|
||||
tdb_printfreelist(tdb);
|
||||
return 0;
|
||||
case CMD_INFO:
|
||||
info_tdb();
|
||||
return 0;
|
||||
case CMD_FIRST:
|
||||
bIterate = 1;
|
||||
first_record(tdb, &iterate_kbuf);
|
||||
return 0;
|
||||
case CMD_NEXT:
|
||||
if (bIterate)
|
||||
next_record(tdb, &iterate_kbuf);
|
||||
return 0;
|
||||
case CMD_HELP:
|
||||
help();
|
||||
return 0;
|
||||
case CMD_CREATE_TDB:
|
||||
case CMD_OPEN_TDB:
|
||||
case CMD_SYSTEM:
|
||||
case CMD_QUIT:
|
||||
/*
|
||||
* unhandled commands. cases included here to avoid compiler
|
||||
* warnings.
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char *convert_string(char *instring, size_t *sizep)
|
||||
{
|
||||
size_t length = 0;
|
||||
char *outp, *inp;
|
||||
char temp[3];
|
||||
|
||||
|
||||
outp = inp = instring;
|
||||
|
||||
while (*inp) {
|
||||
if (*inp == '\\') {
|
||||
inp++;
|
||||
if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
|
||||
temp[0] = *inp++;
|
||||
temp[1] = '\0';
|
||||
if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
|
||||
temp[1] = *inp++;
|
||||
temp[2] = '\0';
|
||||
}
|
||||
*outp++ = (char)strtol((const char *)temp,NULL,16);
|
||||
} else {
|
||||
*outp++ = *inp++;
|
||||
}
|
||||
} else {
|
||||
*outp++ = *inp++;
|
||||
}
|
||||
length++;
|
||||
}
|
||||
*sizep = length;
|
||||
return instring;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
cmdname = "";
|
||||
arg1 = NULL;
|
||||
arg1len = 0;
|
||||
arg2 = NULL;
|
||||
arg2len = 0;
|
||||
|
||||
if (argv[1]) {
|
||||
cmdname = "open";
|
||||
arg1 = argv[1];
|
||||
do_command();
|
||||
cmdname = "";
|
||||
arg1 = NULL;
|
||||
}
|
||||
|
||||
switch (argc) {
|
||||
case 1:
|
||||
case 2:
|
||||
/* Interactive mode */
|
||||
while ((cmdname = tdb_getline("tdb> "))) {
|
||||
arg2 = arg1 = NULL;
|
||||
if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
|
||||
arg1++;
|
||||
arg2 = arg1;
|
||||
while (*arg2) {
|
||||
if (*arg2 == ' ') {
|
||||
*arg2++ = '\0';
|
||||
break;
|
||||
}
|
||||
if ((*arg2++ == '\\') && (*arg2 == ' ')) {
|
||||
arg2++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (arg1) arg1 = convert_string(arg1,&arg1len);
|
||||
if (arg2) arg2 = convert_string(arg2,&arg2len);
|
||||
if (do_command()) break;
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
arg2 = convert_string(argv[4],&arg2len);
|
||||
case 4:
|
||||
arg1 = convert_string(argv[3],&arg1len);
|
||||
case 3:
|
||||
cmdname = argv[2];
|
||||
default:
|
||||
do_command();
|
||||
break;
|
||||
}
|
||||
|
||||
if (tdb) tdb_close(tdb);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue