move filewritable() into /src/os/fs.c and rename it

This commit is contained in:
Stefan Hoffmann
2014-03-16 16:06:55 +01:00
committed by Thiago de Arruda
parent 071d28076f
commit f762a9e195
7 changed files with 39 additions and 31 deletions

View File

@@ -8877,7 +8877,8 @@ static void f_filereadable(typval_T *argvars, typval_T *rettv)
*/ */
static void f_filewritable(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, static void findfilendir(typval_T *argvars, typval_T *rettv,

View File

@@ -1666,33 +1666,6 @@ void sort_strings(char_u **files, int count)
qsort((void *)files, (size_t)count, sizeof(char_u *), sort_compare); 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. * 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. * This is not in message.c to avoid a warning for prototypes.

View File

@@ -72,7 +72,6 @@ int vim_chdirfile(char_u *fname);
int illegal_slash(char *name); int illegal_slash(char *name);
int vim_chdir(char_u *new_dir); int vim_chdir(char_u *new_dir);
void sort_strings(char_u **files, int count); 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 emsg3(char_u *s, char_u *a1, char_u *a2);
int emsgn(char_u *s, long n); int emsgn(char_u *s, long n);
int get2c(FILE *fd); int get2c(FILE *fd);

View File

@@ -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;
}

View File

@@ -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); int os_get_uname(uid_t uid, char *s, size_t len);
char *os_get_user_directory(const char *name); char *os_get_user_directory(const char *name);
int os_file_is_readonly(const char *name); int os_file_is_readonly(const char *name);
int os_file_is_writable(const char *name);
#endif // NEOVIM_OS_OS_H #endif // NEOVIM_OS_OS_H

View File

@@ -8585,7 +8585,7 @@ static void init_spellfile(void)
else else
/* Copy the path from 'runtimepath' to buf[]. */ /* Copy the path from 'runtimepath' to buf[]. */
copy_option_part(&rtp, buf, MAXPATHL, ","); 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 /* Use the first language name from 'spelllang' and the
* encoding used in the first loaded .spl file. */ * encoding used in the first loaded .spl file. */
if (aspath) if (aspath)
@@ -8595,7 +8595,7 @@ static void init_spellfile(void)
/* Create the "spell" directory if it doesn't exist yet. */ /* Create the "spell" directory if it doesn't exist yet. */
l = (int)STRLEN(buf); l = (int)STRLEN(buf);
vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell"); vim_snprintf((char *)buf + l, MAXPATHL - l, "/spell");
if (filewritable(buf) != 2) if (os_file_is_writable((char *)buf) != 2)
vim_mkdir(buf, 0755); vim_mkdir(buf, 0755);
l = (int)STRLEN(buf); l = (int)STRLEN(buf);

View File

@@ -20,6 +20,7 @@ int32_t os_getperm(char_u *name);
int os_setperm(char_u *name, long perm); int os_setperm(char_u *name, long perm);
int os_file_exists(const char_u *name); int os_file_exists(const char_u *name);
int os_file_is_readonly(char *fname); int os_file_is_readonly(char *fname);
int os_file_is_writable(const char *name);
]] ]]
-- import constants parsed by ffi -- import constants parsed by ffi
@@ -286,6 +287,9 @@ describe 'fs function', ->
os_file_is_readonly = (filename) -> os_file_is_readonly = (filename) ->
fs.os_file_is_readonly (to_cstr 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) -> bit_set = (number, check_bit) ->
if 0 == (bit.band number, check_bit) then false else true 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', -> it 'returns FALSE if the file is writable', ->
eq FALSE, os_file_is_readonly 'unit-test-directory/test.file' 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', -> describe 'os_file_exists', ->
os_file_exists = (filename) -> os_file_exists = (filename) ->
fs.os_file_exists (to_cstr filename) fs.os_file_exists (to_cstr filename)