aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/eval.c3
-rw-r--r--src/misc2.c27
-rw-r--r--src/misc2.h1
-rw-r--r--src/os/fs.c13
-rw-r--r--src/os/os.h1
-rw-r--r--src/spell.c4
-rw-r--r--test/unit/os/fs.moon21
7 files changed, 39 insertions, 31 deletions
diff --git a/src/eval.c b/src/eval.c
index e29853a1b5..787ef7b30a 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -8877,7 +8877,8 @@ static void f_filereadable(typval_T *argvars, typval_T *rettv)
*/
static void f_filewritable(typval_T *argvars, typval_T *rettv)
{
- rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
+ char *filename = (char *)get_tv_string(&argvars[0]);
+ rettv->vval.v_number = os_file_is_writable(filename);
}
static void findfilendir(typval_T *argvars, typval_T *rettv,
diff --git a/src/misc2.c b/src/misc2.c
index cefcc568cc..180ebedcd8 100644
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -1667,33 +1667,6 @@ void sort_strings(char_u **files, int count)
}
/*
- * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
- * rights to write into.
- */
-int filewritable(char_u *fname)
-{
- int retval = 0;
-#if defined(UNIX) || defined(VMS)
- int perm = 0;
-#endif
-
-#if defined(UNIX) || defined(VMS)
- perm = os_getperm(fname);
-#endif
- if (
-# if defined(UNIX) || defined(VMS)
- (perm & 0222) &&
-# endif
- mch_access((char *)fname, W_OK) == 0
- ) {
- ++retval;
- if (os_isdir(fname))
- ++retval;
- }
- return retval;
-}
-
-/*
* Print an error message with one or two "%s" and one or two string arguments.
* This is not in message.c to avoid a warning for prototypes.
*/
diff --git a/src/misc2.h b/src/misc2.h
index 66971c4d4f..41a87ab372 100644
--- a/src/misc2.h
+++ b/src/misc2.h
@@ -72,7 +72,6 @@ int vim_chdirfile(char_u *fname);
int illegal_slash(char *name);
int vim_chdir(char_u *new_dir);
void sort_strings(char_u **files, int count);
-int filewritable(char_u *fname);
int emsg3(char_u *s, char_u *a1, char_u *a2);
int emsgn(char_u *s, long n);
int get2c(FILE *fd);
diff --git a/src/os/fs.c b/src/os/fs.c
index 71dee4dd3d..db366769b2 100644
--- a/src/os/fs.c
+++ b/src/os/fs.c
@@ -288,3 +288,16 @@ int os_file_is_readonly(const char *name)
}
}
+// return 0 for not writable, 1 for writable file, 2 for a dir which we have
+// rights to write into.
+int os_file_is_writable(const char *name)
+{
+ if (mch_access(name, W_OK) == 0) {
+ if (os_isdir((char_u *)name)) {
+ return 2;
+ }
+ return 1;
+ }
+ return 0;
+}
+
diff --git a/src/os/os.h b/src/os/os.h
index e4f5af5057..8999d1480b 100644
--- a/src/os/os.h
+++ b/src/os/os.h
@@ -69,5 +69,6 @@ int os_get_user_name(char *s, size_t len);
int os_get_uname(uid_t uid, char *s, size_t len);
char *os_get_user_directory(const char *name);
int os_file_is_readonly(const char *name);
+int os_file_is_writable(const char *name);
#endif // NEOVIM_OS_OS_H
diff --git a/src/spell.c b/src/spell.c
index 7a986d98d2..52a85171eb 100644
--- a/src/spell.c
+++ b/src/spell.c
@@ -8585,7 +8585,7 @@ static void init_spellfile(void)
else
/* Copy the path from 'runtimepath' to buf[]. */
copy_option_part(&rtp, buf, MAXPATHL, ",");
- if (filewritable(buf) == 2) {
+ if (os_file_is_writable((char *)buf) == 2) {
/* Use the first language name from 'spelllang' and the
* encoding used in the first loaded .spl file. */
if (aspath)
@@ -8595,7 +8595,7 @@ static void init_spellfile(void)
/* Create the "spell" directory if it doesn't exist yet. */
l = (int)STRLEN(buf);
vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
- if (filewritable(buf) != 2)
+ if (os_file_is_writable((char *)buf) != 2)
vim_mkdir(buf, 0755);
l = (int)STRLEN(buf);
diff --git a/test/unit/os/fs.moon b/test/unit/os/fs.moon
index 82414186d9..5c04bef2e5 100644
--- a/test/unit/os/fs.moon
+++ b/test/unit/os/fs.moon
@@ -20,6 +20,7 @@ int32_t os_getperm(char_u *name);
int os_setperm(char_u *name, long perm);
int os_file_exists(const char_u *name);
int os_file_is_readonly(char *fname);
+int os_file_is_writable(const char *name);
]]
-- import constants parsed by ffi
@@ -286,6 +287,9 @@ describe 'fs function', ->
os_file_is_readonly = (filename) ->
fs.os_file_is_readonly (to_cstr filename)
+ os_file_is_writable = (filename) ->
+ fs.os_file_is_writable (to_cstr filename)
+
bit_set = (number, check_bit) ->
if 0 == (bit.band number, check_bit) then false else true
@@ -340,6 +344,23 @@ describe 'fs function', ->
it 'returns FALSE if the file is writable', ->
eq FALSE, os_file_is_readonly 'unit-test-directory/test.file'
+ describe 'os_file_is_writable', ->
+ it 'returns FALSE if the file is readonly', ->
+ perm = os_getperm 'unit-test-directory/test.file'
+ perm_orig = perm
+ perm = unset_bit perm, ffi.C.kS_IWUSR
+ perm = unset_bit perm, ffi.C.kS_IWGRP
+ perm = unset_bit perm, ffi.C.kS_IWOTH
+ eq OK, (os_setperm 'unit-test-directory/test.file', perm)
+ eq FALSE, os_file_is_writable 'unit-test-directory/test.file'
+ eq OK, (os_setperm 'unit-test-directory/test.file', perm_orig)
+
+ it 'returns TRUE if the file is writable', ->
+ eq TRUE, os_file_is_writable 'unit-test-directory/test.file'
+
+ it 'returns 2 when given a folder with rights to write into', ->
+ eq 2, os_file_is_writable 'unit-test-directory'
+
describe 'os_file_exists', ->
os_file_exists = (filename) ->
fs.os_file_exists (to_cstr filename)