]> 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>
Fri, 31 Mar 2017 09:28:01 +0000 (12:28 +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 913c13789210adbd901066b45c48060bc1a1fbe1..965e72a6654718d2196b86cc7a46f64b8bb72bad 100644 (file)
@@ -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)