static void check(mailmessage * msg_info)
{
  if (msg_info->msg_flags != NULL) {
    mail_flags_store_set(get_session_ancestor_data(msg_info)->md_flags_store, msg_info);
    
    mail_flags_store_set(get_cached_session_data(msg_info)->md_flags_store, msg_info);
    /* ignore errors */
  }
}
static void imap_check(mailmessage * msg_info)
{
  if (msg_info->msg_flags != NULL) {
    mail_flags_store_set(get_session_data(msg_info)->imap_flags_store,
        msg_info);
    /* ignore errors */
  }
}
static void mh_check(mailmessage * msg_info)
{
  int r;

  if (msg_info->msg_flags != NULL) {
    r = mail_flags_store_set(get_cached_session_data(msg_info)->mh_flags_store,
        msg_info);
    /* ignore errors */
  }
}
static int get_envelopes_list(mailsession * session,
    struct mailmessage_list * env_list)
{
  int r;
  struct maildir * md;
  unsigned int i;
  int res;

  check_folder(session);

  md = get_maildir_session(session);
  if (md == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  r = maildir_update(md);
  if (r != MAILDIR_NO_ERROR) {
    res = maildirdriver_maildir_error_to_mail_error(r);
    goto err;
  }

  r = maildriver_generic_get_envelopes_list(session, env_list);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i++) {
    struct maildir_msg * md_msg;
    mailmessage * msg;
    uint32_t driver_flags;
    clist * ext;
    chashdatum key;
    chashdatum value;

    msg = carray_get(env_list->msg_tab, i);

    key.data = msg->msg_uid;
    key.len = strlen(msg->msg_uid);
    r = chash_get(md->mdir_msg_hash, &key, &value);
    if (r < 0)
      continue;

    md_msg = value.data;

    driver_flags = maildirdriver_maildir_flags_to_flags(md_msg->msg_flags);

    if (msg->msg_flags == NULL) {
      ext = clist_new();
      if (ext == NULL) {
        res = MAIL_ERROR_MEMORY;
        continue;
      }

      msg->msg_flags = mail_flags_new(driver_flags, ext);
      if (msg->msg_flags == NULL) {
        clist_free(ext);
        res = MAIL_ERROR_MEMORY;
        continue;
      }

      if ((md_msg->msg_flags & MAILDIR_FLAG_NEW) != 0) {
        mail_flags_store_set(get_data(session)->md_flags_store, msg);
      }
    }
    else {
      msg->msg_flags->fl_flags &= MAIL_FLAG_FORWARDED;
      msg->msg_flags->fl_flags |= driver_flags;
    }
  }

  return MAIL_NO_ERROR;

 err:
  return res;
}