Exemple #1
0
static void show_tdb(void)
{
	char *k = get_token(1);
	TDB_DATA key, dbuf;

	if (!k) {
		help();
		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;
		}
	}
	
	/* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
	print_rec(tdb, key, dbuf, NULL);
	
	free( dbuf.dptr );
	
	return;
}
static void iterate_bundle_links(void (*func)(char *))
{
	TDB_DATA key, rec, pp;
	char *p, *q;

	key.dptr = blinks_id;
	key.dsize = strlen(blinks_id);
	rec = tdb_fetch(pppdb, key);
	if (rec.dptr == NULL || rec.dsize <= 0) {
		error("bundle link list not found (iterating list)");
		if (rec.dptr != NULL)
			free(rec.dptr);
		return;
	}
	p = rec.dptr;
	p[rec.dsize-1] = 0;
	while ((q = strchr(p, ';')) != NULL) {
		*q = 0;
		key.dptr = p;
		key.dsize = q - p;
		pp = tdb_fetch(pppdb, key);
		if (pp.dptr != NULL && pp.dsize > 0) {
			pp.dptr[pp.dsize-1] = 0;
			func(pp.dptr);
		}
		if (pp.dptr != NULL)
			free(pp.dptr);
		p = q + 1;
	}
	free(rec.dptr);
}
Exemple #3
0
int main(int argc, char *argv[])
{
	struct tdb_context *tdb;
	TDB_DATA key, data;

	plan_tests(10);
	tdb = tdb_open_ex("run.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.dsize = strlen("world");
	data.dptr = (void *)"world";

	ok1(tdb_store(tdb, key, data, TDB_MODIFY) < 0);
	ok1(tdb_error(tdb) == TDB_ERR_NOEXIST);
	ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
	ok1(tdb_store(tdb, key, data, TDB_INSERT) < 0);
	ok1(tdb_error(tdb) == TDB_ERR_EXISTS);
	ok1(tdb_store(tdb, key, data, TDB_MODIFY) == 0);

	data = tdb_fetch(tdb, key);
	ok1(data.dsize == strlen("world"));
	ok1(memcmp(data.dptr, "world", strlen("world")) == 0);
	free(data.dptr);

	key.dsize++;
	data = tdb_fetch(tdb, key);
	ok1(data.dptr == NULL);
	tdb_close(tdb);

	return exit_status();
}
Exemple #4
0
/*
  write a record to a normal database
*/
int ctdb_ltdb_store(struct ctdb_db_context *ctdb_db, TDB_DATA key, 
		    struct ctdb_ltdb_header *header, TDB_DATA data)
{
	struct ctdb_context *ctdb = ctdb_db->ctdb;
	TDB_DATA rec;
	int ret;
	bool seqnum_suppressed = false;

	if (ctdb_db->ctdb_ltdb_store_fn) {
		return ctdb_db->ctdb_ltdb_store_fn(ctdb_db, key, header, data);
	}

	if (ctdb->flags & CTDB_FLAG_TORTURE) {
		struct ctdb_ltdb_header *h2;
		rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
		h2 = (struct ctdb_ltdb_header *)rec.dptr;
		if (rec.dptr && rec.dsize >= sizeof(h2) && h2->rsn > header->rsn) {
			DEBUG(DEBUG_CRIT,("RSN regression! %llu %llu\n",
				 (unsigned long long)h2->rsn, (unsigned long long)header->rsn));
		}
		if (rec.dptr) free(rec.dptr);
	}

	rec.dsize = sizeof(*header) + data.dsize;
	rec.dptr = talloc_size(ctdb, rec.dsize);
	CTDB_NO_MEMORY(ctdb, rec.dptr);

