[LCP]Berkeley DB Woes

David Lloyd lloy0076 at rebel.net.au
Tue Jul 31 20:50:08 UTC 2001


I can't get this to work:


#include <stdio.h>
#include <errno.h>

#include <db.h>

int main(int argc, char *argv[]) {
	DB *dbp;

	DBT key;
	DBT data;

	DBC *dbc;

	int t_ret, ret;

/* Now we do some initialisation tasks */

	memset(&key, 0, sizeof(key));
	memset(&data, 0, sizeof(data));


/* First we create the actual database */

	if ((ret=db_create(&dbp, NULL, 0) != 0)) {
		fprintf(stderr, "vote: %s\n", db_strerror(ret));
	}

/* Now we'll open it */

	if ((ret=dbp->open(dbp, "vote.dbm", "vote", DB_HASH, DB_CREATE, 0664)
!= 0)) {
		dbp->err(dbp, ret, "vote->db_open:");
		goto err;
	}

/* Now we'll start to store some values */

	key.data="author";
	key.size=sizeof("author");
	data.data="David Lloyd";
	data.size=sizeof("David Lloyd");

	if ((ret=dbp->put(dbp, NULL, &key, &data, 0) != 0)) {
		dbp->err(dbp, ret, "vote->put:");
		goto err;
	} else {
		printf("Key: %s and Data: %s\n", (char *)key.data, (char *)data.data);
	}

/* Now we'll deliberately crash the get function */

	key.data="foo";
	key.size=sizeof("foo");

	if ((ret=dbp->get(dbp, NULL, &key, &data, 0) != 0)) {
		switch(ret) {
			case DB_NOTFOUND:
				dbp->err(dbp, ret, "vote->get: NOT FOUND ");
				break;

			case EINVAL:
				dbp->err(dbp, ret, "vote->get: EINVAL ");
				goto err;

			default:
				dbp->err(dbp, ret, "vote->get: %d", ret);
				goto err;
		}
	}

/* Close the database and exit gracefully */
err:
	if ((t_ret=dbp->close(dbp, 0) != 0) && (ret == 0)) {
		ret=t_ret;
	}

	return (ret);
}

Making this and running it returns:

(computer output)
vote->get: 1: Operation not permitted

However, vote.dbm is able to be read by the user and group and I can't
see what's wrong.

(I'm on RH 7.1)

DSL



More information about the linuxCprogramming mailing list