www.pudn.com > DTMS.rar > STORE.C


#include	"mail.h"

/* Store a record in the database.
 * Return 0 if OK
 * -1 if messageid is iterate  */

int
mail_store(MAIL *mail, struct message *msg)
{
	int		 keylen, datlen;
	off_t	ptrval;

        _mail_gethint(msg);    
	keylen = strlen(msg->messageid)+strlen(hint)+1;/*+1 for seperator*/
	datlen = strlen(msg->content) + 1;		/* +1 for newline at end */
	if (datlen < DATLEN_MIN || datlen > DATLEN_MAX)
		err_dump("invalid data length");

		/* _mail_find() calculates which hash table this new record
		   goes into (mail->chainoff), regardless whether it already
		   exists or not.  The calls to _mail_writeptr() below
		   change the hash table entry for this chain to point to
		   the new record.  This means the new record is added to
		   the front of the hash chain. */

	if (_mail_find(mail, msg->messageid, 1)== 0)
                return -1;         /*record found,messageid has exist*/
           else {   /* record not found */

			/* _mail_find() locked the hash chain for us; read the
			   chain ptr to the first index record on hash chain */
		ptrval = _mail_readptr(mail, mail->chainoff);

		if (_mail_findfree(mail, keylen, datlen) < 0) {
			/* An empty record of the correct size was not found.
				 We have to append the new record to the ends of
				   the index and data files */
			_mail_writedat(mail, msg->content, 0, SEEK_END);
			_mail_writeidx(mail, msg, 0, SEEK_END, ptrval);
			/* mail->idxoff was set by _mail_writeidx().  The new
				  record goes to the front of the hash chain. */
			_mail_writeptr(mail, mail->chainoff, mail->idxoff);
			/*mail->cnt_stor1++;*/
		} else {
			/* We can reuse an empty record.
			 _mail_findfree() removed the record from the free
		         list and set both mail->datoff and mail->idxoff. */
		_mail_writedat(mail, msg->content, mail->datoff, SEEK_SET);
		_mail_writeidx(mail, msg, mail->idxoff, SEEK_SET, ptrval);
			/* reused record goes to the front of the hash chain. */
			_mail_writeptr(mail, mail->chainoff, mail->idxoff);
			/*mail->cnt_stor2++;*/
		}

	} 
	if (un_lock(mail->idxfd, mail->chainoff, SEEK_SET, 1) < 0)
		err_dump("un_lock error");
        
	return(0);
}

void _mail_gethint(struct message *msg)
{  
 char    space[2],*recvtime;
         space[0]=' ';
         space[1]='\0';
         recvtime=ctime(&msg->recvtime);
         recvtime[24]=' ';
         hint=strcat(recvtime,msg->from);
         strcat(hint,space);
         strcat(hint,msg->subject);

}