Macros | Functions
fs.c File Reference
#include <stdbool.h>
#include <stddef.h>
#include <assert.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include "auto/config.h"
#include <uv.h>
#include "nvim/os/os.h"
#include "nvim/os/os_defs.h"
#include "nvim/ascii.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/misc1.h"
#include "nvim/path.h"
#include "nvim/strings.h"

Macros

#define RUN_UV_FS_FUNC(ret, func, ...)
 

Functions

void fs_init (void)
 
int os_chdir (const char *path) FUNC_ATTR_NONNULL_ALL
 
int os_dirname (char_u *buf, size_t len) FUNC_ATTR_NONNULL_ALL
 
bool os_isrealdir (const char *name) FUNC_ATTR_NONNULL_ALL
 
bool os_isdir (const char_u *name) FUNC_ATTR_NONNULL_ALL
 
bool os_isdir_executable (const char *name) FUNC_ATTR_NONNULL_ALL
 
int os_nodetype (const char *name) FUNC_ATTR_NONNULL_ALL
 
int os_exepath (char *buffer, size_t *size) FUNC_ATTR_NONNULL_ALL
 
bool os_can_exe (const char_u *name, char_u **abspath, bool use_path) FUNC_ATTR_NONNULL_ARG(1)
 
int os_open (const char *path, int flags, int mode)
 
int os_set_cloexec (const int fd)
 Sets file descriptor fd to close-on-exec. More...
 
int os_close (const int fd)
 
int os_dup (const int fd) FUNC_ATTR_WARN_UNUSED_RESULT
 
ptrdiff_t os_read (const int fd, bool *const ret_eof, char *const ret_buf, const size_t size, const bool non_blocking) FUNC_ATTR_WARN_UNUSED_RESULT
 
ptrdiff_t os_write (const int fd, const char *const buf, const size_t size, const bool non_blocking) FUNC_ATTR_WARN_UNUSED_RESULT
 
int os_copy (const char *path, const char *new_path, int flags)
 
int os_fsync (int fd)
 
int32_t os_getperm (const char *name)
 
int os_setperm (const char *const name, int perm) FUNC_ATTR_NONNULL_ALL
 
int os_chown (const char *path, uv_uid_t owner, uv_gid_t group)
 
int os_fchown (int fd, uv_uid_t owner, uv_gid_t group)
 
bool os_path_exists (const char_u *path)
 
bool os_file_is_readable (const char *name) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
int os_file_is_writable (const char *name) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
int os_rename (const char_u *path, const char_u *new_path) FUNC_ATTR_NONNULL_ALL
 
int os_mkdir (const char *path, int32_t mode) FUNC_ATTR_NONNULL_ALL
 
int os_mkdir_recurse (const char *const dir, int32_t mode, char **const failed_dir) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
int os_mkdtemp (const char *template, char *path) FUNC_ATTR_NONNULL_ALL
 
int os_rmdir (const char *path) FUNC_ATTR_NONNULL_ALL
 
bool os_scandir (Directory *dir, const char *path) FUNC_ATTR_NONNULL_ALL
 
const char * os_scandir_next (Directory *dir) FUNC_ATTR_NONNULL_ALL
 
void os_closedir (Directory *dir) FUNC_ATTR_NONNULL_ALL
 
int os_remove (const char *path) FUNC_ATTR_NONNULL_ALL
 
bool os_fileinfo (const char *path, FileInfo *file_info) FUNC_ATTR_NONNULL_ARG(2)
 
bool os_fileinfo_link (const char *path, FileInfo *file_info) FUNC_ATTR_NONNULL_ARG(2)
 
