Skip to content

Commit

Permalink
lib-dict: dict_lookup() returns now error string
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen authored and GitLab committed May 8, 2016
1 parent 8f9a181 commit b5052fb
Show file tree
Hide file tree
Showing 17 changed files with 247 additions and 178 deletions.
5 changes: 3 additions & 2 deletions src/auth/db-dict.c
Expand Up @@ -397,6 +397,7 @@ static int db_dict_iter_lookup_key_values(struct db_dict_value_iter *iter)
{
struct db_dict_iter_key *key;
string_t *path;
const char *error;
int ret;

/* sort the keys so that we'll first lookup the keys without
Expand All @@ -413,14 +414,14 @@ static int db_dict_iter_lookup_key_values(struct db_dict_value_iter *iter)
str_truncate(path, strlen(DICT_PATH_SHARED));
var_expand(path, key->key->key, iter->var_expand_table);
ret = dict_lookup(iter->conn->dict, iter->pool,
str_c(path), &key->value);
str_c(path), &key->value, &error);
if (ret > 0) {
auth_request_log_debug(iter->auth_request, AUTH_SUBSYS_DB,
"Lookup: %s = %s", str_c(path),
key->value);
} else if (ret < 0) {
auth_request_log_error(iter->auth_request, AUTH_SUBSYS_DB,
"Failed to lookup key %s", str_c(path));
"Failed to lookup key %s: %s", str_c(path), error);
return -1;
} else if (key->key->default_value != NULL) {
auth_request_log_debug(iter->auth_request, AUTH_SUBSYS_DB,
Expand Down
6 changes: 3 additions & 3 deletions src/doveadm/doveadm-dict.c
Expand Up @@ -95,7 +95,7 @@ cmd_dict_init(int *argc, char **argv[],
static void cmd_dict_get(int argc, char *argv[])
{
struct dict *dict;
const char *value;
const char *value, *error;
int ret;

if (cmd_dict_init(&argc, &argv, 1, 0, cmd_dict_get, &dict) < 0)
Expand All @@ -104,9 +104,9 @@ static void cmd_dict_get(int argc, char *argv[])
doveadm_print_init(DOVEADM_PRINT_TYPE_TABLE);
doveadm_print_header("value", "", DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE);

ret = dict_lookup(dict, pool_datastack_create(), argv[0], &value);
ret = dict_lookup(dict, pool_datastack_create(), argv[0], &value, &error);
if (ret < 0) {
i_error("dict_lookup(%s) failed", argv[0]);
i_error("dict_lookup(%s) failed: %s", argv[0], error);
doveadm_exit_code = EX_TEMPFAIL;
} else if (ret == 0) {
i_error("%s doesn't exist", argv[0]);
Expand Down
18 changes: 12 additions & 6 deletions src/lib-dict-extra/dict-fs.c
Expand Up @@ -79,19 +79,20 @@ static const char *fs_dict_get_full_key(struct fs_dict *dict, const char *key)
}
}

static int fs_dict_lookup(struct dict *_dict, pool_t pool,
const char *key, const char **value_r)
static int fs_dict_lookup(struct dict *_dict, pool_t pool, const char *key,
const char **value_r, const char **error_r)
{
struct fs_dict *dict = (struct fs_dict *)_dict;
struct fs_file *file;
struct istream *input;
const unsigned char *data;
size_t size;
const char *path;
string_t *str;
int ret;

file = fs_file_init(dict->fs, fs_dict_get_full_key(dict, key),
FS_OPEN_MODE_READONLY);
path = fs_dict_get_full_key(dict, key);
file = fs_file_init(dict->fs, path, FS_OPEN_MODE_READONLY);
input = fs_read_stream(file, IO_BLOCK_SIZE);
i_stream_read(input);

Expand All @@ -109,6 +110,10 @@ static int fs_dict_lookup(struct dict *_dict, pool_t pool,
*value_r = NULL;
if (input->stream_errno == ENOENT)
ret = 0;
else {
*error_r = t_strdup_printf("read(%s) failed: %s",
path, i_stream_get_error(input));
}
}

i_stream_unref(&input);
Expand Down Expand Up @@ -145,7 +150,7 @@ static bool fs_dict_iterate(struct dict_iterate_context *ctx,
struct fs_dict_iterate_context *iter =
(struct fs_dict_iterate_context *)ctx;
struct fs_dict *dict = (struct fs_dict *)ctx->dict;
const char *path;
const char *path, *error;
int ret;

*key_r = fs_iter_next(iter->fs_iter);
Expand All @@ -166,8 +171,9 @@ static bool fs_dict_iterate(struct dict_iterate_context *ctx,
}
p_clear(iter->value_pool);
path = t_strconcat(iter->paths[iter->path_idx], *key_r, NULL);
if ((ret = fs_dict_lookup(ctx->dict, iter->value_pool, path, value_r)) < 0) {
if ((ret = fs_dict_lookup(ctx->dict, iter->value_pool, path, value_r, &error)) < 0) {
/* I/O error */
i_error("%s", error);
iter->failed = TRUE;
return FALSE;
} else if (ret == 0) {
Expand Down
23 changes: 13 additions & 10 deletions src/lib-dict-extra/dict-ldap.c
Expand Up @@ -301,22 +301,25 @@ ldap_dict_lookup_callback(struct ldap_result *result, struct dict_ldap_op *op)
pool_unref(&pool);
}

static
int ldap_dict_lookup(struct dict *dict, pool_t pool,
const char *key, const char **value_r)
static int
ldap_dict_lookup(struct dict *dict, pool_t pool, const char *key,
const char **value_r, const char **error_r)
{
struct dict_lookup_result res;
pool_t orig_pool = pool;
int ret;

ldap_dict_lookup_async(dict, key, ldap_dict_lookup_done, &res);

if ((ret = ldap_dict_wait(dict)) == 0) {
if (res.ret == 0) {
*value_r = p_strdup(orig_pool, res.value);
} else ret = res.ret;
if (ldap_dict_wait(dict) < 0) {
*error_r = "ldap: Communication failure";
return -1;
}
if (res.ret < 0) {
*error_r = res.error;
return -1;
}
return ret;
if (res.ret > 0)
*value_r = p_strdup(pool, res.value);
return res.ret;
}

/*
Expand Down

0 comments on commit b5052fb

Please sign in to comment.