From 8410c72b18b0c2d8cfbf98e57e2ab8869dd6af01 Mon Sep 17 00:00:00 2001 From: Said Al Attrach Date: Sun, 10 Mar 2019 18:08:45 +0100 Subject: fs: add os_copy function that uses uv_fs_copyfile --- src/nvim/fileio.c | 24 +++--------------------- src/nvim/os/fs.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index f111376d84..2828e7bddc 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -3517,28 +3517,10 @@ restore_backup: MSG(_(e_interr)); ui_flush(); } - if ((fd = os_open((char *)backup, O_RDONLY, 0)) >= 0) { - if ((write_info.bw_fd = os_open((char *)fname, - O_WRONLY | O_CREAT | O_TRUNC, - perm & 0777)) >= 0) { - // copy the file. - write_info.bw_buf = smallbuf; -#ifdef HAS_BW_FLAGS - write_info.bw_flags = FIO_NOCONVERT; -#endif - while ((write_info.bw_len = read_eintr(fd, smallbuf, - SMBUFSIZE)) > 0) { - if (buf_write_bytes(&write_info) == FAIL) { - break; - } - } - if (close(write_info.bw_fd) >= 0 - && write_info.bw_len == 0) { - end = 1; // success - } - } - close(fd); // ignore errors for closing read file + // copy the file. + if (os_copy((char *)backup, (char *)fname, 0) == 0) { + end = 1; } } else { if (vim_rename(backup, fname) == 0) { diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 27db675c52..81d3b0a2e9 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -643,6 +643,20 @@ ptrdiff_t os_write(const int fd, const char *const buf, const size_t size, return (ptrdiff_t)written_bytes; } +/// Copies a file from path to new_path. Currently this passes +/// the arguments through to uv_fs_copyfile. +/// +/// @param path Path of file to be copied +/// @param path_new Path of new file +/// @param flags Bitwise OR of flags defined in +/// @return libuv error code on error +int os_copy(const char *path, const char *new_path, int flags) +{ + int r; + RUN_UV_FS_FUNC(r, uv_fs_copyfile, path, new_path, flags, NULL); + return r; +} + /// Flushes file modifications to disk. /// /// @param fd the file descriptor of the file to flush to disk. -- cgit From 7a6d85a778625ba83954a9e73e53e22ea9d53b4d Mon Sep 17 00:00:00 2001 From: Said Al Attrach Date: Sun, 10 Mar 2019 19:48:56 +0100 Subject: fs: replace another custom copy with os_copy --- src/nvim/fileio.c | 78 +++++++------------------------------------------------ 1 file changed, 10 insertions(+), 68 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 2828e7bddc..1e89baa8ce 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2762,7 +2762,6 @@ buf_write ( backup_ext = p_bex; if (backup_copy && (fd = os_open((char *)fname, O_RDONLY, 0)) >= 0) { - int bfd; char_u *copybuf, *wp; int some_error = FALSE; char_u *dirp; @@ -2855,81 +2854,24 @@ buf_write ( if (backup != NULL) { /* remove old backup, if present */ os_remove((char *)backup); - /* Open with O_EXCL to avoid the file being created while - * we were sleeping (symlink hacker attack?) */ - bfd = os_open((char *)backup, - O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, - perm & 0777); - if (bfd < 0) { - xfree(backup); - backup = NULL; - } else { - // set file protection same as original file, but - // strip s-bit. - (void)os_setperm((const char *)backup, perm & 0777); - -#ifdef UNIX - /* - * Try to set the group of the backup same as the - * original file. If this fails, set the protection - * bits for the group same as the protection bits for - * others. - */ - if (file_info_new.stat.st_gid != file_info_old.stat.st_gid - && os_fchown(bfd, -1, file_info_old.stat.st_gid) != 0) { - os_setperm((const char *)backup, - (perm & 0707) | ((perm & 07) << 3)); - } -# ifdef HAVE_SELINUX - mch_copy_sec(fname, backup); -# endif -#endif - /* - * copy the file. - */ - write_info.bw_fd = bfd; - write_info.bw_buf = copybuf; -#ifdef HAS_BW_FLAGS - write_info.bw_flags = FIO_NOCONVERT; -#endif - while ((write_info.bw_len = read_eintr(fd, copybuf, - BUFSIZE)) > 0) { - if (buf_write_bytes(&write_info) == FAIL) { - SET_ERRMSG(_( - "E506: Can't write to backup file (add ! to override)")); - break; - } - os_breakcheck(); - if (got_int) { - SET_ERRMSG(_(e_interr)); - break; - } - } + /* copy the file */ + if (os_copy((char *)fname, (char *)backup, 0) != 0) { + SET_ERRMSG(_("E506: Can't write to backup file (add ! to override)")); + } - int error; - if ((error = os_close(bfd)) != 0 && errmsg == NULL) { - SET_ERRMSG_ARG(_("E507: Close error for backup file " - "(add ! to override): %s"), - error); - } - if (write_info.bw_len < 0) { - SET_ERRMSG(_( - "E508: Can't read file for backup (add ! to override)")); - } #ifdef UNIX - set_file_time(backup, - file_info_old.stat.st_atim.tv_sec, - file_info_old.stat.st_mtim.tv_sec); + set_file_time(backup, + file_info_old.stat.st_atim.tv_sec, + file_info_old.stat.st_mtim.tv_sec); #endif #ifdef HAVE_ACL - mch_set_acl(backup, acl); + mch_set_acl(backup, acl); #endif #ifdef HAVE_SELINUX - mch_copy_sec(fname, backup); + mch_copy_sec(fname, backup); #endif - break; - } + break; } } nobackup: -- cgit From 6db1757b53d562c0bdae43028aca5ec955c39acc Mon Sep 17 00:00:00 2001 From: Said Al Attrach Date: Sun, 10 Mar 2019 20:50:56 +0100 Subject: fs: remove unecessary copybuf and os_open call --- src/nvim/fileio.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 1e89baa8ce..7a77e7df24 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2761,19 +2761,12 @@ buf_write ( else backup_ext = p_bex; - if (backup_copy && (fd = os_open((char *)fname, O_RDONLY, 0)) >= 0) { - char_u *copybuf, *wp; + if (backup_copy) { + char_u *wp; int some_error = FALSE; char_u *dirp; char_u *rootname; - copybuf = verbose_try_malloc(BUFSIZE + 1); - if (copybuf == NULL) { - // out of memory - some_error = TRUE; - goto nobackup; - } - /* * Try to make the backup in each directory in the 'bdir' option. * @@ -2791,8 +2784,8 @@ buf_write ( /* * Isolate one directory name, using an entry in 'bdir'. */ - (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); - rootname = get_file_in_dir(fname, copybuf); + (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); + rootname = get_file_in_dir(fname, IObuff); if (rootname == NULL) { some_error = TRUE; /* out of memory */ goto nobackup; @@ -2874,10 +2867,8 @@ buf_write ( break; } } -nobackup: - os_close(fd); // Ignore errors for closing read file. - xfree(copybuf); +nobackup: if (backup == NULL && errmsg == NULL) { SET_ERRMSG(_( "E509: Cannot create backup file (add ! to override)")); -- cgit From b4d894c06709b4ae807d540dfda21ab3220a989a Mon Sep 17 00:00:00 2001 From: Said Al Attrach Date: Mon, 11 Mar 2019 11:55:58 +0100 Subject: fs: add UV_FS_COPYFILE_FICLONE flag to os_copy --- src/nvim/fileio.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 7a77e7df24..5ef576e9bb 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2849,7 +2849,7 @@ buf_write ( os_remove((char *)backup); /* copy the file */ - if (os_copy((char *)fname, (char *)backup, 0) != 0) { + if (os_copy((char *)fname, (char *)backup, UV_FS_COPYFILE_FICLONE) != 0) { SET_ERRMSG(_("E506: Can't write to backup file (add ! to override)")); } @@ -3452,7 +3452,7 @@ restore_backup: } // copy the file. - if (os_copy((char *)backup, (char *)fname, 0) == 0) { + if (os_copy((char *)backup, (char *)fname, UV_FS_COPYFILE_FICLONE) == 0) { end = 1; } } else { -- cgit From f4c2de4c458f3f80771f717b3de0b50bd548e906 Mon Sep 17 00:00:00 2001 From: Said Al Attrach Date: Mon, 11 Mar 2019 12:31:14 +0100 Subject: style: make linter happy with fileio.c --- src/nvim/fileio.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 5ef576e9bb..08a4b76783 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2763,7 +2763,7 @@ buf_write ( if (backup_copy) { char_u *wp; - int some_error = FALSE; + int some_error = false; char_u *dirp; char_u *rootname; @@ -2848,9 +2848,11 @@ buf_write ( /* remove old backup, if present */ os_remove((char *)backup); - /* copy the file */ - if (os_copy((char *)fname, (char *)backup, UV_FS_COPYFILE_FICLONE) != 0) { - SET_ERRMSG(_("E506: Can't write to backup file (add ! to override)")); + // copy the file + if (os_copy((char *)fname, (char *)backup, UV_FS_COPYFILE_FICLONE) + != 0) { + SET_ERRMSG(_("E506: Can't write to backup file " + "(add ! to override)")); } #ifdef UNIX @@ -3452,7 +3454,8 @@ restore_backup: } // copy the file. - if (os_copy((char *)backup, (char *)fname, UV_FS_COPYFILE_FICLONE) == 0) { + if (os_copy((char *)backup, (char *)fname, UV_FS_COPYFILE_FICLONE) + == 0) { end = 1; } } else { -- cgit From 6feb9cb09d243eb811e301b39dbfbba5863617be Mon Sep 17 00:00:00 2001 From: Said Al Attrach Date: Sat, 30 Mar 2019 18:15:08 +0100 Subject: docs: explicitly state return value on success --- src/nvim/os/fs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 81d3b0a2e9..9b9b4581f4 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -649,7 +649,7 @@ ptrdiff_t os_write(const int fd, const char *const buf, const size_t size, /// @param path Path of file to be copied /// @param path_new Path of new file /// @param flags Bitwise OR of flags defined in -/// @return libuv error code on error +/// @return 0 on success, or libuv error code on failure. int os_copy(const char *path, const char *new_path, int flags) { int r; -- cgit From 646c3423dd580aebb677d9a3fe0ed26f74f97e31 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 20 May 2019 22:43:10 +0200 Subject: fileio: set group of backup file Restores code removed in #9709. uv_fs_copyfile() copies the perm bits but not the group name. https://github.com/libuv/libuv/pull/1547 ref #9709 ref #8288 --- src/nvim/fileio.c | 17 +++++++++++++++++ src/nvim/os/fs.c | 20 ++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 55463bdf30..15d7a885d5 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2868,6 +2868,23 @@ buf_write ( /* remove old backup, if present */ os_remove((char *)backup); + // set file protection same as original file, but + // strip s-bit. + (void)os_setperm((const char *)backup, perm & 0777); + +#ifdef UNIX + // + // Try to set the group of the backup same as the original file. If + // this fails, set the protection bits for the group same as the + // protection bits for others. + // + if (file_info_new.stat.st_gid != file_info_old.stat.st_gid + && os_chown((char *)backup, -1, file_info_old.stat.st_gid) != 0) { + os_setperm((const char *)backup, + (perm & 0707) | ((perm & 07) << 3)); + } +#endif + // copy the file if (os_copy((char *)fname, (char *)backup, UV_FS_COPYFILE_FICLONE) != 0) { diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 8c7dfcdee7..d1d088cf24 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -712,12 +712,24 @@ int os_setperm(const char *const name, int perm) return (r == kLibuvSuccess ? OK : FAIL); } -/// Changes the ownership of the file referred to by the open file descriptor. +/// Changes the owner and group of a file, like chown(2). /// -/// @return `0` on success, a libuv error code on failure. +/// @return 0 on success, or libuv error code on failure. +/// +/// @note If `owner` or `group` is -1, then that ID is not changed. +int os_chown(const char* path, uv_uid_t owner, uv_gid_t group) +{ + int r; + RUN_UV_FS_FUNC(r, uv_fs_chown, path, owner, group, NULL); + return r; +} + +/// Changes the owner and group of the file referred to by the open file +/// descriptor, like fchown(2). +/// +/// @return 0 on success, or libuv error code on failure. /// -/// @note If the `owner` or `group` is specified as `-1`, then that ID is not -/// changed. +/// @note If `owner` or `group` is -1, then that ID is not changed. int os_fchown(int fd, uv_uid_t owner, uv_gid_t group) { int r; -- cgit From 5b04a4fa09e0ee09678aec23b1d0233e7c25e3e6 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 20 May 2019 23:06:14 +0200 Subject: lua/shared: share trim() impl --- src/nvim/lua/vim.lua | 12 ++++-------- src/nvim/os/fs.c | 2 +- 2 files changed, 5 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index 9cd8e232d5..848bccaae6 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -154,14 +154,11 @@ local function _update_package_paths() last_nvim_paths = cur_nvim_paths end ---- Trim whitespace (Lua pattern "%%s") from both sides of a string. +--- Return a human-readable representation of the given object. --- ---@see https://www.lua.org/pil/20.2.html ---@param s String to trim ---@returns String with whitespace removed from its beginning and end -local function trim(s) - assert(type(s) == 'string', 'Only strings can be trimmed') - return s:match('^%s*(.*%S)') or '' +--@see https://github.com/kikito/inspect.lua +local function inspect(object, options) -- luacheck: no unused + error(object, options) -- Stub for gen_vimdoc.py end local function __index(t, key) @@ -180,7 +177,6 @@ local module = { _os_proc_children = _os_proc_children, _os_proc_info = _os_proc_info, _system = _system, - trim = trim, } setmetatable(module, { diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index d1d088cf24..d5500b230c 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -717,7 +717,7 @@ int os_setperm(const char *const name, int perm) /// @return 0 on success, or libuv error code on failure. /// /// @note If `owner` or `group` is -1, then that ID is not changed. -int os_chown(const char* path, uv_uid_t owner, uv_gid_t group) +int os_chown(const char *path, uv_uid_t owner, uv_gid_t group) { int r; RUN_UV_FS_FUNC(r, uv_fs_chown, path, owner, group, NULL); -- cgit