4c02e4c
To: vim_dev@googlegroups.com
4c02e4c
Subject: Patch 7.3.083
4c02e4c
Fcc: outbox
4c02e4c
From: Bram Moolenaar <Bram@moolenaar.net>
4c02e4c
Mime-Version: 1.0
4c02e4c
Content-Type: text/plain; charset=UTF-8
4c02e4c
Content-Transfer-Encoding: 8bit
4c02e4c
------------
4c02e4c
4c02e4c
Patch 7.3.083
4c02e4c
Problem:    When a read() or write() is interrupted by a signal it fails.
4c02e4c
Solution:   Add read_eintr() and write_eintr().
4c02e4c
Files:	    src/fileio.c, src/proto/fileio.pro, src/memfile.c, src/memline.c,
4c02e4c
	    src/os_unix.c, src/undo.c, src/vim.h
4c02e4c
4c02e4c
4c02e4c
*** ../vim-7.3.082/src/fileio.c	2010-08-15 21:57:26.000000000 +0200
4c02e4c
--- src/fileio.c	2010-12-17 16:04:30.000000000 +0100
4c02e4c
***************
4c02e4c
*** 918,924 ****
4c02e4c
  	    {
4c02e4c
  		/* Read the first line (and a bit more).  Immediately rewind to
4c02e4c
  		 * the start of the file.  If the read() fails "len" is -1. */
4c02e4c
! 		len = vim_read(fd, firstline, 80);
4c02e4c
  		lseek(fd, (off_t)0L, SEEK_SET);
4c02e4c
  		for (p = firstline; p < firstline + len; ++p)
4c02e4c
  		    if (*p >= 0x80)
4c02e4c
--- 918,924 ----
4c02e4c
  	    {
4c02e4c
  		/* Read the first line (and a bit more).  Immediately rewind to
4c02e4c
  		 * the start of the file.  If the read() fails "len" is -1. */
4c02e4c
! 		len = read_eintr(fd, firstline, 80);
4c02e4c
  		lseek(fd, (off_t)0L, SEEK_SET);
4c02e4c
  		for (p = firstline; p < firstline + len; ++p)
4c02e4c
  		    if (*p >= 0x80)
4c02e4c
***************
4c02e4c
*** 1373,1379 ****
4c02e4c
  		    /*
4c02e4c
  		     * Read bytes from the file.
4c02e4c
  		     */
4c02e4c
! 		    size = vim_read(fd, ptr, size);
4c02e4c
  		}
4c02e4c
  
4c02e4c
  		if (size <= 0)
4c02e4c
--- 1373,1379 ----
4c02e4c
  		    /*
4c02e4c
  		     * Read bytes from the file.
4c02e4c
  		     */
4c02e4c
! 		    size = read_eintr(fd, ptr, size);
4c02e4c
  		}
4c02e4c
  
4c02e4c
  		if (size <= 0)
4c02e4c
***************
4c02e4c
*** 4000,4006 ****
4c02e4c
  #ifdef HAS_BW_FLAGS
4c02e4c
  			write_info.bw_flags = FIO_NOCONVERT;
4c02e4c
  #endif
4c02e4c
! 			while ((write_info.bw_len = vim_read(fd, copybuf,
4c02e4c
  								BUFSIZE)) > 0)
