From 3ce1b4015f8e830af72b679b6d39ab0bf4f69768 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 11 Aug 2015 16:34:45 -0400 Subject: fs.c: implement os_file_is_readable() Use access() because: - We already use it for os_file_is_writable() - Vim's old check_file_readonly() ends up using access() after all. --- src/nvim/eval.c | 23 ++++------------------- src/nvim/os/fs.c | 11 ++++++++++- 2 files changed, 14 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/nvim/eval.c b/src/nvim/eval.c index c7c67cfca4..bf9a219e28 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -8562,27 +8562,12 @@ static void f_feedkeys(typval_T *argvars, typval_T *rettv) } } -/* - * "filereadable()" function - */ +/// "filereadable()" function static void f_filereadable(typval_T *argvars, typval_T *rettv) { - int fd; - char_u *p; - int n; - -#ifndef O_NONBLOCK -# define O_NONBLOCK 0 -#endif - p = get_tv_string(&argvars[0]); - if (*p && !os_isdir(p) && (fd = os_open((char *)p, - O_RDONLY | O_NONBLOCK, 0)) >= 0) { - n = TRUE; - close(fd); - } else - n = FALSE; - - rettv->vval.v_number = n; + char_u *p = get_tv_string(&argvars[0]); + rettv->vval.v_number = + (*p && !os_isdir(p) && os_file_is_readable((char*)p)); } /* diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 5eeb275701..4cb1beb22f 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -279,13 +279,22 @@ bool os_file_is_readonly(const char *name) return access(name, W_OK) != 0; } +/// Check if a file is readable. +/// +/// @return true if `name` is readable, otherwise false. +bool os_file_is_readable(const char *name) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + return (access(name, R_OK) == 0); +} + /// Check if a file is writable. /// /// @return `0` if `name` is not writable, /// @return `1` if `name` is writable, /// @return `2` for a directory which we have rights to write into. int os_file_is_writable(const char *name) - FUNC_ATTR_NONNULL_ALL + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { if (access(name, W_OK) == 0) { if (os_isdir((char_u *)name)) { -- cgit From b1e18dc39325ef50121a28750463a068048d63cb Mon Sep 17 00:00:00 2001 From: Rui Abreu Ferreira Date: Wed, 9 Jul 2014 17:24:12 +0100 Subject: win_defs.h: define R_OK, W_OK for MSVC. --- src/nvim/os/win_defs.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/nvim/os/win_defs.h b/src/nvim/os/win_defs.h index 19d796bd08..a51898c9e7 100644 --- a/src/nvim/os/win_defs.h +++ b/src/nvim/os/win_defs.h @@ -21,4 +21,12 @@ // - SYS_VIMRC_FILE // - SPECIAL_WILDCHAR +// _access(): https://msdn.microsoft.com/en-us/library/1w06ktdy.aspx +#ifndef R_OK +# define R_OK 4 +#endif +#ifndef W_OK +# define W_OK 2 +#endif + #endif // NVIM_OS_WIN_DEFS_H -- cgit From 86c38ef1264f0896510abe7b6eb1f1915986b594 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Mon, 17 Aug 2015 00:44:58 -0400 Subject: os_file_is_*: libuv impl - uv_fs_access() is far more robust than access(). In particular, it handles utf16 paths (Windows). - Still need R_OK, W_OK in win_defs.h --- src/nvim/os/fs.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 4cb1beb22f..e4776999e5 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -285,7 +285,10 @@ bool os_file_is_readonly(const char *name) bool os_file_is_readable(const char *name) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { - return (access(name, R_OK) == 0); + uv_fs_t req; + int r = uv_fs_access(&fs_loop, &req, name, R_OK, NULL); + uv_fs_req_cleanup(&req); + return (r == 0); } /// Check if a file is writable. @@ -296,11 +299,11 @@ bool os_file_is_readable(const char *name) int os_file_is_writable(const char *name) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { - if (access(name, W_OK) == 0) { - if (os_isdir((char_u *)name)) { - return 2; - } - return 1; + uv_fs_t req; + int r = uv_fs_access(&fs_loop, &req, name, W_OK, NULL); + uv_fs_req_cleanup(&req); + if (r == 0) { + return os_isdir((char_u *)name) ? 2 : 1; } return 0; } -- cgit