1999-12-21 06:04:37 +03:00
# 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 "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 ) ;
2000-01-03 02:00:27 +03:00
tdb = tdb_open ( tok , 0 , TDB_CLEAR_IF_FIRST ,
O_RDWR | O_CREAT | O_TRUNC , 0600 ) ;
1999-12-21 06:04:37 +03:00
}
static void open_tdb ( void )
{
char * tok = strtok ( NULL , " " ) ;
if ( ! tok ) {
help ( ) ;
return ;
}
if ( tdb ) tdb_close ( tdb ) ;
2000-01-03 02:00:27 +03:00
tdb = tdb_open ( tok , 0 , 0 , O_RDWR , 0600 ) ;
1999-12-21 06:04:37 +03:00
}
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 " ) ;
}
}
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
static int print_rec ( TDB_CONTEXT * tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
1999-12-21 06:04:37 +03:00
{
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 ;
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
static int traverse_fn ( TDB_CONTEXT * tdb , TDB_DATA key , TDB_DATA dbuf , void * state )
1999-12-21 06:04:37 +03:00
{
total_bytes + = dbuf . dsize ;
return 0 ;
}
static void info_tdb ( void )
{
int count ;
total_bytes = 0 ;
1) added void* state argument to tdb_traverse. guess what! there were
two places i found where it was appropriate to _use_ that third argument,
in locking.c and brlock.c! there was a static traverse_function and
i removed the static variable, typecast it to a void*, passed it to
tdb_traverse and re-cast it back to the traverse_function inside the
tdb_traverse function. this makes the use of tdb_traverse() reentrant,
which is never going to happen, i know, i just don't like to see
statics lying about when there's no need for them.
as i had to do in samba-tng, all uses of tdb_traverse modified to take
the new void* state argument.
2) disabled rpcclient: referring people to use SAMBA_TNG rpcclient.
i don't know how the other samba team members would react if i deleted
rpcclient from cvs main. damn, that code's so old, it's unreal.
20 rpcclient commands, instead of about 70 in SAMBA_TNG.
(This used to be commit 49d7f0afbc1c5425d53019e234d54ddf205c8e9a)
2000-02-04 07:59:31 +03:00
count = tdb_traverse ( tdb , traverse_fn , NULL ) ;
1999-12-21 06:04:37 +03:00
printf ( " %d records totalling %d bytes \n " , count , total_bytes ) ;
}
2000-01-07 06:02:13 +03:00
static char * getline ( char * prompt )
{
static char line [ 1024 ] ;
char * p ;
fputs ( prompt , stdout ) ;
line [ 0 ] = 0 ;
p = fgets ( line , sizeof ( line ) - 1 , stdin ) ;
if ( p ) p = strchr ( p , ' \n ' ) ;
if ( p ) * p = 0 ;
return p ? line : NULL ;
}
2000-02-28 03:37:13 +03:00
static int do_delete_fn ( TDB_CONTEXT * tdb , TDB_DATA key , TDB_DATA dbuf ,
void * state )
{
return tdb_delete ( tdb , key ) ;
}
1999-12-21 06:04:37 +03:00
int main ( int argc , char * argv [ ] )
{
2000-03-13 04:35:09 +03:00
char * line ;
char * tok ;
1999-12-24 11:45:02 +03:00
2000-03-13 04:35:09 +03:00
while ( ( line = getline ( " tdb> " ) ) ) {
/* Shell command */
if ( line [ 0 ] = = ' ! ' ) {
system ( line + 1 ) ;
continue ;
}
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 , do_delete_fn , NULL ) ;
} else if ( strcmp ( tok , " delete " ) = = 0 ) {
delete_tdb ( ) ;
} else if ( strcmp ( tok , " dump " ) = = 0 ) {
tdb_traverse ( tdb , print_rec , NULL ) ;
} else if ( strcmp ( tok , " info " ) = = 0 ) {
info_tdb ( ) ;
} else {
help ( ) ;
}
}
if ( tdb ) tdb_close ( tdb ) ;
return 0 ;
1999-12-21 06:04:37 +03:00
}