nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH 2/2] files: check for writability by the access bits


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH 2/2] files: check for writability by the access bits, not by trying to append
Date: Sun, 12 May 2019 10:57:14 +0200

Trying to append does not work on a fifo, and we'd like to be able to
open a fifo again.  Also, the append test causes a close event for the
given file at the moment of opening it, which makes using inotify for
waiting for this file to be closed useless.

Commit f8f90272 added the append test, but the original request
(https://lists.gnu.org/archive/html/info-nano/2009-05/msg00000.html
by Damien Joldersma) asked only for a warning when the user did not
have enough privileges to write to the file.

So, drop the append test and just check the access bits.

This fixes https://bugs.debian.org/583196
and fixes https://savannah.gnu.org/bugs/?29312.
---
 src/files.c | 37 +------------------------------------
 1 file changed, 1 insertion(+), 36 deletions(-)

diff --git a/src/files.c b/src/files.c
index 8646cd81..07403853 100644
--- a/src/files.c
+++ b/src/files.c
@@ -705,41 +705,6 @@ bool close_buffer(void)
 }
 #endif /* ENABLE_MULTIBUFFER */
 
-/* Do a quick permissions check by verifying whether the file is appendable.
- * Err on the side of permissiveness (reporting TRUE when it might be wrong)
- * to not fluster users editing on odd filesystems by printing incorrect
- * warnings. */
-int is_file_writable(const char *filename)
-{
-       char *full_filename;
-       struct stat fileinfo;
-       int fd;
-       FILE *f;
-       bool result = TRUE;
-
-       if (ISSET(VIEW_MODE))
-               return TRUE;
-
-       full_filename = get_full_path(filename);
-
-       /* If the absolute path is unusable, use the given relative one. */
-       if (full_filename == NULL || stat(full_filename, &fileinfo) == -1)
-               full_filename = mallocstrcpy(NULL, filename);
-
-       if ((fd = open(full_filename, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR |
-                               S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | 
S_IWOTH)) == -1)
-               result = FALSE;
-       else if ((f = fdopen(fd, "a")) == NULL) {
-               result = FALSE;
-               close(fd);
-       } else
-               fclose(f);
-
-       free(full_filename);
-
-       return result;
-}
-
 /* Encode any NUL bytes in the given line of text, which is of length buf_len,
  * and return a dynamically allocated copy of the resultant string. */
 char *encode_data(char *buf, size_t buf_len)
@@ -886,7 +851,7 @@ void read_file(FILE *f, int fd, const char *filename, bool 
undoable)
        fclose(f);
        if (fd > 0 && !undoable) {
                close(fd);
-               writable = is_file_writable(filename);
+               writable = (ISSET(VIEW_MODE) || access(filename, W_OK) == 0);
        }
 
        /* If the file ended with newline, or it was entirely empty, make the
-- 
2.20.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]