Exemple #1
0
/*
  upgrade from a old style tdb
*/
static bool mapping_upgrade(const char *tdb_path)
{
	static TDB_CONTEXT *tdb;
	int ret, status=0;

	tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDWR, 0600);
	if (tdb == NULL) goto failed;

	/* we have to do the map records first, as alias records may
	   reference them */
	ret = tdb_traverse(tdb, upgrade_map_record, &status);
	if (ret == -1 || status == -1) goto failed;

	ret = tdb_traverse(tdb, upgrade_alias_record, &status);
	if (ret == -1 || status == -1) goto failed;

	if (tdb) {
		tdb_close(tdb);
		tdb = NULL;
	}

	{
		const char *old_path = tdb_path;
		char *new_path = state_path("group_mapping.tdb.upgraded");

		if (!new_path) {
			goto failed;
		}
		if (rename(old_path, new_path) != 0) {
			DEBUG(0,("Failed to rename old group mapping database\n"));
			goto failed;
		}
	}
	return True;

failed:
	DEBUG(0,("Failed to upgrade group mapping database\n"));
	if (tdb) tdb_close(tdb);
	return False;
}
Exemple #2
0
int main(int argc, char** argv)
{
    static uint8_t uuid[16];
    const char *fields[] = {"a", "b"};
    const char *values[] = {"foo", "ba"};
    const uint64_t lengths[] = {3, 2};
    tdb_opt_value val = {.value = UINT64_MAX};
    uint64_t i;
    char pkgname[4096];
    strcpy(pkgname, getenv("TDB_TMP_DIR"));
    strcat(pkgname, "test.tdb");
    tdb_cons* c = tdb_cons_init();
    test_cons_settings(c);
    assert(tdb_cons_open(c, pkgname, fields, 2) == 0);

    assert(tdb_cons_set_opt(c, TDB_OPT_ONLY_DIFF_ITEMS, TDB_TRUE) ==
           TDB_ERR_UNKNOWN_OPTION);

    assert(tdb_cons_set_opt(c, TDB_OPT_CONS_OUTPUT_FORMAT, val) ==
           TDB_ERR_INVALID_OPTION_VALUE);

    assert(tdb_cons_set_opt(c,
                            TDB_OPT_CONS_OUTPUT_FORMAT,
                            opt_val(TDB_OPT_CONS_OUTPUT_FORMAT_PACKAGE)) == 0);

    assert(tdb_cons_get_opt(c, TDB_OPT_ONLY_DIFF_ITEMS, &val) ==
           TDB_ERR_UNKNOWN_OPTION);

    assert(tdb_cons_get_opt(c, TDB_OPT_CONS_OUTPUT_FORMAT, &val) == 0);
    assert(val.value == TDB_OPT_CONS_OUTPUT_FORMAT_PACKAGE);

    for (i = 0; i < NUM_EVENTS; i++)
       assert(tdb_cons_add(c, uuid, i, values, lengths) == 0);

    assert(tdb_cons_finalize(c) == 0);
    tdb_cons_close(c);
    tdb* t = tdb_init();
    assert(tdb_open(t, pkgname) == 0);

    tdb_cursor *cursor = tdb_cursor_new(t);
    const tdb_event *event;
    assert(tdb_get_trail(cursor, 0) == 0);

    for (i = 0; (event = tdb_cursor_next(cursor)); i++){
        assert(event->timestamp == i);
        assert(event->num_items == 2);
    }
    assert(i == NUM_EVENTS);
    tdb_close(t);
    tdb_cursor_free(cursor);
    return 0;
}
Exemple #3
0
static int cmd_append_all(const char* output_path, const char* input)
{
	tdb* db = tdb_init(); assert(db);
	int err = tdb_open(db, input);
	if(err) {
		REPORT_ERROR("Failed to open TDB. error=%i\n", err);
		return 1;
	}

	const uint64_t num_fields = tdb_num_fields(db) - 1;
	const char**   field_ids  = duplicate_fieldids(db);
	tdb_cons*      cons       = tdb_cons_init();
	assert(cons);

	err = tdb_cons_open(cons, output_path, field_ids, num_fields);
	if(err) {
		REPORT_ERROR("Failed to create TDB cons. error=%i\n", err);
		goto free_fieldids;
	}

	TIMED("tdb_cons_append()", err, tdb_cons_append(cons, db));
	if(err) {
		REPORT_ERROR("Failed to append DB. error=%i\n", err);
		goto close_cons;
	}

	err = tdb_cons_finalize(cons);
	if(err) {
		REPORT_ERROR("Failed to finalize output DB. error=%i\n", err);
		goto close_cons;
	}

	printf("Successfully converted / rewritten DB.\n");


close_cons:
	tdb_cons_close(cons);

free_fieldids:
	/* to make the compiler not complain about casting const'ness away,
	   let's pull out this small, dirty trick */
	for(uint64_t i = 0; i < num_fields; ++i) {
		void* make_compiler_happy;
		memcpy(&make_compiler_happy, field_ids + i, sizeof(void*));
		free(make_compiler_happy);
	}
	free(field_ids);

	tdb_close(db);

	return err ? 1 : 0;
}
Exemple #4
0
static void open_tdb(void)
{
  char *tok = get_token(1);
  if (!tok) {
    help();
    return;
  }
  if (tdb) tdb_close(tdb);
  tdb = tdb_open(tok, 0, 0, O_RDWR, 0600);
  if (!tdb) {
    printf("Could not open %s: %s\n", tok, strerror(errno));
  }
}
Exemple #5
0
static void tdbsam_tdbclose ( struct tdbsam_privates *state )
{
	if ( !state )
		return;
		
	if ( state->passwd_tdb ) {
		tdb_close( state->passwd_tdb );
		state->passwd_tdb = NULL;
	}
	
	return;
		
}
Exemple #6
0
bool netsamlogon_cache_init(void)
{
    bool first_try = true;
    char *path = NULL;
    int ret;
    struct tdb_context *tdb;

    if (netsamlogon_tdb) {
        return true;
    }

    path = cache_path(NETSAMLOGON_TDB);
    if (path == NULL) {
        return false;
    }
again:
    tdb = tdb_open_log(path, 0, TDB_DEFAULT|TDB_INCOMPATIBLE_HASH,
                       O_RDWR | O_CREAT, 0600);
    if (tdb == NULL) {
        DEBUG(0,("tdb_open_log('%s') - failed\n", path));
        goto clear;
    }

    ret = tdb_check(tdb, NULL, NULL);
    if (ret != 0) {
        tdb_close(tdb);
        DEBUG(0,("tdb_check('%s') - failed\n", path));
        goto clear;
    }

    netsamlogon_tdb = tdb;
    talloc_free(path);
    return true;

clear:
    if (!first_try) {
        talloc_free(path);
        return false;
    }
    first_try = false;

    DEBUG(0,("retry after truncate for '%s'\n", path));
    ret = truncate(path, 0);
    if (ret == -1) {
        DBG_ERR("truncate failed: %s\n", strerror(errno));
        talloc_free(path);
        return false;
    }

    goto again;
}
int main(int argc, char *argv[])
{
	struct tdb_context *tdb;
	TDB_DATA key, data;
	union tdb_attribute hsize;

	hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
	hsize.base.next = &tap_log_attr;
	hsize.tdb1_hashsize.hsize = 1024;

	plan_tests(13);
	agent = prepare_external_agent1();
	if (!agent)
		err(1, "preparing agent");

	tdb = tdb_open("run-traverse-in-transaction.tdb1",
		       TDB_VERSION1, O_CREAT|O_TRUNC|O_RDWR,
		       0600, &hsize);
	ok1(tdb);

	key.dsize = strlen("hi");
	key.dptr = (void *)"hi";
	data.dptr = (void *)"world";
	data.dsize = strlen("world");

	ok1(tdb_store(tdb, key, data, TDB_INSERT) == TDB_SUCCESS);

	ok1(external_agent_operation1(agent, OPEN, tdb->name) == SUCCESS);

	ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
	ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
	    == WOULD_HAVE_BLOCKED);
	tdb_traverse(tdb, traverse, NULL);

	/* That should *not* release the transaction lock! */
	ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
	    == WOULD_HAVE_BLOCKED);
	tdb_traverse(tdb, traverse, NULL);

	/* That should *not* release the transaction lock! */
	ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
	    == WOULD_HAVE_BLOCKED);
	ok1(tdb_transaction_commit(tdb) == TDB_SUCCESS);
	/* Now we should be fine. */
	ok1(external_agent_operation1(agent, TRANSACTION_START, tdb->name)
	    == SUCCESS);

	tdb_close(tdb);

	return exit_status();
}
Exemple #8
0
int main(int argc, char *argv[])
{
	unsigned int i, j;
	struct tdb_context *tdb;
	int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
			TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
			TDB_NOMMAP|TDB_CONVERT };
	TDB_DATA key = { (unsigned char *)&j, sizeof(j) };
	TDB_DATA data = { (unsigned char *)&j, sizeof(j) };
	char *summary;

	plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		tdb = tdb_open("run-summary.tdb1", flags[i]|TDB_VERSION1,
			       O_RDWR|O_CREAT|O_TRUNC, 0600, NULL);
		ok1(tdb);
		if (!tdb)
			continue;

		/* Put some stuff in there. */
		for (j = 0; j < 500; j++) {
			/* Make sure padding varies to we get some graphs! */
			data.dsize = j % (sizeof(j) + 1);
			if (tdb_store(tdb, key, data, TDB_REPLACE)
			    != TDB_SUCCESS) {
				fail("Storing in tdb");
			}
		}

		summary = tdb1_summary(tdb);
		diag("%s", summary);
		ok1(strstr(summary, "Size of file/data: "));
		ok1(strstr(summary, "Number of records: 500\n"));
		ok1(strstr(summary, "Smallest/average/largest keys: 4/4/4\n"));
		ok1(strstr(summary, "Smallest/average/largest data: 0/2/4\n"));
		ok1(strstr(summary, "Smallest/average/largest padding: "));
		ok1(strstr(summary, "Number of dead records: 0\n"));
		ok1(strstr(summary, "Number of free records: 1\n"));
		ok1(strstr(summary, "Smallest/average/largest free records: "));
		ok1(strstr(summary, "Number of hash chains: 131\n"));
		ok1(strstr(summary, "Smallest/average/largest hash chains: "));
		ok1(strstr(summary, "Number of uncoalesced records: 0\n"));
		ok1(strstr(summary, "Smallest/average/largest uncoalesced runs: 0/0/0\n"));
		ok1(strstr(summary, "Percentage keys/data/padding/free/dead/rechdrs&tailers/hashes: "));

		free(summary);
		tdb_close(tdb);
	}

	return exit_status();
}
Exemple #9
0
static void open_tdb(const char *tdbname)
{
	union tdb_attribute log_attr;
	log_attr.base.attr = TDB_ATTRIBUTE_LOG;
	log_attr.base.next = NULL;
	log_attr.log.fn = tdb_log;

	if (tdb) tdb_close(tdb);
	tdb = tdb_open(tdbname, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600,
		       &log_attr);
	if (!tdb) {
		printf("Could not open %s: %s\n", tdbname, strerror(errno));
	}
}
int main(int argc, char *argv[])
{
	unsigned int i, j;
	struct tdb_context *tdb;
	int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
			TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
			TDB_NOMMAP|TDB_CONVERT };

	plan_tests(sizeof(flags) / sizeof(flags[0]) * 9 + 1);

	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		TDB_DATA k;
		uint64_t size;
		bool was_empty = false;

		k.dptr = (void *)&j;
		k.dsize = sizeof(j);

		tdb = tdb_open("run-30-exhaust-before-expand.tdb", flags[i],
			       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
		ok1(tdb);
		if (!tdb)
			continue;

		ok1(empty_freetable(tdb));
		/* Need some hash lock for expand. */
		ok1(tdb_lock_hashes(tdb, 0, 1, F_WRLCK, TDB_LOCK_WAIT) == 0);
		/* Create some free space. */
		ok1(tdb_expand(tdb, 1) == 0);
		ok1(tdb_unlock_hashes(tdb, 0, 1, F_WRLCK) == 0);
		ok1(tdb_check(tdb, NULL, NULL) == 0);
		ok1(!empty_freetable(tdb));

		size = tdb->map_size;
		/* Insert minimal-length records until we expand. */
		for (j = 0; tdb->map_size == size; j++) {
			was_empty = empty_freetable(tdb);
			if (tdb_store(tdb, k, k, TDB_INSERT) != 0)
				err(1, "Failed to store record %i", j);
		}

		/* Would have been empty before expansion, but no longer. */
		ok1(was_empty);
		ok1(!empty_freetable(tdb));
		tdb_close(tdb);
	}

	ok1(tap_log_messages == 0);
	return exit_status();
}
Exemple #11
0
/*
  wrapped connection to a tdb database. The caller should _not_ free
  this as it is not a talloc structure (as tdb does not use talloc
  yet). It will auto-close when the caller frees the mem_ctx that is
  passed to this call
 */
struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
				   const char *path, int hash_size, 
				   int tdb_flags,
				   int open_flags, mode_t mode, 
				   struct ldb_context *ldb)
{
	struct ltdb_wrap *w;
	struct stat st;
	struct tdb_logging_context log_ctx;

	log_ctx.log_fn = ltdb_log_fn;
	log_ctx.log_private = ldb;

	if (stat(path, &st) == 0) {
		for (w=tdb_list;w;w=w->next) {
			if (st.st_dev == w->device && st.st_ino == w->inode) {
				if (!talloc_reference(mem_ctx, w)) {
					return NULL;
				}
				return w->tdb;
			}
		}
	}

	w = talloc(mem_ctx, struct ltdb_wrap);
	if (w == NULL) {
		return NULL;
	}

	w->tdb = tdb_open_ex(path, hash_size, tdb_flags, open_flags, mode, &log_ctx, NULL);
	if (w->tdb == NULL) {
		talloc_free(w);
		return NULL;
	}

	if (fstat(tdb_fd(w->tdb), &st) != 0) {
		tdb_close(w->tdb);
		talloc_free(w);
		return NULL;
	}

	w->device = st.st_dev;
	w->inode  = st.st_ino;

	talloc_set_destructor(w, ltdb_wrap_destructor);

	DLIST_ADD(tdb_list, w);
	
