static int _commit_one(struct mailbox *mailbox, struct index_change *change)
{
    indexbuffer_t ibuf;
    unsigned char *buf = ibuf.buf;
    size_t offset;
    struct index_record *record = &change->record;
    uint32_t recno = record->recno;

    mailbox_index_record_to_buf(&change->record, mailbox->i.minor_version, buf);

    offset = mailbox->i.start_offset + ((recno-1) * mailbox->i.record_size);

    /* any failure here is a disaster! */
    if (lseek(mailbox->index_fd, offset, SEEK_SET) == -1) {
        xsyslog(LOG_ERR, "IOERROR: seeking index record failed",
                         "mailbox=<%s> record=<%u>",
                         mailbox_name(mailbox), recno);
        return IMAP_IOERROR;
    }

    if (retry_write(mailbox->index_fd, buf, mailbox->i.record_size)
        != mailbox->i.record_size)
    {
        xsyslog(LOG_ERR, "IOERROR: writing index record failed",
                         "mailbox=<%s> record=<%u>",
                         mailbox_name(mailbox), recno);
        return IMAP_IOERROR;
    }

    /* audit logging */
    if (config_auditlog) {
        char flagstr[FLAGMAPSTR_MAXLEN];
        flags_to_str(record, flagstr);
        if (change->flags & CHANGE_ISAPPEND)
            /* note: messageid doesn't have <> wrappers because it already includes them */
            syslog(LOG_NOTICE, "auditlog: append sessionid=<%s> "
                   "mailbox=<%s> uniqueid=<%s> uid=<%u> modseq=<%llu> "
                   "sysflags=<%s> guid=<%s> messageid=%s size=<%u>",
                   session_id(), mailbox_name(mailbox), mailbox_uniqueid(mailbox), record->uid,
                   record->modseq, flagstr,
                   message_guid_encode(&record->guid), change->msgid,
                   record->size);

        if ((record->internal_flags & FLAG_INTERNAL_EXPUNGED) && !(change->flags & CHANGE_WASEXPUNGED))
            syslog(LOG_NOTICE, "auditlog: expunge sessionid=<%s> "
                   "mailbox=<%s> uniqueid=<%s> uid=<%u> modseq=<%llu> "
                   "sysflags=<%s> guid=<%s> size=<%u>",
                   session_id(), mailbox_name(mailbox), mailbox_uniqueid(mailbox), record->uid,
                   record->modseq, flagstr,
                   message_guid_encode(&record->guid),
                   record->size);

        if ((record->internal_flags & FLAG_INTERNAL_UNLINKED) && !(change->flags & CHANGE_WASUNLINKED))
            syslog(LOG_NOTICE, "auditlog: unlink sessionid=<%s> "
                   "mailbox=<%s> uniqueid=<%s> uid=<%u> modseq=<%llu> "
                   "sysflags=<%s> guid=<%s>",
                   session_id(), mailbox_name(mailbox), mailbox_uniqueid(mailbox), record->uid,
                   record->modseq, flagstr,
                   message_guid_encode(&record->guid));
    }

    return 0;
}
static int _commit_one(struct mailbox *mailbox, struct index_change *change)
{
    indexbuffer_t ibuf;
    unsigned char *buf = ibuf.buf;
    size_t offset;
    struct index_record *record = &change->record;
    uint32_t recno = record->recno;

    mailbox_index_record_to_buf(&change->record, mailbox->i.minor_version, buf);

    offset = mailbox->i.start_offset + ((recno-1) * mailbox->i.record_size);

     
    if (lseek(mailbox->index_fd, offset, SEEK_SET) == -1) {
        xsyslog(LOG_ERR, "IOERROR: seeking index record failed",
                         "mailbox=<%s> record=<%u>",
                         mailbox_name(mailbox), recno);
        return IMAP_IOERROR;
    }

    if (retry_write(mailbox->index_fd, buf, mailbox->i.record_size)
        != mailbox->i.record_size)
    {
        xsyslog(LOG_ERR, "IOERROR: writing index record failed",
                         "mailbox=<%s> record=<%u>",
                         mailbox_name(mailbox), recno);
        return IMAP_IOERROR;
    }

     
    if (config_auditlog) {
        char flagstr[FLAGMAPSTR_MAXLEN];
        flags_to_str(record, flagstr);
        if (change->flags & CHANGE_ISAPPEND)
             
            syslog(LOG_NOTICE, "auditlog: append sessionid=<%s> "
                   "mailbox=<%s> uniqueid=<%s> uid=<%u> modseq=<%llu> "
                   "sysflags=<%s> guid=<%s> messageid=%s size=<%u>",
                   session_id(), mailbox_name(mailbox), mailbox_uniqueid(mailbox), record->uid,
                   record->modseq, flagstr,
                   message_guid_encode(&record->guid), change->msgid,
                   record->size);

        if ((record->internal_flags & FLAG_INTERNAL_EXPUNGED) && !(change->flags & CHANGE_WASEXPUNGED))
            syslog(LOG_NOTICE, "auditlog: expunge sessionid=<%s> "
                   "mailbox=<%s> uniqueid=<%s> uid=<%u> modseq=<%llu> "
                   "sysflags=<%s> guid=<%s> size=<%u>",
                   session_id(), mailbox_name(mailbox), mailbox_uniqueid(mailbox), record->uid,
                   record->modseq, flagstr,
                   message_guid_encode(&record->guid),
                   record->size);

        if ((record->internal_flags & FLAG_INTERNAL_UNLINKED) && !(change->flags & CHANGE_WASUNLINKED))
            syslog(LOG_NOTICE, "auditlog: unlink sessionid=<%s> "
                   "mailbox=<%s> uniqueid=<%s> uid=<%u> modseq=<%llu> "
                   "sysflags=<%s> guid=<%s>",
                   session_id(), mailbox_name(mailbox), mailbox_uniqueid(mailbox), record->uid,
                   record->modseq, flagstr,
                   message_guid_encode(&record->guid));
    }

    return 0;
}