mirror of
https://github.com/neovim/neovim.git
synced 2025-02-25 18:55:25 -06:00
os_open: impl mch_open with libuv. ref #133
- use return value instead of open_req.result - libuv uv_fs_open() returns `-errno` instead of always -1 - libuv always sets open_req.result to the return value, _except_ for OOM where it only sets the return value. So always use the return value. - replace calls to mch_open macro. - update call sites expecting -1 error
This commit is contained in:
parent
bf6b0e3c0a
commit
180c84ed37
@ -8317,7 +8317,7 @@ static void f_filereadable(typval_T *argvars, typval_T *rettv)
|
||||
# define O_NONBLOCK 0
|
||||
#endif
|
||||
p = get_tv_string(&argvars[0]);
|
||||
if (*p && !os_isdir(p) && (fd = mch_open((char *)p,
|
||||
if (*p && !os_isdir(p) && (fd = os_open((char *)p,
|
||||
O_RDONLY | O_NONBLOCK, 0)) >= 0) {
|
||||
n = TRUE;
|
||||
close(fd);
|
||||
|
@ -1575,19 +1575,19 @@ void write_viminfo(char_u *file, int forceit)
|
||||
if (tempname != NULL) {
|
||||
int fd;
|
||||
|
||||
/* Use mch_open() to be able to use O_NOFOLLOW and set file
|
||||
/* Use os_open() to be able to use O_NOFOLLOW and set file
|
||||
* protection:
|
||||
* Unix: same as original file, but strip s-bit. Reset umask to
|
||||
* avoid it getting in the way.
|
||||
* Others: r&w for user only. */
|
||||
# ifdef UNIX
|
||||
umask_save = umask(0);
|
||||
fd = mch_open((char *)tempname,
|
||||
fd = os_open((char *)tempname,
|
||||
O_CREAT|O_EXCL|O_WRONLY|O_NOFOLLOW,
|
||||
(int)((old_info.stat.st_mode & 0777) | 0600));
|
||||
(void)umask(umask_save);
|
||||
# else
|
||||
fd = mch_open((char *)tempname,
|
||||
fd = os_open((char *)tempname,
|
||||
O_CREAT|O_EXCL|O_WRONLY|O_NOFOLLOW, 0600);
|
||||
# endif
|
||||
if (fd < 0)
|
||||
|
@ -2372,9 +2372,9 @@ int source_level(void *cookie)
|
||||
*/
|
||||
static FILE *fopen_noinh_readbin(char *filename)
|
||||
{
|
||||
int fd_tmp = mch_open(filename, O_RDONLY, 0);
|
||||
int fd_tmp = os_open(filename, O_RDONLY, 0);
|
||||
|
||||
if (fd_tmp == -1)
|
||||
if (fd_tmp < 0)
|
||||
return NULL;
|
||||
|
||||
# ifdef HAVE_FD_CLOEXEC
|
||||
|
@ -428,7 +428,7 @@ readfile (
|
||||
#ifdef UNIX
|
||||
/*
|
||||
* On Unix it is possible to read a directory, so we have to
|
||||
* check for it before the mch_open().
|
||||
* check for it before os_open().
|
||||
*/
|
||||
perm = os_getperm(fname);
|
||||
if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */
|
||||
@ -507,13 +507,13 @@ readfile (
|
||||
if (!read_buffer && !read_stdin) {
|
||||
if (!newfile || readonlymode) {
|
||||
file_readonly = TRUE;
|
||||
} else if ((fd = mch_open((char *)fname, O_RDWR, 0)) < 0) {
|
||||
} else if ((fd = os_open((char *)fname, O_RDWR, 0)) < 0) {
|
||||
// opening in readwrite mode failed => file is readonly
|
||||
file_readonly = TRUE;
|
||||
}
|
||||
if (file_readonly == TRUE) {
|
||||
// try to open readonly
|
||||
fd = mch_open((char *)fname, O_RDONLY, 0);
|
||||
fd = os_open((char *)fname, O_RDONLY, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@ -696,9 +696,7 @@ readfile (
|
||||
if (!read_stdin && (curbuf != old_curbuf
|
||||
|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
|
||||
|| (using_b_fname && (old_b_fname != curbuf->b_fname))
|
||||
|| (fd =
|
||||
mch_open((char *)fname, O_RDONLY,
|
||||
0)) < 0)) {
|
||||
|| (fd = os_open((char *)fname, O_RDONLY, 0)) < 0)) {
|
||||
--no_wait_return;
|
||||
msg_scroll = msg_save;
|
||||
if (fd < 0)
|
||||
@ -2157,9 +2155,9 @@ readfile_charconvert (
|
||||
if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
|
||||
fname, tmpname) == FAIL)
|
||||
errmsg = (char_u *)_("Conversion with 'charconvert' failed");
|
||||
if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
|
||||
O_RDONLY, 0)) < 0)
|
||||
if (errmsg == NULL && (*fdp = os_open((char *)tmpname, O_RDONLY, 0)) < 0) {
|
||||
errmsg = (char_u *)_("can't read output of 'charconvert'");
|
||||
}
|
||||
}
|
||||
|
||||
if (errmsg != NULL) {
|
||||
@ -2174,8 +2172,9 @@ readfile_charconvert (
|
||||
}
|
||||
|
||||
/* If the input file is closed, open it (caller should check for error). */
|
||||
if (*fdp < 0)
|
||||
*fdp = mch_open((char *)fname, O_RDONLY, 0);
|
||||
if (*fdp < 0) {
|
||||
*fdp = os_open((char *)fname, O_RDONLY, 0);
|
||||
}
|
||||
|
||||
return tmpname;
|
||||
}
|
||||
@ -2737,7 +2736,7 @@ buf_write (
|
||||
break;
|
||||
}
|
||||
}
|
||||
fd = mch_open((char *)IObuff,
|
||||
fd = os_open((char *)IObuff,
|
||||
O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
|
||||
if (fd < 0) /* can't write in directory */
|
||||
backup_copy = TRUE;
|
||||
@ -2791,8 +2790,7 @@ buf_write (
|
||||
else
|
||||
backup_ext = p_bex;
|
||||
|
||||
if (backup_copy
|
||||
&& (fd = mch_open((char *)fname, O_RDONLY, 0)) >= 0) {
|
||||
if (backup_copy && (fd = os_open((char *)fname, O_RDONLY, 0)) >= 0) {
|
||||
int bfd;
|
||||
char_u *copybuf, *wp;
|
||||
int some_error = FALSE;
|
||||
@ -2890,7 +2888,7 @@ buf_write (
|
||||
os_remove((char *)backup);
|
||||
/* Open with O_EXCL to avoid the file being created while
|
||||
* we were sleeping (symlink hacker attack?) */
|
||||
bfd = mch_open((char *)backup,
|
||||
bfd = os_open((char *)backup,
|
||||
O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW,
|
||||
perm & 0777);
|
||||
if (bfd < 0) {
|
||||
@ -3194,7 +3192,7 @@ nobackup:
|
||||
* (this may happen when the user reached his quotum for number of files).
|
||||
* Appending will fail if the file does not exist and forceit is FALSE.
|
||||
*/
|
||||
while ((fd = mch_open((char *)wfname, O_WRONLY | (append
|
||||
while ((fd = os_open((char *)wfname, O_WRONLY | (append
|
||||
? (forceit ? (
|
||||
O_APPEND |
|
||||
O_CREAT) :
|
||||
@ -3515,8 +3513,8 @@ restore_backup:
|
||||
MSG(_(e_interr));
|
||||
out_flush();
|
||||
}
|
||||
if ((fd = mch_open((char *)backup, O_RDONLY, 0)) >= 0) {
|
||||
if ((write_info.bw_fd = mch_open((char *)fname,
|
||||
if ((fd = os_open((char *)backup, O_RDONLY, 0)) >= 0) {
|
||||
if ((write_info.bw_fd = os_open((char *)fname,
|
||||
O_WRONLY | O_CREAT | O_TRUNC,
|
||||
perm & 0777)) >= 0) {
|
||||
/* copy the file. */
|
||||
@ -3646,7 +3644,7 @@ restore_backup:
|
||||
int empty_fd;
|
||||
|
||||
if (org == NULL
|
||||
|| (empty_fd = mch_open(org,
|
||||
|| (empty_fd = os_open(org,
|
||||
O_CREAT | O_EXCL | O_NOFOLLOW,
|
||||
perm < 0 ? 0666 : (perm & 0777))) < 0)
|
||||
EMSG(_("E206: patchmode: can't touch empty original file"));
|
||||
@ -4615,7 +4613,7 @@ int vim_rename(char_u *from, char_u *to)
|
||||
/* For systems that support ACL: get the ACL from the original file. */
|
||||
acl = mch_get_acl(from);
|
||||
#endif
|
||||
fd_in = mch_open((char *)from, O_RDONLY, 0);
|
||||
fd_in = os_open((char *)from, O_RDONLY, 0);
|
||||
if (fd_in == -1) {
|
||||
#ifdef HAVE_ACL
|
||||
mch_free_acl(acl);
|
||||
@ -4624,7 +4622,7 @@ int vim_rename(char_u *from, char_u *to)
|
||||
}
|
||||
|
||||
/* Create the new file with same permissions as the original. */
|
||||
fd_out = mch_open((char *)to,
|
||||
fd_out = os_open((char *)to,
|
||||
O_CREAT|O_EXCL|O_WRONLY|O_NOFOLLOW, (int)perm);
|
||||
if (fd_out == -1) {
|
||||
close(fd_in);
|
||||
@ -4660,7 +4658,7 @@ int vim_rename(char_u *from, char_u *to)
|
||||
errmsg = _("E210: Error reading \"%s\"");
|
||||
to = from;
|
||||
}
|
||||
#ifndef UNIX /* for Unix mch_open() already set the permission */
|
||||
#ifndef UNIX /* for Unix os_open() already set the permission */
|
||||
os_setperm(to, perm);
|
||||
#endif
|
||||
#ifdef HAVE_ACL
|
||||
|
@ -108,13 +108,12 @@
|
||||
#endif
|
||||
|
||||
# define mch_fopen(n, p) fopen((n), (p))
|
||||
# define mch_open(n, m, p) open((n), (m), (p))
|
||||
|
||||
/* mch_open_rw(): invoke mch_open() with third argument for user R/W. */
|
||||
/* mch_open_rw(): invoke os_open() with third argument for user R/W. */
|
||||
#if defined(UNIX) /* open in rw------- mode */
|
||||
# define mch_open_rw(n, f) mch_open((n), (f), (mode_t)0600)
|
||||
# define mch_open_rw(n, f) os_open((n), (f), (mode_t)0600)
|
||||
#else
|
||||
# define mch_open_rw(n, f) mch_open((n), (f), 0)
|
||||
# define mch_open_rw(n, f) os_open((n), (f), 0)
|
||||
#endif
|
||||
|
||||
#ifdef STARTUPTIME
|
||||
|
@ -440,7 +440,7 @@ void ml_setname(buf_T *buf)
|
||||
}
|
||||
|
||||
if (mfp->mf_fd == -1) { /* need to (re)open the swap file */
|
||||
mfp->mf_fd = mch_open((char *)mfp->mf_fname, O_RDWR, 0);
|
||||
mfp->mf_fd = os_open((char *)mfp->mf_fname, O_RDWR, 0);
|
||||
if (mfp->mf_fd < 0) {
|
||||
/* could not (re)open the swap file, what can we do???? */
|
||||
EMSG(_("E301: Oops, lost the swap file!!!"));
|
||||
@ -1517,7 +1517,7 @@ static time_t swapfile_info(char_u *fname)
|
||||
/*
|
||||
* print the original file name
|
||||
*/
|
||||
fd = mch_open((char *)fname, O_RDONLY, 0);
|
||||
fd = os_open((char *)fname, O_RDONLY, 0);
|
||||
if (fd >= 0) {
|
||||
if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0)) {
|
||||
if (STRNCMP(b0.b0_version, "VIM 3.0", 7) == 0) {
|
||||
@ -3329,7 +3329,7 @@ findswapname (
|
||||
* Try to read block 0 from the swap file to get the original
|
||||
* file name (and inode number).
|
||||
*/
|
||||
fd = mch_open((char *)fname, O_RDONLY, 0);
|
||||
fd = os_open((char *)fname, O_RDONLY, 0);
|
||||
if (fd >= 0) {
|
||||
if (read_eintr(fd, &b0, sizeof(b0)) == sizeof(b0)) {
|
||||
/*
|
||||
|
@ -11,6 +11,7 @@
|
||||
#include "nvim/misc2.h"
|
||||
#include "nvim/path.h"
|
||||
#include "nvim/strings.h"
|
||||
#include "nvim/log.h"
|
||||
|
||||
#ifdef INCLUDE_GENERATED_DECLARATIONS
|
||||
# include "os/fs.c.generated.h"
|
||||
@ -150,6 +151,24 @@ static bool is_executable_in_path(const char_u *name)
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Opens or creates a file and returns a non-negative integer representing
|
||||
/// the lowest-numbered unused file descriptor, for use in subsequent system
|
||||
/// calls (read, write, lseek, fcntl, etc.). If the operation fails, `-errno`
|
||||
/// is returned, and no file is created or modified.
|
||||
///
|
||||
/// @param flags bitwise OR of flags defined in <fcntl.h>
|
||||
/// @param mode permissions for the newly-created file (IGNORED if 'flags' is
|
||||
/// not `O_CREAT` or `O_TMPFILE`)
|
||||
/// @return file descriptor, or negative `errno` on failure
|
||||
int os_open(const char* path, int flags, int mode)
|
||||
{
|
||||
uv_fs_t open_req;
|
||||
int r = uv_fs_open(uv_default_loop(), &open_req, path, flags, mode, NULL);
|
||||
uv_fs_req_cleanup(&open_req);
|
||||
//`r` is the same as open_req.result, except when OOM. So just use `r`.
|
||||
return r;
|
||||
}
|
||||
|
||||
/// Get stat information for a file.
|
||||
///
|
||||
/// @return OK on success, FAIL if a failure occurred.
|
||||
@ -291,7 +310,7 @@ int os_remove(const char *path)
|
||||
|
||||
/// Get the file information for a given path
|
||||
///
|
||||
/// @param file_descriptor File descriptor of the file.
|
||||
/// @param path Path to the file.
|
||||
/// @param[out] file_info Pointer to a FileInfo to put the information in.
|
||||
/// @return `true` on success, `false` for failure.
|
||||
bool os_get_file_info(const char *path, FileInfo *file_info)
|
||||
|
@ -1045,7 +1045,7 @@ void u_write_undo(char_u *name, int forceit, buf_T *buf, char_u *hash)
|
||||
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, 0);
|
||||
fd = os_open((char *)file_name, O_RDONLY, 0);
|
||||
if (fd < 0) {
|
||||
if (name != NULL || p_verbose > 0) {
|
||||
if (name == NULL)
|
||||
@ -1089,7 +1089,7 @@ void u_write_undo(char_u *name, int forceit, buf_T *buf, char_u *hash)
|
||||
goto theend;
|
||||
}
|
||||
|
||||
fd = mch_open((char *)file_name,
|
||||
fd = os_open((char *)file_name,
|
||||
O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
|
||||
if (fd < 0) {
|
||||
EMSG2(_(e_not_open), file_name);
|
||||
|
Loading…
Reference in New Issue
Block a user