changeset 20879:c9e32ef49048

lib-fs: Added internal fs_metadata_find() helper function
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Tue, 11 Oct 2016 01:04:39 +0300
parents 4c1924b74356
children 59a3ab804bcc
files src/lib-fs/fs-api-private.h src/lib-fs/fs-api.c
diffstat 2 files changed, 19 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-fs/fs-api-private.h	Tue Oct 11 00:46:59 2016 +0300
+++ b/src/lib-fs/fs-api-private.h	Tue Oct 11 01:04:39 2016 +0300
@@ -168,6 +168,8 @@
 void fs_metadata_init_or_clear(struct fs_file *file);
 void fs_default_set_metadata(struct fs_file *file,
 			     const char *key, const char *value);
+const char *fs_metadata_find(const ARRAY_TYPE(fs_metadata) *metadata,
+			     const char *key);
 int fs_default_copy(struct fs_file *src, struct fs_file *dest);
 
 void fs_file_timing_end(struct fs_file *file, enum fs_op op);
--- a/src/lib-fs/fs-api.c	Tue Oct 11 00:46:59 2016 +0300
+++ b/src/lib-fs/fs-api.c	Tue Oct 11 01:04:39 2016 +0300
@@ -323,6 +323,21 @@
 	metadata->value = p_strdup(file->metadata_pool, value);
 }
 
+const char *fs_metadata_find(const ARRAY_TYPE(fs_metadata) *metadata,
+			     const char *key)
+{
+	const struct fs_metadata *md;
+
+	if (array_is_created(metadata))
+		return NULL;
+
+	array_foreach(metadata, md) {
+		if (strcmp(md->key, key) == 0)
+			return md->value;
+	}
+	return NULL;
+}
+
 void fs_set_metadata(struct fs_file *file, const char *key, const char *value)
 {
 	i_assert(key != NULL);
@@ -403,18 +418,11 @@
 		       const char **value_r)
 {
 	const ARRAY_TYPE(fs_metadata) *metadata;
-	const struct fs_metadata *md;
 
 	if (fs_get_metadata(file, &metadata) < 0)
 		return -1;
-	array_foreach(metadata, md) {
-		if (strcmp(md->key, key) == 0) {
-			*value_r = md->value;
-			return 1;
-		}
-	}
-	*value_r = NULL;
-	return 0;
+	*value_r = fs_metadata_find(metadata, key);
+	return *value_r != NULL ? 1 : 0;
 }
 
 const char *fs_file_path(struct fs_file *file)