Commit b91dafc6 authored by Rusty Russell's avatar Rusty Russell

tdb2: remove: it's now in SAMBA where it belongs.

parent 0b93bd10
../../licenses/LGPL-3
\ No newline at end of file
#include <string.h>
#include <stdio.h>
/**
* tdb2 - [[WORK IN PROGRESS!]] The trivial (64bit transactional) database
*
* The tdb2 module provides an efficient keyword data mapping (usually
* within a file). It supports transactions, so the contents of the
* database is reliable even across crashes.
*
* Example:
* #include <ccan/tdb2/tdb2.h>
* #include <ccan/str/str.h>
* #include <err.h>
* #include <stdio.h>
*
* static void usage(const char *argv0)
* {
* errx(1, "Usage: %s fetch <dbfile> <key>\n"
* "OR %s store <dbfile> <key> <data>", argv0, argv0);
* }
*
* int main(int argc, char *argv[])
* {
* struct tdb_context *tdb;
* TDB_DATA key, value;
* enum TDB_ERROR error;
*
* if (argc < 4)
* usage(argv[0]);
*
* tdb = tdb_open(argv[2], TDB_DEFAULT, O_CREAT|O_RDWR,0600, NULL);
* if (!tdb)
* err(1, "Opening %s", argv[2]);
*
* key.dptr = (void *)argv[3];
* key.dsize = strlen(argv[3]);
*
* if (streq(argv[1], "fetch")) {
* if (argc != 4)
* usage(argv[0]);
* error = tdb_fetch(tdb, key, &value);
* if (error)
* errx(1, "fetch %s: %s",
* argv[3], tdb_errorstr(error));
* printf("%.*s\n", value.dsize, (char *)value.dptr);
* free(value.dptr);
* } else if (streq(argv[1], "store")) {
* if (argc != 5)
* usage(argv[0]);
* value.dptr = (void *)argv[4];
* value.dsize = strlen(argv[4]);
* error = tdb_store(tdb, key, value, 0);
* if (error)
* errx(1, "store %s: %s",
* argv[3], tdb_errorstr(error));
* } else
* usage(argv[0]);
*
* return 0;
* }
*
* Maintainer: Rusty Russell <rusty@rustcorp.com.au>
*
* Author: Rusty Russell
*
* License: LGPL (v3 or any later version)
*
* Ccanlint:
* // valgrind breaks fcntl locks.
* tests_pass_valgrind test/api-83-openhook.c:FAIL
*/
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/asprintf\n");
printf("ccan/hash\n");
printf("ccan/likely\n");
printf("ccan/asearch\n");
printf("ccan/compiler\n");
printf("ccan/build_assert\n");
printf("ccan/ilog\n");
printf("ccan/failtest\n");
printf("ccan/tally\n");
printf("ccan/typesafe_cb\n");
printf("ccan/cast\n");
printf("ccan/endian\n");
return 0;
}
return 1;
}
This diff is collapsed.
Interface differences between TDB1 and TDB2.
- tdb2 uses 'struct tdb_data', tdb1 uses 'struct TDB_DATA'. Use the
TDB_DATA typedef if you want portability between the two.
- tdb2 functions return 0 on success, and a negative error on failure,
whereas tdb1 functions returned 0 on success, and -1 on failure.
tdb1 then used tdb_error() to determine the error; this is also
supported in tdb2 to ease backwards compatibility, though the other
form is preferred.
- tdb2's tdb_fetch() returns an error, tdb1's returned the data directly
(or tdb_null, and you were supposed to check tdb_error() to find out why).
- tdb2's tdb_nextkey() frees the old key's dptr, in tdb2 you needed to do
this manually.
- tdb1's tdb_open/tdb_open_ex took an explicit hash size. tdb2's hash table
resizes as required.
- tdb2 uses a linked list of attribute structures to implement logging and
alternate hashes. tdb1 used tdb_open_ex, which was not extensible.
- tdb2 does locking on read-only databases (ie. O_RDONLY passed to tdb_open).
tdb1 did not: use the TDB_NOLOCK flag if you want to suppress locking.
- tdb2's log function is simpler than tdb1's log function. The string is
already formatted, and it takes an enum tdb_log_level not a tdb_debug_level,
and which has only three values: TDB_LOG_ERROR, TDB_LOG_USE_ERROR and
TDB_LOG_WARNING.
- tdb2 provides tdb_deq() for comparing two struct tdb_data.
- tdb2's tdb_name() returns a copy of the name even for TDB_INTERNAL dbs.
- tdb2 does not need tdb_reopen() or tdb_reopen_all(). If you call
fork() after during certain operations the child should close the
tdb, or complete the operations before continuing to use the tdb:
tdb_transaction_start(): child must tdb_transaction_cancel()
tdb_lockall(): child must call tdb_unlockall()
tdb_lockall_read(): child must call tdb_unlockall_read()
tdb_chainlock(): child must call tdb_chainunlock()
tdb_parse() callback: child must return from tdb_parse()
- tdb2 will not open a non-tdb file, even if O_CREAT is specified.
- There is no tdb_traverse_read. For operating on TDB1 files, you can
simulate it by tdb_add_flag(tdb, TDB_RDONLY); tdb_traverse();
tdb_remove_flag(tdb, TDB_RDONLY). This may be desirable because
traverse on TDB1 files use a write lock on the entire database
unless it's read-only.
- Failure inside a transaction (such as a lock function failing) does
not implicitly cancel the transaction; you still need to call
tdb_transaction_cancel().
TDB1 Compatibility:
- tdb2's offers a tdb1_incompatible_hash function, which is the same
as the default hash with the TDB_INCOMPATIBLE_HASH flag. There is
no way of marking an old TDB incompatible with versions < 1.2.6
while using any other hash.
- The TDB_ATTRIBUTE_TDB1_HASHSIZE attribute can be used to control the
hash size, but only when creating (ie. O_CREAT) a TDB1
(ie. TDB_VERSION1).
- There is no TDB_CLEAR_IF_FIRST flag; it has severe scalability and
API problems. If necessary, you can emulate this by using the open
hook and placing a 1-byte lock at offset 4. If your program forks,
you will need to place this lock again in the child.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#ifndef CCAN_TDB2_TDB1_PRIVATE_H
#define CCAN_TDB2_TDB1_PRIVATE_H
/*
Unix SMB/CIFS implementation.
trivial database library - private includes
Copyright (C) Andrew Tridgell 2005
** 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "private.h"
#include <limits.h>
/* #define TDB_TRACE 1 */
#ifndef HAVE_GETPAGESIZE
#define getpagesize() 0x2000
#endif
#ifndef __STRING
#define __STRING(x) #x
#endif
#ifndef __STRINGSTRING
#define __STRINGSTRING(x) __STRING(x)
#endif
#ifndef __location__
#define __location__ __FILE__ ":" __STRINGSTRING(__LINE__)
#endif
#ifndef offsetof
#define offsetof(t,f) ((unsigned int)&((t *)0)->f)
#endif
#define TDB1_VERSION (0x26011967 + 6)
#define TDB1_MAGIC (0x26011999U)
#define TDB1_FREE_MAGIC (~TDB1_MAGIC)
#define TDB1_DEAD_MAGIC (0xFEE1DEAD)
#define TDB1_RECOVERY_MAGIC (0xf53bc0e7U)
#define TDB1_RECOVERY_INVALID_MAGIC (0x0)
#define TDB1_HASH_RWLOCK_MAGIC (0xbad1a51U)
#define TDB1_ALIGNMENT 4
#define TDB1_DEFAULT_HASH_SIZE 131
#define TDB1_FREELIST_TOP (sizeof(struct tdb1_header))
#define TDB1_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
#define TDB1_DEAD(r) ((r)->magic == TDB1_DEAD_MAGIC)
#define TDB1_BAD_MAGIC(r) ((r)->magic != TDB1_MAGIC && !TDB1_DEAD(r))
#define TDB1_HASH_TOP(hash) (TDB1_FREELIST_TOP + (TDB1_BUCKET(hash)+1)*sizeof(tdb1_off_t))
#define TDB1_HASHTABLE_SIZE(tdb) ((tdb->tdb1.header.hash_size+1)*sizeof(tdb1_off_t))
#define TDB1_DATA_START(hash_size) (TDB1_HASH_TOP(hash_size-1) + sizeof(tdb1_off_t))
#define TDB1_RECOVERY_HEAD offsetof(struct tdb1_header, recovery_start)
#define TDB1_SEQNUM_OFS offsetof(struct tdb1_header, sequence_number)
#define TDB1_PAD_BYTE 0x42
#define TDB1_PAD_U32 0x42424242
/* lock offsets */
#define TDB1_OPEN_LOCK 0
#define TDB1_ACTIVE_LOCK 4
#define TDB1_TRANSACTION_LOCK 8
/* free memory if the pointer is valid and zero the pointer */
#ifndef SAFE_FREE
#define SAFE_FREE(x) do { if ((x) != NULL) {free((void *)x); (x)=NULL;} } while(0)
#endif
#define TDB1_BUCKET(hash) ((hash) % tdb->tdb1.header.hash_size)
#define TDB1_DOCONV() (tdb->flags & TDB_CONVERT)
#define TDB1_CONV(x) (TDB1_DOCONV() ? tdb1_convert(&x, sizeof(x)) : &x)
/* the body of the database is made of one tdb1_record for the free space
plus a separate data list for each hash value */
struct tdb1_record {
tdb1_off_t next; /* offset of the next record in the list */
tdb1_len_t rec_len; /* total byte length of record */
tdb1_len_t key_len; /* byte length of key */
tdb1_len_t data_len; /* byte length of data */
uint32_t full_hash; /* the full 32 bit hash of the key */
uint32_t magic; /* try to catch errors */
/* the following union is implied:
union {
char record[rec_len];
struct {
char key[key_len];
char data[data_len];
}
uint32_t totalsize; (tailer)
}
*/
};
struct tdb1_methods {
int (*tdb1_read)(struct tdb_context *, tdb1_off_t , void *, tdb1_len_t , int );
int (*tdb1_write)(struct tdb_context *, tdb1_off_t, const void *, tdb1_len_t);
void (*next_hash_chain)(struct tdb_context *, uint32_t *);
int (*tdb1_oob)(struct tdb_context *, tdb1_off_t, tdb1_len_t, int );
int (*tdb1_expand_file)(struct tdb_context *, tdb1_off_t , tdb1_off_t );
};
/*
internal prototypes
*/
int tdb1_munmap(struct tdb_context *tdb);
void tdb1_mmap(struct tdb_context *tdb);
int tdb1_lock(struct tdb_context *tdb, int list, int ltype);
int tdb1_nest_lock(struct tdb_context *tdb, uint32_t offset, int ltype,
enum tdb_lock_flags flags);
int tdb1_nest_unlock(struct tdb_context *tdb, uint32_t offset, int ltype);
int tdb1_unlock(struct tdb_context *tdb, int list, int ltype);
int tdb1_brlock(struct tdb_context *tdb,
int rw_type, tdb1_off_t offset, size_t len,
enum tdb_lock_flags flags);
int tdb1_brunlock(struct tdb_context *tdb,
int rw_type, tdb1_off_t offset, size_t len);
bool tdb1_have_extra_locks(struct tdb_context *tdb);
void tdb1_release_transaction_locks(struct tdb_context *tdb);
int tdb1_transaction_lock(struct tdb_context *tdb, int ltype,
enum tdb_lock_flags lockflags);
int tdb1_transaction_unlock(struct tdb_context *tdb, int ltype);
int tdb1_recovery_area(struct tdb_context *tdb,
const struct tdb1_methods *methods,
tdb1_off_t *recovery_offset,
struct tdb1_record *rec);
int tdb1_allrecord_upgrade(struct tdb_context *tdb);
int tdb1_write_lock_record(struct tdb_context *tdb, tdb1_off_t off);
int tdb1_write_unlock_record(struct tdb_context *tdb, tdb1_off_t off);
int tdb1_ofs_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
int tdb1_ofs_write(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
void *tdb1_convert(void *buf, uint32_t size);
int tdb1_free(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
tdb1_off_t tdb1_allocate(struct tdb_context *tdb, tdb1_len_t length, struct tdb1_record *rec);
int tdb1_ofs_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
int tdb1_ofs_write(struct tdb_context *tdb, tdb1_off_t offset, tdb1_off_t *d);
int tdb1_lock_record(struct tdb_context *tdb, tdb1_off_t off);
int tdb1_unlock_record(struct tdb_context *tdb, tdb1_off_t off);
tdb_bool_err tdb1_needs_recovery(struct tdb_context *tdb);
int tdb1_rec_read(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
int tdb1_rec_write(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec);
int tdb1_do_delete(struct tdb_context *tdb, tdb1_off_t rec_ptr, struct tdb1_record *rec);
unsigned char *tdb1_alloc_read(struct tdb_context *tdb, tdb1_off_t offset, tdb1_len_t len);
enum TDB_ERROR tdb1_parse_data(struct tdb_context *tdb, TDB_DATA key,
tdb1_off_t offset, tdb1_len_t len,
enum TDB_ERROR (*parser)(TDB_DATA key,
TDB_DATA data,
void *private_data),
void *private_data);
tdb1_off_t tdb1_find_lock_hash(struct tdb_context *tdb, TDB_DATA key, uint32_t hash, int locktype,
struct tdb1_record *rec);
void tdb1_io_init(struct tdb_context *tdb);
int tdb1_expand(struct tdb_context *tdb, tdb1_off_t size);
tdb1_off_t tdb1_expand_adjust(tdb1_off_t map_size, tdb1_off_t size, int page_size);
int tdb1_rec_free_read(struct tdb_context *tdb, tdb1_off_t off,
struct tdb1_record *rec);
bool tdb1_write_all(int fd, const void *buf, size_t count);
void tdb1_header_hash(struct tdb_context *tdb,
uint32_t *magic1_hash, uint32_t *magic2_hash);
uint64_t tdb1_old_hash(const void *key, size_t len, uint64_t seed, void *);
size_t tdb1_dead_space(struct tdb_context *tdb, tdb1_off_t off);
#endif /* CCAN_TDB2_TDB1_PRIVATE_H */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#include <ccan/tdb2/tdb2.h>
#include <ccan/tap/tap.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "logging.h"
int main(int argc, char *argv[])
{
unsigned int i;
struct tdb_context *tdb;
int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
TDB_NOMMAP|TDB_CONVERT,
TDB_INTERNAL|TDB_VERSION1, TDB_VERSION1,
TDB_NOMMAP|TDB_VERSION1,
TDB_INTERNAL|TDB_CONVERT|TDB_VERSION1,
TDB_CONVERT|TDB_VERSION1,
TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
plan_tests(sizeof(flags) / sizeof(flags[0]) * 3);
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
tdb = tdb_open("api-80-tdb_fd.tdb", flags[i],
O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
if (!ok1(tdb))
continue;
if (flags[i] & TDB_INTERNAL)
ok1(tdb_fd(tdb) == -1);
else
ok1(tdb_fd(tdb) > 2);
tdb_close(tdb);
ok1(tap_log_messages == 0);
}
return exit_status();
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment