⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unix.c

📁 Vovida 社区开源的 SIP 协议源码
💻 C
📖 第 1 页 / 共 5 页
字号:
  LOCAL->fd = LOCAL->ld = -1;	/* no file or state locking yet */  LOCAL->buf = (char *) fs_get ((LOCAL->buflen = CHUNK) + 1);  stream->sequence++;		/* bump sequence number */				/* make lock for read/write access */  if (!stream->rdonly) while (retry) {				/* try to lock file */    if ((fd = lockname (tmp,stream->mailbox,LOCK_EX|LOCK_NB,&i)) < 0) {      if (retry-- == KODRETRY) {/* no, first time through? */	if (i) {		/* learned the other guy's PID? */	  kill ((int) i,SIGUSR2);	  sprintf (tmp,"Trying to get mailbox lock from process %ld",i);	  mm_log (tmp,WARN);	}	else retry = 0;		/* give up */      }      if (!stream->silent) {	/* nothing if silent stream */	if (retry) sleep (1);	/* wait a second before trying again */	else mm_log ("Mailbox is open by another process, access is readonly",		     WARN);      }    }    else {			/* got the lock, nobody else can alter state */      LOCAL->ld = fd;		/* note lock's fd and name */      LOCAL->lname = cpystr (tmp);				/* make sure mode OK (don't use fchmod()) */      chmod (LOCAL->lname,(int) mail_parameters (NIL,GET_LOCKPROTECTION,NIL));      if (stream->silent) i = 0;/* silent streams won't accept KOD */      else {			/* note our PID in the lock */	sprintf (tmp,"%d",getpid ());	write (fd,tmp,(i = strlen (tmp))+1);      }      ftruncate (fd,i);		/* make sure tied off */      fsync (fd);		/* make sure it's available */      retry = 0;		/* no more need to try */    }  }				/* parse mailbox */  stream->nmsgs = stream->recent = 0;				/* will we be able to get write access? */  if ((LOCAL->ld >= 0) && access (stream->mailbox,W_OK) && (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,&lock,LOCK_SH)) {    unix_unlock (LOCAL->fd,stream,&lock);    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;    if (!stream->uid_nosticky) {/* users with lives get 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 ((char *) (lines->text.data =					 (unsigned char *) "Status"));    lines = lines->next = mail_newstringlist ();    lines->text.size = strlen ((char *) (lines->text.data =					 (unsigned char *) "X-Status"));    lines = lines->next = mail_newstringlist ();    lines->text.size = strlen ((char *) (lines->text.data =					 (unsigned char *) "X-Keywords"));    lines = lines->next = mail_newstringlist ();    lines->text.size = strlen ((char *) (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';				/* squeeze out CRs (in case from PC) */    if (s = strchr (LOCAL->buf,'\r')) {      char *t,*tl;      for (t = s,tl = LOCAL->buf + *length; t <= tl; t++)	if ((*t != '\r') || (t[1] != '\n')) *s++ = *t;				/* adjust length */      LOCAL->buf[*length = s - LOCAL->buf - 1] = '\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 = strcrlfcpy (&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';				/* squeeze out CRs (in case from PC) */    if (s = strchr (LOCAL->buf,'\r')) {      char *t,*tl;      for (t = s,tl = LOCAL->buf + *length; t <= tl; t++)	if ((*t != '\r') || (t[1] != '\n')) *s++ = *t;				/* adjust length */      *length = s - LOCAL->buf - 1;    }  }  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){  DOTLOCK lock;  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){  DOTLOCK lock;				/* 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;  DOTLOCK lock;  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];  DOTLOCK lock;  time_t tp[2];  unsigned long i,j;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -