📄 mtxnt.c
字号:
}}/* MTX mail fetch flags * Accepts: MAIL stream * sequence * option flags * Sniffs at file to see if some other process changed the flags */void mtx_flags (MAILSTREAM *stream,char *sequence,long flags){ unsigned long i; if (mtx_ping (stream) && /* ping mailbox, get new status for messages */ ((flags & FT_UID) ? mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence))) for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->sequence) mtx_elt (stream,i);}/* MTX mail fetch message header * Accepts: MAIL stream * message # to fetch * pointer to returned header text length * option flags * Returns: message header in RFC822 format */char *mtx_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *length, long flags){ *length = 0; /* default to empty */ if (flags & FT_UID) return "";/* UID call "impossible" */ /* get to header position */ lseek (LOCAL->fd,mtx_hdrpos (stream,msgno,length),L_SET); /* is buffer big enough? */ if (*length > LOCAL->buflen) { fs_give ((void **) &LOCAL->buf); LOCAL->buf = (char *) fs_get ((LOCAL->buflen = *length) + 1); } LOCAL->buf[*length] = '\0'; /* tie off string */ /* slurp the data */ read (LOCAL->fd,LOCAL->buf,*length); return LOCAL->buf;}/* MTX mail fetch message text (body only) * Accepts: MAIL stream * message # to fetch * pointer to returned header text length * option flags * Returns: T, always */long mtx_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags){ unsigned long i,j; MESSAGECACHE *elt; /* UID call "impossible" */ if (flags & FT_UID) return NIL; elt = mtx_elt (stream,msgno); /* get message status */ /* if message not seen */ if (!(flags & FT_PEEK) && !elt->seen) { elt->seen = T; /* mark message as seen */ /* recalculate status */ mtx_update_status (stream,msgno,T); mm_flags (stream,msgno); } /* find header position */ i = mtx_hdrpos (stream,msgno,&j); /* go to text position */ lseek (LOCAL->fd,i + j,L_SET); /* is buffer big enough? */ if ((i = elt->rfc822_size - j) > LOCAL->buflen) { fs_give ((void **) &LOCAL->buf); LOCAL->buf = (char *) fs_get ((LOCAL->buflen = i) + 1); } read (LOCAL->fd,LOCAL->buf,i);/* slurp the data */ LOCAL->buf[i] = '\0'; /* tie off string */ /* set up stringstruct */ INIT (bs,mail_string,LOCAL->buf,i); return T; /* success */}/* MTX mail modify flags * Accepts: MAIL stream * sequence * flag(s) * option flags */void mtx_flag (MAILSTREAM *stream,char *sequence,char *flag,long flags){ struct stat sbuf; if (!stream->rdonly) { /* make sure the update takes */ fsync (LOCAL->fd); fstat (LOCAL->fd,&sbuf); /* get current write time */ LOCAL->filetime = sbuf.st_mtime; }}/* MTX mail per-message modify flags * Accepts: MAIL stream * message cache element */void mtx_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt){ struct stat sbuf; /* maybe need to do a checkpoint? */ if (LOCAL->filetime && !LOCAL->shouldcheck) { fstat (LOCAL->fd,&sbuf); /* get current write time */ if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T; LOCAL->filetime = 0; /* don't do this test for any other messages */ } /* recalculate status */ mtx_update_status (stream,elt->msgno,NIL);}/* MTX mail ping mailbox * Accepts: MAIL stream * Returns: T if stream still alive, NIL if not */long mtx_ping (MAILSTREAM *stream){ unsigned long i = 1; long r = T; int ld; char lock[MAILTMPLEN]; struct stat sbuf; if (stream && LOCAL) { /* only if stream already open */ fstat (LOCAL->fd,&sbuf); /* get current file poop */ if (LOCAL->filetime && !(LOCAL->mustcheck || LOCAL->shouldcheck) && (LOCAL->filetime < sbuf.st_mtime)) LOCAL->shouldcheck = T; /* check for changed message status */ if (LOCAL->mustcheck || LOCAL->shouldcheck) { if (LOCAL->shouldcheck) /* babble when we do this unilaterally */ mm_notify (stream,"[CHECK] Checking for flag updates",NIL); while (i <= stream->nmsgs) mtx_elt (stream,i++); LOCAL->mustcheck = LOCAL->shouldcheck = NIL; } /* get shared parse/append permission */ if ((sbuf.st_size != LOCAL->filesize) && ((ld = lockname (lock,stream->mailbox,LOCK_SH)) >= 0)) { /* parse resulting mailbox */ r = (mtx_parse (stream)) ? T : NIL; unlockfd (ld,lock); /* release shared parse/append permission */ } else if ((sbuf.st_ctime > sbuf.st_atime)||(sbuf.st_ctime > sbuf.st_mtime)){ struct utimbuf times; /* whack the times if necessary */ LOCAL->filetime = times.actime = times.modtime = time (0); utime (stream->mailbox,×); } } return r; /* return result of the parse */}/* MTX mail check mailbox (reparses status too) * Accepts: MAIL stream */void mtx_check (MAILSTREAM *stream){ /* mark that a check is desired */ if (LOCAL) LOCAL->mustcheck = T; if (mtx_ping (stream)) mm_log ("Check completed",(long) NIL);}/* MTX mail expunge mailbox * Accepts: MAIL stream */void mtx_expunge (MAILSTREAM *stream){ struct stat sbuf; off_t pos = 0; int ld; unsigned long i = 1; unsigned long j,k,m,recent; unsigned long n = 0; unsigned long delta = 0; char lock[MAILTMPLEN]; MESSAGECACHE *elt; /* do nothing if stream dead */ if (!mtx_ping (stream)) return; if (stream->rdonly) { /* won't do on readonly files! */ mm_log ("Expunge ignored on readonly mailbox",WARN); return; } if (LOCAL->filetime && !LOCAL->shouldcheck) { fstat (LOCAL->fd,&sbuf); /* get current write time */ if (LOCAL->filetime < sbuf.st_mtime) LOCAL->shouldcheck = T; } /* get exclusive parse/append permission */ if ((ld = lockname (lock,stream->mailbox,LOCK_EX)) < 0) { mm_log ("Unable to lock expunge mailbox",ERROR); return; } /* get exclusive access */ if (flock (LOCAL->fd,LOCK_EX|LOCK_NB)) { flock (LOCAL->fd,LOCK_SH); /* recover previous lock */ mm_log("Can't expunge because mailbox is in use by another process",ERROR); unlockfd (ld,lock); /* release exclusive parse/append permission */ return; } mm_critical (stream); /* go critical */ recent = stream->recent; /* get recent now that pinged and locked */ while (i <= stream->nmsgs) { /* for each message */ elt = mtx_elt (stream,i); /* get cache element */ /* number of bytes to smash or preserve */ k = elt->private.special.text.size + elt->rfc822_size; if (elt->deleted) { /* if deleted */ if (elt->recent) --recent;/* if recent, note one less recent message */ delta += k; /* number of bytes to delete */ mail_expunged (stream,i); /* notify upper levels */ n++; /* count up one more expunged message */ } else if (i++ && delta) { /* preserved message */ /* first byte to preserve */ j = elt->private.special.offset; do { /* read from source position */ m = min (k,LOCAL->buflen); lseek (LOCAL->fd,j,L_SET); read (LOCAL->fd,LOCAL->buf,m); pos = j - delta; /* write to destination position */ while (T) { lseek (LOCAL->fd,pos,L_SET); if (write (LOCAL->fd,LOCAL->buf,m) > 0) break; mm_notify (stream,strerror (errno),WARN); mm_diskerror (stream,errno,T); } pos += m; /* new position */ j += m; /* next chunk, perhaps */ } while (k -= m); /* until done */ /* note the new address of this text */ elt->private.special.offset -= delta; } /* preserved but no deleted messages */ else pos = elt->private.special.offset + k; } if (n) { /* truncate file after last message */ if (pos != (LOCAL->filesize -= delta)) { sprintf (LOCAL->buf,"Calculated size mismatch %lu != %lu, delta = %lu", (unsigned long) pos,(unsigned long) LOCAL->filesize,delta); mm_log (LOCAL->buf,WARN); LOCAL->filesize = pos; /* fix it then */ } ftruncate (LOCAL->fd,LOCAL->filesize); sprintf (LOCAL->buf,"Expunged %lu messages",n); /* output the news */ mm_log (LOCAL->buf,(long) NIL); } else mm_log ("No messages deleted, so no update needed",(long) NIL); fsync (LOCAL->fd); /* force disk update */ fstat (LOCAL->fd,&sbuf); /* get new write time */ LOCAL->filetime = sbuf.st_mtime; mm_nocritical (stream); /* release critical */ /* notify upper level of new mailbox size */ mail_exists (stream,stream->nmsgs); mail_recent (stream,recent); flock (LOCAL->fd,LOCK_SH); /* allow sharers again */ unlockfd (ld,lock); /* release exclusive parse/append permission */}/* MTX mail copy message(s) * Accepts: MAIL stream * sequence * destination mailbox * copy options * Returns: T if success, NIL if failed */long mtx_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options){ struct stat sbuf; struct utimbuf times; MESSAGECACHE *elt; unsigned long i,j,k; long ret = LONGT; int fd,ld; char file[MAILTMPLEN],lock[MAILTMPLEN]; mailproxycopy_t pc = (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL); /* make sure valid mailbox */ if (!mtx_isvalid (mailbox,LOCAL->buf)) switch (errno) { case ENOENT: /* no such file? */ mm_notify (stream,"[TRYCREATE] Must create mailbox before copy",NIL); return NIL; case 0: /* merely empty file? */ break; case EINVAL: if (pc) return (*pc) (stream,sequence,mailbox,options); sprintf (LOCAL->buf,"Invalid MTX-format mailbox name: %.80s",mailbox); mm_log (LOCAL->buf,ERROR); return NIL; default: if (pc) return (*pc) (stream,sequence,mailbox,options); sprintf (LOCAL->buf,"Not a MTX-format mailbox: %.80s",mailbox); mm_log (LOCAL->buf,ERROR); return NIL; } if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence))) return NIL; /* got file? */ if ((fd = open (mailboxfile (file,mailbox),O_BINARY|O_RDWR|O_CREAT, S_IREAD|S_IWRITE)) < 0) { sprintf (LOCAL->buf,"Unable to open copy mailbox: %.80s",strerror (errno)); mm_log (LOCAL->buf,ERROR); return NIL; } mm_critical (stream); /* go critical */ /* get exclusive parse/append permission */ if ((ld = lockname (lock,mailbox,LOCK_EX)) < 0) { mm_log ("Unable to lock copy mailbox",ERROR); return NIL; } fstat (fd,&sbuf); /* get current file size */ lseek (fd,sbuf.st_size,L_SET);/* move to end of file */ /* for each requested message */ for (i = 1; ret && (i <= stream->nmsgs); i++) if ((elt = mail_elt (stream,i))->sequence) { lseek (LOCAL->fd,elt->private.special.offset,L_SET); /* number of bytes to copy */ k = elt->private.special.text.size + elt->rfc822_size; do { /* read from source position */ j = min (k,LOCAL->buflen); read (LOCAL->fd,LOCAL->buf,j); if (write (fd,LOCAL->buf,j) < 0) ret = NIL; } while (ret && (k -= j));/* until done */ } /* make sure all the updates take */ if (!(ret && (ret = !fsync (fd)))) { sprintf (LOCAL->buf,"Unable to write message: %s",strerror (errno)); mm_log (LOCAL->buf,ERROR); ftruncate (fd,sbuf.st_size); } times.actime = sbuf.st_atime; /* preserve atime and mtime */ times.modtime = sbuf.st_mtime; utime (file,×); /* set the times */ unlockfd (ld,lock); /* release exclusive parse/append permission */ close (fd); /* close the file */ mm_nocritical (stream); /* release critical */ /* delete all requested messages */ if (ret && (options & CP_MOVE)) { for (i = 1; i <= stream->nmsgs; i++) if ((elt = mtx_elt (stream,i))->sequence) { elt->deleted = T; /* mark message deleted */ /* recalculate status */ mtx_update_status (stream,i,NIL); } if (!stream->rdonly) { /* make sure the update takes */ fsync (LOCAL->fd); fstat (LOCAL->fd,&sbuf); /* get current write time */ LOCAL->filetime = sbuf.st_mtime; } } return ret;}/* MTX mail append message from stringstruct * Accepts: MAIL stream * destination mailbox * append callback * data for callback * Returns: T if append successful, else NIL */long mtx_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -