mirror of
https://github.com/neovim/neovim.git
synced 2025-09-14 23:38:17 +00:00
Revive vim_fname (-> os_file_exists); fix misuse of mch_getperm.
* Move vim_fname from misc1 to os/fs:os_file_exists. * Add unit tests for os_file_exists. * Replace misuse of mch_getperm with os_file_exists.
This commit is contained in:

committed by
Thiago de Arruda

parent
c83e8b4dc7
commit
1949acc806
@@ -2310,7 +2310,7 @@ check_overwrite (
|
|||||||
|| (buf->b_flags & BF_READERR))
|
|| (buf->b_flags & BF_READERR))
|
||||||
&& !p_wa
|
&& !p_wa
|
||||||
&& !bt_nofile(buf)
|
&& !bt_nofile(buf)
|
||||||
&& vim_fexists(ffname)) {
|
&& os_file_exists(ffname)) {
|
||||||
if (!eap->forceit && !eap->append) {
|
if (!eap->forceit && !eap->append) {
|
||||||
#ifdef UNIX
|
#ifdef UNIX
|
||||||
/* with UNIX it is possible to open a directory */
|
/* with UNIX it is possible to open a directory */
|
||||||
@@ -2336,7 +2336,6 @@ check_overwrite (
|
|||||||
if (other && !emsg_silent) {
|
if (other && !emsg_silent) {
|
||||||
char_u *dir;
|
char_u *dir;
|
||||||
char_u *p;
|
char_u *p;
|
||||||
int r;
|
|
||||||
char_u *swapname;
|
char_u *swapname;
|
||||||
|
|
||||||
/* We only try the first entry in 'directory', without checking if
|
/* We only try the first entry in 'directory', without checking if
|
||||||
@@ -2358,8 +2357,7 @@ check_overwrite (
|
|||||||
}
|
}
|
||||||
swapname = makeswapname(fname, ffname, curbuf, dir);
|
swapname = makeswapname(fname, ffname, curbuf, dir);
|
||||||
vim_free(dir);
|
vim_free(dir);
|
||||||
r = vim_fexists(swapname);
|
if (os_file_exists(swapname)) {
|
||||||
if (r) {
|
|
||||||
if (p_confirm || cmdmod.confirm) {
|
if (p_confirm || cmdmod.confirm) {
|
||||||
char_u buff[DIALOG_MSG_SIZE];
|
char_u buff[DIALOG_MSG_SIZE];
|
||||||
|
|
||||||
|
@@ -7424,7 +7424,7 @@ open_exfile (
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (!forceit && *mode != 'a' && vim_fexists(fname)) {
|
if (!forceit && *mode != 'a' && os_file_exists(fname)) {
|
||||||
EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
|
EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@@ -837,7 +837,7 @@ char_u *vim_findfile(void *search_ctx_arg)
|
|||||||
for (;; ) {
|
for (;; ) {
|
||||||
/* if file exists and we didn't already find it */
|
/* if file exists and we didn't already find it */
|
||||||
if ((path_with_url(file_path)
|
if ((path_with_url(file_path)
|
||||||
|| (mch_getperm(file_path) >= 0
|
|| (os_file_exists(file_path)
|
||||||
&& (search_ctx->ffsc_find_what
|
&& (search_ctx->ffsc_find_what
|
||||||
== FINDFILE_BOTH
|
== FINDFILE_BOTH
|
||||||
|| ((search_ctx->ffsc_find_what
|
|| ((search_ctx->ffsc_find_what
|
||||||
@@ -1513,7 +1513,7 @@ find_file_in_path_option (
|
|||||||
buf = suffixes;
|
buf = suffixes;
|
||||||
for (;; ) {
|
for (;; ) {
|
||||||
if (
|
if (
|
||||||
(mch_getperm(NameBuff) >= 0
|
(os_file_exists(NameBuff)
|
||||||
&& (find_what == FINDFILE_BOTH
|
&& (find_what == FINDFILE_BOTH
|
||||||
|| ((find_what == FINDFILE_DIR)
|
|| ((find_what == FINDFILE_DIR)
|
||||||
== mch_isdir(NameBuff))))) {
|
== mch_isdir(NameBuff))))) {
|
||||||
|
@@ -3324,12 +3324,12 @@ nobackup:
|
|||||||
* delete an existing one, try to use another name.
|
* delete an existing one, try to use another name.
|
||||||
* Change one character, just before the extension.
|
* Change one character, just before the extension.
|
||||||
*/
|
*/
|
||||||
if (!p_bk && mch_getperm(backup) >= 0) {
|
if (!p_bk && os_file_exists(backup)) {
|
||||||
p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
|
p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
|
||||||
if (p < backup) /* empty file name ??? */
|
if (p < backup) /* empty file name ??? */
|
||||||
p = backup;
|
p = backup;
|
||||||
*p = 'z';
|
*p = 'z';
|
||||||
while (*p > 'a' && mch_getperm(backup) >= 0)
|
while (*p > 'a' && os_file_exists(backup))
|
||||||
--*p;
|
--*p;
|
||||||
/* They all exist??? Must be something wrong! */
|
/* They all exist??? Must be something wrong! */
|
||||||
if (*p == 'a') {
|
if (*p == 'a') {
|
||||||
@@ -5497,7 +5497,7 @@ buf_check_timestamp (
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
|
} else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
|
||||||
&& vim_fexists(buf->b_ffname)) {
|
&& os_file_exists(buf->b_ffname)) {
|
||||||
retval = 1;
|
retval = 1;
|
||||||
mesg = _("W13: Warning: File \"%s\" has been created after editing started");
|
mesg = _("W13: Warning: File \"%s\" has been created after editing started");
|
||||||
buf->b_flags |= BF_NEW_W;
|
buf->b_flags |= BF_NEW_W;
|
||||||
|
@@ -3636,7 +3636,7 @@ findswapname (
|
|||||||
* check below for a 8.3 file name is used.
|
* check below for a 8.3 file name is used.
|
||||||
*/
|
*/
|
||||||
if (!(buf->b_p_sn || buf->b_shortname) && buf_fname != NULL
|
if (!(buf->b_p_sn || buf->b_shortname) && buf_fname != NULL
|
||||||
&& mch_getperm(buf_fname) < 0)
|
&& !os_file_exists(buf_fname))
|
||||||
dummyfd = mch_fopen((char *)buf_fname, "w");
|
dummyfd = mch_fopen((char *)buf_fname, "w");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -3753,7 +3753,7 @@ findswapname (
|
|||||||
/*
|
/*
|
||||||
* check if the swapfile already exists
|
* check if the swapfile already exists
|
||||||
*/
|
*/
|
||||||
if (mch_getperm(fname) < 0) { /* it does not exist */
|
if (!os_file_exists(fname)) { /* it does not exist */
|
||||||
#ifdef HAVE_LSTAT
|
#ifdef HAVE_LSTAT
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
|
|
||||||
@@ -3961,7 +3961,7 @@ findswapname (
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* If the file was deleted this fname can be used. */
|
/* If the file was deleted this fname can be used. */
|
||||||
if (mch_getperm(fname) < 0)
|
if (!os_file_exists(fname))
|
||||||
break;
|
break;
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
|
18
src/misc1.c
18
src/misc1.c
@@ -3827,18 +3827,6 @@ void preserve_exit(void) {
|
|||||||
getout(1);
|
getout(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* return TRUE if "fname" exists.
|
|
||||||
*/
|
|
||||||
int vim_fexists(char_u *fname)
|
|
||||||
{
|
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
if (mch_stat((char *)fname, &st))
|
|
||||||
return FALSE;
|
|
||||||
return TRUE;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Check for CTRL-C pressed, but only once in a while.
|
* Check for CTRL-C pressed, but only once in a while.
|
||||||
* Should be used instead of ui_breakcheck() for functions that check for
|
* Should be used instead of ui_breakcheck() for functions that check for
|
||||||
@@ -4196,7 +4184,7 @@ unix_expandpath (
|
|||||||
/* remove backslashes for the remaining components only */
|
/* remove backslashes for the remaining components only */
|
||||||
if (*path_end != NUL)
|
if (*path_end != NUL)
|
||||||
backslash_halve(buf + len + 1);
|
backslash_halve(buf + len + 1);
|
||||||
if (mch_getperm(buf) >= 0) { /* add existing file */
|
if (os_file_exists(buf)) { /* add existing file */
|
||||||
#ifdef MACOS_CONVERT
|
#ifdef MACOS_CONVERT
|
||||||
size_t precomp_len = STRLEN(buf)+1;
|
size_t precomp_len = STRLEN(buf)+1;
|
||||||
char_u *precomp_buf =
|
char_u *precomp_buf =
|
||||||
@@ -4782,7 +4770,7 @@ gen_expand_wildcards (
|
|||||||
* "vim c:/" work. */
|
* "vim c:/" work. */
|
||||||
if (flags & EW_NOTFOUND)
|
if (flags & EW_NOTFOUND)
|
||||||
addfile(&ga, t, flags | EW_DIR | EW_FILE);
|
addfile(&ga, t, flags | EW_DIR | EW_FILE);
|
||||||
else if (mch_getperm(t) >= 0)
|
else if (os_file_exists(t))
|
||||||
addfile(&ga, t, flags);
|
addfile(&ga, t, flags);
|
||||||
vim_free(t);
|
vim_free(t);
|
||||||
}
|
}
|
||||||
@@ -4884,7 +4872,7 @@ addfile (
|
|||||||
int isdir;
|
int isdir;
|
||||||
|
|
||||||
/* if the file/dir doesn't exist, may not add it */
|
/* if the file/dir doesn't exist, may not add it */
|
||||||
if (!(flags & EW_NOTFOUND) && mch_getperm(f) < 0)
|
if (!(flags & EW_NOTFOUND) && !os_file_exists(f))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#ifdef FNAME_ILLEGAL
|
#ifdef FNAME_ILLEGAL
|
||||||
|
@@ -81,7 +81,6 @@ void add_pathsep(char_u *p);
|
|||||||
char_u *FullName_save(char_u *fname, int force);
|
char_u *FullName_save(char_u *fname, int force);
|
||||||
void prepare_to_exit(void);
|
void prepare_to_exit(void);
|
||||||
void preserve_exit(void);
|
void preserve_exit(void);
|
||||||
int vim_fexists(char_u *fname);
|
|
||||||
void line_breakcheck(void);
|
void line_breakcheck(void);
|
||||||
void fast_breakcheck(void);
|
void fast_breakcheck(void);
|
||||||
int expand_wildcards_eval(char_u **pat, int *num_file, char_u ***file,
|
int expand_wildcards_eval(char_u **pat, int *num_file, char_u ***file,
|
||||||
|
17
src/os/fs.c
17
src/os/fs.c
@@ -315,3 +315,20 @@ int mch_setperm(const char_u *name, int perm)
|
|||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* return TRUE if "name" exists.
|
||||||
|
*/
|
||||||
|
int os_file_exists(char_u *name)
|
||||||
|
{
|
||||||
|
uv_fs_t request;
|
||||||
|
int result = uv_fs_stat(uv_default_loop(), &request, (const char*) name, NULL);
|
||||||
|
uv_fs_req_cleanup(&request);
|
||||||
|
|
||||||
|
if (result != 0) {
|
||||||
|
return FALSE;
|
||||||
|
} else {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -19,5 +19,6 @@ int mch_get_uname(uid_t uid, char *s, size_t len);
|
|||||||
char *mch_get_user_directory(const char *name);
|
char *mch_get_user_directory(const char *name);
|
||||||
long mch_getperm(const char_u *name);
|
long mch_getperm(const char_u *name);
|
||||||
int mch_setperm(const char_u *name, int perm);
|
int mch_setperm(const char_u *name, int perm);
|
||||||
|
int os_file_exists(char_u *name);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -3063,7 +3063,7 @@ int flags; /* EW_* flags */
|
|||||||
*/
|
*/
|
||||||
for (j = 0, i = 0; i < *num_file; ++i) {
|
for (j = 0, i = 0; i < *num_file; ++i) {
|
||||||
/* Require the files to exist. Helps when using /bin/sh */
|
/* Require the files to exist. Helps when using /bin/sh */
|
||||||
if (!(flags & EW_NOTFOUND) && mch_getperm((*file)[i]) < 0)
|
if (!(flags & EW_NOTFOUND) && !os_file_exists((*file)[i]))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* check if this entry should be included */
|
/* check if this entry should be included */
|
||||||
|
@@ -615,7 +615,7 @@ restofline:
|
|||||||
*regmatch.endp[i] = c;
|
*regmatch.endp[i] = c;
|
||||||
|
|
||||||
if (vim_strchr((char_u *)"OPQ", idx) != NULL
|
if (vim_strchr((char_u *)"OPQ", idx) != NULL
|
||||||
&& mch_getperm(namebuf) == -1)
|
&& !os_file_exists(namebuf))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ((i = (int)fmt_ptr->addr[1]) > 0) { /* %n */
|
if ((i = (int)fmt_ptr->addr[1]) > 0) { /* %n */
|
||||||
@@ -750,7 +750,7 @@ restofline:
|
|||||||
} else if (vim_strchr((char_u *)"OPQ", idx) != NULL) {
|
} else if (vim_strchr((char_u *)"OPQ", idx) != NULL) {
|
||||||
/* global file names */
|
/* global file names */
|
||||||
valid = FALSE;
|
valid = FALSE;
|
||||||
if (*namebuf == NUL || mch_getperm(namebuf) >= 0) {
|
if (*namebuf == NUL || os_file_exists(namebuf)) {
|
||||||
if (*namebuf && idx == 'P')
|
if (*namebuf && idx == 'P')
|
||||||
currfile = qf_push_dir(namebuf, &file_stack);
|
currfile = qf_push_dir(namebuf, &file_stack);
|
||||||
else if (idx == 'Q')
|
else if (idx == 'Q')
|
||||||
@@ -1131,7 +1131,7 @@ static int qf_get_fnum(char_u *directory, char_u *fname)
|
|||||||
* "leaving directory"-messages we might have missed a
|
* "leaving directory"-messages we might have missed a
|
||||||
* directory change.
|
* directory change.
|
||||||
*/
|
*/
|
||||||
if (mch_getperm(ptr) < 0) {
|
if (!os_file_exists(ptr)) {
|
||||||
vim_free(ptr);
|
vim_free(ptr);
|
||||||
directory = qf_guess_filepath(fname);
|
directory = qf_guess_filepath(fname);
|
||||||
if (directory)
|
if (directory)
|
||||||
@@ -1289,7 +1289,7 @@ static char_u *qf_guess_filepath(char_u *filename)
|
|||||||
/* If concat_fnames failed, just go on. The worst thing that can happen
|
/* If concat_fnames failed, just go on. The worst thing that can happen
|
||||||
* is that we delete the entire stack.
|
* is that we delete the entire stack.
|
||||||
*/
|
*/
|
||||||
if ((fullname != NULL) && (mch_getperm(fullname) >= 0))
|
if (fullname != NULL && os_file_exists(fullname))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
ds_ptr = ds_ptr->next;
|
ds_ptr = ds_ptr->next;
|
||||||
@@ -2609,7 +2609,7 @@ static char_u *get_mef_name(void) {
|
|||||||
STRCPY(name, p_mef);
|
STRCPY(name, p_mef);
|
||||||
sprintf((char *)name + (p - p_mef), "%d%d", start, off);
|
sprintf((char *)name + (p - p_mef), "%d%d", start, off);
|
||||||
STRCAT(name, p + 2);
|
STRCAT(name, p + 2);
|
||||||
if (mch_getperm(name) < 0
|
if (!os_file_exists(name)
|
||||||
#ifdef HAVE_LSTAT
|
#ifdef HAVE_LSTAT
|
||||||
/* Don't accept a symbolic link, its a security risk. */
|
/* Don't accept a symbolic link, its a security risk. */
|
||||||
&& mch_lstat((char *)name, &sb) < 0
|
&& mch_lstat((char *)name, &sb) < 0
|
||||||
|
@@ -2450,7 +2450,7 @@ jumpto_tag (
|
|||||||
* file. Also accept a file name for which there is a matching BufReadCmd
|
* file. Also accept a file name for which there is a matching BufReadCmd
|
||||||
* autocommand event (e.g., http://sys/file).
|
* autocommand event (e.g., http://sys/file).
|
||||||
*/
|
*/
|
||||||
if (mch_getperm(fname) < 0
|
if (!os_file_exists(fname)
|
||||||
&& !has_autocmd(EVENT_BUFREADCMD, fname, NULL)
|
&& !has_autocmd(EVENT_BUFREADCMD, fname, NULL)
|
||||||
) {
|
) {
|
||||||
retval = NOTAGFILE;
|
retval = NOTAGFILE;
|
||||||
|
@@ -1173,7 +1173,7 @@ void u_write_undo(char_u *name, int forceit, buf_T *buf, char_u *hash)
|
|||||||
|
|
||||||
/* If the undo file already exists, verify that it actually is an undo
|
/* If the undo file already exists, verify that it actually is an undo
|
||||||
* file, and delete it. */
|
* file, and delete it. */
|
||||||
if (mch_getperm(file_name) >= 0) {
|
if (os_file_exists(file_name)) {
|
||||||
if (name == NULL || !forceit) {
|
if (name == NULL || !forceit) {
|
||||||
/* Check we can read it and it's an undo file. */
|
/* Check we can read it and it's an undo file. */
|
||||||
fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
|
fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0);
|
||||||
|
@@ -18,6 +18,7 @@ int is_executable(char_u *name);
|
|||||||
int mch_can_exe(char_u *name);
|
int mch_can_exe(char_u *name);
|
||||||
long mch_getperm(char_u *name);
|
long mch_getperm(char_u *name);
|
||||||
int mch_setperm(char_u *name, long perm);
|
int mch_setperm(char_u *name, long perm);
|
||||||
|
int os_file_exists(const char_u *name);
|
||||||
]]
|
]]
|
||||||
|
|
||||||
-- import constants parsed by ffi
|
-- import constants parsed by ffi
|
||||||
@@ -320,3 +321,13 @@ describe 'fs function', ->
|
|||||||
it 'fails if given file does not exist', ->
|
it 'fails if given file does not exist', ->
|
||||||
perm = ffi.C.kS_IXUSR
|
perm = ffi.C.kS_IXUSR
|
||||||
eq FAIL, (mch_setperm 'non-existing-file', perm)
|
eq FAIL, (mch_setperm 'non-existing-file', perm)
|
||||||
|
|
||||||
|
describe 'os_file_exists', ->
|
||||||
|
os_file_exists = (filename) ->
|
||||||
|
fs.os_file_exists (to_cstr filename)
|
||||||
|
|
||||||
|
it 'returns FALSE when given a non-existing file', ->
|
||||||
|
eq FALSE, (os_file_exists 'non-existing-file')
|
||||||
|
|
||||||
|
it 'returns TRUE when given an existing file', ->
|
||||||
|
eq TRUE, (os_file_exists 'unit-test-directory/test.file')
|
||||||
|
Reference in New Issue
Block a user