diff options
-rw-r--r-- | src/ex_cmds.c | 6 | ||||
-rw-r--r-- | src/ex_docmd.c | 2 | ||||
-rw-r--r-- | src/file_search.c | 4 | ||||
-rw-r--r-- | src/fileio.c | 6 | ||||
-rw-r--r-- | src/memline.c | 6 | ||||
-rw-r--r-- | src/misc1.c | 18 | ||||
-rw-r--r-- | src/misc1.h | 1 | ||||
-rw-r--r-- | src/os/fs.c | 17 | ||||
-rw-r--r-- | src/os/os.h | 1 | ||||
-rw-r--r-- | src/os_unix.c | 2 | ||||
-rw-r--r-- | src/quickfix.c | 10 | ||||
-rw-r--r-- | src/tag.c | 2 | ||||
-rw-r--r-- | src/undo.c | 2 | ||||
-rw-r--r-- | test/unit/os/fs.moon | 11 |
14 files changed, 51 insertions, 37 deletions
diff --git a/src/ex_cmds.c b/src/ex_cmds.c index c4da8d65e7..73a51fcf06 100644 --- a/src/ex_cmds.c +++ b/src/ex_cmds.c @@ -2310,7 +2310,7 @@ check_overwrite ( || (buf->b_flags & BF_READERR)) && !p_wa && !bt_nofile(buf) - && vim_fexists(ffname)) { + && os_file_exists(ffname)) { if (!eap->forceit && !eap->append) { #ifdef UNIX /* with UNIX it is possible to open a directory */ @@ -2336,7 +2336,6 @@ check_overwrite ( if (other && !emsg_silent) { char_u *dir; char_u *p; - int r; char_u *swapname; /* We only try the first entry in 'directory', without checking if @@ -2358,8 +2357,7 @@ check_overwrite ( } swapname = makeswapname(fname, ffname, curbuf, dir); vim_free(dir); - r = vim_fexists(swapname); - if (r) { + if (os_file_exists(swapname)) { if (p_confirm || cmdmod.confirm) { char_u buff[DIALOG_MSG_SIZE]; diff --git a/src/ex_docmd.c b/src/ex_docmd.c index 3e9c9c28ca..dc9680dd5b 100644 --- a/src/ex_docmd.c +++ b/src/ex_docmd.c @@ -7424,7 +7424,7 @@ open_exfile ( return NULL; } #endif - if (!forceit && *mode != 'a' && vim_fexists(fname)) { + if (!forceit && *mode != 'a' && os_file_exists(fname)) { EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname); return NULL; } diff --git a/src/file_search.c b/src/file_search.c index c80557f7ec..7411e19da0 100644 --- a/src/file_search.c +++ b/src/file_search.c @@ -837,7 +837,7 @@ char_u *vim_findfile(void *search_ctx_arg) for (;; ) { /* if file exists and we didn't already find it */ if ((path_with_url(file_path) - || (mch_getperm(file_path) >= 0 + || (os_file_exists(file_path) && (search_ctx->ffsc_find_what == FINDFILE_BOTH || ((search_ctx->ffsc_find_what @@ -1513,7 +1513,7 @@ find_file_in_path_option ( buf = suffixes; for (;; ) { if ( - (mch_getperm(NameBuff) >= 0 + (os_file_exists(NameBuff) && (find_what == FINDFILE_BOTH || ((find_what == FINDFILE_DIR) == mch_isdir(NameBuff))))) { diff --git a/src/fileio.c b/src/fileio.c index 83efddfd28..498f282555 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -3324,12 +3324,12 @@ nobackup: * delete an existing one, try to use another name. * 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); if (p < backup) /* empty file name ??? */ p = backup; *p = 'z'; - while (*p > 'a' && mch_getperm(backup) >= 0) + while (*p > 'a' && os_file_exists(backup)) --*p; /* They all exist??? Must be something wrong! */ if (*p == 'a') { @@ -5497,7 +5497,7 @@ buf_check_timestamp ( } } 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; mesg = _("W13: Warning: File \"%s\" has been created after editing started"); buf->b_flags |= BF_NEW_W; diff --git a/src/memline.c b/src/memline.c index e5700ce62c..3ec74b06ee 100644 --- a/src/memline.c +++ b/src/memline.c @@ -3636,7 +3636,7 @@ findswapname ( * check below for a 8.3 file name is used. */ 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"); #endif @@ -3753,7 +3753,7 @@ findswapname ( /* * 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 struct stat sb; @@ -3961,7 +3961,7 @@ findswapname ( } /* If the file was deleted this fname can be used. */ - if (mch_getperm(fname) < 0) + if (!os_file_exists(fname)) break; } else #endif diff --git a/src/misc1.c b/src/misc1.c index 91e5313c96..bb9b43dd0c 100644 --- a/src/misc1.c +++ b/src/misc1.c @@ -3828,18 +3828,6 @@ void preserve_exit(void) { } /* - * 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. * Should be used instead of ui_breakcheck() for functions that check for * each line in the file. Calling ui_breakcheck() each time takes too much @@ -4196,7 +4184,7 @@ unix_expandpath ( /* remove backslashes for the remaining components only */ if (*path_end != NUL) backslash_halve(buf + len + 1); - if (mch_getperm(buf) >= 0) { /* add existing file */ + if (os_file_exists(buf)) { /* add existing file */ #ifdef MACOS_CONVERT size_t precomp_len = STRLEN(buf)+1; char_u *precomp_buf = @@ -4782,7 +4770,7 @@ gen_expand_wildcards ( * "vim c:/" work. */ if (flags & EW_NOTFOUND) addfile(&ga, t, flags | EW_DIR | EW_FILE); - else if (mch_getperm(t) >= 0) + else if (os_file_exists(t)) addfile(&ga, t, flags); vim_free(t); } @@ -4884,7 +4872,7 @@ addfile ( int isdir; /* 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; #ifdef FNAME_ILLEGAL diff --git a/src/misc1.h b/src/misc1.h index df7eb4bca8..c70b9dd8de 100644 --- a/src/misc1.h +++ b/src/misc1.h @@ -81,7 +81,6 @@ void add_pathsep(char_u *p); char_u *FullName_save(char_u *fname, int force); void prepare_to_exit(void); void preserve_exit(void); -int vim_fexists(char_u *fname); void line_breakcheck(void); void fast_breakcheck(void); int expand_wildcards_eval(char_u **pat, int *num_file, char_u ***file, diff --git a/src/os/fs.c b/src/os/fs.c index 4c1b05b678..aa19b2f7f7 100644 --- a/src/os/fs.c +++ b/src/os/fs.c @@ -315,3 +315,20 @@ int mch_setperm(const char_u *name, int perm) 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; + } +} + diff --git a/src/os/os.h b/src/os/os.h index 5a8c8fc005..2bfcd8f162 100644 --- a/src/os/os.h +++ b/src/os/os.h @@ -19,5 +19,6 @@ int mch_get_uname(uid_t uid, char *s, size_t len); char *mch_get_user_directory(const char *name); long mch_getperm(const char_u *name); int mch_setperm(const char_u *name, int perm); +int os_file_exists(char_u *name); #endif diff --git a/src/os_unix.c b/src/os_unix.c index c7e083a2d5..bbb27b239b 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -3063,7 +3063,7 @@ int flags; /* EW_* flags */ */ for (j = 0, i = 0; i < *num_file; ++i) { /* 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; /* check if this entry should be included */ diff --git a/src/quickfix.c b/src/quickfix.c index f429b533a3..85436d66b3 100644 --- a/src/quickfix.c +++ b/src/quickfix.c @@ -615,7 +615,7 @@ restofline: *regmatch.endp[i] = c; if (vim_strchr((char_u *)"OPQ", idx) != NULL - && mch_getperm(namebuf) == -1) + && !os_file_exists(namebuf)) continue; } if ((i = (int)fmt_ptr->addr[1]) > 0) { /* %n */ @@ -750,7 +750,7 @@ restofline: } else if (vim_strchr((char_u *)"OPQ", idx) != NULL) { /* global file names */ valid = FALSE; - if (*namebuf == NUL || mch_getperm(namebuf) >= 0) { + if (*namebuf == NUL || os_file_exists(namebuf)) { if (*namebuf && idx == 'P') currfile = qf_push_dir(namebuf, &file_stack); 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 * directory change. */ - if (mch_getperm(ptr) < 0) { + if (!os_file_exists(ptr)) { vim_free(ptr); directory = qf_guess_filepath(fname); 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 * is that we delete the entire stack. */ - if ((fullname != NULL) && (mch_getperm(fullname) >= 0)) + if (fullname != NULL && os_file_exists(fullname)) break; ds_ptr = ds_ptr->next; @@ -2609,7 +2609,7 @@ static char_u *get_mef_name(void) { STRCPY(name, p_mef); sprintf((char *)name + (p - p_mef), "%d%d", start, off); STRCAT(name, p + 2); - if (mch_getperm(name) < 0 + if (!os_file_exists(name) #ifdef HAVE_LSTAT /* Don't accept a symbolic link, its a security risk. */ && 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 * autocommand event (e.g., http://sys/file). */ - if (mch_getperm(fname) < 0 + if (!os_file_exists(fname) && !has_autocmd(EVENT_BUFREADCMD, fname, NULL) ) { retval = NOTAGFILE; diff --git a/src/undo.c b/src/undo.c index d1c3cc92ed..67e7fe7a36 100644 --- a/src/undo.c +++ b/src/undo.c @@ -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 * file, and delete it. */ - if (mch_getperm(file_name) >= 0) { + if (os_file_exists(file_name)) { if (name == NULL || !forceit) { /* Check we can read it and it's an undo file. */ fd = mch_open((char *)file_name, O_RDONLY|O_EXTRA, 0); diff --git a/test/unit/os/fs.moon b/test/unit/os/fs.moon index c111c89049..30e6abb31c 100644 --- a/test/unit/os/fs.moon +++ b/test/unit/os/fs.moon @@ -18,6 +18,7 @@ int is_executable(char_u *name); int mch_can_exe(char_u *name); long mch_getperm(char_u *name); int mch_setperm(char_u *name, long perm); +int os_file_exists(const char_u *name); ]] -- import constants parsed by ffi @@ -320,3 +321,13 @@ describe 'fs function', -> it 'fails if given file does not exist', -> perm = ffi.C.kS_IXUSR 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') |