static int imap_fetch_envelope(mailmessage * msg_info, struct mailimf_fields ** result) { struct mailimf_fields * fields; int r; struct mail_cache_db * cache_db; MMAPString * mmapstr; char filename[PATH_MAX]; struct imap_cached_session_state_data * data; int res; data = get_cached_session_data(msg_info); if (data->imap_quoted_mb == NULL) { res = MAIL_ERROR_BAD_STATE; goto err; } snprintf(filename, PATH_MAX, "%s/%s", data->imap_quoted_mb, ENV_NAME); r = mail_cache_db_open_lock(filename, &cache_db); if (r < 0) { res = MAIL_ERROR_FILE; goto err; } mmapstr = mmap_string_new(""); if (mmapstr == NULL) { res = MAIL_ERROR_MEMORY; goto close_db; } r = imapdriver_get_cached_envelope(cache_db, mmapstr, msg_info->msg_session, msg_info, &fields); if ((r != MAIL_ERROR_CACHE_MISS) && (r != MAIL_NO_ERROR)) { res = r; goto close_db; } r = mailmessage_fetch_envelope(get_ancestor(msg_info), &fields); if (r != MAIL_NO_ERROR) { res = r; goto close_db; } r = imapdriver_write_cached_envelope(cache_db, mmapstr, msg_info->msg_session, msg_info, fields); * result = fields; mmap_string_free(mmapstr); mail_cache_db_close_unlock(filename, cache_db); return MAIL_NO_ERROR; close_db: mail_cache_db_close_unlock(filename, cache_db); err: return res; }
static void nntp_check(mailmessage * msg_info) { if (msg_info->msg_flags != NULL) { mail_flags_store_set(get_cached_session_data(msg_info)->nntp_flags_store, msg_info); /* ignore errors */ } }
static void build_cache_name(char * filename, size_t size, mailmessage * msg, char * key) { char * quoted_mb; quoted_mb = get_cached_session_data(msg)->imap_quoted_mb; snprintf(filename, size, "%s/%s", quoted_mb, key); }
static void mbox_check(mailmessage * msg_info) { int r; if (msg_info->msg_flags != NULL) { r = mail_flags_store_set(get_cached_session_data(msg_info)->mbox_flags_store, msg_info); /* ignore errors */ } }
static int nntp_fetch_header(mailmessage * msg_info, char ** result, size_t * result_len) { struct generic_message_t * msg; char * headers; size_t headers_length; struct nntp_cached_session_state_data * cached_data; struct nntp_session_state_data * ancestor_data; int r; char filename[PATH_MAX]; msg = msg_info->msg_data; if (msg->msg_message != NULL) return mailmessage_generic_fetch_header(msg_info, result, result_len); /* we try the cached message */ cached_data = get_cached_session_data(msg_info); ancestor_data = get_ancestor_session_data(msg_info); snprintf(filename, PATH_MAX, "%s/%s/%i-header", cached_data->nntp_cache_directory, ancestor_data->nntp_group_name, msg_info->msg_index); r = generic_cache_read(filename, &headers, &headers_length); if (r == MAIL_NO_ERROR) { * result = headers; * result_len = headers_length; return MAIL_NO_ERROR; } /* we get the message through the network */ r = nntpdriver_head(get_ancestor_session(msg_info), msg_info->msg_index, &headers, &headers_length); if (r != MAIL_NO_ERROR) return r; /* we write the message cache */ generic_cache_store(filename, headers, headers_length); * result = headers; * result_len = headers_length; return MAIL_NO_ERROR; }
static int nntp_prefetch(mailmessage * msg_info) { char * msg_content; size_t msg_length; struct generic_message_t * msg; int r; struct nntp_cached_session_state_data * cached_data; struct nntp_session_state_data * ancestor_data; char filename[PATH_MAX]; /* we try the cached message */ cached_data = get_cached_session_data(msg_info); ancestor_data = get_ancestor_session_data(msg_info); snprintf(filename, PATH_MAX, "%s/%s/%i", cached_data->nntp_cache_directory, ancestor_data->nntp_group_name, msg_info->msg_index); r = generic_cache_read(filename, &msg_content, &msg_length); if (r == MAIL_NO_ERROR) { msg = msg_info->msg_data; msg->msg_message = msg_content; msg->msg_length = msg_length; return MAIL_NO_ERROR; } /* we get the message through the network */ r = nntpdriver_article(get_ancestor_session(msg_info), msg_info->msg_index, &msg_content, &msg_length); if (r != MAIL_NO_ERROR) return r; /* we write the message cache */ generic_cache_store(filename, msg_content, msg_length); msg = msg_info->msg_data; msg->msg_message = msg_content; msg->msg_length = msg_length; return MAIL_NO_ERROR; }
static int nntp_get_flags(mailmessage * msg_info, struct mail_flags ** result) { int r; struct mail_flags * flags; struct mail_cache_db * cache_db_flags; char filename_flags[PATH_MAX]; int res; MMAPString * mmapstr; if (msg_info->msg_flags != NULL) { * result = msg_info->msg_flags; return MAIL_NO_ERROR; } flags = mail_flags_store_get(get_cached_session_data(msg_info)->nntp_flags_store, msg_info->msg_index); if (flags == NULL) { struct nntp_cached_session_state_data * cached_data; struct nntp_session_state_data * ancestor_data; cached_data = get_cached_session_data(msg_info); ancestor_data = get_ancestor_session_data(msg_info); if (ancestor_data->nntp_group_name == NULL) { res = MAIL_ERROR_BAD_STATE; goto err; } snprintf(filename_flags, PATH_MAX, "%s/%s/%s", cached_data->nntp_flags_directory, ancestor_data->nntp_group_name, FLAGS_NAME); r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); if (r < 0) { res = MAIL_ERROR_FILE; goto err; } mmapstr = mmap_string_new(""); if (mmapstr == NULL) { res = MAIL_ERROR_MEMORY; goto close_db_flags; } r = nntpdriver_get_cached_flags(cache_db_flags, mmapstr, msg_info->msg_index, &flags); if (r != MAIL_NO_ERROR) { flags = mail_flags_new_empty(); if (flags == NULL) { res = MAIL_ERROR_MEMORY; goto free_mmapstr; } } mmap_string_free(mmapstr); mail_cache_db_close_unlock(filename_flags, cache_db_flags); } msg_info->msg_flags = flags; * result = flags; return MAIL_NO_ERROR; free_mmapstr: mmap_string_free(mmapstr); close_db_flags: mail_cache_db_close_unlock(filename_flags, cache_db_flags); err: return res; }
static inline mailsession * get_ancestor_session(mailmessage * msg) { return get_cached_session_data(msg)->nntp_ancestor; }
static int get_flags(mailmessage * msg_info, struct mail_flags ** result) { struct mail_cache_db * cache_db_flags; chashdatum key; chashdatum value; struct maildir * md; struct mail_flags * flags; struct maildir_cached_session_state_data * data; struct maildir_msg * md_msg; int r; uint32_t driver_flags; char filename_flags[PATH_MAX]; char keyname[PATH_MAX]; MMAPString * mmapstr; if (msg_info->msg_flags != NULL) { * result = msg_info->msg_flags; return MAIL_NO_ERROR; } data = get_cached_session_data(msg_info); flags = mail_flags_store_get(data->md_flags_store, msg_info->msg_index); if (flags != NULL) { msg_info->msg_flags = flags; * result = msg_info->msg_flags; return MAIL_NO_ERROR; } snprintf(filename_flags, PATH_MAX, "%s%c%s%c%s", data->md_flags_directory, MAIL_DIR_SEPARATOR, data->md_quoted_mb, MAIL_DIR_SEPARATOR, FLAGS_NAME); r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); if (r < 0) return MAIL_ERROR_FILE; snprintf(keyname, PATH_MAX, "%s-flags", msg_info->msg_uid); mmapstr = mmap_string_new(""); if (mmapstr == NULL) { mail_cache_db_close_unlock(filename_flags, cache_db_flags); return MAIL_ERROR_MEMORY; } r = generic_cache_flags_read(cache_db_flags, mmapstr, keyname, &flags); mmap_string_free(mmapstr); mail_cache_db_close_unlock(filename_flags, cache_db_flags); if (r != MAIL_NO_ERROR) { flags = mail_flags_new_empty(); if (flags == NULL) return MAIL_ERROR_MEMORY; } md = get_maildir_session(msg_info); if (md == NULL) return MAIL_ERROR_BAD_STATE; key.data = msg_info->msg_uid; key.len = (unsigned int) strlen(msg_info->msg_uid); r = chash_get(md->mdir_msg_hash, &key, &value); if (r < 0) return MAIL_ERROR_MSG_NOT_FOUND; md_msg = value.data; driver_flags = maildirdriver_maildir_flags_to_flags(md_msg->msg_flags); flags->fl_flags = driver_flags; msg_info->msg_flags = flags; * result = msg_info->msg_flags; return MAIL_NO_ERROR; }
static int mbox_get_flags(mailmessage * msg_info, struct mail_flags ** result) { int r; struct mail_flags * flags; struct mail_cache_db * cache_db_flags; char filename_flags[PATH_MAX]; int res; struct mbox_cached_session_state_data * cached_data; MMAPString * mmapstr; struct mailmbox_folder * folder; if (msg_info->msg_flags != NULL) { * result = msg_info->msg_flags; return MAIL_NO_ERROR; } flags = mail_flags_store_get(get_cached_session_data(msg_info)->mbox_flags_store, msg_info->msg_index); if (flags == NULL) { folder = get_mbox_session(msg_info); if (folder == NULL) { res = MAIL_ERROR_BAD_STATE; goto err; } cached_data = get_cached_session_data(msg_info); if (cached_data->mbox_quoted_mb == NULL) { res = MAIL_ERROR_BAD_STATE; goto err; } snprintf(filename_flags, PATH_MAX, "%s/%s/%s", cached_data->mbox_flags_directory, cached_data->mbox_quoted_mb, FLAGS_NAME); r = mail_cache_db_open_lock(filename_flags, &cache_db_flags); if (r < 0) { res = MAIL_ERROR_FILE; goto err; } mmapstr = mmap_string_new(""); if (mmapstr == NULL) { res = MAIL_ERROR_MEMORY; goto close_db_flags; } if (msg_info->msg_index > folder->mb_written_uid) { flags = mail_flags_new_empty(); } else { r = mboxdriver_get_cached_flags(cache_db_flags, mmapstr, msg_info->msg_session, msg_info->msg_index, &flags); if (r != MAIL_NO_ERROR) { flags = mail_flags_new_empty(); if (flags == NULL) { res = MAIL_ERROR_MEMORY; goto free_mmapstr; } } } mmap_string_free(mmapstr); mail_cache_db_close_unlock(filename_flags, cache_db_flags); } msg_info->msg_flags = flags; * result = flags; return MAIL_NO_ERROR; free_mmapstr: mmap_string_free(mmapstr); close_db_flags: mail_cache_db_close_unlock(filename_flags, cache_db_flags); err: return res; }