Skip to content

Commit

Permalink
zlib-plugin: Check that UID is assigned
Browse files Browse the repository at this point in the history
When caching, code has to check whether UID is
assigned to avoid reusing cache for different
email since they all have UID = 0 before they
are actually committed.
  • Loading branch information
cmouse authored and sirainen committed Sep 7, 2016
1 parent 1bac2e4 commit ff405d5
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/plugins/zlib/zlib-plugin.c
Expand Up @@ -75,13 +75,14 @@ static void zlib_mail_cache_close(struct zlib_user *zuser)

static struct istream *
zlib_mail_cache_open(struct zlib_user *zuser, struct mail *mail,
struct istream *input)
struct istream *input, bool do_cache)
{
struct zlib_mail_cache *cache = &zuser->cache;
struct istream *inputs[2];
string_t *temp_prefix = t_str_new(128);

zlib_mail_cache_close(zuser);
if (do_cache)
zlib_mail_cache_close(zuser);

/* zlib istream is seekable, but very slow. create a seekable istream
which we can use to quickly seek around in the stream that's been
Expand All @@ -101,15 +102,18 @@ zlib_mail_cache_open(struct zlib_user *zuser, struct mail *mail,
i_stream_get_name(inputs[0])));
i_stream_unref(&inputs[0]);

cache->to = timeout_add(ZLIB_MAIL_CACHE_EXPIRE_MSECS,
zlib_mail_cache_close, zuser);
cache->box = mail->box;
cache->uid = mail->uid;
cache->input = input;

/* index-mail wants the stream to be destroyed at close, so create
a new stream instead of just increasing reference. */
return i_stream_create_limit(cache->input, (uoff_t)-1);
if (do_cache) {
cache->to = timeout_add(ZLIB_MAIL_CACHE_EXPIRE_MSECS,
zlib_mail_cache_close, zuser);
cache->box = mail->box;
cache->uid = mail->uid;
cache->input = input;
/* index-mail wants the stream to be destroyed at close, so create
a new stream instead of just increasing reference. */
return i_stream_create_limit(cache->input, (uoff_t)-1);
} else {
return input;
}
}

static int zlib_istream_opened(struct mail *_mail, struct istream **stream)
Expand All @@ -127,7 +131,7 @@ static int zlib_istream_opened(struct mail *_mail, struct istream **stream)
return zmail->module_ctx.super.istream_opened(_mail, stream);
}

if (cache->uid == _mail->uid && cache->box == _mail->box) {
if (_mail->uid > 0 && cache->uid == _mail->uid && cache->box == _mail->box) {
/* use the cached stream. when doing partial reads it should
already be seeked into the wanted offset. */
i_stream_unref(stream);
Expand All @@ -148,8 +152,8 @@ static int zlib_istream_opened(struct mail *_mail, struct istream **stream)
input = *stream;
*stream = handler->create_istream(input, TRUE);
i_stream_unref(&input);

*stream = zlib_mail_cache_open(zuser, _mail, *stream);
/* dont cache the stream if _mail->uid is 0 */
*stream = zlib_mail_cache_open(zuser, _mail, *stream, (_mail->uid > 0));
}
return zmail->module_ctx.super.istream_opened(_mail, stream);
}
Expand All @@ -162,7 +166,7 @@ static void zlib_mail_close(struct mail *_mail)
struct zlib_mail_cache *cache = &zuser->cache;
uoff_t size;

if (cache->uid == _mail->uid && cache->box == _mail->box) {
if (_mail->uid > 0 && cache->uid == _mail->uid && cache->box == _mail->box) {
/* make sure we have read the entire email into the seekable
stream (which causes the original input stream to be
unrefed). we can't safely keep the original input stream
Expand Down

0 comments on commit ff405d5

Please sign in to comment.