e5bffa4
To: vim-dev@vim.org
e5bffa4
Subject: patch 7.1.017
e5bffa4
Fcc: outbox
e5bffa4
From: Bram Moolenaar <Bram@moolenaar.net>
e5bffa4
Mime-Version: 1.0
e5bffa4
Content-Type: text/plain; charset=ISO-8859-1
e5bffa4
Content-Transfer-Encoding: 8bit
e5bffa4
------------
e5bffa4
e5bffa4
Patch 7.1.017
e5bffa4
Problem:    ":confirm w" does give a prompt when 'readonly' is set, but not
e5bffa4
	    when the file permissions are read-only.  (Michael Schaap)
e5bffa4
Solution:   Provide a dialog in both situations.  (Chris Lubinski)
e5bffa4
Files:	    src/ex_cmds.c, src/fileio.c, src/proto/fileio.pro
e5bffa4
e5bffa4
e5bffa4
*** ../vim-7.1.016/src/ex_cmds.c	Tue Jun 19 11:54:23 2007
e5bffa4
--- src/ex_cmds.c	Tue Jun 19 22:37:25 2007
e5bffa4
***************
e5bffa4
*** 2912,2933 ****
e5bffa4
  }
e5bffa4
  
e5bffa4
  /*
e5bffa4
!  * Check if a buffer is read-only.  Ask for overruling in a dialog.
e5bffa4
!  * Return TRUE and give an error message when the buffer is readonly.
e5bffa4
   */
e5bffa4
      static int
e5bffa4
  check_readonly(forceit, buf)
e5bffa4
      int		*forceit;
e5bffa4
      buf_T	*buf;
e5bffa4
  {
e5bffa4
!     if (!*forceit && buf->b_p_ro)
e5bffa4
      {
e5bffa4
  #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
e5bffa4
  	if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
e5bffa4
  	{
e5bffa4
  	    char_u	buff[IOSIZE];
e5bffa4
  
e5bffa4
! 	    dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
e5bffa4
  		    buf->b_fname);
e5bffa4
  
e5bffa4
  	    if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
e5bffa4
--- 2912,2946 ----
e5bffa4
  }
e5bffa4
  
e5bffa4
  /*
e5bffa4
!  * Check if a buffer is read-only (either 'readonly' option is set or file is
e5bffa4
!  * read-only). Ask for overruling in a dialog. Return TRUE and give an error
e5bffa4
!  * message when the buffer is readonly.
e5bffa4
   */
e5bffa4
      static int
e5bffa4
  check_readonly(forceit, buf)
e5bffa4
      int		*forceit;
e5bffa4
      buf_T	*buf;
e5bffa4
  {
e5bffa4
!     struct stat	st;
e5bffa4
! 
e5bffa4
!     /* Handle a file being readonly when the 'readonly' option is set or when
e5bffa4
!      * the file exists and permissions are read-only.
e5bffa4
!      * We will send 0777 to check_file_readonly(), as the "perm" variable is
e5bffa4
!      * important for device checks but not here. */
e5bffa4
!     if (!*forceit && (buf->b_p_ro
e5bffa4
! 		|| (mch_stat((char *)buf->b_ffname, &st) >= 0
e5bffa4
! 		    && check_file_readonly(buf->b_ffname, 0777))))
e5bffa4
      {
e5bffa4
  #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
e5bffa4
  	if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL)
e5bffa4
  	{
e5bffa4
  	    char_u	buff[IOSIZE];
e5bffa4
  
e5bffa4
! 	    if (buf->b_p_ro)
e5bffa4
! 		dialog_msg(buff, _("'readonly' option is set for \"%s\".\nDo you wish to write anyway?"),
e5bffa4
! 		    buf->b_fname);
e5bffa4
! 	    else
e5bffa4
! 		dialog_msg(buff, _("File permissions of \"%s\" are read-only.\nIt may still be possible to write it.\nDo you wish to try?"),
e5bffa4
  		    buf->b_fname);
e5bffa4
  
e5bffa4
  	    if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 2) == VIM_YES)
e5bffa4
***************
e5bffa4
*** 2941,2949 ****
e5bffa4
--- 2954,2967 ----
e5bffa4
  	}
e5bffa4
  	else
e5bffa4
  #endif
e5bffa4
+ 	if (buf->b_p_ro)
e5bffa4
  	    EMSG(_(e_readonly));
e5bffa4
+ 	else
e5bffa4
+ 	    EMSG2(_("E505: \"%s\" is read-only (add ! to override)"),
e5bffa4
+ 		    buf->b_fname);
e5bffa4
  	return TRUE;
e5bffa4
      }
e5bffa4
+ 
e5bffa4
      return FALSE;
e5bffa4
  }
e5bffa4
  
e5bffa4
*** ../vim-7.1.016/src/fileio.c	Thu May 10 19:32:17 2007
e5bffa4
--- src/fileio.c	Thu Jun 28 21:54:18 2007
e5bffa4
***************
e5bffa4
*** 424,430 ****
e5bffa4
  	 */
e5bffa4
  	if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
