Bram,

In src/fileio.c's readfile function, when the file couldn't be opened,
stat(2) is used to determine why.  The problem being that certain error
conditions are checked in a block that is never reached, thus Vim
incorrectly shows a "[New File]" status message and leaves the buffer
writeable.

The attached patch specifically checks that errno is ENOENT for the
"[New File]"/"[New Directory]" case and moves the other checks inside
the "if (perm < 0)" block.  Now files which the user doesn't have
permission to edit or are too large to edit will be properly flagged as
such and the buffer marked readonly.

-- 
James
GPG Key: 1024D/61326D40 2003-09-02 James Vega <james...@debian.org>
diff --git a/src/fileio.c b/src/fileio.c
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -597,75 +597,83 @@
 	    {
 		if (perm < 0)
 		{
-		    /*
-		     * Set the 'new-file' flag, so that when the file has
-		     * been created by someone else, a ":w" will complain.
-		     */
-		    curbuf->b_flags |= BF_NEW;
-
-		    /* Create a swap file now, so that other Vims are warned
-		     * that we are editing this file.  Don't do this for a
-		     * "nofile" or "nowrite" buffer type. */
+		    if (errno == ENOENT)
+		    {
+			/*
+			 * Set the 'new-file' flag, so that when the file has
+			 * been created by someone else, a ":w" will complain.
+			 */
+			curbuf->b_flags |= BF_NEW;
+
+			/* Create a swap file now, so that other Vims are warned
+			 * that we are editing this file.  Don't do this for a
+			 * "nofile" or "nowrite" buffer type. */
 #ifdef FEAT_QUICKFIX
-		    if (!bt_dontwrite(curbuf))
-#endif
+			if (!bt_dontwrite(curbuf))
+#endif
+			{
+			    check_need_swap(newfile);
+#ifdef FEAT_AUTOCMD
+			    /* SwapExists autocommand may mess things up */
+			    if (curbuf != old_curbuf
+				|| (using_b_ffname
+				    && (old_b_ffname != curbuf->b_ffname))
+				|| (using_b_fname
+				    && (old_b_fname != curbuf->b_fname)))
+			    {
+				EMSG(_(e_auchangedbuf));
+				return FAIL;
+			    }
+#endif
+			}
+			if (dir_of_file_exists(fname))
+			    filemess(curbuf, sfname,
+				     (char_u *)_("[New File]"), 0);
+			else
+			    filemess(curbuf, sfname,
+				     (char_u *)_("[New DIRECTORY]"), 0);
+#ifdef FEAT_VIMINFO
+			/* Even though this is a new file, it might have been
+			 * edited before and deleted.  Get the old marks. */
+			check_marks_read();
+#endif
+#ifdef FEAT_MBYTE
+			if (eap != NULL && eap->force_enc != 0)
+			{
+			    /* set forced 'fileencoding' */
+			    fenc = enc_canonize(eap->cmd + eap->force_enc);
+			    if (fenc != NULL)
+				set_string_option_direct((char_u *)"fenc", -1,
+							 fenc, OPT_FREE|OPT_LOCAL, 0);
+			    vim_free(fenc);
+			}
+#endif
+#ifdef FEAT_AUTOCMD
+			apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
+					     FALSE, curbuf, eap);
+#endif
+			/* remember the current fileformat */
+			save_file_ff(curbuf);
+
+#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
+			if (aborting())   /* autocmds may abort script processing */
+			    return FAIL;
+#endif
+			return OK;	    /* a new file is not an error */
+		    }
+		    else
 		    {
-			check_need_swap(newfile);
-#ifdef FEAT_AUTOCMD
-			/* SwapExists autocommand may mess things up */
-			if (curbuf != old_curbuf
-				|| (using_b_ffname
-					&& (old_b_ffname != curbuf->b_ffname))
-				|| (using_b_fname
-					 && (old_b_fname != curbuf->b_fname)))
-			{
-			    EMSG(_(e_auchangedbuf));
-			    return FAIL;
-			}
-#endif
+			filemess(curbuf, sfname, (char_u *)(
+# ifdef EFBIG
+				(errno == EFBIG) ? _("[File too big]") :
+# endif
+# ifdef EOVERFLOW
+				(errno == EOVERFLOW) ? _("[File too big]") :
+# endif
+						    _("[Permission Denied]")),
+				 0);
+			curbuf->b_p_ro = TRUE;	/* must use "w!" now */
 		    }
-		    if (dir_of_file_exists(fname))
-			filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
-		    else
-			filemess(curbuf, sfname,
-					   (char_u *)_("[New DIRECTORY]"), 0);
-#ifdef FEAT_VIMINFO
-		    /* Even though this is a new file, it might have been
-		     * edited before and deleted.  Get the old marks. */
-		    check_marks_read();
-#endif
-#ifdef FEAT_MBYTE
-		    if (eap != NULL && eap->force_enc != 0)
-		    {
-			/* set forced 'fileencoding' */
-			fenc = enc_canonize(eap->cmd + eap->force_enc);
-			if (fenc != NULL)
-			    set_string_option_direct((char_u *)"fenc", -1,
-						 fenc, OPT_FREE|OPT_LOCAL, 0);
-			vim_free(fenc);
-		    }
-#endif
-#ifdef FEAT_AUTOCMD
-		    apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
-							  FALSE, curbuf, eap);
-#endif
-		    /* remember the current fileformat */
-		    save_file_ff(curbuf);
-
-#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
-		    if (aborting())   /* autocmds may abort script processing */
-			return FAIL;
-#endif
-		    return OK;	    /* a new file is not an error */
-		}
-		else
-		{
-		    filemess(curbuf, sfname, (char_u *)(
-# ifdef EFBIG
-			    (errno == EFBIG) ? _("[File too big]") :
-# endif
-						_("[Permission Denied]")), 0);
-		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
 		}
 	    }
 

Attachment: signature.asc
Description: Digital signature

Reply via email to