	return w->tdb;
}
Exemple #12
0
static void move_rec(void)
{
	char *k = get_token(1);
	char *file = get_token(0);	
	TDB_DATA key, dbuf;
	TDB_CONTEXT *dst_tdb;

	if (!k) {
		help();
		return;
	}
	
	if ( !file ) {
		terror("need destination tdb name");
		return;
	}

	key.dptr = k;
	key.dsize = strlen(k)+1;

	dbuf = tdb_fetch(tdb, key);
	if (!dbuf.dptr) {
		/* maybe it is non-NULL terminated key? */
		key.dsize = strlen(k); 
		dbuf = tdb_fetch(tdb, key);
		
		if ( !dbuf.dptr ) {
			terror("fetch failed");
			return;
		}
	}
	
	print_rec(tdb, key, dbuf, NULL);
	
	dst_tdb = tdb_open(file, 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;
}
Exemple #13
0
void close_all_print_db(void)
{
	struct tdb_print_db *p = NULL, *next_p = NULL;

	for (p = print_db_head; p; p = next_p) {
		next_p = p->next;

		if (p->tdb)
			tdb_close(p->tdb);
		DLIST_REMOVE(print_db_head, p);
		ZERO_STRUCTP(p);
		SAFE_FREE(p);
	}
}
Exemple #14
0
static void create_tdb(void)
{
  char *tok = get_token(1);
  if (!tok) {
    help();
    return;
  }
  if (tdb) tdb_close(tdb);
  tdb = tdb_open(tok, 0, TDB_CLEAR_IF_FIRST,
           O_RDWR | O_CREAT | O_TRUNC, 0600);
  if (!tdb) {
    printf("Could not create %s: %s\n", tok, strerror(errno));
  }
}
Exemple #15
0
/* reopen a tdb - this can be used after a fork to ensure that we have an independent
   seek pointer from our parent and to re-establish locks */
int tdb_reopen(struct tdb_context *tdb)
{
	struct stat st;

	if (tdb->flags & TDB_INTERNAL) {
		return 0; /* Nothing to do. */
	}

	if (tdb->num_locks != 0 || tdb->global_lock.count) {
		TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed with locks held\n"));
		goto fail;
	}

	if (tdb->transaction != 0) {
		TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_reopen: reopen not allowed inside a transaction\n"));
		goto fail;
	}

	if (tdb_munmap(tdb) != 0) {
		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: munmap failed (%s)\n", strerror(errno)));
		goto fail;
	}
	if (close(tdb->fd) != 0)
		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
	tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0);
	if (tdb->fd == -1) {
		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: open failed (%s)\n", strerror(errno)));
		goto fail;
	}
	if ((tdb->flags & TDB_CLEAR_IF_FIRST) && 
	    (tdb->methods->tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0, 1) == -1)) {
		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: failed to obtain active lock\n"));
		goto fail;
	}
	if (fstat(tdb->fd, &st) != 0) {
		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: fstat failed (%s)\n", strerror(errno)));
		goto fail;
	}
	if (st.st_ino != tdb->inode || st.st_dev != tdb->device) {
		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_reopen: file dev/inode has changed!\n"));
		goto fail;
	}
	tdb_mmap(tdb);

	return 0;

