From 8d09301090156712ae1a99888866831cf1dc3bcf Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 7 Aug 2016 18:49:27 -0400 Subject: readfile(): Ensure `perm` for non-Unix. In the (!read_buffer && !read_stdin) case, always set `perm` for all platforms. This also means we no longer need to set `perm` in the case of (fd < 0) for non-Unix. --- src/nvim/fileio.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 3b598bd64b..a4f069be04 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -282,11 +282,9 @@ readfile ( int error = FALSE; /* errors encountered */ int ff_error = EOL_UNKNOWN; /* file format with errors */ long linerest = 0; /* remaining chars in line */ -#ifdef UNIX int perm = 0; +#ifdef UNIX int swap_mode = -1; /* protection bits for swap file */ -#else - int perm; #endif int fileformat = 0; /* end-of-line format */ int keep_fileformat = FALSE; @@ -418,23 +416,21 @@ readfile ( } } - if (!read_stdin && !read_buffer) { -#ifdef UNIX - /* - * On Unix it is possible to read a directory, so we have to - * check for it before os_open(). - */ + if (!read_buffer && !read_stdin) { perm = os_getperm(fname); - if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ +#ifdef UNIX + // On Unix it is possible to read a directory, so we have to + // check for it before os_open(). + if (perm >= 0 && !S_ISREG(perm) // not a regular file ... # ifdef S_ISFIFO - && !S_ISFIFO(perm) /* ... or fifo */ + && !S_ISFIFO(perm) // ... or fifo # endif # ifdef S_ISSOCK - && !S_ISSOCK(perm) /* ... or socket */ + && !S_ISSOCK(perm) // ... or socket # endif # ifdef OPEN_CHR_FILES && !(S_ISCHR(perm) && is_dev_fd_file(fname)) - /* ... or a character special file named /dev/fd/ */ + // ... or a character special file named /dev/fd/ # endif ) { if (S_ISDIR(perm)) @@ -503,13 +499,10 @@ readfile ( fd = os_open((char *)fname, O_RDONLY, 0); } - if (fd < 0) { /* cannot open at all */ + if (fd < 0) { // cannot open at all msg_scroll = msg_save; #ifndef UNIX - /* - * On non-unix systems we can't open a directory, check here. - */ - perm = os_getperm(fname); /* check if the file exists */ + // On non-unix systems we can't open a directory, check here. if (os_isdir(fname)) { filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); curbuf->b_p_ro = TRUE; /* must use "w!" now */ @@ -518,7 +511,7 @@ readfile ( if (!newfile) { return FAIL; } - if (perm == UV_ENOENT) { + if (perm == UV_ENOENT) { // check if the file exists /* * Set the 'new-file' flag, so that when the file has * been created by someone else, a ":w" will complain. -- cgit From 8625c1d1040037167d47da9dc6a8e79b348df681 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sun, 7 Aug 2016 19:24:50 -0400 Subject: win32: readfile(): Directories should not show "[Permission Denied]". 77135447e09903b45d1482da45869946212f7904 introduced: if (!newfile) { return FAIL; } which changed the semantics of the un-braced `else` in the `#ifndef UNIX` block immediately above it. This commit restores the semantics of Vim. Until now it mostly worked by accident, but on Windows it would mean that opening a directory would show "[Permission Denied]". --- src/nvim/fileio.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index a4f069be04..e0b6ae1215 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -505,17 +505,15 @@ readfile ( // On non-unix systems we can't open a directory, check here. if (os_isdir(fname)) { filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); - curbuf->b_p_ro = TRUE; /* must use "w!" now */ - } else + curbuf->b_p_ro = true; // must use "w!" now + } else { #endif if (!newfile) { return FAIL; } if (perm == UV_ENOENT) { // check if the file exists - /* - * Set the 'new-file' flag, so that when the file has - * been created by someone else, a ":w" will complain. - */ + // 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 @@ -566,6 +564,9 @@ readfile ( return FAIL; } +#ifndef UNIX + } +#endif /* * Only set the 'ro' flag for readonly files the first time they are -- cgit