	memcpy(rec.dptr, header, sizeof(*header));
	memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);

	/* Databases with seqnum updates enabled only get their seqnum
	   changes when/if we modify the data */
	if (ctdb_db->seqnum_update != NULL) {
		TDB_DATA old;
		old = tdb_fetch(ctdb_db->ltdb->tdb, key);

		if ( (old.dsize == rec.dsize)
		&& !memcmp(old.dptr+sizeof(struct ctdb_ltdb_header),
			  rec.dptr+sizeof(struct ctdb_ltdb_header),
			  rec.dsize-sizeof(struct ctdb_ltdb_header)) ) {
			tdb_remove_flags(ctdb_db->ltdb->tdb, TDB_SEQNUM);
			seqnum_suppressed = true;
		}
		if (old.dptr) free(old.dptr);
	}
	ret = tdb_store(ctdb_db->ltdb->tdb, key, rec, TDB_REPLACE);
	if (ret != 0) {
		DEBUG(DEBUG_ERR, (__location__ " Failed to store dynamic data\n"));
	}
	if (seqnum_suppressed) {
		tdb_add_flags(ctdb_db->ltdb->tdb, TDB_SEQNUM);
	}

	talloc_free(rec.dptr);

	return ret;
}
Exemple #5
0
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 };
	struct tdb_data key = tdb_mkdata("key", 3);
	struct tdb_data data = tdb_mkdata("data", 4);

	failtest_init(argc, argv);
	failtest_hook = block_repeat_failures;
	failtest_exit_check = exit_check_log;

	failtest_suppress = true;
	plan_tests(sizeof(flags) / sizeof(flags[0]) * 8 + 1);
	for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
		tdb = tdb_open("run-11-simple-fetch.tdb", flags[i],
			       O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
		ok1(tdb);
		if (tdb) {
			struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */

			/* fetch should fail. */
			failtest_suppress = false;
			if (!ok1(tdb_fetch(tdb, key, &d) == TDB_ERR_NOEXIST))
				goto fail;
			failtest_suppress = true;
			ok1(tdb_check(tdb, NULL, NULL) == 0);
			/* Insert should succeed. */
			ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
			ok1(tdb_check(tdb, NULL, NULL) == 0);
			/* Fetch should now work. */
			failtest_suppress = false;
			if (!ok1(tdb_fetch(tdb, key, &d) == TDB_SUCCESS))
				goto fail;
			failtest_suppress = true;
			ok1(tdb_deq(d, data));
			free(d.dptr);
			ok1(tdb_check(tdb, NULL, NULL) == 0);
			tdb_close(tdb);
		}
	}
	ok1(tap_log_messages == 0);
	failtest_exit(exit_status());