4c02e4c
  			{
4c02e4c
  			    if (buf_write_bytes(&write_info) == FAIL)
4c02e4c
--- 4000,4006 ----
4c02e4c
  #ifdef HAS_BW_FLAGS
4c02e4c
  			write_info.bw_flags = FIO_NOCONVERT;
4c02e4c
  #endif
4c02e4c
! 			while ((write_info.bw_len = read_eintr(fd, copybuf,
4c02e4c
  								BUFSIZE)) > 0)
4c02e4c
  			{
4c02e4c
  			    if (buf_write_bytes(&write_info) == FAIL)
4c02e4c
***************
4c02e4c
*** 4813,4819 ****
4c02e4c
  #ifdef HAS_BW_FLAGS
4c02e4c
  			write_info.bw_flags = FIO_NOCONVERT;
4c02e4c
  #endif
4c02e4c
! 			while ((write_info.bw_len = vim_read(fd, smallbuf,
4c02e4c
  						      SMBUFSIZE)) > 0)
4c02e4c
  			    if (buf_write_bytes(&write_info) == FAIL)
4c02e4c
  				break;
4c02e4c
--- 4813,4819 ----
4c02e4c
  #ifdef HAS_BW_FLAGS
4c02e4c
  			write_info.bw_flags = FIO_NOCONVERT;
4c02e4c
  #endif
4c02e4c
! 			while ((write_info.bw_len = read_eintr(fd, smallbuf,
4c02e4c
  						      SMBUFSIZE)) > 0)
4c02e4c
  			    if (buf_write_bytes(&write_info) == FAIL)
4c02e4c
  				break;
4c02e4c
***************
4c02e4c
*** 5330,5336 ****
4c02e4c
  
4c02e4c
  /*
4c02e4c
   * Call write() to write a number of bytes to the file.
4c02e4c
!  * Also handles encryption and 'encoding' conversion.
4c02e4c
   *
4c02e4c
   * Return FAIL for failure, OK otherwise.
4c02e4c
   */
4c02e4c
--- 5330,5336 ----
4c02e4c
  
4c02e4c
  /*
4c02e4c
   * Call write() to write a number of bytes to the file.
4c02e4c
!  * Handles encryption and 'encoding' conversion.
4c02e4c
   *
4c02e4c
   * Return FAIL for failure, OK otherwise.
4c02e4c
   */
4c02e4c
***************
4c02e4c
*** 5702,5717 ****
4c02e4c
  	crypt_encode(buf, len, buf);
4c02e4c
  #endif
4c02e4c
  
4c02e4c
!     /* Repeat the write(), it may be interrupted by a signal. */
4c02e4c
!     while (len > 0)
4c02e4c
!     {
4c02e4c
! 	wlen = vim_write(ip->bw_fd, buf, len);
4c02e4c
! 	if (wlen <= 0)		    /* error! */
4c02e4c
! 	    return FAIL;
4c02e4c
! 	len -= wlen;
4c02e4c
! 	buf += wlen;
4c02e4c
!     }
4c02e4c
!     return OK;
4c02e4c
  }
4c02e4c
  
4c02e4c
  #ifdef FEAT_MBYTE
4c02e4c
--- 5702,5709 ----
4c02e4c
  	crypt_encode(buf, len, buf);
4c02e4c
  #endif
4c02e4c
  
4c02e4c
!     wlen = write_eintr(ip->bw_fd, buf, len);
4c02e4c
!     return (wlen < len) ? FAIL : OK;
4c02e4c
  }
4c02e4c
  
4c02e4c
  #ifdef FEAT_MBYTE
4c02e4c
***************
4c02e4c
*** 6662,6669 ****
4c02e4c
  	return -1;
4c02e4c
      }
4c02e4c
  
4c02e4c
!     while ((n = vim_read(fd_in, buffer, BUFSIZE)) > 0)
4c02e4c
! 	if (vim_write(fd_out, buffer, n) != n)
4c02e4c
  	{
4c02e4c
  	    errmsg = _("E208: Error writing to \"%s\"");
4c02e4c
  	    break;
4c02e4c
--- 6654,6661 ----
4c02e4c
  	return -1;
4c02e4c
      }
4c02e4c
  
4c02e4c
!     while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0)
4c02e4c
! 	if (write_eintr(fd_out, buffer, n) != n)
4c02e4c
  	{
4c02e4c
  	    errmsg = _("E208: Error writing to \"%s\"");
4c02e4c
  	    break;
4c02e4c
***************
4c02e4c
*** 10304,10306 ****
4c02e4c
--- 10296,10350 ----
4c02e4c
      }
4c02e4c
      return reg_pat;
4c02e4c
  }
4c02e4c
+ 
4c02e4c
+ #if defined(EINTR) || defined(PROTO)
4c02e4c
+ /*
4c02e4c
+  * Version of read() that retries when interrupted by EINTR (possibly
4c02e4c
+  * by a SIGWINCH).
4c02e4c
+  */
4c02e4c
+     long
4c02e4c
+ read_eintr(fd, buf, bufsize)
4c02e4c
+     int	    fd;
4c02e4c
+     void    *buf;
4c02e4c
+     size_t  bufsize;
4c02e4c
+ {
4c02e4c
+     long ret;
4c02e4c
+ 
4c02e4c
+     for (;;)
4c02e4c
+     {
4c02e4c
+ 	ret = vim_read(fd, buf, bufsize);
4c02e4c
+ 	if (ret >= 0 || errno != EINTR)
4c02e4c
+ 	    break;
4c02e4c
+     }
4c02e4c
+     return ret;
4c02e4c
+ }
4c02e4c
+ 
4c02e4c
+ /*
4c02e4c
+  * Version of write() that retries when interrupted by EINTR (possibly
4c02e4c
+  * by a SIGWINCH).
4c02e4c
+  */
4c02e4c
+     long
4c02e4c
+ write_eintr(fd, buf, bufsize)
4c02e4c
+     int	    fd;
4c02e4c
+     void    *buf;
4c02e4c
+     size_t  bufsize;
4c02e4c
+ {
4c02e4c
+     long    ret = 0;
4c02e4c
+     long    wlen;
4c02e4c
+ 
4c02e4c
+     /* Repeat the write() so long it didn't fail, other than being interrupted
4c02e4c
+      * by a signal. */
4c02e4c
+     while (ret < (long)bufsize)
4c02e4c
+     {
4c02e4c
+ 	wlen = vim_write(fd, buf + ret, bufsize - ret);
4c02e4c
+ 	if (wlen < 0)
4c02e4c
+ 	{
4c02e4c
+ 	    if (errno != EINTR)
4c02e4c
+ 		break;
4c02e4c
+ 	}
4c02e4c
+ 	else
4c02e4c
+ 	    ret += wlen;
4c02e4c
+     }
4c02e4c
+     return ret;
4c02e4c
+ }
4c02e4c
+ #endif
4c02e4c
*** ../vim-7.3.082/src/proto/fileio.pro	2010-08-15 21:57:28.000000000 +0200
4c02e4c
--- src/proto/fileio.pro	2010-12-17 15:01:26.000000000 +0100
4c02e4c
***************
4c02e4c
*** 54,57 ****
4c02e4c
--- 54,59 ----
4c02e4c
  int match_file_pat __ARGS((char_u *pattern, regprog_T *prog, char_u *fname, char_u *sfname, char_u *tail, int allow_dirs));
