Beispiel #1
0
int
db_get_instances(int node_no, const char *app, const char *key, void (*func)(char *str, size_t size))
{
	struct databases db;
	DBC *cursor = NULL;
	DBT pair[2];
	int e, ret = -1;
	char *match;

	memset(&pair[0], 0, sizeof(DBT) * 2);

	if (open_env(&db, PROFILE_DB)) goto out;
	blaa = &db;

	db.profile->cursor(db.profile, NULL, &cursor, 0);

	// FIXME: multiple instance keys?
	match = make_profile_key(node_no, app, key, NULL);
	if (!match) goto out;
	while ((e = cursor->c_get(cursor, &pair[0], &pair[1], DB_NEXT)) == 0) {
		if (strncmp(match, pair[0].data, strlen(match)) == 0)
			func(((char *) pair[0].data) + strlen(match), pair[0].size - strlen(match));
	}
	if (e != DB_NOTFOUND) {
		goto out;
	}

	ret = 0;
out:
	blaa = NULL;
	if (cursor) cursor->c_close(cursor);
	close_env(&db);
	return ret;
}
Beispiel #2
0
struct pack *
db_get_profile(int node_no, const char *app, const char *inst_key, const char *inst_value)
{
	struct databases db;
	struct pack *p = NULL;
	int e;
	char *key, *data;
	size_t size;

	if (!blaa) {
	if (open_env(&db, PROFILE_DB)) goto out;
	}

	// FIXME: multiple instance keys?
	key = make_profile_key(node_no, app, inst_key, inst_value);
	if (!key) goto out;
	if (blaa)
	data = get_data(blaa->profile, key, &size, &e);
	else
	data = get_data(db.profile, key, &size, &e);
	free(key);
	// FIXME: handle notfound separately, see also csl.c/c_get_profile()
	if (e) goto out;

	p = pack_wrap(data, size);

out:
	if (!blaa) close_env(&db);
	return p;
}
Beispiel #3
0
void
db_del_profile(int node_no, const char *app, struct pack *args)
{
	struct databases db;
	char *key = NULL;
	char *inst_key = NULL;
	char *inst_value = NULL;
	char *t;
	size_t ts;

	if (open_env(&db, PROFILE_DB)) goto out;

	while (pack_get(args, &t, &ts)) {
		if (model_is_instance(node_no, t)) {
			inst_key = t;
			pack_get(args, &t, &ts);
			inst_value = t;
		} else {
			pack_get(args, &t, &ts);
		}
	}

	key = make_profile_key(node_no, app, inst_key, inst_value);
	if (!key) goto out;

	del_data(db.profile, key);

out:
	if (key) free(key);
	close_env(&db);
}
Beispiel #4
0
int
db_put_script(int node_no, const char *app, const char *buffer, size_t size)
{
	struct databases db;
	char *key;
	int e, ret = -1;

	if (open_env(&db, APP_DB | MODEL_DB | CODE_DB)) goto out;

	key = make_key(node_no, app);
	if (!key) goto out;

	e = append_item(db.app, app, model_get_path(node_no));
	if (e) goto out;

	e = put_data(db.code, key, buffer, size);
	if (e) goto out;

	e = append_item(db.model, model_get_path(node_no), app);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	return ret;
}
Beispiel #5
0
/*!
 * \brief Force the reading or the GISRC or MAPSET/VAR files
 * and overwrite/append the specified variables
 * 
 */ 
