diff options
author | Daniel Xu <danobi@users.noreply.github.com> | 2016-07-05 22:40:25 -0700 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2016-07-06 01:40:25 -0400 |
commit | 5f1a153831d480180b2203120cff776d771ec1a4 (patch) | |
tree | 09fc7791734a1e3ed5a72c6e8195707db7e2ece4 | |
parent | bd6dad06dd1f4182cc65c41575af8bf1e3ea22a9 (diff) | |
download | rneovim-5f1a153831d480180b2203120cff776d771ec1a4.tar.gz rneovim-5f1a153831d480180b2203120cff776d771ec1a4.tar.bz2 rneovim-5f1a153831d480180b2203120cff776d771ec1a4.zip |
os/fs: Rename os_file_exists to os_path_exists (#4973)
Because the old name did not indicate that the function
would return true on directories as well.
-rw-r--r-- | src/nvim/event/socket.c | 2 | ||||
-rw-r--r-- | src/nvim/ex_cmds.c | 20 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 2 | ||||
-rw-r--r-- | src/nvim/file_search.c | 14 | ||||
-rw-r--r-- | src/nvim/fileio.c | 35 | ||||
-rw-r--r-- | src/nvim/if_cscope.c | 3 | ||||
-rw-r--r-- | src/nvim/memline.c | 10 | ||||
-rw-r--r-- | src/nvim/os/fs.c | 8 | ||||
-rw-r--r-- | src/nvim/os_unix.c | 7 | ||||
-rw-r--r-- | src/nvim/path.c | 9 | ||||
-rw-r--r-- | src/nvim/quickfix.c | 15 | ||||
-rw-r--r-- | src/nvim/spell.c | 4 | ||||
-rw-r--r-- | src/nvim/tag.c | 2 | ||||
-rw-r--r-- | src/nvim/undo.c | 4 | ||||
-rw-r--r-- | test/unit/os/fs_spec.lua | 29 | ||||
-rw-r--r-- | test/unit/tempfile_spec.lua | 2 |
16 files changed, 93 insertions, 73 deletions
diff --git a/src/nvim/event/socket.c b/src/nvim/event/socket.c index 93cc592683..cdaf40849b 100644 --- a/src/nvim/event/socket.c +++ b/src/nvim/event/socket.c @@ -103,7 +103,7 @@ int socket_watcher_start(SocketWatcher *watcher, int backlog, socket_cb cb) // Libuv converts ENOENT to EACCES for Windows compatibility, but if // the parent directory does not exist, ENOENT would be more accurate. *path_tail((char_u *)watcher->addr) = NUL; - if (!os_file_exists((char_u *)watcher->addr)) { + if (!os_path_exists((char_u *)watcher->addr)) { result = -ENOENT; } } diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index 86f1a16216..d0661b6b9b 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -1745,14 +1745,14 @@ check_overwrite ( * write to other file or b_flags set or not writing the whole file: * overwriting only allowed with '!' */ - if ( (other - || (buf->b_flags & BF_NOTEDITED) - || ((buf->b_flags & BF_NEW) - && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) - || (buf->b_flags & BF_READERR)) - && !p_wa - && !bt_nofile(buf) - && os_file_exists(ffname)) { + if ((other + || (buf->b_flags & BF_NOTEDITED) + || ((buf->b_flags & BF_NEW) + && vim_strchr(p_cpo, CPO_OVERNEW) == NULL) + || (buf->b_flags & BF_READERR)) + && !p_wa + && !bt_nofile(buf) + && os_path_exists(ffname)) { if (!eap->forceit && !eap->append) { #ifdef UNIX // It is possible to open a directory on Unix. @@ -1795,7 +1795,7 @@ check_overwrite ( } swapname = makeswapname(fname, ffname, curbuf, dir); xfree(dir); - if (os_file_exists(swapname)) { + if (os_path_exists(swapname)) { if (p_confirm || cmdmod.confirm) { char_u buff[DIALOG_MSG_SIZE]; @@ -1909,7 +1909,7 @@ static int check_readonly(int *forceit, buf_T *buf) /* Handle a file being readonly when the 'readonly' option is set or when * the file exists and permissions are read-only. */ if (!*forceit && (buf->b_p_ro - || (os_file_exists(buf->b_ffname) + || (os_path_exists(buf->b_ffname) && !os_file_is_writable((char *)buf->b_ffname)))) { if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) { char_u buff[DIALOG_MSG_SIZE]; diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index 6864e2b914..ad17d4dcbd 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -7650,7 +7650,7 @@ open_exfile ( return NULL; } #endif - if (!forceit && *mode != 'a' && os_file_exists(fname)) { + if (!forceit && *mode != 'a' && os_path_exists(fname)) { EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname); return NULL; } diff --git a/src/nvim/file_search.c b/src/nvim/file_search.c index f7555c99fa..78b224f04c 100644 --- a/src/nvim/file_search.c +++ b/src/nvim/file_search.c @@ -791,7 +791,7 @@ char_u *vim_findfile(void *search_ctx_arg) for (;; ) { /* if file exists and we didn't already find it */ if ((path_with_url((char *)file_path) - || (os_file_exists(file_path) + || (os_path_exists(file_path) && (search_ctx->ffsc_find_what == FINDFILE_BOTH || ((search_ctx->ffsc_find_what @@ -1442,12 +1442,12 @@ find_file_in_path_option ( buf = suffixes; for (;; ) { if ( - (os_file_exists(NameBuff) - && (find_what == FINDFILE_BOTH - || ((find_what == FINDFILE_DIR) - == os_isdir(NameBuff))))) { - file_name = vim_strsave(NameBuff); - goto theend; + (os_path_exists(NameBuff) + && (find_what == FINDFILE_BOTH + || ((find_what == FINDFILE_DIR) + == os_isdir(NameBuff))))) { + file_name = vim_strsave(NameBuff); + goto theend; } if (*buf == NUL) break; diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 4d9e10fb85..154558b332 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2990,14 +2990,15 @@ nobackup: * delete an existing one, try to use another name. * Change one character, just before the extension. */ - if (!p_bk && os_file_exists(backup)) { + if (!p_bk && os_path_exists(backup)) { p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); if (p < backup) /* empty file name ??? */ p = backup; *p = 'z'; - while (*p > 'a' && os_file_exists(backup)) - --*p; - /* They all exist??? Must be something wrong! */ + while (*p > 'a' && os_path_exists(backup)) { + (*p)--; + } + // They all exist??? Must be something wrong! if (*p == 'a') { xfree(backup); backup = NULL; @@ -3224,12 +3225,12 @@ restore_backup: * This may not work if the vim_rename() fails. * In that case we leave the copy around. */ - /* If file does not exist, put the copy in its place */ - if (!os_file_exists(fname)) { + // If file does not exist, put the copy in its place + if (!os_path_exists(fname)) { vim_rename(backup, fname); } - /* if original file does exist throw away the copy */ - if (os_file_exists(fname)) { + // if original file does exist throw away the copy + if (os_path_exists(fname)) { os_remove((char *)backup); } } else { @@ -3238,8 +3239,8 @@ restore_backup: } } - /* if original file no longer exists give an extra warning */ - if (!newfile && !os_file_exists(fname)) { + // if original file no longer exists give an extra warning + if (!newfile && !os_path_exists(fname)) { end = 0; } } @@ -3597,9 +3598,9 @@ restore_backup: * If the original file does not exist yet * the current backup file becomes the original file */ - if (org == NULL) + if (org == NULL) { EMSG(_("E205: Patchmode: can't save original file")); - else if (!os_file_exists((char_u *)org)) { + } else if (!os_path_exists((char_u *)org)) { vim_rename(backup, (char_u *)org); xfree(backup); /* don't delete the file */ backup = NULL; @@ -4514,9 +4515,11 @@ int vim_rename(char_u *from, char_u *to) if (STRLEN(from) >= MAXPATHL - 5) return -1; STRCPY(tempname, from); - for (n = 123; n < 99999; ++n) { - sprintf((char *)path_tail(tempname), "%d", n); - if (!os_file_exists(tempname)) { + for (n = 123; n < 99999; n++) { + char * tail = (char *)path_tail(tempname); + snprintf(tail, (MAXPATHL + 1) - (tail - (char *)tempname - 1), "%d", n); + + if (!os_path_exists(tempname)) { if (os_rename(from, tempname) == OK) { if (os_rename(tempname, to) == OK) return 0; @@ -4863,7 +4866,7 @@ buf_check_timestamp ( } } else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) - && os_file_exists(buf->b_ffname)) { + && os_path_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/nvim/if_cscope.c b/src/nvim/if_cscope.c index 2f9ec0b3ff..f2432dd71d 100644 --- a/src/nvim/if_cscope.c +++ b/src/nvim/if_cscope.c @@ -480,8 +480,9 @@ staterr: if (arg2 != NULL) { ppath = xmalloc(MAXPATHL + 1); expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL); - if (!os_file_exists((char_u *)ppath)) + if (!os_path_exists((char_u *)ppath)) { goto staterr; + } } int i; diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 9c20f15727..673205f08f 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -1359,7 +1359,7 @@ recover_names ( if (*dirp == NUL && file_count + num_files == 0 && fname != NULL) { char_u *swapname = (char_u *)modname((char *)fname_res, ".swp", TRUE); if (swapname != NULL) { - if (os_file_exists(swapname)) { + if (os_path_exists(swapname)) { files = xmalloc(sizeof(char_u *)); files[0] = swapname; swapname = NULL; @@ -3426,11 +3426,11 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname, break; } - /* If the file was deleted this fname can be used. */ - if (!os_file_exists((char_u *) fname)) + // If the file was deleted this fname can be used. + if (!os_path_exists((char_u *)fname)) { break; - } else - { + } + } else { MSG_PUTS("\n"); if (msg_silent == 0) /* call wait_return() later */ diff --git a/src/nvim/os/fs.c b/src/nvim/os/fs.c index 49c42cb63d..d12d34d595 100644 --- a/src/nvim/os/fs.c +++ b/src/nvim/os/fs.c @@ -599,14 +599,14 @@ int os_fchown(int fd, uv_uid_t owner, uv_gid_t group) return r; } -/// Check if a file exists. +/// Check if a path exists. /// -/// @return `true` if `name` exists. -bool os_file_exists(const char_u *name) +/// @return `true` if `path` exists +bool os_path_exists(const char_u *path) FUNC_ATTR_NONNULL_ALL { uv_stat_t statbuf; - return os_stat((char *)name, &statbuf) == kLibuvSuccess; + return os_stat((char *)path, &statbuf) == kLibuvSuccess; } /// Check if a file is readable. diff --git a/src/nvim/os_unix.c b/src/nvim/os_unix.c index 2ed0c2c856..def7e3b0e5 100644 --- a/src/nvim/os_unix.c +++ b/src/nvim/os_unix.c @@ -566,10 +566,11 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file, /* * Move the file names to allocated memory. */ - for (j = 0, i = 0; i < *num_file; ++i) { - /* Require the files to exist. Helps when using /bin/sh */ - if (!(flags & EW_NOTFOUND) && !os_file_exists((*file)[i])) + for (j = 0, i = 0; i < *num_file; i++) { + // Require the files to exist. Helps when using /bin/sh + if (!(flags & EW_NOTFOUND) && !os_path_exists((*file)[i])) { continue; + } /* check if this entry should be included */ dir = (os_isdir((*file)[i])); diff --git a/src/nvim/path.c b/src/nvim/path.c index 41fd69f238..57499429ec 100644 --- a/src/nvim/path.c +++ b/src/nvim/path.c @@ -684,7 +684,7 @@ static size_t do_path_expand(garray_T *gap, const char_u *path, } // add existing file or symbolic link if ((flags & EW_ALLLINKS) ? os_fileinfo_link((char *)buf, &file_info) - : os_file_exists(buf)) { + : os_path_exists(buf)) { addfile(gap, buf, flags); } } @@ -1205,10 +1205,11 @@ int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file, /* When EW_NOTFOUND is used, always add files and dirs. Makes * "vim c:/" work. */ - if (flags & EW_NOTFOUND) + if (flags & EW_NOTFOUND) { addfile(&ga, t, flags | EW_DIR | EW_FILE); - else if (os_file_exists(t)) + } else if (os_path_exists(t)) { addfile(&ga, t, flags); + } xfree(t); } @@ -1327,7 +1328,7 @@ void addfile( if (!(flags & EW_NOTFOUND) && ((flags & EW_ALLLINKS) ? !os_fileinfo_link((char *)f, &file_info) - : !os_file_exists(f))) { + : !os_path_exists(f))) { return; } diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 629e7858b3..dfd795b0ba 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -571,8 +571,9 @@ restofline: *regmatch.endp[i] = c; if (vim_strchr((char_u *)"OPQ", idx) != NULL - && !os_file_exists(namebuf)) + && !os_path_exists(namebuf)) { continue; + } } if ((i = (int)fmt_ptr->addr[1]) > 0) { /* %n */ if (regmatch.startp[i] == NULL) @@ -706,11 +707,12 @@ restofline: } else if (vim_strchr((char_u *)"OPQ", idx) != NULL) { // global file names valid = false; - if (*namebuf == NUL || os_file_exists(namebuf)) { - if (*namebuf && idx == 'P') + if (*namebuf == NUL || os_path_exists(namebuf)) { + if (*namebuf && idx == 'P') { currfile = qf_push_dir(namebuf, &file_stack); - else if (idx == 'Q') + } else if (idx == 'Q') { currfile = qf_pop_dir(&file_stack); + } *namebuf = NUL; if (tail && *tail) { STRMOVE(IObuff, skipwhite(tail)); @@ -1080,7 +1082,7 @@ static int qf_get_fnum(char_u *directory, char_u *fname) * "leaving directory"-messages we might have missed a * directory change. */ - if (!os_file_exists(ptr)) { + if (!os_path_exists(ptr)) { xfree(ptr); directory = qf_guess_filepath(fname); if (directory) @@ -1232,8 +1234,9 @@ static char_u *qf_guess_filepath(char_u *filename) xfree(fullname); fullname = (char_u *)concat_fnames((char *)ds_ptr->dirname, (char *)filename, TRUE); - if (os_file_exists(fullname)) + if (os_path_exists(fullname)) { break; + } ds_ptr = ds_ptr->next; } diff --git a/src/nvim/spell.c b/src/nvim/spell.c index d2401b6776..fba6fac821 100644 --- a/src/nvim/spell.c +++ b/src/nvim/spell.c @@ -7630,7 +7630,7 @@ mkspell ( else { // Check for overwriting before doing things that may take a lot of // time. - if (!over_write && os_file_exists(wfname)) { + if (!over_write && os_path_exists(wfname)) { EMSG(_(e_exists)); goto theend; } @@ -7686,7 +7686,7 @@ mkspell ( spin.si_region = 1 << i; vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[i]); - if (os_file_exists(fname)) { + if (os_path_exists(fname)) { // Read the .aff file. Will init "spin->si_conv" based on the // "SET" line. afile[i] = spell_read_aff(&spin, fname); diff --git a/src/nvim/tag.c b/src/nvim/tag.c index 7885d467d8..5d15196784 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -2353,7 +2353,7 @@ jumpto_tag ( * file. Also accept a file name for which there is a matching BufReadCmd * autocommand event (e.g., http://sys/file). */ - if (!os_file_exists(fname) + if (!os_path_exists(fname) && !has_autocmd(EVENT_BUFREADCMD, fname, NULL) ) { retval = NOTAGFILE; diff --git a/src/nvim/undo.c b/src/nvim/undo.c index f16b4264d7..fc5d6acaa4 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -712,7 +712,7 @@ char *u_get_undo_file_name(const char *const buf_ffname, const bool reading) // When reading check if the file exists. if (undo_file_name != NULL - && (!reading || os_file_exists((char_u *)undo_file_name))) { + && (!reading || os_path_exists((char_u *)undo_file_name))) { break; } xfree(undo_file_name); @@ -1094,7 +1094,7 @@ void u_write_undo(const char *const name, const bool forceit, buf_T *const buf, /* If the undo file already exists, verify that it actually is an undo * file, and delete it. */ - if (os_file_exists((char_u *)file_name)) { + if (os_path_exists((char_u *)file_name)) { if (name == NULL || !forceit) { /* Check we can read it and it's an undo file. */ fd = os_open(file_name, O_RDONLY, 0); diff --git a/test/unit/os/fs_spec.lua b/test/unit/os/fs_spec.lua index cc10b0cfa4..7e7eddb6fc 100644 --- a/test/unit/os/fs_spec.lua +++ b/test/unit/os/fs_spec.lua @@ -75,6 +75,8 @@ describe('fs function', function() io.open('unit-test-directory/test_2.file', 'w').close() lfs.link('test.file', 'unit-test-directory/test_link.file', true) + + lfs.link('non_existing_file.file', 'unit-test-directory/test_broken_link.file', true) -- Since the tests are executed, they are called by an executable. We use -- that executable for several asserts. absolute_executable = arg[0] @@ -88,6 +90,7 @@ describe('fs function', function() os.remove('unit-test-directory/test_2.file') os.remove('unit-test-directory/test_link.file') os.remove('unit-test-directory/test_hlink.file') + os.remove('unit-test-directory/test_broken_link.file') lfs.rmdir('unit-test-directory') end) @@ -363,8 +366,8 @@ describe('fs function', function() end) describe('file operations', function() - local function os_file_exists(filename) - return fs.os_file_exists((to_cstr(filename))) + local function os_path_exists(filename) + return fs.os_path_exists((to_cstr(filename))) end local function os_rename(path, new_path) return fs.os_rename((to_cstr(path)), (to_cstr(new_path))) @@ -421,13 +424,21 @@ describe('fs function', function() return fs.os_write(fd, data, data and #data or 0) end - describe('os_file_exists', function() + describe('os_path_exists', function() it('returns false when given a non-existing file', function() - eq(false, (os_file_exists('non-existing-file'))) + eq(false, (os_path_exists('non-existing-file'))) end) it('returns true when given an existing file', function() - eq(true, (os_file_exists('unit-test-directory/test.file'))) + eq(true, (os_path_exists('unit-test-directory/test.file'))) + end) + + it('returns false when given a broken symlink', function() + eq(false, (os_path_exists('unit-test-directory/test_broken_link.file'))) + end) + + it('returns true when given a directory', function() + eq(true, (os_path_exists('unit-test-directory'))) end) end) @@ -437,8 +448,8 @@ describe('fs function', function() it('can rename file if destination file does not exist', function() eq(OK, (os_rename(test, not_exist))) - eq(false, (os_file_exists(test))) - eq(true, (os_file_exists(not_exist))) + eq(false, (os_path_exists(test))) + eq(true, (os_path_exists(not_exist))) eq(OK, (os_rename(not_exist, test))) -- restore test file end) @@ -454,8 +465,8 @@ describe('fs function', function() file:close() eq(OK, (os_rename(other, test))) - eq(false, (os_file_exists(other))) - eq(true, (os_file_exists(test))) + eq(false, (os_path_exists(other))) + eq(true, (os_path_exists(test))) file = io.open(test, 'r') eq('other', (file:read('*all'))) file:close() diff --git a/test/unit/tempfile_spec.lua b/test/unit/tempfile_spec.lua index 7975d11aed..cf0d78b7a7 100644 --- a/test/unit/tempfile_spec.lua +++ b/test/unit/tempfile_spec.lua @@ -43,7 +43,7 @@ describe('tempfile related functions', function() it('generate name of non-existing file', function() local file = vim_tempname() assert.truthy(file) - assert.False(os.os_file_exists(file)) + assert.False(os.os_path_exists(file)) end) it('generate different names on each call', function() |