fail:
	failtest_suppress = true;
	tdb_close(tdb);
	failtest_exit(exit_status());
}
static int check_filesystem(TDB_CONTEXT *tdb, io_channel channel)
{
	__u32   s_mtime;
	__u8    s_uuid[16];
	errcode_t retval;
	TDB_DATA tdb_key, tdb_data;
	struct ext2_super_block super;

	io_channel_set_blksize(channel, SUPERBLOCK_OFFSET);
	retval = io_channel_read_blk64(channel, 1, -SUPERBLOCK_SIZE, &super);
	if (retval) {
		com_err(prg_name,
			retval, _("Failed to read the file system data \n"));
		return retval;
	}

	tdb_key.dptr = mtime_key;
	tdb_key.dsize = sizeof(mtime_key);
	tdb_data = tdb_fetch(tdb, tdb_key);
	if (!tdb_data.dptr) {
		retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
		com_err(prg_name, retval,
			_("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
		return retval;
	}

	s_mtime = *(__u32 *)tdb_data.dptr;
	if (super.s_mtime != s_mtime) {

		com_err(prg_name, 0,
			_("The file system Mount time didn't match %u\n"),
			s_mtime);

		return  -1;
	}


	tdb_key.dptr = uuid_key;
	tdb_key.dsize = sizeof(uuid_key);
	tdb_data = tdb_fetch(tdb, tdb_key);
	if (!tdb_data.dptr) {
		retval = EXT2_ET_TDB_SUCCESS + tdb_error(tdb);
		com_err(prg_name, retval,
			_("Failed tdb_fetch %s\n"), tdb_errorstr(tdb));
		return retval;
	}
	memcpy(s_uuid, tdb_data.dptr, sizeof(s_uuid));
	if (memcmp(s_uuid, super.s_uuid, sizeof(s_uuid))) {
		com_err(prg_name, 0,
			_("The file system UUID didn't match \n"));
		return -1;
	}

	return 0;
}
Exemple #7
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;
}
pa_datum* pa_database_next(pa_database *db, const pa_datum *key, pa_datum *next, pa_datum *data) {
    TDB_DATA tdb_key, tdb_data;

    pa_assert(db);
    pa_assert(key);

    tdb_key = tdb_nextkey(MAKE_TDB_CONTEXT(db), *datum_to_tdb(&tdb_key, key));

    if (!tdb_key.dptr)
        return NULL;

    if (data) {
        tdb_data = tdb_fetch(MAKE_TDB_CONTEXT(db), tdb_key);

        if (!tdb_data.dptr) {
            free(tdb_key.dptr);
            return NULL;
        }

        datum_from_tdb(data, &tdb_data);
    }

    datum_from_tdb(next, &tdb_key);

    return next;
}
Exemple #9
0
/*******************************************************************
 Get all share mode entries for a dev/inode pair.
********************************************************************/
int get_share_modes(struct vfs_connection_struct *conn,
		    SMB_DEV_T dev, SMB_INO_T inode,
		    share_mode_entry ** shares)
{
	TDB_DATA dbuf;
	struct locking_data *data;
	int ret;

	*shares = NULL;

	dbuf = tdb_fetch(tdb, locking_key(dev, inode));
	if (!dbuf.dptr)
		return 0;

	data = (struct locking_data *)dbuf.dptr;
	ret = data->num_share_mode_entries;
	if (ret)
		*shares =
			(share_mode_entry *) memdup(dbuf.dptr + sizeof(*data),
						    ret * sizeof(**shares));
	free(dbuf.dptr);

	if (!*shares)
		return 0;

	return ret;
}
Exemple #10
0
int main(int argc, char *argv[])
{
	struct tdb_context *tdb;
	TDB_DATA key, data;

	plan_tests(4);
	tdb = tdb_open_ex(NULL, 1024, TDB_INTERNAL, O_CREAT|O_TRUNC|O_RDWR,
			  0600, &taplogctx, NULL);
	ok1(tdb);

	/* Tickle bug on appending zero length buffer to zero length buffer. */
	key.dsize = strlen("hi");
	key.dptr = (void *)"hi";
	data.dptr = (void *)"world";
	data.dsize = 0;

	ok1(tdb_append(tdb, key, data) == 0);
	ok1(tdb_append(tdb, key, data) == 0);
	data = tdb_fetch(tdb, key);
	ok1(data.dsize == 0);
	tdb_close(tdb);
	free(data.dptr);

	return exit_status();
}
Exemple #11
0
/* 
   fetch an integer value from the config database, with a default value
*/
int config_get_int(const char *name, int default_v)
{
	TDB_DATA data;
	TDB_DATA key;
	int v;

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

	data = tdb_fetch(config_db, key);

	/* this trick allows config variables to show up in 'aconfig' as
	   soon as they are used */
	if (!data.dptr) {
		char *s = NULL;
		data.dsize = asprintf(&s, "%d", default_v) + 1;
		data.dptr = s;
		tdb_store(config_db, key, data, TDB_REPLACE);
	}

	v = atoi(data.dptr);
	free(data.dptr);

	return v;
}
Exemple #12
0
/**
 * data read
 *
 * @param int total read total
 */
int _db_read_test(int total, char *df){
	char n[16];
	char *dat = NULL;
	int i = 0;
	int success = 0;
	int s = 0;

	if (total <= 0){
		return 0;
	}

	clock_t start, finish;   
	double duration;   
	start=clock();

	TDB *db = tdb_open(df, "r");
	if ( !db ){
		printf("db_open() %s fail.\n", df);
		return 0;
	}
	for (i=0; i<total; i++){
		sprintf(n, "num%d", i);
		dat = tdb_fetch(db, n);
		if (NULL != dat){
			success++;
		}
	}
	tdb_close(db);

	finish=clock();   
	duration=(double)(finish-start)/CLOCKS_PER_SEC;   
	printf("db read data test success: %d, used time: %fs\n", success, duration);

	return success;
}
Exemple #13
0
static BOOL retrieve_all_messages(char **msgs_buf, size_t *total_len)
{
	TDB_DATA kbuf;
	TDB_DATA dbuf;
	TDB_DATA null_dbuf;

	ZERO_STRUCT(null_dbuf);

	*msgs_buf = NULL;
	*total_len = 0;

	kbuf = message_key_pid(pid_to_procid(sys_getpid()));

	if (tdb_chainlock(tdb, kbuf) == -1)
		return False;

	dbuf = tdb_fetch(tdb, kbuf);
	/*
	 * Replace with an empty record to keep the allocated
	 * space in the tdb.
	 */
	tdb_store(tdb, kbuf, null_dbuf, TDB_REPLACE);
	tdb_chainunlock(tdb, kbuf);

	if (dbuf.dptr == NULL || dbuf.dsize == 0) {
		SAFE_FREE(dbuf.dptr);
		return False;
	}

	*msgs_buf = dbuf.dptr;
	*total_len = dbuf.dsize;

	return True;
}
static void remove_bundle_link()
{
	TDB_DATA key, rec;
	char entry[32];
	char *p, *q;
	int l;

	key.dptr = blinks_id;
	key.dsize = strlen(blinks_id);
	slprintf(entry, sizeof(entry), "%s;", db_key);

	rec = tdb_fetch(pppdb, key);
	if (rec.dptr == NULL || rec.dsize <= 0) {
		if (rec.dptr != NULL)
			free(rec.dptr);
		return;
	}
	rec.dptr[rec.dsize-1] = 0;
	p = strstr(rec.dptr, entry);
	if (p != NULL) {
		q = p + strlen(entry);
		l = strlen(q) + 1;
		memmove(p, q, l);
		rec.dsize = p - rec.dptr + l;
		if (tdb_store(pppdb, key, rec, TDB_REPLACE))
			error("couldn't update bundle link list (removal)");
	}
	free(rec.dptr);
}
Exemple #15
0
/*
  fetch a record from the ltdb, separating out the header information
  and returning the body of the record.
  if the record does not exist, *header will be NULL
  and data = {0, NULL}
*/
int ctdb_ltdb_fetch_with_header(struct ctdb_db_context *ctdb_db, 
		    TDB_DATA key, struct ctdb_ltdb_header *header, 
		    TALLOC_CTX *mem_ctx, TDB_DATA *data)
{
	TDB_DATA rec;

	rec = tdb_fetch(ctdb_db->ltdb->tdb, key);
	if (rec.dsize < sizeof(*header)) {
		free(rec.dptr);

		data->dsize = 0;
		data->dptr = NULL;
		return -1;
	}

	*header = *(struct ctdb_ltdb_header *)rec.dptr;
	if (data) {
		data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
		data->dptr = talloc_memdup(mem_ctx, 
					   sizeof(struct ctdb_ltdb_header)+rec.dptr,
					   data->dsize);
	}

	free(rec.dptr);

	return 0;
}
Exemple #16
0
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(5);
	tdb = tdb_open(NULL, TDB_INTERNAL|TDB_VERSION1, O_CREAT|O_TRUNC|O_RDWR,
		       0600, &hsize);
	ok1(tdb);

	/* Tickle bug on appending zero length buffer to zero length buffer. */
	key = tdb_mkdata("hi", strlen("hi"));
	data = tdb_mkdata("world", 0);

	ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
	ok1(tdb_append(tdb, key, data) == TDB_SUCCESS);
	ok1(tdb_fetch(tdb, key, &data) == TDB_SUCCESS);
	ok1(data.dsize == 0);
	free(data.dptr);
	tdb_close(tdb);

	return exit_status();
}
Exemple #17
0
pa_datum* pa_database_first(pa_database *db, pa_datum *key, pa_datum *data) {
    TDB_DATA tdb_key, tdb_data;

    pa_assert(db);
    pa_assert(key);

    tdb_key = tdb_firstkey(MAKE_TDB_CONTEXT(db));

    if (!tdb_key.dptr)
        return NULL;

    if (data) {
        tdb_data = tdb_fetch(MAKE_TDB_CONTEXT(db), tdb_key);

        if (!tdb_data.dptr) {
            free(tdb_key.dptr);
            return NULL;
        }

        datum_from_tdb(data, &tdb_data);
    }

    datum_from_tdb(key, &tdb_key);

    return key;
}
Exemple #18
0
/**
 * Returns a single value from the database
 *
 * @warning The caller must free the returned data.
 *
 * @param [out]	   value	pointer to the value being read
 * @param [out]    size	    number of bytes of @c value parameter
 * @param [in]     key		pointer to a string containing the key in the ENV db
 * @param [in]     db		pointer of a tdb object;
 *
 * @return error code
 * 	@retval ERR_OK = OK
 * 	@retval ERR_DB_INVALID = failed to access tdb
 * 	@retval ERR_STRING_NULL = key not found
 */
