[dovecot-cvs] dovecot/src/lib-index mail-cache-compress.c, 1.7, 1.8 mail-cache-lookup.c, 1.10, 1.11 mail-cache.h, 1.16, 1.17

cras at procontrol.fi cras at procontrol.fi
Mon Jul 5 00:07:45 EEST 2004


Update of /home/cvs/dovecot/src/lib-index
In directory talvi:/tmp/cvs-serv30743/lib-index

Modified Files:
	mail-cache-compress.c mail-cache-lookup.c mail-cache.h 
Log Message:
Cache API updates. Don't return direct pointers to mmaped cache file anymore.



Index: mail-cache-compress.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-cache-compress.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- mail-cache-compress.c	4 Jul 2004 20:00:47 -0000	1.7
+++ mail-cache-compress.c	4 Jul 2004 21:07:43 -0000	1.8
@@ -16,14 +16,15 @@
 	enum mail_cache_field cached_fields, field;
 	struct mail_cache_record cache_rec;
 	buffer_t *buffer;
+	pool_t pool;
 	const void *data;
 	size_t size, pos;
-	uint32_t size32;
+	uint32_t *p, size32 = 0;
 	int i;
 
 	memset(&cache_rec, 0, sizeof(cache_rec));
-	buffer = buffer_create_dynamic(pool_datastack_create(),
-				       4096, (size_t)-1);
+	pool = pool_datastack_create();
+	buffer = buffer_create_dynamic(pool, 4096, (size_t)-1);
 
 	cached_fields = orig_cached_fields & ~MAIL_CACHE_HEADERS_MASK;
 	buffer_append(buffer, &cache_rec, sizeof(cache_rec));
@@ -31,18 +32,24 @@
 		if ((cached_fields & field) == 0)
 			continue;
 
-		if (!mail_cache_lookup_field(view, seq, field, &data, &size)) {
+		pos = buffer_get_used_size(buffer);
+		if ((field & MAIL_CACHE_FIXED_MASK) == 0)
+			buffer_append(buffer, &size32, sizeof(size32));
+
+		if (!mail_cache_lookup_field(view, buffer, seq, field)) {
 			cached_fields &= ~field;
+			buffer_set_used_size(buffer, pos);
 			continue;
 		}
 
-		size32 = (uint32_t)size;
+		if ((field & MAIL_CACHE_FIXED_MASK) == 0) {
+			p = buffer_get_space_unsafe(buffer, pos,
+						    sizeof(uint32_t));
+			*p = (uint32_t)size;
+		}
 
-		if ((field & MAIL_CACHE_FIXED_MASK) == 0)
-			buffer_append(buffer, &size32, sizeof(size32));
-		buffer_append(buffer, data, size);
-		if ((size32 & 3) != 0)
-			buffer_append(buffer, null4, 4 - (size32 & 3));
+		if ((size & 3) != 0)
+			buffer_append(buffer, null4, 4 - (size & 3));
 	}
 
 	/* now merge all the headers if we have them all */
@@ -53,15 +60,15 @@
 
 		for (i = 0; i <= header_idx; i++) {
 			field = mail_cache_header_fields[i];
-			if (mail_cache_lookup_field(view, seq, field,
-						    &data, &size) && size > 1) {
-				size--; /* terminating \0 */
-				buffer_append(buffer, data, size);
-				size32 += size;
+			if (mail_cache_lookup_field(view, buffer, seq, field)) {
+				/* remove terminating \0 */
+				buffer_set_used_size(buffer,
+					buffer_get_used_size(buffer)-1);
 			}
 		}
 		buffer_append(buffer, null4, 1);
-		size32++;
+
+		size32 = buffer_get_used_size(buffer) - pos;
 		if ((size32 & 3) != 0)
 			buffer_append(buffer, null4, 4 - (size32 & 3));
 		buffer_write(buffer, pos, &size32, sizeof(size32));

Index: mail-cache-lookup.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-cache-lookup.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- mail-cache-lookup.c	4 Jul 2004 20:00:47 -0000	1.10
+++ mail-cache-lookup.c	4 Jul 2004 21:07:43 -0000	1.11
@@ -2,6 +2,7 @@
 
 #include "lib.h"
 #include "buffer.h"
+#include "str.h"
 #include "mail-cache-private.h"
 
 #define CACHE_PREFETCH 1024
@@ -204,8 +205,7 @@
 
 static int cache_get_field(struct mail_cache *cache,
 			   const struct mail_cache_record *cache_rec,
-			   enum mail_cache_field field,
-			   const void **data_r, size_t *size_r)
+			   enum mail_cache_field field, buffer_t *dest_buf)
 {
 	unsigned int mask;
 	uint32_t data_size;
@@ -247,8 +247,9 @@
 							 "Field size is 0");
 				return FALSE;
 			}
