diff options
author | zeertzjq <zeertzjq@outlook.com> | 2024-08-06 20:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-06 12:49:59 +0000 |
commit | 28fbba2092adb9659253434605cb94252241f5e0 (patch) | |
tree | e7665e98c3c87f3b57ab348d143da98196efdfcc /src | |
parent | b5f92c4e5c7428fe1c1f91620f4b545b30b49855 (diff) | |
download | rneovim-28fbba2092adb9659253434605cb94252241f5e0.tar.gz rneovim-28fbba2092adb9659253434605cb94252241f5e0.tar.bz2 rneovim-28fbba2092adb9659253434605cb94252241f5e0.zip |
vim-patch:9.1.0465: missing filecopy() function (#29989)
Problem: missing filecopy() function
Solution: implement filecopy() Vim script function
(Shougo Matsushita)
closes: vim/vim#12346
https://github.com/vim/vim/commit/60c8743ab6c90e402e6ed49d27455ef7e5698abe
Co-authored-by: Shougo Matsushita <Shougo.Matsu@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.lua | 17 | ||||
-rw-r--r-- | src/nvim/eval/fs.c | 21 | ||||
-rw-r--r-- | src/nvim/fileio.c | 48 |
3 files changed, 79 insertions, 7 deletions
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 4bb82e5b3e..430ee20081 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -2521,6 +2521,23 @@ M.funcs = { params = { { 'file', 'string' } }, signature = 'file_readable({file})', }, + filecopy = { + args = 2, + base = 1, + desc = [[ + Copy the file pointed to by the name {from} to {to}. The + result is a Number, which is |TRUE| if the file was copied + successfully, and |FALSE| when it failed. + If a file with name {to} already exists, it will fail. + Note that it does not handle directories (yet). + + This function is not available in the |sandbox|. + ]], + name = 'filecopy', + params = { { 'from', 'string' }, { 'to', 'string' } }, + returns = '0|1', + signature = 'filecopy({from}, {to})', + }, filereadable = { args = 1, base = 1, diff --git a/src/nvim/eval/fs.c b/src/nvim/eval/fs.c index 9719caa52e..381fee1a3f 100644 --- a/src/nvim/eval/fs.c +++ b/src/nvim/eval/fs.c @@ -154,6 +154,27 @@ void f_exepath(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_string = path; } +/// "filecopy()" function +void f_filecopy(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + rettv->vval.v_number = false; + + if (check_secure() + || tv_check_for_string_arg(argvars, 0) == FAIL + || tv_check_for_string_arg(argvars, 1) == FAIL) { + return; + } + + const char *from = tv_get_string(&argvars[0]); + + FileInfo from_info; + if (os_fileinfo_link(from, &from_info) + && (S_ISREG(from_info.stat.st_mode) || S_ISLNK(from_info.stat.st_mode))) { + rettv->vval.v_number + = vim_copyfile(tv_get_string(&argvars[0]), tv_get_string(&argvars[1])) == OK; + } +} + /// "filereadable()" function void f_filereadable(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 6fc1d0717a..cfce9c2954 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2654,7 +2654,6 @@ static int rename_with_tmp(const char *const from, const char *const to) int vim_rename(const char *from, const char *to) FUNC_ATTR_NONNULL_ALL { - char *errmsg = NULL; bool use_tmp_file = false; // When the names are identical, there is nothing to do. When they refer @@ -2698,13 +2697,49 @@ int vim_rename(const char *from, const char *to) } // Rename() failed, try copying the file. + int ret = vim_copyfile(from, to); + if (ret != OK) { + return -1; + } + + if (os_fileinfo(from, &from_info)) { + os_remove(from); + } + + return 0; +} + +/// Create the new file with same permissions as the original. +/// Return -1 for failure, 0 for success. +int vim_copyfile(const char *from, const char *to) +{ + char *errmsg = NULL; + +#ifdef HAVE_READLINK + FileInfo from_info; + if (os_fileinfo_link(from, &from_info) && S_ISLNK(from_info.stat.st_mode)) { + int ret = FAIL; + + char linkbuf[MAXPATHL + 1]; + ssize_t len = readlink(from, linkbuf, MAXPATHL); + if (len > 0) { + linkbuf[len] = NUL; + + // Create link + ret = symlink(linkbuf, to); + } + + return ret == 0 ? OK : FAIL; + } +#endif + int perm = os_getperm(from); // For systems that support ACL: get the ACL from the original file. vim_acl_T acl = os_get_acl(from); int fd_in = os_open(from, O_RDONLY, 0); if (fd_in < 0) { os_free_acl(acl); - return -1; + return FAIL; } // Create the new file with same permissions as the original. @@ -2712,7 +2747,7 @@ int vim_rename(const char *from, const char *to) if (fd_out < 0) { close(fd_in); os_free_acl(acl); - return -1; + return FAIL; } // Avoid xmalloc() here as vim_rename() is called by buf_write() when nvim @@ -2722,7 +2757,7 @@ int vim_rename(const char *from, const char *to) close(fd_out); close(fd_in); os_free_acl(acl); - return -1; + return FAIL; } int n; @@ -2749,10 +2784,9 @@ int vim_rename(const char *from, const char *to) os_free_acl(acl); if (errmsg != NULL) { semsg(errmsg, to); - return -1; + return FAIL; } - os_remove(from); - return 0; + return OK; } static bool already_warned = false; |