mirror of
https://github.com/neovim/neovim.git
synced 2025-09-13 23:08:16 +00:00
move filewritable() into /src/os/fs.c and rename it
This commit is contained in:

committed by
Thiago de Arruda

parent
071d28076f
commit
f762a9e195
@@ -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,
|
||||
|
27
src/misc2.c
27
src/misc2.c
@@ -1666,33 +1666,6 @@ void sort_strings(char_u **files, int count)
|
||||
qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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.
|
||||
|
@@ -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);
|
||||
|
13
src/os/fs.c
13
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;
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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);
|
||||
|
@@ -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)
|
||||
|
Reference in New Issue
Block a user