fail:
	tdb_close(tdb);
	return -1;
}
Exemple #16
0
_PUBLIC_ bool emsabp_destructor(void *data)
{
	struct emsabp_context	*emsabp_ctx = (struct emsabp_context *)data;

	if (emsabp_ctx) {
		if (emsabp_ctx->ttdb_ctx) {
			tdb_close(emsabp_ctx->ttdb_ctx);
		}

		talloc_free(emsabp_ctx->mem_ctx);
		return true;
	}

	return false;
}
/*
 * Free the dir_info structure when it isn't needed any more.
 */
void e2fsck_free_dir_info(e2fsck_t ctx)
{
    if (ctx->dir_info) {
        if (ctx->dir_info->tdb)
            tdb_close(ctx->dir_info->tdb);
        if (ctx->dir_info->tdb_fn) {
            unlink(ctx->dir_info->tdb_fn);
            free(ctx->dir_info->tdb_fn);
        }
        ctx->dir_info->size = 0;
        ctx->dir_info->count = 0;
        ext2fs_free_mem(&ctx->dir_info);
        ctx->dir_info = 0;
    }
}
Exemple #18
0
static void create_tdb(const char *tdbname)
{
	struct tdb_logging_context log_ctx = { NULL, NULL};
	log_ctx.log_fn = tdb_log;

	if (tdb) tdb_close(tdb);
	tdb = tdb_open_ex(tdbname, 0,
			  TDB_CLEAR_IF_FIRST |
			  (disable_mmap?TDB_NOMMAP:0) |
			  (disable_lock?TDB_NOLOCK:0),
			  O_RDWR | O_CREAT | O_TRUNC, 0600, &log_ctx, NULL);
	if (!tdb) {
		printf("Could not create %s: %s\n", tdbname, strerror(errno));
	}
}
Exemple #19
0
 int main(int argc, const char *argv[])
{
	int i, seed=0;
	int loops = 10000;
	int num_entries;
	char test_gdbm[] = "test.gdbm";

	unlink("test.gdbm");

	db = tdb_open("test.tdb", 0, TDB_CLEAR_IF_FIRST, 
		      O_RDWR | O_CREAT | O_TRUNC, 0600);
	gdbm = gdbm_open(test_gdbm, 512, GDBM_WRITER|GDBM_NEWDB|GDBM_FAST, 
			 0600, NULL);

	if (!db || !gdbm) {
		fatal("db open failed");
	}

#if 1
	srand(seed);
	_start_timer();
	for (i=0;i<loops;i++) addrec_gdbm();
	printf("gdbm got %.2f ops/sec\n", i/_end_timer());
#endif

	merge_test();

	srand(seed);
	_start_timer();
	for (i=0;i<loops;i++) addrec_db();
	printf("tdb got %.2f ops/sec\n", i/_end_timer());

	if (tdb_validate_freelist(db, &num_entries) == -1) {
		printf("tdb freelist is corrupt\n");
	} else {
		printf("tdb freelist is good (%d entries)\n", num_entries);
	}

	compare_db();

	printf("traversed %d records\n", tdb_traverse(db, traverse_fn, NULL));
	printf("traversed %d records\n", tdb_traverse(db, traverse_fn, NULL));

	tdb_close(db);
	gdbm_close(gdbm);

	return 0;
}
Exemple #20
0
uint32_t reg_perfcount_get_counter_names(uint32_t base_index, char **retbuf)
{
	char *buf1 = NULL;
	uint32_t buffer_size = 0;
	TDB_CONTEXT *names;
	char *fname;
	int i;

	if (base_index == 0) {
		return 0;
	}

	fname = counters_directory(NAMES_DB);
	if (fname == NULL) {
		return 0;
	}

	names = tdb_open_log(fname, 0, TDB_DEFAULT, O_RDONLY, 0444);

	if (names == NULL) {
		DEBUG(1, ("reg_perfcount_get_counter_names: unable to open [%s].\n", fname));
		TALLOC_FREE(fname);
		return 0;
	}
	TALLOC_FREE(fname);

	buffer_size = _reg_perfcount_multi_sz_from_tdb(names, 1, retbuf, buffer_size);

	for(i = 1; i <= base_index; i++)
	{
		buffer_size = _reg_perfcount_multi_sz_from_tdb(names, i*2, retbuf, buffer_size);
	}
	tdb_close(names);

	/* Now terminate the MULTI_SZ with a double unicode NULL */
	buf1 = *retbuf;
	buf1 = (char *)SMB_REALLOC(buf1, buffer_size + 2);
	if(!buf1) {
		buffer_size = 0;
	} else {
		buf1[buffer_size++] = '\0';
		buf1[buffer_size++] = '\0';
	}

	*retbuf=buf1;

	return buffer_size;
}
Exemple #21
0
static PyObject *obj_close(PyTdbObject *self)
{
	int ret;
	if (self->closed)
		Py_RETURN_NONE;
	ret = tdb_close(self->ctx);
	self->closed = true;
	if (ret != 0) {
		PyErr_SetObject(PyExc_RuntimeError,
				Py_BuildValue("(i,s)",
					      TDB_ERR_IO,
					      "Failed to close database"));
		return NULL;
	}
	Py_RETURN_NONE;
}
Exemple #22
0
void brl_shutdown(int read_only)
{
	if (!tdb)
		return;

#if DONT_DO_THIS
	/* doing this traversal could kill solaris machines under high load (tridge) */
	/* delete any dead locks */
	if (!read_only) {
		BOOL check_self = True;
		tdb_traverse(tdb, delete_fn, &check_self);
	}
#endif

	tdb_close(tdb);
}
void TdbDatabase::truncate() {

    if (m_tdbFile) {
        tdb_close(m_tdbFile);
    }

    m_tdbFile = tdb_open(m_databaseName.c_str(), 512, 0, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU | S_IRGRP | S_IROTH);


    if (m_tdbFile == NULL) {
        string message;
        message += "Error opening tdb database: ";
        message += m_databaseName;
        throw DatabaseException(CONNECT_ERROR, message.c_str());
    }
}
Exemple #24
0
static void dump_user_stats(void)
{
	TDB_CONTEXT *tdb;
	int nump = 0;
	message_register(MSG_USR_STATS, handle_usr_stat_reply, NULL);
	tdb = tdb_open_log(lock_path("sessionid.tdb"), 0, TDB_DEFAULT, O_RDONLY, 0);
	if (!tdb) {
		d_printf("\nsessionid.tdb not initialised\n");
	} else {
		d_printf("\nPID     Username      Group         OpCount              ByteCount            \n");
		d_printf("------------------------------------------------------------------------------\n");
		nump = tdb_traverse(tdb, traverse_processes, NULL);
		//DEBUG(10,("Total %d procs traversed\n", nump));
		tdb_close(tdb);
	}
	message_deregister(MSG_USR_STATS);
}
Exemple #25
0
/*
 * tdb validation function.
 * returns 0 if tdb is ok, != 0 if it isn't.
 * this is a wrapper around the actual validation function that opens and closes
 * the tdb.
 */