-			*data_r = CONST_PTR_OFFSET(cache_rec, offset);
-			*size_r = data_size;
+			buffer_append(dest_buf,
+				      CONST_PTR_OFFSET(cache_rec, offset),
+				      data_size);
 			return TRUE;
 		}
 		offset = prev_offset;
@@ -258,9 +259,8 @@
 	return FALSE;
 }
 
-int mail_cache_lookup_field(struct mail_cache_view *view, uint32_t seq,
-			    enum mail_cache_field field,
-			    const void **data_r, size_t *size_r)
+int mail_cache_lookup_field(struct mail_cache_view *view, buffer_t *dest_buf,
+			    uint32_t seq, enum mail_cache_field field)
 {
 	struct mail_cache_record *cache_rec;
 
@@ -270,7 +270,7 @@
 	while (cache_rec != NULL) {
 		if ((cache_rec->fields & field) != 0) {
 			return cache_get_field(view->cache, cache_rec, field,
-					       data_r, size_r);
+					       dest_buf);
 		}
 		cache_rec = mail_cache_get_record(view->cache,
 						  cache_rec->prev_offset);
@@ -279,45 +279,25 @@
 	return FALSE;
 }
 
-const char *
-mail_cache_lookup_string_field(struct mail_cache_view *view, uint32_t seq,
-			       enum mail_cache_field field)
+int mail_cache_lookup_string_field(struct mail_cache_view *view, string_t *dest,
+				   uint32_t seq, enum mail_cache_field field)
 {
-	const void *data;
-	size_t size;
+	size_t old_size, new_size;
 
 	i_assert((field & MAIL_CACHE_STRING_MASK) != 0);
 
-	if (!mail_cache_lookup_field(view, seq, field, &data, &size))
-		return NULL;
+	old_size = str_len(dest);
+	if (!mail_cache_lookup_field(view, dest, seq, field))
+		return FALSE;
 
-	if (((const char *)data)[size-1] != '\0') {
+	new_size = str_len(dest);
+	if (old_size == new_size ||
+	    str_data(dest)[new_size-1] != '\0') {
 		mail_cache_set_corrupted(view->cache,
 			"String field %x doesn't end with NUL", field);
-		return NULL;
-	}
-	return data;
-}
-
-int mail_cache_copy_fixed_field(struct mail_cache_view *view, uint32_t seq,
-				enum mail_cache_field field,
-				void *buffer, size_t buffer_size)
-{
-	const void *data;
-	size_t size;
-
-	i_assert((field & MAIL_CACHE_FIXED_MASK) != 0);
-
-	if (!mail_cache_lookup_field(view, seq, field, &data, &size))
 		return FALSE;
-
-	if (buffer_size != size) {
-		i_panic("cache: fixed field %x wrong size "
-			"(%"PRIuSIZE_T" vs %"PRIuSIZE_T")",
-			field, size, buffer_size);
 	}
-
-	memcpy(buffer, data, buffer_size);
+	str_truncate(dest, new_size-1);
 	return TRUE;
 }
 

Index: mail-cache.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib-index/mail-cache.h,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -d -r1.16 -r1.17
--- mail-cache.h	4 Jul 2004 20:00:47 -0000	1.16
+++ mail-cache.h	4 Jul 2004 21:07:43 -0000	1.17
@@ -113,21 +113,12 @@
 /* Set data_r and size_r to point to wanted field in cache file.
    Returns TRUE if field was found. If field contains multiple fields,
    first one found is returned. This is mostly useful for finding headers. */
-int mail_cache_lookup_field(struct mail_cache_view *view, uint32_t seq,
-			    enum mail_cache_field field,
-			    const void **data_r, size_t *size_r);
+int mail_cache_lookup_field(struct mail_cache_view *view, buffer_t *dest_buf,
+			    uint32_t seq, enum mail_cache_field field);
 
 /* Return string field. */
-const char *
-mail_cache_lookup_string_field(struct mail_cache_view *view, uint32_t seq,
-			       enum mail_cache_field field);
-
-/* Copy fixed size field to given buffer. buffer_size must be exactly the
-   expected size. The result will be converted to host byte order.
-   Returns TRUE if field was found. */
-int mail_cache_copy_fixed_field(struct mail_cache_view *view, uint32_t seq,
-				enum mail_cache_field field,
-				void *buffer, size_t buffer_size);
+int mail_cache_lookup_string_field(struct mail_cache_view *view, string_t *dest,
+				   uint32_t seq, enum mail_cache_field field);
 
 /* Mark given field as missing, ie. it should be cached when possible. */
 void mail_cache_mark_missing(struct mail_cache_view *view, uint32_t uid,



More information about the dovecot-cvs mailing list