4c02e4c
  int match_file_list __ARGS((char_u *list, char_u *sfname, char_u *ffname));
4c02e4c
  char_u *file_pat_to_reg_pat __ARGS((char_u *pat, char_u *pat_end, char *allow_dirs, int no_bslash));
4c02e4c
+ long read_eintr __ARGS((int fd, void *buf, size_t bufsize));
4c02e4c
+ long write_eintr __ARGS((int fd, void *buf, size_t bufsize));
4c02e4c
  /* vim: set ft=c : */
4c02e4c
*** ../vim-7.3.082/src/memfile.c	2010-08-15 21:57:25.000000000 +0200
4c02e4c
--- src/memfile.c	2010-12-17 16:02:54.000000000 +0100
4c02e4c
***************
4c02e4c
*** 1049,1055 ****
4c02e4c
  	PERROR(_("E294: Seek error in swap file read"));
4c02e4c
  	return FAIL;
4c02e4c
      }
4c02e4c
!     if ((unsigned)vim_read(mfp->mf_fd, hp->bh_data, size) != size)
4c02e4c
      {
4c02e4c
  	PERROR(_("E295: Read error in swap file"));
4c02e4c
  	return FAIL;
4c02e4c
--- 1049,1055 ----
4c02e4c
  	PERROR(_("E294: Seek error in swap file read"));
4c02e4c
  	return FAIL;
4c02e4c
      }
4c02e4c
!     if ((unsigned)read_eintr(mfp->mf_fd, hp->bh_data, size) != size)
4c02e4c
      {
4c02e4c
  	PERROR(_("E295: Read error in swap file"));
4c02e4c
  	return FAIL;
4c02e4c
***************
4c02e4c
*** 1168,1174 ****
4c02e4c
      }
4c02e4c
  #endif
4c02e4c
  
4c02e4c
!     if ((unsigned)vim_write(mfp->mf_fd, data, size) != size)
4c02e4c
  	result = FAIL;
4c02e4c
  
4c02e4c
  #ifdef FEAT_CRYPT
4c02e4c
--- 1168,1174 ----
4c02e4c
      }
4c02e4c
  #endif
4c02e4c
  
4c02e4c
!     if ((unsigned)write_eintr(mfp->mf_fd, data, size) != size)
4c02e4c
  	result = FAIL;
4c02e4c
  
4c02e4c
  #ifdef FEAT_CRYPT
4c02e4c
*** ../vim-7.3.082/src/memline.c	2010-12-08 13:16:58.000000000 +0100
4c02e4c
--- src/memline.c	2010-12-17 15:46:49.000000000 +0100
4c02e4c
***************
4c02e4c
*** 2062,2068 ****
4c02e4c
      fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4c02e4c
      if (fd >= 0)