int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
{
	TDB_CONTEXT *tdb = NULL;
	int ret = 1;

	DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));

	tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0);
	if (!tdb) {
		DEBUG(1, ("Error opening tdb %s\n", tdb_path));
		return ret;
	}

	ret = tdb_validate(tdb, validate_fn);
	tdb_close(tdb);
	return ret;
}
int main(int argc, char *argv[])
{
    struct tdb_context *tdb;
    TDB_DATA key, data;

    plan_tests(13);
    agent = prepare_external_agent();
    if (!agent)
        err(1, "preparing agent");

    tdb = tdb_open_ex("run-traverse-in-transaction.tdb",
                      1024, TDB_CLEAR_IF_FIRST, O_CREAT|O_TRUNC|O_RDWR,
                      0600, &taplogctx, NULL);
    ok1(tdb);

    key.dsize = strlen("hi");
    key.dptr = (void *)"hi";
    data.dptr = (void *)"world";
    data.dsize = strlen("world");

    ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);

    ok1(external_agent_operation(agent, OPEN, tdb_name(tdb)) == SUCCESS);

    ok1(tdb_transaction_start(tdb) == 0);
    ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
        == WOULD_HAVE_BLOCKED);
    tdb_traverse(tdb, traverse, NULL);

    /* That should *not* release the transaction lock! */
    ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
        == WOULD_HAVE_BLOCKED);
    tdb_traverse_read(tdb, traverse, NULL);

    /* That should *not* release the transaction lock! */
    ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
        == WOULD_HAVE_BLOCKED);
    ok1(tdb_transaction_commit(tdb) == 0);
    /* Now we should be fine. */
    ok1(external_agent_operation(agent, TRANSACTION_START, tdb_name(tdb))
        == SUCCESS);

    tdb_close(tdb);

    return exit_status();
}
Exemple #27
0
int regdb_close( void )
{
	int ret;

	tdb_refcount--;

	DEBUG(10,("regdb_close: decrementing refcount (%d)\n", tdb_refcount));

	if ( tdb_refcount > 0 )
		return 0;

	SMB_ASSERT( tdb_refcount >= 0 );

	ret = tdb_close( tdb_reg );
	tdb_reg = NULL;

	return ret;
}
Exemple #28
0
PyObject *py_tdb_close(PyObject *self, PyObject *args)
{
	tdb_hnd_object *obj;

	if (!PyArg_ParseTuple(args, "O!", &tdb_hnd_type, &obj))
		return NULL;

	if (tdb_close(obj->tdb) == -1) {
		obj->tdb = NULL;
		PyErr_SetString(py_tdb_error, strerror(errno));
		return NULL;
	}

	obj->tdb = NULL;

	Py_INCREF(Py_None);
	return Py_None;
}
Exemple #29
0
/**
 * copies a subset of data from one DB into another one. The subset is
 * given by field names
 */
