To: vim_dev@googlegroups.com Subject: Patch 8.0.1305 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.0.1305 Problem: Writefile() never calls fsync(). Solution: Follow the 'fsync' option with override to enable or disable. Files: src/fileio.c, src/evalfunc.c, runtime/doc/eval.txt, src/globals.h, src/testdir/test_writefile.vim *** ../vim-8.0.1304/src/fileio.c 2017-11-16 17:03:41.956727768 +0100 --- src/fileio.c 2017-11-16 22:49:09.246257823 +0100 *************** *** 4771,4777 **** */ if (p_fs && fsync(fd) != 0 && !device) { ! errmsg = (char_u *)_("E667: Fsync failed"); end = 0; } #endif --- 4771,4777 ---- */ if (p_fs && fsync(fd) != 0 && !device) { ! errmsg = (char_u *)_(e_fsync); end = 0; } #endif *** ../vim-8.0.1304/src/evalfunc.c 2017-11-12 19:21:06.561379665 +0100 --- src/evalfunc.c 2017-11-16 23:01:10.771161654 +0100 *************** *** 13348,13353 **** --- 13348,13356 ---- { int binary = FALSE; int append = FALSE; + #ifdef HAVE_FSYNC + int do_fsync = p_fs; + #endif char_u *fname; FILE *fd; int ret = 0; *************** *** 13380,13385 **** --- 13383,13394 ---- binary = TRUE; if (vim_strchr(arg2, 'a') != NULL) append = TRUE; + #ifdef HAVE_FSYNC + if (vim_strchr(arg2, 's') != NULL) + do_fsync = TRUE; + else if (vim_strchr(arg2, 'S') != NULL) + do_fsync = FALSE; + #endif } fname = get_tv_string_chk(&argvars[1]); *************** *** 13398,13403 **** --- 13407,13416 ---- { if (write_list(fd, list, binary) == FAIL) ret = -1; + #ifdef HAVE_FSYNC + else if (do_fsync && fsync(fileno(fd)) != 0) + EMSG(_(e_fsync)); + #endif fclose(fd); } *** ../vim-8.0.1304/runtime/doc/eval.txt 2017-11-12 19:21:06.553379784 +0100 --- runtime/doc/eval.txt 2017-11-16 22:42:50.768073457 +0100 *************** *** 8736,8743 **** appended to the file: > :call writefile(["foo"], "event.log", "a") :call writefile(["bar"], "event.log", "a") ! > ! < All NL characters are replaced with a NUL character. Inserting CR characters needs to be done before passing {list} to writefile(). An existing file is overwritten, if possible. --- 8762,8778 ---- appended to the file: > :call writefile(["foo"], "event.log", "a") :call writefile(["bar"], "event.log", "a") ! < ! When {flags} contains "s" then fsync() is called after writing ! the file. This flushes the file to disk, if possible. This ! takes more time but avoids losing the file if the system ! crashes. ! When {flags} does not contain "S" or "s" then fsync is called ! if the 'fsync' option is set. ! When {flags} contains "S" then fsync() is not called, even ! when 'fsync' is set. ! ! All NL characters are replaced with a NUL character. Inserting CR characters needs to be done before passing {list} to writefile(). An existing file is overwritten, if possible. *** ../vim-8.0.1304/src/globals.h 2017-10-28 21:08:38.979457009 +0200 --- src/globals.h 2017-11-16 22:48:28.786879760 +0100 *************** *** 1449,1454 **** --- 1449,1457 ---- #ifdef FEAT_LIBCALL EXTERN char_u e_libcall[] INIT(= N_("E364: Library call failed for \"%s()\"")); #endif + #ifdef HAVE_FSYNC + EXTERN char_u e_fsync[] INIT(= N_("E667: Fsync failed")); + #endif #if defined(DYNAMIC_PERL) \ || defined(DYNAMIC_PYTHON) || defined(DYNAMIC_PYTHON3) \ || defined(DYNAMIC_RUBY) \ *** ../vim-8.0.1304/src/testdir/test_writefile.vim 2017-10-14 16:06:16.094677647 +0200 --- src/testdir/test_writefile.vim 2017-11-16 23:00:09.908097885 +0100 *************** *** 93,95 **** --- 93,102 ---- endif bwipe Xfile endfunc + + func Test_writefile_sync_arg() + " This doesn't check if fsync() works, only that the argument is accepted. + call writefile(['one'], 'Xtest', 's') + call writefile(['two'], 'Xtest', 'S') + call delete('Xtest') + endfunc *** ../vim-8.0.1304/src/version.c 2017-11-16 22:20:35.028615026 +0100 --- src/version.c 2017-11-16 22:36:57.233498366 +0100 *************** *** 768,769 **** --- 768,771 ---- { /* Add new patch number below this line */ + /**/ + 1305, /**/ -- I used to be indecisive, now I'm not sure. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///