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.
This commit is contained in:
Daniel Xu
2016-07-05 22:40:25 -07:00
committed by Justin M. Keyes
parent bd6dad06dd
commit 5f1a153831
16 changed files with 93 additions and 73 deletions

View File

@@ -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 // Libuv converts ENOENT to EACCES for Windows compatibility, but if
// the parent directory does not exist, ENOENT would be more accurate. // the parent directory does not exist, ENOENT would be more accurate.
*path_tail((char_u *)watcher->addr) = NUL; *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; result = -ENOENT;
} }
} }

View File

@@ -1752,7 +1752,7 @@ check_overwrite (
|| (buf->b_flags & BF_READERR)) || (buf->b_flags & BF_READERR))
&& !p_wa && !p_wa
&& !bt_nofile(buf) && !bt_nofile(buf)
&& os_file_exists(ffname)) { && os_path_exists(ffname)) {
if (!eap->forceit && !eap->append) { if (!eap->forceit && !eap->append) {
#ifdef UNIX #ifdef UNIX
// It is possible to open a directory on Unix. // It is possible to open a directory on Unix.
@@ -1795,7 +1795,7 @@ check_overwrite (
} }
swapname = makeswapname(fname, ffname, curbuf, dir); swapname = makeswapname(fname, ffname, curbuf, dir);
xfree(dir); xfree(dir);
if (os_file_exists(swapname)) { if (os_path_exists(swapname)) {
if (p_confirm || cmdmod.confirm) { if (p_confirm || cmdmod.confirm) {
char_u buff[DIALOG_MSG_SIZE]; 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 /* Handle a file being readonly when the 'readonly' option is set or when
* the file exists and permissions are read-only. */ * the file exists and permissions are read-only. */
if (!*forceit && (buf->b_p_ro 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)))) { && !os_file_is_writable((char *)buf->b_ffname)))) {
if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) { if ((p_confirm || cmdmod.confirm) && buf->b_fname != NULL) {
char_u buff[DIALOG_MSG_SIZE]; char_u buff[DIALOG_MSG_SIZE];

View File

@@ -7650,7 +7650,7 @@ open_exfile (
return NULL; return NULL;
} }
#endif #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); EMSG2(_("E189: \"%s\" exists (add ! to override)"), fname);
return NULL; return NULL;
} }

View File

@@ -791,7 +791,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((char *)file_path) if ((path_with_url((char *)file_path)
|| (os_file_exists(file_path) || (os_path_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
@@ -1442,7 +1442,7 @@ find_file_in_path_option (
buf = suffixes; buf = suffixes;
for (;; ) { for (;; ) {
if ( if (
(os_file_exists(NameBuff) (os_path_exists(NameBuff)
&& (find_what == FINDFILE_BOTH && (find_what == FINDFILE_BOTH
|| ((find_what == FINDFILE_DIR) || ((find_what == FINDFILE_DIR)
== os_isdir(NameBuff))))) { == os_isdir(NameBuff))))) {

View File

@@ -2990,14 +2990,15 @@ 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 && os_file_exists(backup)) { if (!p_bk && os_path_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' && os_file_exists(backup)) while (*p > 'a' && os_path_exists(backup)) {
--*p; (*p)--;
/* They all exist??? Must be something wrong! */ }
// They all exist??? Must be something wrong!
if (*p == 'a') { if (*p == 'a') {
xfree(backup); xfree(backup);
backup = NULL; backup = NULL;
@@ -3224,12 +3225,12 @@ restore_backup:
* This may not work if the vim_rename() fails. * This may not work if the vim_rename() fails.
* In that case we leave the copy around. * In that case we leave the copy around.
*/ */
/* If file does not exist, put the copy in its place */ // If file does not exist, put the copy in its place
if (!os_file_exists(fname)) { if (!os_path_exists(fname)) {
vim_rename(backup, fname); vim_rename(backup, fname);
} }
/* if original file does exist throw away the copy */ // if original file does exist throw away the copy
if (os_file_exists(fname)) { if (os_path_exists(fname)) {
os_remove((char *)backup); os_remove((char *)backup);
} }
} else { } else {
@@ -3238,8 +3239,8 @@ restore_backup:
} }
} }
/* if original file no longer exists give an extra warning */ // if original file no longer exists give an extra warning
if (!newfile && !os_file_exists(fname)) { if (!newfile && !os_path_exists(fname)) {
end = 0; end = 0;
} }
} }
@@ -3597,9 +3598,9 @@ restore_backup:
* If the original file does not exist yet * If the original file does not exist yet
* the current backup file becomes the original file * the current backup file becomes the original file
*/ */
if (org == NULL) if (org == NULL) {
EMSG(_("E205: Patchmode: can't save original file")); 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); vim_rename(backup, (char_u *)org);
xfree(backup); /* don't delete the file */ xfree(backup); /* don't delete the file */
backup = NULL; backup = NULL;
@@ -4514,9 +4515,11 @@ int vim_rename(char_u *from, char_u *to)
if (STRLEN(from) >= MAXPATHL - 5) if (STRLEN(from) >= MAXPATHL - 5)
return -1; return -1;
STRCPY(tempname, from); STRCPY(tempname, from);
for (n = 123; n < 99999; ++n) { for (n = 123; n < 99999; n++) {
sprintf((char *)path_tail(tempname), "%d", n); char * tail = (char *)path_tail(tempname);
if (!os_file_exists(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(from, tempname) == OK) {
if (os_rename(tempname, to) == OK) if (os_rename(tempname, to) == OK)
return 0; return 0;
@@ -4863,7 +4866,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)
&& os_file_exists(buf->b_ffname)) { && os_path_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;

View File

@@ -480,9 +480,10 @@ staterr:
if (arg2 != NULL) { if (arg2 != NULL) {
ppath = xmalloc(MAXPATHL + 1); ppath = xmalloc(MAXPATHL + 1);
expand_env((char_u *)arg2, (char_u *)ppath, MAXPATHL); 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; goto staterr;
} }
}
int i; int i;
/* if filename is a directory, append the cscope database name to it */ /* if filename is a directory, append the cscope database name to it */

View File

@@ -1359,7 +1359,7 @@ recover_names (
if (*dirp == NUL && file_count + num_files == 0 && fname != NULL) { if (*dirp == NUL && file_count + num_files == 0 && fname != NULL) {
char_u *swapname = (char_u *)modname((char *)fname_res, ".swp", TRUE); char_u *swapname = (char_u *)modname((char *)fname_res, ".swp", TRUE);
if (swapname != NULL) { if (swapname != NULL) {
if (os_file_exists(swapname)) { if (os_path_exists(swapname)) {
files = xmalloc(sizeof(char_u *)); files = xmalloc(sizeof(char_u *));
files[0] = swapname; files[0] = swapname;
swapname = NULL; swapname = NULL;
@@ -3426,11 +3426,11 @@ static char *findswapname(buf_T *buf, char **dirp, char *old_fname,
break; break;
} }
/* If the file was deleted this fname can be used. */ // If the file was deleted this fname can be used.
if (!os_file_exists((char_u *) fname)) if (!os_path_exists((char_u *)fname)) {
break; break;
} else }
{ } else {
MSG_PUTS("\n"); MSG_PUTS("\n");
if (msg_silent == 0) if (msg_silent == 0)
/* call wait_return() later */ /* call wait_return() later */

View File

@@ -599,14 +599,14 @@ int os_fchown(int fd, uv_uid_t owner, uv_gid_t group)
return r; return r;
} }
/// Check if a file exists. /// Check if a path exists.
/// ///
/// @return `true` if `name` exists. /// @return `true` if `path` exists
bool os_file_exists(const char_u *name) bool os_path_exists(const char_u *path)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
uv_stat_t statbuf; uv_stat_t statbuf;
return os_stat((char *)name, &statbuf) == kLibuvSuccess; return os_stat((char *)path, &statbuf) == kLibuvSuccess;
} }
/// Check if a file is readable. /// Check if a file is readable.

View File

@@ -566,10 +566,11 @@ int mch_expand_wildcards(int num_pat, char_u **pat, int *num_file,
/* /*
* Move the file names to allocated memory. * Move the file names to allocated memory.
*/ */
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) && !os_file_exists((*file)[i])) if (!(flags & EW_NOTFOUND) && !os_path_exists((*file)[i])) {
continue; continue;
}
/* check if this entry should be included */ /* check if this entry should be included */
dir = (os_isdir((*file)[i])); dir = (os_isdir((*file)[i]));

View File

@@ -684,7 +684,7 @@ static size_t do_path_expand(garray_T *gap, const char_u *path,
} }
// add existing file or symbolic link // add existing file or symbolic link
if ((flags & EW_ALLLINKS) ? os_fileinfo_link((char *)buf, &file_info) if ((flags & EW_ALLLINKS) ? os_fileinfo_link((char *)buf, &file_info)
: os_file_exists(buf)) { : os_path_exists(buf)) {
addfile(gap, buf, flags); 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 /* When EW_NOTFOUND is used, always add files and dirs. Makes
* "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 (os_file_exists(t)) } else if (os_path_exists(t)) {
addfile(&ga, t, flags); addfile(&ga, t, flags);
}
xfree(t); xfree(t);
} }
@@ -1327,7 +1328,7 @@ void addfile(
if (!(flags & EW_NOTFOUND) if (!(flags & EW_NOTFOUND)
&& ((flags & EW_ALLLINKS) && ((flags & EW_ALLLINKS)
? !os_fileinfo_link((char *)f, &file_info) ? !os_fileinfo_link((char *)f, &file_info)
: !os_file_exists(f))) { : !os_path_exists(f))) {
return; return;
} }

View File

@@ -571,9 +571,10 @@ 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
&& !os_file_exists(namebuf)) && !os_path_exists(namebuf)) {
continue; continue;
} }
}
if ((i = (int)fmt_ptr->addr[1]) > 0) { /* %n */ if ((i = (int)fmt_ptr->addr[1]) > 0) { /* %n */
if (regmatch.startp[i] == NULL) if (regmatch.startp[i] == NULL)
continue; continue;
@@ -706,11 +707,12 @@ 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 || os_file_exists(namebuf)) { if (*namebuf == NUL || os_path_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') {
currfile = qf_pop_dir(&file_stack); currfile = qf_pop_dir(&file_stack);
}
*namebuf = NUL; *namebuf = NUL;
if (tail && *tail) { if (tail && *tail) {
STRMOVE(IObuff, skipwhite(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 * "leaving directory"-messages we might have missed a
* directory change. * directory change.
*/ */
if (!os_file_exists(ptr)) { if (!os_path_exists(ptr)) {
xfree(ptr); xfree(ptr);
directory = qf_guess_filepath(fname); directory = qf_guess_filepath(fname);
if (directory) if (directory)
@@ -1232,8 +1234,9 @@ static char_u *qf_guess_filepath(char_u *filename)
xfree(fullname); xfree(fullname);
fullname = (char_u *)concat_fnames((char *)ds_ptr->dirname, (char *)filename, TRUE); fullname = (char_u *)concat_fnames((char *)ds_ptr->dirname, (char *)filename, TRUE);
if (os_file_exists(fullname)) if (os_path_exists(fullname)) {
break; break;
}
ds_ptr = ds_ptr->next; ds_ptr = ds_ptr->next;
} }

View File

@@ -7630,7 +7630,7 @@ mkspell (
else { else {
// Check for overwriting before doing things that may take a lot of // Check for overwriting before doing things that may take a lot of
// time. // time.
if (!over_write && os_file_exists(wfname)) { if (!over_write && os_path_exists(wfname)) {
EMSG(_(e_exists)); EMSG(_(e_exists));
goto theend; goto theend;
} }
@@ -7686,7 +7686,7 @@ mkspell (
spin.si_region = 1 << i; spin.si_region = 1 << i;
vim_snprintf((char *)fname, MAXPATHL, "%s.aff", innames[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 // Read the .aff file. Will init "spin->si_conv" based on the
// "SET" line. // "SET" line.
afile[i] = spell_read_aff(&spin, fname); afile[i] = spell_read_aff(&spin, fname);

View File

@@ -2353,7 +2353,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 (!os_file_exists(fname) if (!os_path_exists(fname)
&& !has_autocmd(EVENT_BUFREADCMD, fname, NULL) && !has_autocmd(EVENT_BUFREADCMD, fname, NULL)
) { ) {
retval = NOTAGFILE; retval = NOTAGFILE;

View File

@@ -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. // When reading check if the file exists.
if (undo_file_name != NULL if (undo_file_name != NULL
&& (!reading || os_file_exists((char_u *)undo_file_name))) { && (!reading || os_path_exists((char_u *)undo_file_name))) {
break; break;
} }
xfree(undo_file_name); 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 /* If the undo file already exists, verify that it actually is an undo
* file, and delete it. */ * file, and delete it. */
if (os_file_exists((char_u *)file_name)) { if (os_path_exists((char_u *)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 = os_open(file_name, O_RDONLY, 0); fd = os_open(file_name, O_RDONLY, 0);

View File

@@ -75,6 +75,8 @@ describe('fs function', function()
io.open('unit-test-directory/test_2.file', 'w').close() io.open('unit-test-directory/test_2.file', 'w').close()
lfs.link('test.file', 'unit-test-directory/test_link.file', true) 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 -- Since the tests are executed, they are called by an executable. We use
-- that executable for several asserts. -- that executable for several asserts.
absolute_executable = arg[0] 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_2.file')
os.remove('unit-test-directory/test_link.file') os.remove('unit-test-directory/test_link.file')
os.remove('unit-test-directory/test_hlink.file') os.remove('unit-test-directory/test_hlink.file')
os.remove('unit-test-directory/test_broken_link.file')
lfs.rmdir('unit-test-directory') lfs.rmdir('unit-test-directory')
end) end)
@@ -363,8 +366,8 @@ describe('fs function', function()
end) end)
describe('file operations', function() describe('file operations', function()
local function os_file_exists(filename) local function os_path_exists(filename)
return fs.os_file_exists((to_cstr(filename))) return fs.os_path_exists((to_cstr(filename)))
end end
local function os_rename(path, new_path) local function os_rename(path, new_path)
return fs.os_rename((to_cstr(path)), (to_cstr(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) return fs.os_write(fd, data, data and #data or 0)
end end
describe('os_file_exists', function() describe('os_path_exists', function()
it('returns false when given a non-existing file', 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) end)
it('returns true when given an existing file', function() 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)
end) end)
@@ -437,8 +448,8 @@ describe('fs function', function()
it('can rename file if destination file does not exist', function() it('can rename file if destination file does not exist', function()
eq(OK, (os_rename(test, not_exist))) eq(OK, (os_rename(test, not_exist)))
eq(false, (os_file_exists(test))) eq(false, (os_path_exists(test)))
eq(true, (os_file_exists(not_exist))) eq(true, (os_path_exists(not_exist)))
eq(OK, (os_rename(not_exist, test))) -- restore test file eq(OK, (os_rename(not_exist, test))) -- restore test file
end) end)
@@ -454,8 +465,8 @@ describe('fs function', function()
file:close() file:close()
eq(OK, (os_rename(other, test))) eq(OK, (os_rename(other, test)))
eq(false, (os_file_exists(other))) eq(false, (os_path_exists(other)))
eq(true, (os_file_exists(test))) eq(true, (os_path_exists(test)))
file = io.open(test, 'r') file = io.open(test, 'r')
eq('other', (file:read('*all'))) eq('other', (file:read('*all')))
file:close() file:close()

View File

@@ -43,7 +43,7 @@ describe('tempfile related functions', function()
it('generate name of non-existing file', function() it('generate name of non-existing file', function()
local file = vim_tempname() local file = vim_tempname()
assert.truthy(file) assert.truthy(file)
assert.False(os.os_file_exists(file)) assert.False(os.os_path_exists(file))
end) end)
it('generate different names on each call', function() it('generate different names on each call', function()