static int cmd_recode(const char* output_path, const char* input,
		      const char** field_names, int names_length)
{
	assert(names_length > 0);

	tdb* const db = tdb_init(); assert(db);
	int err = tdb_open(db, input);
	if(err) {
		REPORT_ERROR("Failed to open TDB. error=%i\n", err);
		return 1;
	}

	tdb_field* field_ids;
	err = resolve_fieldids(&field_ids, db, field_names, names_length);
	if(err < 0) {
		goto free_tdb;
	}

	tdb_cons* const cons = tdb_cons_init();
	assert(cons);

	err = tdb_cons_open(cons, output_path, field_names, (unsigned)names_length);
	if(err) {
		REPORT_ERROR("Failed to create TDB cons. error=%i\n", err);
		goto free_ids;
	}

	TIMED("recode", err, do_recode(cons, db, field_ids, names_length));
	if(err)
		goto close_cons;

	err = tdb_cons_finalize(cons);
	if(err) {
		REPORT_ERROR("Failed to finalize output DB. error=%i\n", err);
	}

close_cons:
	tdb_cons_close(cons);
free_ids:
	free(field_ids);
free_tdb:
	tdb_close(db);
	return err;
}
int main(int argc, char *argv[])
{
	const int flags[] = { TDB_DEFAULT,
			      TDB_CLEAR_IF_FIRST,
			      TDB_NOMMAP, 
			      TDB_CLEAR_IF_FIRST | TDB_NOMMAP };
	int i;
	struct tdb_context *tdb;
	TDB_DATA key, data;

	plan_tests(20);
	agent = prepare_external_agent();
	if (!agent)
		err(1, "preparing agent");

	unlock_callback = after_unlock;
	for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
		clear_if_first = (flags[i] & TDB_CLEAR_IF_FIRST);
		diag("Test with %s and %s\n",
		     clear_if_first ? "CLEAR" : "DEFAULT",
		     (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
		unlink(TEST_DBNAME);
		tdb = tdb_open_ex(TEST_DBNAME, 1024, flags[i],
				  O_CREAT|O_TRUNC|O_RDWR, 0600,
				  &taplogctx, NULL);
		ok1(tdb);

		opened = true;
		ok1(tdb_transaction_start(tdb) == 0);
		key.dsize = strlen("hi");
		key.dptr = (void *)"hi";
		data.dptr = (void *)"world";
		data.dsize = strlen("world");

		ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
		ok1(tdb_transaction_commit(tdb) == 0);
		ok(!errors, "We had %u open errors", errors);

		opened = false;
		tdb_close(tdb);
	}

	return exit_status();
}