From: Timo Sirainen Date: Fri, 31 Mar 2017 09:28:01 +0000 (+0300) Subject: lib-fs: Allow fs_set_metadata() to update already added metadata. X-Git-Tag: 2.3.0.rc1~1821 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8f900bf5b01a78ac9be748e7502aad7cf3486420;p=thirdparty%2Fdovecot%2Fcore.git lib-fs: Allow fs_set_metadata() to update already added metadata. Previously we were just appending the metadata multiple times with different values, which could have caused problems. --- diff --git a/src/lib-fs/fs-api.c b/src/lib-fs/fs-api.c index 913c137892..965e72a665 100644 --- a/src/lib-fs/fs-api.c +++ b/src/lib-fs/fs-api.c @@ -321,14 +321,30 @@ void fs_metadata_init_or_clear(struct fs_file *file) } T_END; } +static struct fs_metadata * +fs_metadata_find_md(const ARRAY_TYPE(fs_metadata) *metadata, + const char *key) +{ + struct fs_metadata *md; + + array_foreach_modifiable(metadata, md) { + if (strcmp(md->key, key) == 0) + return md; + } + return NULL; +} + void fs_default_set_metadata(struct fs_file *file, const char *key, const char *value) { struct fs_metadata *metadata; fs_metadata_init(file); - metadata = array_append_space(&file->metadata); - metadata->key = p_strdup(file->metadata_pool, key); + metadata = fs_metadata_find_md(&file->metadata, key); + if (metadata == NULL) { + metadata = array_append_space(&file->metadata); + metadata->key = p_strdup(file->metadata_pool, key); + } metadata->value = p_strdup(file->metadata_pool, value); } @@ -340,11 +356,8 @@ const char *fs_metadata_find(const ARRAY_TYPE(fs_metadata) *metadata, if (!array_is_created(metadata)) return NULL; - array_foreach(metadata, md) { - if (strcmp(md->key, key) == 0) - return md->value; - } - return NULL; + md = fs_metadata_find_md(metadata, key); + return md == NULL ? NULL : md->value; } void fs_set_metadata(struct fs_file *file, const char *key, const char *value)