Macros | Functions
fileio.c File Reference
#include <assert.h>
#include <stddef.h>
#include <stdbool.h>
#include <fcntl.h>
#include "auto/config.h"
#include <uv.h>
#include "nvim/os/fileio.h"
#include "nvim/memory.h"
#include "nvim/os/os.h"
#include "nvim/globals.h"
#include "nvim/rbuffer.h"
#include "nvim/macros.h"
#include "nvim/message.h"

Macros

#define FLAG(flags, flag, fcntl_flags, wrval, cond)
 

Functions

int file_open (FileDescriptor *const ret_fp, const char *const fname, const int flags, const int mode) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
int file_open_fd (FileDescriptor *const ret_fp, const int fd, const int flags) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
FileDescriptorfile_open_new (int *const error, const char *const fname, const int flags, const int mode) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
FileDescriptorfile_open_fd_new (int *const error, const int fd, const int flags) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_MALLOC FUNC_ATTR_WARN_UNUSED_RESULT
 
int file_close (FileDescriptor *const fp, const bool do_fsync) FUNC_ATTR_NONNULL_ALL
 
int file_free (FileDescriptor *const fp, const bool do_fsync) FUNC_ATTR_NONNULL_ALL
 
int file_flush (FileDescriptor *const fp) FUNC_ATTR_NONNULL_ALL
 
int file_fsync (FileDescriptor *const fp) FUNC_ATTR_NONNULL_ALL
 
ptrdiff_t file_read (FileDescriptor *const fp, char *const ret_buf, const size_t size) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
ptrdiff_t file_write (FileDescriptor *const fp, const char *const buf, const size_t size) FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1)
 
ptrdiff_t file_skip (FileDescriptor *const fp, const size_t size) FUNC_ATTR_NONNULL_ALL
 
int msgpack_file_write (void *data, const char *buf, size_t len) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
 
int msgpack_file_write_error (const int error)
 

Macro Definition Documentation

#define FLAG (   flags,
  flag,
  fcntl_flags,
  wrval,
  cond 
)
Value:
do { \
if (flags & flag) { \
os_open_flags |= fcntl_flags; \
assert(cond); \
if (wrval != kNone) { \
wr = wrval; \
} \
} \
} while (0)
Definition: globals.h:76
if(regpat==NULL)
Definition: fileio.c:7524
assert(len >=0)

Function Documentation

int file_close ( FileDescriptor *const  fp,
const bool  do_fsync 
)

Close file and free its buffer

Parameters
[in,out]fpFile to close.
[in]do_fsyncIf true, use fsync() to write changes to disk.
Returns
0 or error code.
int file_flush ( FileDescriptor *const  fp)

Flush file modifications to disk

Parameters
[in,out]fpFile to work with.
Returns
0 or error code.
int file_free ( FileDescriptor *const  fp,
const bool  do_fsync 
)

Close and free file obtained using file_open_new()

Parameters
[in,out]fpFile to close.
[in]do_fsyncIf true, use fsync() to write changes to disk.
Returns
0 or error code.
int file_fsync ( FileDescriptor *const  fp)

Flush file modifications to disk and run fsync()

Parameters
[in,out]fpFile to work with.
Returns
0 or error code.
int file_open ( FileDescriptor *const  ret_fp,
const char *const  fname,
const int  flags,
const int  mode 
)

Open file

Parameters
[out]ret_fpAddress where information needed for reading from or writing to a file is saved
[in]fnameFile name to open.
[in]flagsFlags,
See also
FileOpenFlags. Currently reading from and writing to the file at once is not supported, so either kFileWriteOnly or kFileReadOnly is required.
Parameters
[in]modePermissions for the newly created file (ignored if flags does not have kFileCreate*).
Returns
Error code, or 0 on success.
See also
os_strerror()
int file_open_fd ( FileDescriptor *const  ret_fp,
const int  fd,
const int  flags 
)

Wrap file descriptor with FileDescriptor structure

Warning
File descriptor wrapped like this must not be accessed by other means.
Parameters
[out]ret_fpAddress where information needed for reading from or writing to a file is saved
[in]fdFile descriptor to wrap.
[in]flagsFlags,
See also
FileOpenFlags. Currently reading from and writing to the file at once is not supported, so either FILE_WRITE_ONLY or FILE_READ_ONLY is required.
Returns
Error code (
See also
os_strerror()) or 0. Currently always returns 0.
FileDescriptor* file_open_fd_new ( int *const  error,
const int  fd,
const int  flags 
)

Like file_open_fd(), but allocate and return ret_fp

Parameters
[out]errorError code, or 0 on success.
See also
os_strerror()
Parameters
[in]fdFile descriptor to wrap.
[in]flagsFlags,
See also
FileOpenFlags.
Parameters
[in]modePermissions for the newly created file (ignored if flags does not have FILE_CREATE*).
Returns
[allocated] Opened file or NULL in case of error.
FileDescriptor* file_open_new ( int *const  error,
const char *const  fname,
const int  flags,
const int  mode 
)

Like file_open(), but allocate and return ret_fp

Parameters
[out]errorError code, or 0 on success.
See also
os_strerror()
Parameters
[in]fnameFile name to open.
[in]flagsFlags,
See also
FileOpenFlags.
Parameters
[in]modePermissions for the newly created file (ignored if flags does not have kFileCreate*).
Returns
[allocated] Opened file or NULL in case of error.
ptrdiff_t file_read ( FileDescriptor *const  fp,
char *const  ret_buf,
const size_t  size 
)

Read from file

Parameters
[in,out]fpFile to work with.
[out]ret_bufBuffer to read to. Must not be NULL.
[in]sizeNumber of bytes to read. Buffer must have at least ret_buf bytes.
Returns
error_code (< 0) or number of bytes read.
ptrdiff_t file_skip ( FileDescriptor *const  fp,
const size_t  size 
)

Skip some bytes

This is like fseek(fp, size, SEEK_CUR), but actual implementation simply reads to a buffer and discards the result.

ptrdiff_t file_write ( FileDescriptor *const  fp,
const char *const  buf,
const size_t  size 
)

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.
Returns
Number of bytes written or libuv error code (< 0).
int msgpack_file_write ( void *  data,
const char *  buf,
size_t  len 
)

Msgpack callback for writing to a file

Parameters
dataFile to write to.
[in]bufData to write.
[in]lenLength of the data to write.
Returns
0 in case of success, -1 in case of error.
int msgpack_file_write_error ( const int  error)

Print error which occurs when failing to write msgpack data

Parameters
[in]errorError code of the error to print.
Returns
-1 (error return for msgpack_packer callbacks).