4c02e4c
      {
4c02e4c
! 	if (read(fd, (char *)&b0, sizeof(b0)) == sizeof(b0))
4c02e4c
  	{
4c02e4c
  	    if (STRNCMP(b0.b0_version, "VIM 3.0", 7) == 0)
4c02e4c
  	    {
4c02e4c
--- 2062,2068 ----
4c02e4c
      fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4c02e4c
      if (fd >= 0)
4c02e4c
      {
4c02e4c
! 	if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
4c02e4c
  	{
4c02e4c
  	    if (STRNCMP(b0.b0_version, "VIM 3.0", 7) == 0)
4c02e4c
  	    {
4c02e4c
***************
4c02e4c
*** 4392,4398 ****
4c02e4c
  		fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4c02e4c
  		if (fd >= 0)
4c02e4c
  		{
4c02e4c
! 		    if (read(fd, (char *)&b0, sizeof(b0)) == sizeof(b0))
4c02e4c
  		    {
4c02e4c
  			/*
4c02e4c
  			 * If the swapfile has the same directory as the
4c02e4c
--- 4392,4398 ----
4c02e4c
  		fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
4c02e4c
  		if (fd >= 0)
4c02e4c
  		{
4c02e4c
! 		    if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0))
4c02e4c
  		    {
4c02e4c
  			/*
4c02e4c
  			 * If the swapfile has the same directory as the
4c02e4c
*** ../vim-7.3.082/src/os_unix.c	2010-10-20 19:17:43.000000000 +0200
4c02e4c
--- src/os_unix.c	2010-12-17 16:17:43.000000000 +0100
4c02e4c
***************
4c02e4c
*** 4454,4460 ****
4c02e4c
  		    ++noread_cnt;
4c02e4c
  		    while (RealWaitForChar(fromshell_fd, 10L, NULL))
4c02e4c
  		    {
4c02e4c
! 			len = read(fromshell_fd, (char *)buffer
4c02e4c
  # ifdef FEAT_MBYTE
4c02e4c
  				+ buffer_off, (size_t)(BUFLEN - buffer_off)
4c02e4c
  # else
4c02e4c
--- 4454,4460 ----
4c02e4c
  		    ++noread_cnt;
4c02e4c
  		    while (RealWaitForChar(fromshell_fd, 10L, NULL))
4c02e4c
  		    {
4c02e4c
! 			len = read_eintr(fromshell_fd, buffer
4c02e4c
  # ifdef FEAT_MBYTE
4c02e4c
  				+ buffer_off, (size_t)(BUFLEN - buffer_off)
4c02e4c
  # else
4c02e4c
*** ../vim-7.3.082/src/undo.c	2010-11-03 19:32:36.000000000 +0100
4c02e4c
--- src/undo.c	2010-12-17 15:39:24.000000000 +0100
4c02e4c
***************
4c02e4c
*** 1386,1392 ****
4c02e4c
  		char_u	mbuf[UF_START_MAGIC_LEN];
4c02e4c
  		int	len;
4c02e4c
  
4c02e4c
! 		len = vim_read(fd, mbuf, UF_START_MAGIC_LEN);
4c02e4c
  		close(fd);
4c02e4c
  		if (len < UF_START_MAGIC_LEN
4c02e4c
  		      || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
4c02e4c
--- 1386,1392 ----
4c02e4c
  		char_u	mbuf[UF_START_MAGIC_LEN];
4c02e4c
  		int	len;
4c02e4c
  
4c02e4c
! 		len = read_eintr(fd, mbuf, UF_START_MAGIC_LEN);
4c02e4c
  		close(fd);
4c02e4c
  		if (len < UF_START_MAGIC_LEN
4c02e4c
  		      || memcmp(mbuf, UF_START_MAGIC, UF_START_MAGIC_LEN) != 0)
4c02e4c
*** ../vim-7.3.082/src/vim.h	2010-12-02 16:01:23.000000000 +0100
4c02e4c
--- src/vim.h	2010-12-17 14:55:04.000000000 +0100
4c02e4c
***************
4c02e4c
*** 1642,1647 ****
4c02e4c
--- 1642,1652 ----
4c02e4c
  # define USE_INPUT_BUF
4c02e4c
  #endif
4c02e4c
  
4c02e4c
+ #ifndef EINTR
4c02e4c
+ # define read_eintr(fd, buf, count) vim_read((fd), (buf), (count))
4c02e4c
+ # define write_eintr(fd, buf, count) vim_write((fd), (buf), (count))
4c02e4c
+ #endif
4c02e4c
+ 
4c02e4c
  #ifdef MSWIN
4c02e4c
  /* On MS-Windows the third argument isn't size_t.  This matters for Win64,
4c02e4c
   * where sizeof(size_t)==8, not 4 */
4c02e4c
*** ../vim-7.3.082/src/version.c	2010-12-17 12:19:14.000000000 +0100
4c02e4c
--- src/version.c	2010-12-17 16:10:58.000000000 +0100
4c02e4c
***************
4c02e4c
*** 716,717 ****
4c02e4c
--- 716,719 ----
4c02e4c
  {   /* Add new patch number below this line */
4c02e4c
+ /**/
4c02e4c
+     83,
4c02e4c
  /**/
4c02e4c
4c02e4c
-- 
4c02e4c
How To Keep A Healthy Level Of Insanity:
4c02e4c
9. As often as possible, skip rather than walk.
4c02e4c
4c02e4c
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
4c02e4c
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
4c02e4c
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
4c02e4c
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///