int get_value(const unsigned char *key, 
             unsigned char **value, 
             int *size, 
             DATABASE *db){
             
    int ret = ERR_OK;
    TDB_DATA dkey,dvalue ;

    if( NULL == db )
        return ERR_DB_INVALID;
    
    dkey = string_to_tdb(key);
    if( 0 == dkey.dsize )
        return ERR_STRING_NULL;

    while( sem_wait( &db->sem ) == -1 && errno == EINTR );
	dvalue = tdb_fetch(db->tdb, dkey);
	sem_post( &db->sem );

	if (NULL == dvalue.dptr)
		ret = ERR_STRING_NULL;

	*value =  dvalue.dptr;
	*size = dvalue.dsize;

	free(dkey.dptr);
    return ret;
}
Exemple #19
0
/* Append to an entry. Create if not exist. */
int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
{
	u32 hash;
	TDB_DATA dbuf;
	int ret = -1;

	/* find which hash bucket it is in */
	hash = tdb->hash_fn(&key);
	if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)
		return -1;

	dbuf = tdb_fetch(tdb, key);

	if (dbuf.dptr == NULL) {
		dbuf.dptr = (char *)malloc(new_dbuf.dsize);
	} else {
		dbuf.dptr = (char *)realloc(dbuf.dptr,
					    dbuf.dsize + new_dbuf.dsize);
	}

	if (dbuf.dptr == NULL) {
		tdb->ecode = TDB_ERR_OOM;
		goto failed;
	}

	memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);
	dbuf.dsize += new_dbuf.dsize;

	ret = tdb_store(tdb, key, dbuf, 0);
	
failed:
	tdb_unlock(tdb, BUCKET(hash), F_WRLCK);
	SAFE_FREE(dbuf.dptr);
	return ret;
}
Exemple #20
0
static void addrec_db(void)
{
	int klen, dlen;
	char *k, *d;
	TDB_DATA key, data;

	klen = 1 + (rand() % 4);
	dlen = 1 + (rand() % 100);

	k = randbuf(klen);
	d = randbuf(dlen);

	key.dptr = k;
	key.dsize = klen+1;

	data.dptr = d;
	data.dsize = dlen+1;

	if (rand() % DELETE_PROB == 0) {
		tdb_delete(db, key);
	} else if (rand() % STORE_PROB == 0) {
		if (tdb_store(db, key, data, TDB_REPLACE) != 0) {
			fatal("tdb_store failed");
		}
	} else {
		data = tdb_fetch(db, key);
		if (data.dptr) free(data.dptr);
	}

	free(k);
	free(d);
}
Exemple #21
0
static errcode_t get_inode_count(ext2_icount_t icount, ext2_ino_t ino,
				 __u32 *count)
{
	struct ext2_icount_el 	*el;
	TDB_DATA key, data;

	if (icount->tdb) {
		key.dptr = (unsigned char *) &ino;
		key.dsize = sizeof(ext2_ino_t);

		data = tdb_fetch(icount->tdb, key);
		if (data.dptr == NULL) {
			*count = 0;
			return tdb_error(icount->tdb) + EXT2_ET_TDB_SUCCESS;
		}

		*count = *((__u32 *) data.dptr);
		free(data.dptr);
		return 0;
	}
	el = get_icount_el(icount, ino, 0);
	if (!el) {
		*count = 0;
		return ENOENT;
	}

	*count = el->count;
	return 0;
}
Exemple #22
0
BOOL modify_delete_flag( SMB_DEV_T dev, SMB_INO_T inode, BOOL delete_on_close)
{
	TDB_DATA dbuf;
	struct locking_data *data;
	int i;
	share_mode_entry *shares;

	/* read in the existing share modes */
	dbuf = tdb_fetch(tdb, locking_key(dev, inode));
	if (!dbuf.dptr)
		return False;

	data = (struct locking_data *)dbuf.dptr;
	shares = (share_mode_entry *)(dbuf.dptr + sizeof(*data));

	/* Set/Unset the delete on close element. */
	for (i=0;i<data->u.num_share_mode_entries;i++,shares++) {
		shares->share_mode = (delete_on_close ?
			    (shares->share_mode | DELETE_ON_CLOSE_FLAG) :
			    (shares->share_mode & ~DELETE_ON_CLOSE_FLAG) );
	}

	/* store it back */
	if (data->u.num_share_mode_entries) {
		if (tdb_store(tdb, locking_key(dev,inode), dbuf, TDB_REPLACE)==-1) {
			SAFE_FREE(dbuf.dptr);
			return False;
		}
	}

	SAFE_FREE(dbuf.dptr);
	return True;
}
Exemple #23
0
unsigned int messages_pending_for_pid(struct process_id pid)
{
	TDB_DATA kbuf;
	TDB_DATA dbuf;
	char *buf;
	unsigned int message_count = 0;

	kbuf = message_key_pid(pid);

	dbuf = tdb_fetch(tdb, kbuf);
	if (dbuf.dptr == NULL || dbuf.dsize == 0) {
		SAFE_FREE(dbuf.dptr);
		return 0;
	}

	for (buf = dbuf.dptr; dbuf.dsize > sizeof(struct message_rec);) {
		struct message_rec rec;
		memcpy(&rec, buf, sizeof(rec));
		buf += (sizeof(rec) + rec.len);
		dbuf.dsize -= (sizeof(rec) + rec.len);
		message_count++;
	}

	SAFE_FREE(dbuf.dptr);
	return message_count;
}
Exemple #24
0
static bool _reg_perfcount_get_counter_data(TDB_DATA key, TDB_DATA *data)
{
	TDB_CONTEXT *counters;
	char *fname;

	fname = counters_directory(DATA_DB);
	if (fname == NULL) {
		return false;
	}

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

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

	*data = tdb_fetch(counters, key);

	tdb_close(counters);

	return True;
}
Exemple #25
0
static int dump_tdb(const char *fname, const char *keyname, bool emergency)
{
	TDB_CONTEXT *tdb;
	TDB_DATA key, value;
	struct tdb_logging_context logfn = { log_stderr };

	tdb = tdb_open_ex(fname, 0, 0, O_RDONLY, 0, &logfn, NULL);
	if (!tdb) {
		printf("Failed to open %s\n", fname);
		return 1;
	}

	if (emergency) {
		return tdb_rescue(tdb, emergency_walk, keyname) == 0;
	}
	if (!keyname) {
		return tdb_traverse(tdb, traverse_fn, NULL) == -1 ? 1 : 0;
	} else {
		key.dptr = discard_const_p(uint8_t, keyname);
		key.dsize = strlen(keyname);
		value = tdb_fetch(tdb, key);
		if (!value.dptr) {
			return 1;
		} else {
			print_data(value);
			free(value.dptr);
		}
	}

	return 0;
}
Exemple #26
0
/* returns the size of the uncompressed file, or 0 if it could not be
 * determined.
 */