static void force_read_env(int loc)
{
    FILE *fd;
    if ((fd = open_env("r", loc))) {
        parse_env(fd, loc);
        fclose(fd);
    }
}
Beispiel #6
0
int
db_del_app(const char *app)
{
	struct databases db;
	char *list, *list2, *t, *s;
	int e, ret = -1;

	if (open_env(&db, APP_DB | MODEL_DB | CODE_DB)) goto out;

	list = get_data(db.app, app, NULL, &e);
	if (!list) goto out;

	for (t = list; t; t = s) {
		s = strchr(t, '/');
		if (s) {
			*s = '\0';
			++s;
		}

		list2 = get_data(db.model, t, NULL, &e);

		if (list2) {
			char *k;
			int sa = strlen(app);
			k = strstr(list2, app);
			if (k) {
				if (k[sa] == '/') ++sa;
				memmove(k, k + sa, strlen(k) - sa + 1);
				sa = strlen(list2);
				if (sa > 0) {
					if (list2[sa-1] == '/')
						list2[sa-1] = '\0';
				}
				e = put_data(db.model, t, list2, strlen(list2) + 1);
				if (e) goto out;
			}
		}
		free(list2);

		e = db_delete_code(model_lookup_class(t), app);
		e = del_data(db.code, make_key(model_lookup_class(t), app));
		//if (e) goto out;
	}

	free(list);

	e = del_data(db.app, app);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	return ret;
}
Beispiel #7
0
int
db_put_profile(int node_no, const char *app, struct pack *args)
{
	struct databases db;
	struct pack *old_args = NULL;
	int e, ret = -1;
	char *key = NULL;
	char *inst_key = NULL;
	char *inst_value = NULL;
	char *t, *t2, *data;
	size_t ts, size;

	if (open_env(&db, PROFILE_DB)) goto out;

	while (pack_get(args, &t, &ts)) {
		if (model_is_instance(node_no, t)) {
			inst_key = t;
			pack_get(args, &t, &ts);
			inst_value = t;
		} else {
			pack_get(args, &t, &ts);
		}
	}

	key = make_profile_key(node_no, app, inst_key, inst_value);
	if (!key) goto out;

	data = get_data(db.profile, key, &size, &e);
	// FIXME: handle notfound separately, see also csl.c/c_get_profile()
	if (e && e != DB_NOTFOUND) goto out;

	if (!e) {
		old_args = pack_wrap(data, size);
		args->pos = 0;
		while (pack_get(args, &t, &ts)) {
			pack_get(args,&t2, &ts);
			pack_replace(old_args, t, t2, ts);
		}
		e = put_data(db.profile, key, old_args->buffer, old_args->used);
	} else {
		e = put_data(db.profile, key, args->buffer, args->used);
	}

	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	if (old_args) pack_delete(old_args);
	if (key) free(key);
	return ret;
}
Beispiel #8
0
int
db_get_apps(int node_no, char **bufferp)
{
	struct databases db;
	int e, ret = -1;

	if (open_env(&db, MODEL_DB)) goto out;

	*bufferp = get_data(db.model, model_get_path(node_no), NULL, &e);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	return ret;
}
Beispiel #9
0
char *
db_dump_profile(void)
{
	struct databases db;
	struct pack *p;
	DBC *cursor = NULL;
	DBT pair[2];
	int e;
	iks *xml = NULL, *item, *x;
	char *ret = NULL;

	memset(&pair[0], 0, sizeof(DBT) * 2);
	pair[1].flags = DB_DBT_MALLOC;

	if (open_env(&db, PROFILE_DB)) goto out;

	db.profile->cursor(db.profile, NULL, &cursor, 0);

	xml = iks_new("comarProfile");
	iks_insert_cdata(xml, "\n", 1);
	while ((e = cursor->c_get(cursor, &pair[0], &pair[1], DB_NEXT)) == 0) {
		char *t;
		size_t ts;
		item = iks_insert(xml, "item");
		iks_insert_cdata(iks_insert(item, "key"), pair[0].data, pair[0].size);
		p = pack_wrap(pair[1].data, pair[1].size);
		while (pack_get(p, &t, &ts)) {
			iks_insert_cdata(item, "\n", 1);
			x = iks_insert(item, "data");
			iks_insert_attrib(x, "key", t);
			pack_get(p, &t, &ts);
			iks_insert_cdata(iks_insert(x, "value"), t, ts);
		}
		pack_delete(p);
		iks_insert_cdata(xml, "\n", 1);
	}
	if (e != DB_NOTFOUND) {
		goto out;
	}

	ret = iks_string(NULL, xml);
out:
	if (cursor) cursor->c_close(cursor);
	close_env(&db);
	if (xml) iks_delete(xml);
	return ret;
}
Beispiel #10
0
int
db_get_code(int node_no, const char *app, char **bufferp, size_t *sizep)
{
	struct databases db;
	char *key;
	int e, ret = -1;

	if (open_env(&db, CODE_DB)) goto out;

	key = make_key(node_no, app);
	if (!key) goto out;
	*bufferp = get_data(db.code, key, sizep, &e);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	return ret;
}
Beispiel #11
0
static int read_env(int loc)
{

    FILE *fd;

    if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
	return 0;		/* don't use file for GISRC */

    if (G_is_initialized(&st->init[loc]))
	return 1;

    if ((fd = open_env("r", loc))) {
        parse_env(fd, loc);
        fclose(fd);
    }

    G_initialize_done(&st->init[loc]);
    return 0;
}
Beispiel #12
0
int
db_put_script(int node_no, const char *app, const char *buffer, size_t size)
{
	struct databases db;
	int e, ret = -1;

	if (open_env(&db, APP_DB | MODEL_DB)) goto out;

	e = append_item(db.app, app, model_get_path(node_no));
	if (e) goto out;

	e = append_item(db.model, model_get_path(node_no), app);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	if (ret == 0) {
		ret = db_save_code(node_no, app, buffer);
	}
	return ret;
}
Beispiel #13
0
static void write_env(int loc)
{
    FILE *fd;
    int n;
    char dummy[2];
    RETSIGTYPE (*sigint)(int);
#ifdef SIGQUIT
    RETSIGTYPE (*sigquit)(int);
#endif

    if (loc == G_VAR_GISRC && st->varmode == G_GISRC_MODE_MEMORY)
	return;		/* don't use file for GISRC */

    /*
     * THIS CODE NEEDS TO BE PROTECTED FROM INTERRUPTS
     * If interrupted, it can wipe out the GISRC file
     */
    sigint = signal(SIGINT, SIG_IGN);
#ifdef SIGQUIT
    sigquit = signal(SIGQUIT, SIG_IGN);
#endif
    if ((fd = open_env("w", loc))) {
	for (n = 0; n < st->env.count; n++) {
	    struct bind *b = &st->env.binds[n];
	    if (b->name && b->value && b->loc == loc
		&& (sscanf(b->value, "%1s", dummy) == 1))
		fprintf(fd, "%s: %s\n", b->name, b->value);
	}
	fclose(fd);
    }

    signal(SIGINT, sigint);
#ifdef SIGQUIT
    signal(SIGQUIT, sigquit);
#endif
}