bool os_fileinfo_fd (int file_descriptor, FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
 
bool os_fileinfo_id_equal (const FileInfo *file_info_1, const FileInfo *file_info_2) FUNC_ATTR_NONNULL_ALL
 
void os_fileinfo_id (const FileInfo *file_info, FileID *file_id) FUNC_ATTR_NONNULL_ALL
 
uint64_t os_fileinfo_inode (const FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
 
uint64_t os_fileinfo_size (const FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
 
uint64_t os_fileinfo_hardlinks (const FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
 
uint64_t os_fileinfo_blocksize (const FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
 
bool os_fileid (const char *path, FileID *file_id) FUNC_ATTR_NONNULL_ALL
 
bool os_fileid_equal (const FileID *file_id_1, const FileID *file_id_2) FUNC_ATTR_NONNULL_ALL
 
bool os_fileid_equal_fileinfo (const FileID *file_id, const FileInfo *file_info) FUNC_ATTR_NONNULL_ALL
 

Macro Definition Documentation

#define RUN_UV_FS_FUNC (   ret,
  func,
  ... 
)
Value:
do { \
bool did_try_to_free = false; \
uv_call_start: {} \
uv_fs_t req; \
ret = func(&fs_loop, &req, __VA_ARGS__); \
uv_fs_req_cleanup(&req); \
if (ret == UV_ENOMEM && !did_try_to_free) { \
did_try_to_free = true; \
goto uv_call_start; \
} \
} while (0)
if(len)
Definition: encode.c:222
const int ret
Definition: eval.c:967
void try_to_free_memory(void)
Definition: memory.c:43
fc func
Definition: eval.c:22393

Function Documentation

void fs_init ( void  )
bool os_can_exe ( const char_u name,
char_u **  abspath,
bool  use_path 
)

Checks if the file name is executable.

Parameters
[in]nameFilename to check.
[out]abspathReturns resolved executable path, if not NULL.
[in]use_pathAlso search $PATH.
Returns
true if name is executable and
  • can be found in $PATH,
  • is relative to current dir or
  • is absolute.
false otherwise.
int os_chdir ( const char *  path)

Changes the current directory to path.

Returns
0 on success, or negative error code.
int os_chown ( const char *  path,
uv_uid_t  owner,
uv_gid_t  group 
)

Changes the owner and group of a file, like chown(2).

Returns
0 on success, or libuv error code on failure.
Note
If owner or group is -1, then that ID is not changed.
int os_close ( const int  fd)

Close a file

Returns
0 or libuv error code on failure.
void os_closedir ( Directory dir)

Frees memory associated with os_scandir().

Parameters
dirThe directory.
int os_copy ( const char *  path,
const char *  new_path,
int  flags 
)

Copies a file from path to new_path.

See also
http://docs.libuv.org/en/v1.x/fs.html#c.uv_fs_copyfile
Parameters
pathPath of file to be copied
path_newPath of new file
flagsBitwise OR of flags defined in <uv.h>
Returns
0 on success, or libuv error code on failure.
int os_dirname ( char_u buf,
size_t  len 
)

Get the name of current directory.

Parameters
bufBuffer to store the directory name.
lenLength of buf.
Returns
OK for success, FAIL for failure.
int os_dup ( const int  fd)

Duplicate file descriptor

Parameters
[in]fdFile descriptor to duplicate.
Returns
New file descriptor or libuv error code (< 0).
int os_exepath ( char *  buffer,
size_t *  size 
)

Gets the absolute path of the currently running executable. May fail if procfs is missing. #6734

See also
path_exepath
Parameters
[out]bufferFull path to the executable.
[in]sizeSize of buffer.
Returns
0 on success, or libuv error code.
int os_fchown ( int  fd,
uv_uid_t  owner,
uv_gid_t  group 
)

Changes the owner and group of the file referred to by the open file descriptor, like fchown(2).

Returns
0 on success, or libuv error code on failure.
Note
If owner or group is -1, then that ID is not changed.
bool os_file_is_readable ( const char *  name)

Check if a file is readable.

Returns
true if name is readable, otherwise false.
int os_file_is_writable ( const char *  name)

Check if a file is writable.

Returns
0 if name is not writable,
1 if name is writable,
2 for a directory which we have rights to write into.
bool os_fileid ( const char *  path,
FileID file_id 
)

Get the FileID for a given path

Parameters
pathPath to the file.
[out]file_infoPointer to a FileID to fill in.
Returns
true on sucess, false for failure.
bool os_fileid_equal ( const FileID file_id_1,
const FileID file_id_2 
)

Check if two FileIDs are equal

Parameters
file_id_1Pointer to first FileID
file_id_2Pointer to second FileID
Returns
true if the two FileIDs represent te same file.
bool os_fileid_equal_fileinfo ( const FileID file_id,
const FileInfo file_info 
)

Check if a FileID is equal to a FileInfo

Parameters
file_idPointer to a FileID
file_infoPointer to a FileInfo
Returns
true if the FileID and the FileInfo represent te same file.
bool os_fileinfo ( const char *  path,
FileInfo file_info 
)

Get the file information for a given path

Parameters
pathPath to the file.
[out]file_infoPointer to a FileInfo to put the information in.
Returns
true on success, false for failure.
uint64_t os_fileinfo_blocksize ( const FileInfo file_info)

Get the blocksize from a FileInfo.

Returns
blocksize in bytes.
bool os_fileinfo_fd ( int  file_descriptor,
FileInfo file_info 
)

Get the file information for a given file descriptor

Parameters
file_descriptorFile descriptor of the file.
[out]file_infoPointer to a FileInfo to put the information in.
Returns
true on success, false for failure.
uint64_t os_fileinfo_hardlinks ( const FileInfo file_info)

Get the number of hardlinks from a FileInfo.

Returns
number of hardlinks.
void os_fileinfo_id ( const FileInfo file_info,
FileID file_id 
)

Get the FileID of a FileInfo

Parameters
file_infoPointer to the FileInfo
[out]file_idPointer to a FileID
bool os_fileinfo_id_equal ( const FileInfo file_info_1,
const FileInfo file_info_2 
)

Compare the inodes of two FileInfos

Returns
true if the two FileInfos represent the same file.
uint64_t os_fileinfo_inode ( const FileInfo file_info)

Get the inode of a FileInfo

Deprecated:
Use FileID instead, this function is only needed in memline.c
Parameters
file_infoPointer to the FileInfo
Returns
the inode number
bool os_fileinfo_link ( const char *  path,
FileInfo file_info 
)

Get the file information for a given path without following links

Parameters
pathPath to the file.
[out]file_infoPointer to a FileInfo to put the information in.
Returns
true on success, false for failure.
uint64_t os_fileinfo_size ( const FileInfo file_info)

Get the size of a file from a FileInfo.

Returns
filesize in bytes.
int os_fsync ( int  fd)

Flushes file modifications to disk.

Parameters
fdthe file descriptor of the file to flush to disk.
Returns
0 on success, or libuv error code on failure.
int32_t os_getperm ( const char *  name)

Get the file permissions for a given file.

Returns
libuv error code on error.
bool os_isdir ( const char_u name)

Check if the given path is a directory or not.

Returns
true if name is a directory.
bool os_isdir_executable ( const char *  name)

Check if the given path is a directory and is executable. Gives the same results as os_isdir() on Windows.

Returns
true if name is a directory and executable.
bool os_isrealdir ( const char *  name)

Check if the given path is a directory and not a symlink to a directory.

Returns
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.
int os_mkdir ( const char *  path,
int32_t  mode 
)

Make a directory.

Returns
0 for success, libuv error code for failure.
int os_mkdir_recurse ( const char *const  dir,
int32_t  mode,
char **const  failed_dir 
)

Make a directory, with higher levels when needed

Parameters
[in]dirDirectory to create.
[in]modePermissions for the newly-created directory.
[out]failed_dirIf it failed to create directory, then this argument is set to an allocated string containing the name of the directory which os_mkdir_recurse failed to create. I.e. it will contain dir or any of the higher level directories.
Returns
0 for success, libuv error code for failure.
int os_mkdtemp ( const char *  template,
char *  path 
)

Create a unique temporary directory.

Parameters
[in]templateTemplate of the path to the directory with XXXXXX which would be replaced by random chars.
[out]pathPath to created directory for success, undefined for failure.
Returns
0 for success, non-zero for failure.
int os_nodetype ( const char *  name)

Check what name is:

Returns
NODE_NORMAL: file or directory (or doesn't exist) NODE_WRITABLE: writable device, socket, fifo, etc. NODE_OTHER: non-writable things
int os_open ( const char *  path,
int  flags,
int  mode 
)

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, a libuv error code is returned, and no file is created or modified.

Parameters
flagsBitwise OR of flags defined in <fcntl.h>
modePermissions for the newly-created file (IGNORED if 'flags' is not O_CREAT or O_TMPFILE), subject to the current umask
Returns
file descriptor, or libuv error code on failure
bool os_path_exists ( const char_u path)

Check if a path exists.

Returns
true if path exists
ptrdiff_t os_read ( const int  fd,
bool *const  ret_eof,
char *const  ret_buf,
const size_t  size,
const bool  non_blocking 
)

Read from a file

Handles EINTR and ENOMEM, but not other errors.

Parameters
[in]fdFile descriptor to read from.
[out]ret_eofIs set to true if EOF was encountered, otherwise set to false. Initial value is ignored.
[out]ret_bufBuffer to write to. May be NULL if size is zero.
[in]sizeAmount of bytes to read.
[in]non_blockingDo not restart syscall if EAGAIN was encountered.
Returns
Number of bytes read or libuv error code (< 0).
int os_remove ( const char *  path)

Remove a file.

Returns
0 for success, non-zero for failure.
int os_rename ( const char_u path,
const char_u new_path 
)

Rename a file or directory.

Returns
OK for success, FAIL for failure.
int os_rmdir ( const char *  path)

Remove a directory.

Returns
0 for success, non-zero for failure.
bool os_scandir ( Directory dir,
const char *  path 
)

Opens a directory.

Parameters
[out]dirThe Directory object.
pathPath to the directory.
Returns
true if dir contains one or more items, false if not or an error occurred.
const char* os_scandir_next ( Directory dir)

Increments the directory pointer.

Parameters
dirThe Directory object.
Returns
a pointer to the next path in dir or NULL.
int os_set_cloexec ( const int  fd)

Sets file descriptor fd to close-on-exec.

int os_setperm ( const char *const  name,
int  perm 
)

Set the permission of a file.

Returns
OK for success, FAIL for failure.
ptrdiff_t os_write ( const int  fd,
const char *const  buf,
const size_t  size,
const bool  non_blocking 
)

Write to a file

Parameters
[in]fdFile descriptor to write to.
[in]bufData to write. May be NULL if size is zero.
[in]sizeAmount of bytes to write.
[in]non_blockingDo not restart syscall if EAGAIN was encountered.
Returns
Number of bytes written or libuv error code (< 0).