1#ifndef NVIM_OS_FILEIO_H
2#define NVIM_OS_FILEIO_H
3
4#include <stdbool.h>
5#include <stddef.h>
6
7#include "nvim/func_attr.h"
8#include "nvim/rbuffer.h"
9
10/// Structure used to read from/write to file
11typedef struct {
12 int fd; ///< File descriptor.
13 int _error; ///< Error code for use with RBuffer callbacks or zero.
14 RBuffer *rv; ///< Read or write buffer.
15 bool wr; ///< True if file is in write mode.
16 bool eof; ///< True if end of file was encountered.
17 bool non_blocking; ///< True if EAGAIN should not restart syscalls.
18} FileDescriptor;
19
20/// file_open() flags
21typedef enum {
22 kFileReadOnly = 1, ///< Open file read-only. Default.
23 kFileCreate = 2, ///< Create file if it does not exist yet.
24 ///< Implies kFileWriteOnly.
25 kFileWriteOnly = 4, ///< Open file for writing only.
26 ///< Cannot be used with kFileReadOnly.
27 kFileNoSymlink = 8, ///< Do not allow symbolic links.
28 kFileCreateOnly = 16, ///< Only create the file, failing if it already
29 ///< exists. Implies kFileWriteOnly. Cannot be used
30 ///< with kFileCreate.
31 kFileTruncate = 32, ///< Truncate the file if it exists.
32 ///< Implies kFileWriteOnly. Cannot be used with
33 ///< kFileCreateOnly.
34 kFileAppend = 64, ///< Append to the file. Implies kFileWriteOnly. Cannot
35 ///< be used with kFileCreateOnly.
36 kFileNonBlocking = 128, ///< Do not restart read() or write() syscall if
37 ///< EAGAIN was encountered.
38} FileOpenFlags;
39
40static inline bool file_eof(const FileDescriptor *const fp)
41 REAL_FATTR_PURE REAL_FATTR_WARN_UNUSED_RESULT REAL_FATTR_NONNULL_ALL;
42
43/// Check whether end of file was encountered
44///
45/// @param[in] fp File to check.
46///
47/// @return true if it was, false if it was not or read operation was never
48/// performed.
49static inline bool file_eof(const FileDescriptor *const fp)
50{
51 return fp->eof && rbuffer_size(fp->rv) == 0;
52}
53
54static inline int file_fd(const FileDescriptor *const fp)
55 REAL_FATTR_PURE REAL_FATTR_WARN_UNUSED_RESULT REAL_FATTR_NONNULL_ALL;
56
57/// Return the file descriptor associated with the FileDescriptor structure
58///
59/// @param[in] fp File to check.
60///
61/// @return File descriptor.
62static inline int file_fd(const FileDescriptor *const fp)
63{
64 return fp->fd;
65}
66
67enum {
68 /// Read or write buffer size
69 ///
70 /// Currently equal to (IOSIZE - 1), but they do not need to be connected.
71 kRWBufferSize = 1024
72};
73
74#ifdef INCLUDE_GENERATED_DECLARATIONS
75# include "os/fileio.h.generated.h"
76#endif
77#endif // NVIM_OS_FILEIO_H
78