e5bffa4
  	{
e5bffa4
! 	    filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option"), 0);
e5bffa4
  	    msg_end();
e5bffa4
  	    msg_scroll = msg_save;
e5bffa4
  	    return FAIL;
e5bffa4
--- 424,430 ----
e5bffa4
  	 */
e5bffa4
  	if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
e5bffa4
  	{
e5bffa4
! 	    filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
e5bffa4
  	    msg_end();
e5bffa4
  	    msg_scroll = msg_save;
e5bffa4
  	    return FAIL;
e5bffa4
***************
e5bffa4
*** 2734,2739 ****
e5bffa4
--- 2734,2765 ----
e5bffa4
  #endif
e5bffa4
  
e5bffa4
  /*
e5bffa4
+  * Return TRUE if a file appears to be read-only from the file permissions.
e5bffa4
+  */
e5bffa4
+     int
e5bffa4
+ check_file_readonly(fname, perm)
e5bffa4
+     char_u	*fname;		/* full path to file */
e5bffa4
+     int		perm;		/* known permissions on file */
e5bffa4
+ {
e5bffa4
+ #ifndef USE_MCH_ACCESS
e5bffa4
+     int	    fd = 0;
e5bffa4
+ #endif
e5bffa4
+ 
e5bffa4
+     return (
e5bffa4
+ #ifdef USE_MCH_ACCESS
e5bffa4
+ # ifdef UNIX
e5bffa4
+ 	(perm & 0222) == 0 ||
e5bffa4
+ # endif
e5bffa4
+ 	mch_access((char *)fname, W_OK)
e5bffa4
+ #else
e5bffa4
+ 	(fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
e5bffa4
+ 					? TRUE : (close(fd), FALSE)
e5bffa4
+ #endif
e5bffa4
+ 	);
e5bffa4
+ }
e5bffa4
+ 
e5bffa4
+ 
e5bffa4
+ /*
e5bffa4
   * buf_write() - write to file "fname" lines "start" through "end"
e5bffa4
   *
e5bffa4
   * We do our own buffering here because fwrite() is so slow.
e5bffa4
***************
e5bffa4
*** 3219,3235 ****
e5bffa4
  	 * Check if the file is really writable (when renaming the file to
e5bffa4
  	 * make a backup we won't discover it later).
e5bffa4
  	 */
e5bffa4
! 	file_readonly = (
e5bffa4
! # ifdef USE_MCH_ACCESS
e5bffa4
! #  ifdef UNIX
e5bffa4
! 		    (perm & 0222) == 0 ||
e5bffa4
! #  endif
e5bffa4
! 		    mch_access((char *)fname, W_OK)
e5bffa4
! # else
e5bffa4
! 		    (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
e5bffa4
! 						   ? TRUE : (close(fd), FALSE)
e5bffa4
! # endif
e5bffa4
! 		    );
e5bffa4
  	if (!forceit && file_readonly)
e5bffa4
  	{
e5bffa4
  	    if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
e5bffa4
--- 3245,3252 ----
e5bffa4
  	 * Check if the file is really writable (when renaming the file to
e5bffa4
  	 * make a backup we won't discover it later).
e5bffa4
  	 */
e5bffa4
! 	file_readonly = check_file_readonly(fname, (int)perm);
e5bffa4
! 
e5bffa4
  	if (!forceit && file_readonly)
e5bffa4
  	{
e5bffa4
  	    if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
e5bffa4
*** ../vim-7.1.016/src/proto/fileio.pro	Sat May  5 19:59:00 2007
e5bffa4
--- src/proto/fileio.pro	Thu Jun 28 21:09:59 2007
e5bffa4
***************
e5bffa4
*** 2,7 ****
e5bffa4
--- 2,8 ----
e5bffa4
  void filemess __ARGS((buf_T *buf, char_u *name, char_u *s, int attr));
e5bffa4
  int readfile __ARGS((char_u *fname, char_u *sfname, linenr_T from, linenr_T lines_to_skip, linenr_T lines_to_read, exarg_T *eap, int flags));
e5bffa4
  int prep_exarg __ARGS((exarg_T *eap, buf_T *buf));
e5bffa4
+ int check_file_readonly __ARGS((char_u *fname, int perm));
e5bffa4
  int buf_write __ARGS((buf_T *buf, char_u *fname, char_u *sfname, linenr_T start, linenr_T end, exarg_T *eap, int append, int forceit, int reset_changed, int filtering));
e5bffa4
  void msg_add_fname __ARGS((buf_T *buf, char_u *fname));
e5bffa4
  void msg_add_lines __ARGS((int insert_space, long lnum, long nchars));
e5bffa4
*** ../vim-7.1.016/src/version.c	Thu Jun 28 21:23:52 2007
e5bffa4
--- src/version.c	Thu Jun 28 21:49:29 2007
e5bffa4
***************
e5bffa4
*** 668,669 ****
e5bffa4
--- 668,671 ----
e5bffa4
  {   /* Add new patch number below this line */
e5bffa4
+ /**/
e5bffa4
+     17,
e5bffa4
  /**/
e5bffa4
e5bffa4
-- 
e5bffa4
CUSTOMER:     Well, can you hang around a couple of minutes?  He won't be
e5bffa4
              long.
e5bffa4
MORTICIAN:    Naaah, I got to go on to Robinson's -- they've lost nine today.
e5bffa4
CUSTOMER:     Well, when is your next round?
e5bffa4
MORTICIAN:    Thursday.
e5bffa4
DEAD PERSON:  I think I'll go for a walk.
e5bffa4
                                  The Quest for the Holy Grail (Monty Python)
e5bffa4
e5bffa4
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
e5bffa4
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
e5bffa4
\\\        download, build and distribute -- http://www.A-A-P.org        ///
e5bffa4
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///