]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-fs: Allow fs_set_metadata() to update already added metadata.
authorTimo Sirainen <timo.sirainen@dovecot.fi>
Fri, 31 Mar 2017 09:28:01 +0000 (12:28 +0300)
committerTimo Sirainen <timo.sirainen@dovecot.fi>
Mon, 3 Apr 2017 11:00:04 +0000 (14:00 +0300)
Previously we were just appending the metadata multiple times with different
values, which could have caused problems.

src/lib-fs/fs-api.c

index 9eb74a429c8f4a7effeed737518804d3459a2b3c..d3030edd070a40b907cb7fa945c05e156fc258bb 100644 (file)
@@ -312,14 +312,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);
 }
 
@@ -331,11 +347,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)