📄 unixnt.c
字号:
if (!stream->rdonly) { /* make lock for read/write access */ if ((fd = lockname (tmp,stream->mailbox,NIL)) < 0) mm_log ("Can't open mailbox lock, access is readonly",WARN); /* can get the lock? */ else if (flock (fd,LOCK_EX|LOCK_NB)) { mm_log ("Mailbox is open by another process, access is readonly",WARN); close (fd); } else { /* got the lock, nobody else can alter state */ LOCAL->ld = fd; /* note lock's fd and name */ LOCAL->lname = cpystr (tmp); } } /* parse mailbox */ stream->nmsgs = stream->recent = 0; /* will we be able to get write access? */ if ((LOCAL->ld >= 0) && access (stream->mailbox,02) && (errno == EACCES)) { mm_log ("Can't get write access to mailbox, access is readonly",WARN); flock (LOCAL->ld,LOCK_UN); /* release the lock */ close (LOCAL->ld); /* close the lock file */ LOCAL->ld = -1; /* no more lock fd */ unlink (LOCAL->lname); /* delete it */ } /* reset UID validity */ stream->uid_validity = stream->uid_last = 0; if (stream->silent && !stream->rdonly && (LOCAL->ld < 0)) unix_abort (stream); /* abort if can't get RW silent stream */ /* parse mailbox */ else if (unix_parse (stream,tmp,LOCK_SH)) { unix_unlock (LOCAL->fd,stream,tmp); mail_unlock (stream); mm_nocritical (stream); /* done with critical */ } if (!LOCAL) return NIL; /* failure if stream died */ /* make sure upper level knows readonly */ stream->rdonly = (LOCAL->ld < 0); /* notify about empty mailbox */ if (!(stream->nmsgs || stream->silent)) mm_log ("Mailbox is empty",NIL); if (!stream->rdonly) { /* flags stick if readwrite */ stream->perm_seen = stream->perm_deleted = stream->perm_flagged = stream->perm_answered = stream->perm_draft = T; /* have permanent keywords */ stream->perm_user_flags = 0xffffffff; /* and maybe can create them too */ stream->kwd_create = stream->user_flags[NUSERFLAGS-1] ? NIL : T; } return stream; /* return stream alive to caller */}/* UNIX mail close * Accepts: MAIL stream * close options */void unix_close (MAILSTREAM *stream,long options){ int silent = stream->silent; stream->silent = T; /* go silent */ /* expunge if requested */ if (options & CL_EXPUNGE) unix_expunge (stream); /* else dump final checkpoint */ else if (LOCAL->dirty) unix_check (stream); stream->silent = silent; /* restore old silence state */ unix_abort (stream); /* now punt the file and local data */}/* UNIX mail fetch message header * Accepts: MAIL stream * message # to fetch * pointer to returned header text length * option flags * Returns: message header in RFC822 format */ /* lines to filter from header */static STRINGLIST *unix_hlines = NIL;char *unix_header (MAILSTREAM *stream,unsigned long msgno, unsigned long *length,long flags){ MESSAGECACHE *elt; char *s; *length = 0; /* default to empty */ if (flags & FT_UID) return "";/* UID call "impossible" */ elt = mail_elt (stream,msgno);/* get cache */ if (!unix_hlines) { /* once only code */ STRINGLIST *lines = unix_hlines = mail_newstringlist (); lines->text.size = strlen (lines->text.data = (unsigned char *) "Status"); lines = lines->next = mail_newstringlist (); lines->text.size = strlen (lines->text.data = (unsigned char *) "X-Status"); lines = lines->next = mail_newstringlist (); lines->text.size = strlen (lines->text.data = (unsigned char *) "X-Keywords"); lines = lines->next = mail_newstringlist (); lines->text.size = strlen (lines->text.data = (unsigned char *) "X-UID"); lines = lines->next = mail_newstringlist (); lines->text.size = strlen ((char *) (lines->text.data = (unsigned char *) "X-IMAP")); lines = lines->next = mail_newstringlist (); lines->text.size = strlen ((char *) (lines->text.data = (unsigned char *) "X-IMAPbase")); } /* go to header position */ lseek (LOCAL->fd,elt->private.special.offset + elt->private.msg.header.offset,L_SET); if (flags & FT_INTERNAL) { /* initial data OK? */ if (elt->private.msg.header.text.size > LOCAL->buflen) { fs_give ((void **) &LOCAL->buf); LOCAL->buf = (char *) fs_get ((LOCAL->buflen = elt->private.msg.header.text.size) + 1); } /* read message */ read (LOCAL->fd,LOCAL->buf,elt->private.msg.header.text.size); /* got text, tie off string */ LOCAL->buf[*length = elt->private.msg.header.text.size] = '\0'; } else { /* need to make a CRLF version */ read (LOCAL->fd,s = (char *) fs_get (elt->private.msg.header.text.size+1), elt->private.msg.header.text.size); /* tie off string, and convert to CRLF */ s[elt->private.msg.header.text.size] = '\0'; *length = unix_crlfcpy (&LOCAL->buf,&LOCAL->buflen,s, elt->private.msg.header.text.size); fs_give ((void **) &s); /* free readin buffer */ } *length = mail_filter (LOCAL->buf,*length,unix_hlines,FT_NOT); return LOCAL->buf; /* return processed copy */}/* UNIX mail fetch message text * Accepts: MAIL stream * message # to fetch * pointer to returned stringstruct * option flags * Returns: T on success, NIL if failure */long unix_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags){ char *s; unsigned long i; MESSAGECACHE *elt; /* UID call "impossible" */ if (flags & FT_UID) return NIL; elt = mail_elt (stream,msgno);/* get cache element */ /* if message not seen */ if (!(flags & FT_PEEK) && !elt->seen) { /* mark message seen and dirty */ elt->seen = elt->private.dirty = LOCAL->dirty = T; mm_flags (stream,msgno); } s = unix_text_work (stream,elt,&i,flags); INIT (bs,mail_string,s,i); /* set up stringstruct */ return T; /* success */}/* UNIX mail fetch message text worker routine * Accepts: MAIL stream * message cache element * pointer to returned header text length * option flags */char *unix_text_work (MAILSTREAM *stream,MESSAGECACHE *elt, unsigned long *length,long flags){ FDDATA d; STRING bs; char *s,tmp[CHUNK]; /* go to text position */ lseek (LOCAL->fd,elt->private.special.offset + elt->private.msg.text.offset,L_SET); if (flags & FT_INTERNAL) { /* initial data OK? */ if (elt->private.msg.text.text.size > LOCAL->buflen) { fs_give ((void **) &LOCAL->buf); LOCAL->buf = (char *) fs_get ((LOCAL->buflen = elt->private.msg.text.text.size) + 1); } /* read message */ read (LOCAL->fd,LOCAL->buf,elt->private.msg.text.text.size); /* got text, tie off string */ LOCAL->buf[*length = elt->private.msg.text.text.size] = '\0'; } else { /* need to make a CRLF version */ if (elt->rfc822_size > LOCAL->buflen) { /* excessively conservative, but the right thing is too hard to do */ fs_give ((void **) &LOCAL->buf); LOCAL->buf = (char *) fs_get ((LOCAL->buflen = elt->rfc822_size) + 1); } d.fd = LOCAL->fd; /* yes, set up file descriptor */ d.pos = elt->private.special.offset + elt->private.msg.text.offset; d.chunk = tmp; /* initial buffer chunk */ d.chunksize = CHUNK; /* file chunk size */ INIT (&bs,fd_string,&d,elt->private.msg.text.text.size); for (s = LOCAL->buf; SIZE (&bs);) switch (CHR (&bs)) { case '\r': /* carriage return seen */ *s++ = SNX (&bs); /* copy it and any succeeding LF */ if (SIZE (&bs) && (CHR (&bs) == '\n')) *s++ = SNX (&bs); break; case '\n': *s++ = '\r'; /* insert a CR */ default: *s++ = SNX (&bs); /* copy characters */ } *s = '\0'; /* tie off buffer */ *length = s - LOCAL->buf; /* calculate length */ } return LOCAL->buf;}/* UNIX per-message modify flag * Accepts: MAIL stream * message cache element */void unix_flagmsg (MAILSTREAM *stream,MESSAGECACHE *elt){ /* only after finishing */ if (elt->valid) elt->private.dirty = LOCAL->dirty = T;}/* UNIX mail ping mailbox * Accepts: MAIL stream * Returns: T if stream alive, else NIL */long unix_ping (MAILSTREAM *stream){ char lock[MAILTMPLEN]; struct stat sbuf; /* big no-op if not readwrite */ if (LOCAL && (LOCAL->ld >= 0) && !stream->lock) { if (stream->rdonly) { /* does he want to give up readwrite? */ /* checkpoint if we changed something */ if (LOCAL->dirty) unix_check (stream); flock (LOCAL->ld,LOCK_UN);/* release readwrite lock */ close (LOCAL->ld); /* close the readwrite lock file */ LOCAL->ld = -1; /* no more readwrite lock fd */ unlink (LOCAL->lname); /* delete the readwrite lock file */ } else { /* get current mailbox size */ if (LOCAL->fd >= 0) fstat (LOCAL->fd,&sbuf); else stat (stream->mailbox,&sbuf); /* parse if mailbox changed */ if ((sbuf.st_size != LOCAL->filesize) && unix_parse (stream,lock,LOCK_SH)) { /* unlock mailbox */ unix_unlock (LOCAL->fd,stream,lock); mail_unlock (stream); /* and stream */ mm_nocritical (stream); /* done with critical */ } } } return LOCAL ? LONGT : NIL; /* return if still alive */}/* UNIX mail check mailbox * Accepts: MAIL stream */void unix_check (MAILSTREAM *stream){ char lock[MAILTMPLEN]; /* parse and lock mailbox */ if (LOCAL && (LOCAL->ld >= 0) && !stream->lock && unix_parse (stream,lock,LOCK_EX)) { /* any unsaved changes? */ if (LOCAL->dirty && unix_rewrite (stream,NIL,lock)) { if (!stream->silent) mm_log ("Checkpoint completed",NIL); } /* no checkpoint needed, just unlock */ else unix_unlock (LOCAL->fd,stream,lock); mail_unlock (stream); /* unlock the stream */ mm_nocritical (stream); /* done with critical */ }}/* UNIX mail expunge mailbox * Accepts: MAIL stream */void unix_expunge (MAILSTREAM *stream){ unsigned long i; char lock[MAILTMPLEN]; char *msg = NIL; /* parse and lock mailbox */ if (LOCAL && (LOCAL->ld >= 0) && !stream->lock && unix_parse (stream,lock,LOCK_EX)) { /* count expunged messages if not dirty */ if (!LOCAL->dirty) for (i = 1; i <= stream->nmsgs; i++) if (mail_elt (stream,i)->deleted) LOCAL->dirty = T; if (!LOCAL->dirty) { /* not dirty and no expunged messages */ unix_unlock (LOCAL->fd,stream,lock); msg = "No messages deleted, so no update needed"; } else if (unix_rewrite (stream,&i,lock)) { if (i) sprintf (msg = LOCAL->buf,"Expunged %lu messages",i); else msg = "Mailbox checkpointed, but no messages expunged"; } /* rewrite failed */ else unix_unlock (LOCAL->fd,stream,lock); mail_unlock (stream); /* unlock the stream */ mm_nocritical (stream); /* done with critical */ if (msg && !stream->silent) mm_log (msg,NIL); } else if (!stream->silent) mm_log("Expunge ignored on readonly mailbox",WARN);}/* UNIX mail copy message(s) * Accepts: MAIL stream * sequence * destination mailbox * copy options * Returns: T if copy successful, else NIL */long unix_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options){ struct stat sbuf; int fd; char *s,file[MAILTMPLEN],lock[MAILTMPLEN]; struct utimbuf times; unsigned long i,j; MESSAGECACHE *elt; long ret = T; mailproxycopy_t pc = (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL); if (!((options & CP_UID) ? mail_uid_sequence (stream,sequence) : mail_sequence (stream,sequence))) return NIL; /* make sure valid mailbox */ if (!unix_valid (mailbox)) 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 UNIX-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 UNIX-format mailbox: %.80s",mailbox); mm_log (LOCAL->buf,ERROR); return NIL; } LOCAL->buf[0] = '\0'; mm_critical (stream); /* go critical */ if ((fd = unix_lock (dummy_file (file,mailbox), O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IREAD|S_IWRITE, lock,LOCK_EX)) < 0) { mm_nocritical (stream); /* done with critical */ sprintf (LOCAL->buf,"Can't open destination mailbox: %s",strerror (errno)); mm_log (LOCAL->buf,ERROR); /* log the error */ return NIL; /* failed */ } fstat (fd,&sbuf); /* get current file size */ /* write all requested messages to mailbox */ for (i = 1; ret && (i <= stream->nmsgs); i++) if ((elt = mail_elt (stream,i))->sequence) { lseek (LOCAL->fd,elt->private.special.offset,L_SET); read (LOCAL->fd,LOCAL->buf,elt->private.special.text.size);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -