From 85e1a565606d04626966321f63db53005d4b162b Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 30 Jul 2016 18:14:04 +0300 Subject: os/fileio: Allow certain failures during file_fsync MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit According to the documentation fsync() may fail with EROFS or EINVAL if “file descriptor is bound to a special file which does not support synchronization” (e.g. /dev/stderr). This condition is completely valid in this case since main point of `file_fsync()` is dumping buffered input. --- src/nvim/os/fileio.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/os/fileio.c b/src/nvim/os/fileio.c index cf5bfd60ae..02b471ef7e 100644 --- a/src/nvim/os/fileio.c +++ b/src/nvim/os/fileio.c @@ -153,7 +153,11 @@ int file_fsync(FileDescriptor *const fp) fp->_error = 0; return error; } - return os_fsync(fp->fd); + const int error = os_fsync(fp->fd); + if (error != UV_EINVAL && error != UV_EROFS) { + return error; + } + return 0; } /// Buffer used for writing -- cgit From 222d98310a3b196cf21ca5885765b2ddca9195b6 Mon Sep 17 00:00:00 2001 From: ZyX Date: Sat, 30 Jul 2016 18:14:25 +0300 Subject: os/fileio: Support appending to a file --- src/nvim/os/fileio.c | 2 ++ src/nvim/os/fileio.h | 2 ++ 2 files changed, 4 insertions(+) (limited to 'src') diff --git a/src/nvim/os/fileio.c b/src/nvim/os/fileio.c index 02b471ef7e..775f2bd449 100644 --- a/src/nvim/os/fileio.c +++ b/src/nvim/os/fileio.c @@ -62,6 +62,8 @@ int file_open(FileDescriptor *const ret_fp, const char *const fname, FLAG(flags, kFileCreate, O_CREAT|O_WRONLY, kTrue, !(flags & kFileCreateOnly)); FLAG(flags, kFileTruncate, O_TRUNC|O_WRONLY, kTrue, !(flags & kFileCreateOnly)); + FLAG(flags, kFileAppend, O_APPEND|O_WRONLY, kTrue, + !(flags & kFileCreateOnly)); FLAG(flags, kFileReadOnly, O_RDONLY, kFalse, wr != kTrue); #ifdef O_NOFOLLOW FLAG(flags, kFileNoSymlink, O_NOFOLLOW, kNone, true); diff --git a/src/nvim/os/fileio.h b/src/nvim/os/fileio.h index 2cffd5c851..0b55cc695f 100644 --- a/src/nvim/os/fileio.h +++ b/src/nvim/os/fileio.h @@ -30,6 +30,8 @@ typedef enum { kFileTruncate = 32, ///< Truncate the file if it exists. ///< Implies kFileWriteOnly. Cannot be used with ///< kFileCreateOnly. + kFileAppend = 64, ///< Append to the file. Implies kFileWriteOnly. Cannot + ///< be used with kFileCreateOnly. } FileOpenFlags; static inline bool file_eof(const FileDescriptor *const fp) -- cgit From fe0eecf9cc9a9290cc67168779edc101e59d77eb Mon Sep 17 00:00:00 2001 From: ZyX Date: Sun, 31 Jul 2016 20:13:19 +0300 Subject: eval: writefile: Give more adequate IO errors and do not call putc() --- src/nvim/eval.c | 83 ++++++++++++++++++++++++++++++++++++++---------------- src/nvim/ex_cmds.c | 11 ++++---- src/nvim/globals.h | 1 - 3 files changed, 64 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index bbb6565509..b19916d155 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -30,6 +30,7 @@ #include "nvim/ex_eval.h" #include "nvim/ex_getln.h" #include "nvim/fileio.h" +#include "nvim/os/fileio.h" #include "nvim/func_attr.h" #include "nvim/fold.h" #include "nvim/getchar.h" @@ -18021,29 +18022,53 @@ static void f_winsaveview(typval_T *argvars, typval_T *rettv, FunPtr fptr) } /// Writes list of strings to file -static bool write_list(FILE *fd, list_T *list, bool binary) -{ - int ret = true; - - for (listitem_T *li = list->lv_first; li != NULL; li = li->li_next) { - for (char_u *s = get_tv_string(&li->li_tv); *s != NUL; ++s) { - if (putc(*s == '\n' ? NUL : *s, fd) == EOF) { - ret = false; - break; +/// +/// @param fp File to write to. +/// @param[in] list List to write. +/// @param[in] binary Whether to write in binary mode. +/// +/// @return true in case of success, false otherwise. +static bool write_list(FileDescriptor *const fp, const list_T *const list, + const bool binary) +{ + for (const listitem_T *li = list->lv_first; li != NULL; li = li->li_next) { + int error = 0; + const char *const s = (const char *)get_tv_string((typval_T *)&li->li_tv); + const char *hunk_start = s; + for (const char *p = hunk_start;; p++) { + if (*p == NUL || *p == NL) { + if (p != hunk_start) { + const ptrdiff_t written = file_write(fp, hunk_start, + (size_t)(p - hunk_start)); + if (written < 0) { + error = (int)written; + break; + } + } + if (*p == NUL) { + break; + } else { + hunk_start = p + 1; + const ptrdiff_t written = file_write(fp, (char []){ NUL }, 1); + if (written < 0) { + error = (int)written; + break; + } + } } } if (!binary || li->li_next != NULL) { - if (putc('\n', fd) == EOF) { - ret = false; - break; + const ptrdiff_t written = file_write(fp, "\n", 1); + if (written < 0) { + error = (int)written; } } - if (ret == false) { - EMSG(_(e_write)); - break; + if (error != 0) { + emsgf(_("E80: Error while writing: %s"), os_strerror(error)); + return false; } } - return ret; + return true; } /// Saves a typval_T as a string. @@ -18153,17 +18178,25 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr) // Always open the file in binary mode, library functions have a mind of // their own about CR-LF conversion. - char_u *fname = get_tv_string(&argvars[1]); - FILE *fd; - if (*fname == NUL || (fd = mch_fopen((char *)fname, - append ? APPENDBIN : WRITEBIN)) == NULL) { - EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("") : fname); - rettv->vval.v_number = -1; + const char *const fname = (const char *)get_tv_string(&argvars[1]); + FileDescriptor *fp; + int error; + rettv->vval.v_number = -1; + if (*fname == NUL) { + EMSG(_("E482: Can't open file with an empty name")); + } else if ((fp = file_open_new(&error, fname, + ((append ? kFileAppend : kFileTruncate) + | kFileCreate), 0666)) == NULL) { + emsgf(_("E482: Can't open file %s for writing: %s"), + fname, os_strerror(error)); } else { - if (write_list(fd, argvars[0].vval.v_list, binary) == false) { - rettv->vval.v_number = -1; + if (write_list(fp, argvars[0].vval.v_list, binary)) { + rettv->vval.v_number = 0; + } + if ((error = file_free(fp)) != 0) { + emsgf(_("E80: Error when closing file %s: %s"), + fname, os_strerror(error)); } - fclose(fd); } } /* diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 4b3798794c..2297d14cb6 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1131,11 +1131,12 @@ static void do_filter( */ ++no_wait_return; /* don't call wait_return() while busy */ if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, - FALSE, FALSE, FALSE, TRUE) == FAIL) { - msg_putchar('\n'); /* keep message from buf_write() */ - --no_wait_return; - if (!aborting()) - (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */ + false, false, false, true) == FAIL) { + msg_putchar('\n'); // Keep message from buf_write(). + no_wait_return--; + if (!aborting()) { + EMSG2(_("E482: Can't create file %s"), itmp); // Will call wait_return. + } goto filterend; } if (curbuf != old_curbuf) diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 20a00e1d9c..4e20be3fca 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -1145,7 +1145,6 @@ EXTERN char_u e_noprev[] INIT(= N_("E34: No previous command")); EXTERN char_u e_noprevre[] INIT(= N_("E35: No previous regular expression")); EXTERN char_u e_norange[] INIT(= N_("E481: No range allowed")); EXTERN char_u e_noroom[] INIT(= N_("E36: Not enough room")); -EXTERN char_u e_notcreate[] INIT(= N_("E482: Can't create file %s")); EXTERN char_u e_notmp[] INIT(= N_("E483: Can't get temp file name")); EXTERN char_u e_notopen[] INIT(= N_("E484: Can't open file %s")); EXTERN char_u e_notread[] INIT(= N_("E485: Can't read file %s")); -- cgit From 2e179214b84c9a4ba7661dd5e3971c3dc034f29f Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 14 Feb 2017 01:39:43 +0300 Subject: eval: Remove outdated comment --- src/nvim/eval.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index b19916d155..94eee0edf0 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -18176,8 +18176,6 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr) } } - // Always open the file in binary mode, library functions have a mind of - // their own about CR-LF conversion. const char *const fname = (const char *)get_tv_string(&argvars[1]); FileDescriptor *fp; int error; -- cgit From 066e6b8e9b221df4718accf4aef88142f51eae99 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 14 Feb 2017 19:40:16 +0300 Subject: eval: Return immediately after an error in write_list() Previously it could attempt to write trailing newline before returning. --- src/nvim/eval.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 94eee0edf0..941f4cd93a 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -18031,8 +18031,8 @@ static void f_winsaveview(typval_T *argvars, typval_T *rettv, FunPtr fptr) static bool write_list(FileDescriptor *const fp, const list_T *const list, const bool binary) { + int error = 0; for (const listitem_T *li = list->lv_first; li != NULL; li = li->li_next) { - int error = 0; const char *const s = (const char *)get_tv_string((typval_T *)&li->li_tv); const char *hunk_start = s; for (const char *p = hunk_start;; p++) { @@ -18042,7 +18042,7 @@ static bool write_list(FileDescriptor *const fp, const list_T *const list, (size_t)(p - hunk_start)); if (written < 0) { error = (int)written; - break; + goto write_list_error; } } if (*p == NUL) { @@ -18061,14 +18061,14 @@ static bool write_list(FileDescriptor *const fp, const list_T *const list, const ptrdiff_t written = file_write(fp, "\n", 1); if (written < 0) { error = (int)written; + goto write_list_error; } } - if (error != 0) { - emsgf(_("E80: Error while writing: %s"), os_strerror(error)); - return false; - } } return true; +write_list_error: + emsgf(_("E80: Error while writing: %s"), os_strerror(error)); + return false; } /// Saves a typval_T as a string. -- cgit From f489827b5f9b8b1ecd684768a92871389217aa91 Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 14 Feb 2017 20:46:12 +0300 Subject: eval: Fix error messages from writefile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. When calling writefile(list, fname, []) do not show error message twice. 2. Do not allow file name to be overwritten for writefile([1], 2). 3. Do not show “Can’t open file with an empty name” error after error like “using Float as a String” when type of the second argument is not correct. 4. Do not give multiple error messages and still continue for code like `writefile(["test", [], [], [], "tset"])`. Note that to fix 4. ideally I need tv_check_str_or_nr which is currently present in two PRs: #6114 and #5119. I would want to avoid copying this function into a yet another PR. Ref vim/vim#1476. --- src/nvim/eval.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 941f4cd93a..3c2a29bf8a 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -18033,7 +18033,11 @@ static bool write_list(FileDescriptor *const fp, const list_T *const list, { int error = 0; for (const listitem_T *li = list->lv_first; li != NULL; li = li->li_next) { - const char *const s = (const char *)get_tv_string((typval_T *)&li->li_tv); + const char *const s = (const char *)get_tv_string_chk( + (typval_T *)&li->li_tv); + if (s == NULL) { + return false; + } const char *hunk_start = s; for (const char *p = hunk_start;; p++) { if (*p == NUL || *p == NL) { @@ -18168,15 +18172,24 @@ static void f_writefile(typval_T *argvars, typval_T *rettv, FunPtr fptr) bool binary = false; bool append = false; if (argvars[2].v_type != VAR_UNKNOWN) { - if (vim_strchr(get_tv_string(&argvars[2]), 'b')) { + const char *const flags = (const char *)get_tv_string_chk(&argvars[2]); + if (flags == NULL) { + return; + } + if (strchr(flags, 'b')) { binary = true; } - if (vim_strchr(get_tv_string(&argvars[2]), 'a')) { + if (strchr(flags, 'a')) { append = true; } } - const char *const fname = (const char *)get_tv_string(&argvars[1]); + const char buf[NUMBUFLEN]; + const char *const fname = (const char *)get_tv_string_buf_chk(&argvars[1], + (char_u *)buf); + if (fname == NULL) { + return; + } FileDescriptor *fp; int error; rettv->vval.v_number = -1; -- cgit From 0086991b1bab08738da19efda45bad0ed3ce31e6 Mon Sep 17 00:00:00 2001 From: ZyX Date: Wed, 15 Feb 2017 00:28:34 +0300 Subject: eval: Fix linter error --- src/nvim/eval.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 3c2a29bf8a..d9da5346e3 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -18053,7 +18053,7 @@ static bool write_list(FileDescriptor *const fp, const list_T *const list, break; } else { hunk_start = p + 1; - const ptrdiff_t written = file_write(fp, (char []){ NUL }, 1); + const ptrdiff_t written = file_write(fp, (char[]){ NUL }, 1); if (written < 0) { error = (int)written; break; -- cgit From 31cdb227ba60e8867b3cb8e60ae8215290dd85a4 Mon Sep 17 00:00:00 2001 From: ZyX Date: Thu, 23 Feb 2017 01:49:05 +0300 Subject: eval: Flush buffer in write_list This way success/failure return from this function is more precise. --- src/nvim/eval.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index d9da5346e3..61ed815391 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -18069,6 +18069,9 @@ static bool write_list(FileDescriptor *const fp, const list_T *const list, } } } + if ((error = file_fsync(fp)) != 0) { + goto write_list_error; + } return true; write_list_error: emsgf(_("E80: Error while writing: %s"), os_strerror(error)); -- cgit