static off_t get_uncompressed_size(const char *path)
{
        struct ecm *ecm;
        size_t pos;
        TDB_DATA key, data;

        LOG("GET_UNCOMPRESSED_SIZE [%s]\n", path);

        key.dptr = discard_const(path);
        key.dsize = strlen(path);
        data = tdb_fetch(filesize_tdb, key);
        if (data.dptr) {
                off_t size = *(off_t *)data.dptr;
                free(data.dptr);
                return size;
        }

        LOG("GET_UNCOMPRESSED_SIZE SLOW PATH [%s]\n", path);

        ecm = ecm_open_file(dir_fd, path);
        if (ecm == NULL) {
                LOG("Failed to open ECM file %s in get_uncompressed_size\n",
                    path);
                return 0;
        }
        pos = ecm_get_file_size(ecm);
        ecm_close_file(ecm);
        LOG("GET_UNCOMPRESSED_SIZE [%s] %zu\n", path, pos);

        data.dptr = (uint8_t *)&pos;
        data.dsize = sizeof(pos);
        tdb_store(filesize_tdb, key, data, TDB_REPLACE);

        return pos;
}
Exemple #27
0
static PyObject *get_urn_by_id(BaseTDBResolver *self, PyObject *args, PyObject *kwds) {
  static char *kwlist[] = {"id", NULL};
  char buff[BUFF_SIZE];
  uint32_t id;
  PyObject *result, *id_obj;
  TDB_DATA urn;
  TDB_DATA key;

  if(!PyArg_ParseTupleAndKeywords(args, kwds, "O", kwlist, 
				  &id_obj))
    return NULL;

  id_obj = PyNumber_Long(id_obj);
  if(!id_obj) return NULL;

  id = PyLong_AsUnsignedLongLong(id_obj);
  Py_DECREF(id_obj);

  /* We get given an ID and we retrieve the URN it belongs to */
  key.dptr = (unsigned char *)buff;
  key.dsize = from_int(id, buff, BUFF_SIZE);

  urn = tdb_fetch(self->urn_db, key);
  
  if(urn.dptr) {
    result = PyString_FromStringAndSize((char *)urn.dptr, urn.dsize);
    free(urn.dptr);
    return result;
  };

  Py_RETURN_NONE;
};
Exemple #28
0
/** returns the list head in the data file for the uri and attribute
    specified. Return 1 if found, 0 if not found. 
*/
static uint32_t get_data_head(BaseTDBResolver *self, TDB_DATA uri, TDB_DATA attribute, 
			 TDB_DATA_LIST *result) {
  char buff[BUFF_SIZE];
  TDB_DATA data_key;

  data_key.dptr = (unsigned char *)buff;
  data_key.dsize = calculate_key(self, uri, attribute, buff, BUFF_SIZE, 0);

  if(data_key.dsize > 0) {
    // We found these attribute/urn
    TDB_DATA offset_serialised = tdb_fetch(self->data_db, data_key);
    if(offset_serialised.dptr) {
      // We found the head - read the struct
      uint32_t offset = to_int(offset_serialised);
      lseek(self->data_store_fd, offset, SEEK_SET);
      if(read(self->data_store_fd, result, sizeof(*result)) == sizeof(*result)) {
	return offset;
      };
      
      free(offset_serialised.dptr);
    };
  };

  return 0;
};
Exemple #29
0
static void compare_db(void)
{
	TDB_DATA d, key, nextkey;
	datum gd, gkey, gnextkey;

	key = tdb_firstkey(db);
	while (key.dptr) {
		d = tdb_fetch(db, key);
		gkey.dptr = key.dptr;
		gkey.dsize = key.dsize;

		gd = gdbm_fetch(gdbm, gkey);

		if (!gd.dptr) fatal("key not in gdbm");
		if (gd.dsize != d.dsize) fatal("data sizes differ");
		if (memcmp(gd.dptr, d.dptr, d.dsize)) {
			fatal("data differs");
		}

		nextkey = tdb_nextkey(db, key);
		free(key.dptr);
		free(d.dptr);
		free(gd.dptr);
		key = nextkey;
	}

	gkey = gdbm_firstkey(gdbm);
	while (gkey.dptr) {
		gd = gdbm_fetch(gdbm, gkey);
		key.dptr = gkey.dptr;
		key.dsize = gkey.dsize;

		d = tdb_fetch(db, key);

		if (!d.dptr) fatal("key not in db");
		if (d.dsize != gd.dsize) fatal("data sizes differ");
		if (memcmp(d.dptr, gd.dptr, gd.dsize)) {
			fatal("data differs");
		}

		gnextkey = gdbm_nextkey(gdbm, gkey);
		free(gkey.dptr);
		free(gd.dptr);
		free(d.dptr);
		gkey = gnextkey;
	}
}
Exemple #30
0
static uint32_t _reg_perfcount_multi_sz_from_tdb(TDB_CONTEXT *tdb,
					       int keyval,
					       char **retbuf,
					       uint32_t buffer_size)
{
	TDB_DATA kbuf, dbuf;
	char temp[PERFCOUNT_MAX_LEN] = {0};
	char *buf1 = *retbuf;
	uint32_t working_size = 0;
	DATA_BLOB name_index, name;
	bool ok;

	snprintf(temp, sizeof(temp), "%d", keyval);
	kbuf = string_tdb_data(temp);
	dbuf = tdb_fetch(tdb, kbuf);
	if(dbuf.dptr == NULL)
	{
		/* If a key isn't there, just bypass it -- this really shouldn't 
		   happen unless someone's mucking around with the tdb */
		DEBUG(3, ("_reg_perfcount_multi_sz_from_tdb: failed to find key [%s] in [%s].\n",
			  temp, tdb_name(tdb)));
		return buffer_size;
	}
	/* First encode the name_index */
	working_size = (kbuf.dsize + 1)*sizeof(uint16_t);
	buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
	if(!buf1) {
		buffer_size = 0;
		return buffer_size;
	}
	ok = push_reg_sz(talloc_tos(), &name_index, (const char *)kbuf.dptr);
	if (!ok) {
		buffer_size = 0;
		return buffer_size;
	}
	memcpy(buf1+buffer_size, (char *)name_index.data, working_size);
	buffer_size += working_size;
	/* Now encode the actual name */
	working_size = (dbuf.dsize + 1)*sizeof(uint16_t);
	buf1 = (char *)SMB_REALLOC(buf1, buffer_size + working_size);
	if(!buf1) {
		buffer_size = 0;
		return buffer_size;
	}
	memset(temp, 0, sizeof(temp));
	memcpy(temp, dbuf.dptr, dbuf.dsize);
	SAFE_FREE(dbuf.dptr);
	ok = push_reg_sz(talloc_tos(), &name, temp);
	if (!ok) {
		buffer_size = 0;
		return buffer_size;
	}
	memcpy(buf1+buffer_size, (char *)name.data, working_size);
	buffer_size += working_size;

	*retbuf = buf1;

	return buffer_size;
}