// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
// fs.c -- filesystem access
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include "auto/config.h"
#ifdef HAVE_SYS_UIO_H
# include <sys/uio.h>
#endif
#include <uv.h>
#include "nvim/ascii.h"
#include "nvim/assert.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/option.h"
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
#include "nvim/path.h"
#include "nvim/strings.h"
#ifdef WIN32
# include "nvim/mbyte.h" // for utf8_to_utf16, utf16_to_utf8
#endif
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/fs.c.generated.h"
#endif
#define RUN_UV_FS_FUNC(ret, func, ...) \
do { \
bool did_try_to_free = false; \
uv_call_start: {} \
uv_fs_t req; \
fs_loop_lock(); \
ret = func(&fs_loop, &req, __VA_ARGS__); \
uv_fs_req_cleanup(&req); \
fs_loop_unlock(); \
if (ret == UV_ENOMEM && !did_try_to_free) { \
try_to_free_memory(); \
did_try_to_free = true; \
goto uv_call_start; \
} \
} while (0)
// Many fs functions from libuv return that value on success.
static const int kLibuvSuccess = 0;
static uv_loop_t fs_loop;
static uv_mutex_t fs_loop_mutex;
// Initialize the fs module
void fs_init(void)
{
uv_loop_init(&fs_loop);
uv_mutex_init_recursive(&fs_loop_mutex);
}
/// TODO(bfredl): some of these operations should
/// be possible to do the private libuv loop of the
/// thread, instead of contending the global fs loop
void fs_loop_lock(void)
{
uv_mutex_lock(&fs_loop_mutex);
}
void fs_loop_unlock(void)
{
uv_mutex_unlock(&fs_loop_mutex);
}
/// Changes the current directory to `path`.
///
/// @return 0 on success, or negative error code.
int os_chdir(const char *path)
FUNC_ATTR_NONNULL_ALL
{
if (p_verbose >= 5) {
verbose_enter();
smsg("chdir(%s)", path);
verbose_leave();
}
return uv_chdir(path);
}
/// Get the name of current directory.
///
/// @param buf Buffer to store the directory name.
/// @param len Length of `buf`.
/// @return `OK` for success, `FAIL` for failure.
int os_dirname(char_u *buf, size_t len)
FUNC_ATTR_NONNULL_ALL
{
int error_number;
if ((error_number = uv_cwd((char *)buf, &len)) != kLibuvSuccess) {
STRLCPY(buf, uv_strerror(error_number), len);
return FAIL;
}
return OK;
}
/// Check if the given path is a directory and not a symlink to a directory.
/// @return `true` if `name` is a directory and NOT a symlink to a directory.
/// `false` if `name` is not a directory or if an error occurred.
bool os_isrealdir(const char *name)
FUNC_ATTR_NONNULL_ALL
{
uv_fs_t request;
fs_loop_lock();
if (uv_fs_lstat(&fs_loop, &request, name, NULL) != kLibuvSuccess) {
fs_loop_unlock();
return false;
}
fs_loop_unlock();
if (S_ISLNK(request.statbuf.st_mode)) {
return false;
} else {
return S_ISDIR(request.statbuf.st_mode);
}
}
/// Check if the given path exists and is a directory.
///
/// @return `true` if `name` is a directory.
bool os_isdir(const char_u *name)
FUNC_ATTR_NONNULL_ALL
{
int32_t mode = os_getperm((const char *)name);
if (mode < 0) {
return false;
}
if (!S_ISDIR(mode)) {
return false;
}
return true;
}
/// Check what `name` is:
/// @return NODE_NORMAL: file or directory (or doesn't exist)
/// NODE_WRITABLE: writable device, socket, fifo, etc.
/// NODE_OTHER: non-writable things
int os_nodetype(const char *name)
FUNC_ATTR_NONNULL_ALL
{
#ifndef WIN32 // Unix
uv_stat_t statbuf;
if (0 != os_stat(name, &statbuf)) {
return NODE_NORMAL; // File doesn't exist.
}
// uv_handle_type does not distinguish BLK and DIR.
// Related: https://github.com/joyent/libuv/pull/1421
if (S_ISREG(statbuf.st_mode) || S_ISDIR(statbuf.st_mode)) {
return NODE_NORMAL;
}
if (S_ISBLK(statbuf.st_mode)) { // block device isn't writable
return NODE_OTHER;
}
// Everything else is writable?
// buf_write() expects NODE_WRITABLE for char device /dev/stderr.
return NODE_WRITABLE;
#else // Windows
// Edge case from Vim os_win32.c:
// We can't open a file with a name "\\.\con" or "\\.\prn", trying to read
// from it later will cause Vim to hang. Thus return NODE_WRITABLE here.
if (STRNCMP(name, "\\\\.\\", 4) == 0) {
return NODE_WRITABLE;
}
// Vim os_win32.c:mch_nodetype does (since 7.4.015):
// wn = enc_to_utf16(name, NULL);
// hFile = CreatFile(wn, ...)
// to get a HANDLE. Whereas libuv just calls _get_osfhandle() on the fd we
// give it. But uv_fs_open later calls fs__capture_path which does a similar
// utf8-to-utf16 dance and saves us the hassle.
// macOS: os_open(/dev/stderr) would return UV_EACCES.
int fd = os_open(name, O_RDONLY
# ifdef O_NONBLOCK
|