Files
API for opening, reading and writing files.
Types
oc_file
typedef struct oc_file
{
u64 h;
} oc_file;
An opaque handle identifying an opened file.
Fields
hOpaque file handle.
oc_file_open_flags
typedef u16 oc_file_open_flags;
The type of file open flags describing file open options.
oc_file_open_flags_enum
typedef enum oc_file_open_flags_enum
{
OC_FILE_OPEN_NONE = 0,
OC_FILE_OPEN_APPEND = 1,
OC_FILE_OPEN_TRUNCATE = 2,
OC_FILE_OPEN_CREATE = 4,
OC_FILE_OPEN_SYMLINK = 8,
OC_FILE_OPEN_NO_FOLLOW = 16,
OC_FILE_OPEN_RESTRICT = 32
} oc_file_open_flags_enum;
Flags for the oc_file_open() function.
Enum Constants
OC_FILE_OPEN_NONENo options.OC_FILE_OPEN_APPENDOpen the file in 'append' mode. All writes append data at the end of the file.OC_FILE_OPEN_TRUNCATETruncate the file to 0 bytes when opening.OC_FILE_OPEN_CREATECreate the file if it does not exist.OC_FILE_OPEN_SYMLINKIf the file is a symlink, open the symlink itself instead of following it.OC_FILE_OPEN_NO_FOLLOWIf the file is a symlink, the call to open will fail.OC_FILE_OPEN_RESTRICTReserved.
oc_file_access
typedef u16 oc_file_access;
oc_file_access_enum
typedef enum oc_file_access_enum
{
OC_FILE_ACCESS_NONE = 0,
OC_FILE_ACCESS_READ = 1,
OC_FILE_ACCESS_WRITE = 2
} oc_file_access_enum;
This enum describes the access permissions of a file handle.
Enum Constants
OC_FILE_ACCESS_NONEThe file handle has no access permissions.OC_FILE_ACCESS_READThe file handle can be used for reading from the file.OC_FILE_ACCESS_WRITEThe file handle can be used for writing to the file.
oc_file_whence
typedef enum oc_file_whence
{
OC_FILE_SEEK_SET = 0,
OC_FILE_SEEK_END = 1,
OC_FILE_SEEK_CURRENT = 2
} oc_file_whence;
This enum is used in oc_file_seek() to specify the starting point of the seek operation.
Enum Constants
OC_FILE_SEEK_SETSet the file position relative to the beginning of the file.OC_FILE_SEEK_ENDSet the file position relative to the end of the file.OC_FILE_SEEK_CURRENTSet the file position relative to the current position.
oc_io_req_id
typedef u64 oc_io_req_id;
A type used to identify I/O requests.
oc_io_op
typedef u32 oc_io_op;
A type used to identify I/O operations.
oc_io_op_enum
typedef enum oc_io_op_enum
{
OC_IO_OPEN_AT = 0,
OC_IO_CLOSE = 1,
OC_IO_FSTAT = 2,
OC_IO_SEEK = 3,
OC_IO_READ = 4,
OC_IO_WRITE = 5,
OC_OC_IO_ERROR = 6
} oc_io_op_enum;
This enum declares all I/O operations.
Enum Constants
-
OC_IO_OPEN_ATOpen a file at a path relative to a given root directory.handleis the handle to the root directory. If it is nil, the application's default directory is used.sizeis the size of the path, in bytes.bufferpoints to an array containing the path of the file to open, relative to the directory identified byhandle.opencontains the permissions and flags for the open operation.
-
OC_IO_CLOSEClose a file handle.handleis the handle to close.
-
OC_IO_FSTATGet status information for a file handle.handleis the handle to stat.sizeis the size of the result buffer. It should be at leastsizeof(oc_file_status).bufferis the result buffer.
-
OC_IO_SEEKMove the file position in a file.handleis the handle of the file.offsetspecifies the offset of the new position, relative to the base position specified bywhence.whencedetermines the base position for the seek operation.
-
OC_IO_READRead data from a file.handleis the handle of the file.sizeis the number of bytes to read.bufferis the result buffer. It should be big enough to holdsizebytes.
-
OC_IO_WRITEWrite data to a file.handleis the handle of the file.sizeis the number of bytes to write.buffercontains the data to write to the file.
-
OC_OC_IO_ERRORGet the error attached to a file handle.handleis the handle of the file.
oc_io_req
typedef struct oc_io_req
{
oc_io_req_id id;
oc_io_op op;
oc_file handle;
i64 offset;
u64 size;
union
{
char* buffer;
u64 unused;
};
union
{
struct
{
oc_file_access rights;
oc_file_open_flags flags;
} open;
oc_file_whence whence;
};
} oc_io_req;
A structure describing an I/O request.
Fields
idAn identifier for the request. You can set this to any value you want. It is passed back in theoc_io_cmpcompletion and can be used to match requests and completions.opThe requested operation.handleA file handle used by some operations.offsetAn offset used by some operations.sizeA size indicating the capacity of the buffer pointed to bybuffer, in bytes.
oc_io_error
typedef i32 oc_io_error;
A type identifying an I/O error.
oc_io_error_enum
typedef enum oc_io_error_enum
{
OC_IO_OK = 0,
OC_IO_ERR_UNKNOWN = 1,
OC_IO_ERR_OP = 2,
OC_IO_ERR_HANDLE = 3,
OC_IO_ERR_PREV = 4,
OC_IO_ERR_ARG = 5,
OC_IO_ERR_PERM = 6,
OC_IO_ERR_SPACE = 7,
OC_IO_ERR_NO_ENTRY = 8,
OC_IO_ERR_EXISTS = 9,
OC_IO_ERR_NOT_DIR = 10,
OC_IO_ERR_DIR = 11,
OC_IO_ERR_MAX_FILES = 12,
OC_IO_ERR_MAX_LINKS = 13,
OC_IO_ERR_PATH_LENGTH = 14,
OC_IO_ERR_FILE_SIZE = 15,
OC_IO_ERR_OVERFLOW = 16,
OC_IO_ERR_NOT_READY = 17,
OC_IO_ERR_MEM = 18,
OC_IO_ERR_INTERRUPT = 19,
OC_IO_ERR_PHYSICAL = 20,
OC_IO_ERR_NO_DEVICE = 21,
OC_IO_ERR_WALKOUT = 22
} oc_io_error_enum;
This enum declares all I/O error values.
Enum Constants
OC_IO_OKNo error.OC_IO_ERR_UNKNOWNAn unexpected error happened.OC_IO_ERR_OPThe request had an invalid operation.OC_IO_ERR_HANDLEThe request had an invalid handle.OC_IO_ERR_PREVThe operation was not carried out because the file handle has previous errors.OC_IO_ERR_ARGThe request contained wrong arguments.OC_IO_ERR_PERMThe operation requires permissions that the file handle doesn't have.OC_IO_ERR_SPACEThe operation couldn't complete due to a lack of space in the result buffer.OC_IO_ERR_NO_ENTRYOne of the directory in the path does not exist or couldn't be traversed.OC_IO_ERR_EXISTSThe file already exists.OC_IO_ERR_NOT_DIRThe file is not a directory.OC_IO_ERR_DIRThe file is a directory.OC_IO_ERR_MAX_FILESThere are too many opened files.OC_IO_ERR_MAX_LINKSThe path contains too many symbolic links (this may be indicative of a symlink loop).OC_IO_ERR_PATH_LENGTHThe path is too long.OC_IO_ERR_FILE_SIZEThe file is too large.OC_IO_ERR_OVERFLOWThe file is too large to be opened.OC_IO_ERR_NOT_READYThe file is locked or the device on which it is stored is not ready.OC_IO_ERR_MEMThe system is out of memory.OC_IO_ERR_INTERRUPTThe operation was interrupted by a signal.OC_IO_ERR_PHYSICALA physical error happened.OC_IO_ERR_NO_DEVICEThe device on which the file is stored was not found.OC_IO_ERR_WALKOUTOne element along the path is outside the root directory subtree.
oc_io_cmp
typedef struct oc_io_cmp
{
oc_io_req_id id;
oc_io_error error;
union
{
i64 result;
u64 size;
i64 offset;
oc_file handle;
};
} oc_io_cmp;
A structure describing the completion of an I/O operation.
Fields
idThe request ID as passed in theoc_io_reqrequest that generated this completion.errorThe error value for the operation.
oc_file_type
typedef enum oc_file_type
{
OC_FILE_UNKNOWN = 0,
OC_FILE_REGULAR = 1,
OC_FILE_DIRECTORY = 2,
OC_FILE_SYMLINK = 3,
OC_FILE_BLOCK = 4,
OC_FILE_CHARACTER = 5,
OC_FILE_FIFO = 6,
OC_FILE_SOCKET = 7
} oc_file_type;
An enum identifying the type of a file.
Enum Constants
OC_FILE_UNKNOWNThe file is of unknown type.OC_FILE_REGULARThe file is a regular file.OC_FILE_DIRECTORYThe file is a directory.OC_FILE_SYMLINKThe file is a symbolic link.OC_FILE_BLOCKThe file is a block device.OC_FILE_CHARACTERThe file is a character device.OC_FILE_FIFOThe file is a FIFO pipe.OC_FILE_SOCKETThe file is a socket.
oc_file_perm
typedef u16 oc_file_perm;
A type describing file permissions.
oc_file_perm_enum
typedef enum oc_file_perm_enum
{
OC_FILE_OTHER_EXEC = 1,
OC_FILE_OTHER_WRITE = 2,
OC_FILE_OTHER_READ = 4,
OC_FILE_GROUP_EXEC = 8,
OC_FILE_GROUP_WRITE = 16,
OC_FILE_GROUP_READ = 32,
OC_FILE_OWNER_EXEC = 64,
OC_FILE_OWNER_WRITE = 128,
OC_FILE_OWNER_READ = 256,
OC_FILE_STICKY_BIT = 512,
OC_FILE_SET_GID = 1024,
OC_FILE_SET_UID = 2048
} oc_file_perm_enum;
Enum Constants
OC_FILE_OTHER_EXECOC_FILE_OTHER_WRITEOC_FILE_OTHER_READOC_FILE_GROUP_EXECOC_FILE_GROUP_WRITEOC_FILE_GROUP_READOC_FILE_OWNER_EXECOC_FILE_OWNER_WRITEOC_FILE_OWNER_READOC_FILE_STICKY_BITOC_FILE_SET_GIDOC_FILE_SET_UID
oc_datestamp
typedef struct oc_datestamp
{
i64 seconds;
u64 fraction;
} oc_datestamp;
oc_file_status
typedef struct oc_file_status
{
u64 uid;
oc_file_type type;
oc_file_perm perm;
u64 size;
oc_datestamp creationDate;
oc_datestamp accessDate;
oc_datestamp modificationDate;
} oc_file_status;
oc_file_list
typedef struct oc_file_list
{
oc_list list;
u64 eltCount;
} oc_file_list;
An type describing a list of enumerated files in a given directory.
oc_file_listdir_elt
typedef struct oc_file_listdir_elt
{
oc_list_elt listElt;
oc_str8 basename;
oc_file_type type;
} oc_file_listdir_elt;
Functions
oc_io_wait_single_req
oc_io_cmp oc_io_wait_single_req(oc_io_req* req);
Send a single I/O request and wait for its completion.
Parameters
reqThe I/O request to send.
Return
The result of the operation.
oc_file_nil
oc_file oc_file_nil();
Returns a nil file handle
oc_file_is_nil
bool oc_file_is_nil(oc_file handle);
Test if a file handle is nil.
Parameters
handleThe handle to test.
Return
true if the handle is nil, and false otherwise.
oc_file_open
oc_file oc_file_open(oc_str8 path, oc_file_access rights, oc_file_open_flags flags);
Open a file in the applications' default directory subtree.
Parameters
pathThe path of the file, relative to the applications' default directory.rightsThe request access rights for the file.flagsFlags controlling various options for the open operation. Seeoc_file_open_flags.
Return
A handle to the opened file.
oc_file_open_at
oc_file oc_file_open_at(oc_file dir, oc_str8 path, oc_file_access rights, oc_file_open_flags flags);
Open a file in a given directory's subtree.
Parameters
dirA directory handle identifying the root of the open operation.pathThe path of the file to open, relative todir.rightsThe request access rights for the file.flagsFlags controlling various options for the open operation. Seeoc_file_open_flags.
Return
A handle to the opened file.
oc_file_close
void oc_file_close(oc_file file);
Close a file.
Parameters
fileThe file handle to close.
oc_file_pos
i64 oc_file_pos(oc_file file);
Get the current position in a file.
Parameters
fileA handle to the file.
Return
The current position in the file.
oc_file_seek
i64 oc_file_seek(oc_file file, i64 offset, oc_file_whence whence);
Set the current position in a file.
Parameters
fileA handle to the file.offsetThe offset at which to move the file position, relative to the base position indicated bywhence.whenceThe base position for the seek operation.
Return
The new position in the file, or -1 if an error occurred.
oc_file_write
u64 oc_file_write(oc_file file, u64 size, char* buffer);
Write data to a file.
Parameters
fileA handle to the file.sizeThe number of bytes to write.bufferThe buffer containing the data to write. It must be at leastsizelong.
Return
The number of bytes written. A short count indicates an error.
oc_file_read
u64 oc_file_read(oc_file file, u64 size, char* buffer);
Read from a file.
Parameters
fileA handle to the file.sizeThe number of bytes to read.bufferThe buffer where to store the read data. It must be capable of holding at leastsizebytes.
Return
The number of bytes read. A short count indicates an error.
oc_file_last_error
oc_io_error oc_file_last_error(oc_file handle);
Get the last error on a file handle.
Parameters
handleA handle to a file.
Return
The last error stored on the file handle.
oc_file_get_status
oc_file_status oc_file_get_status(oc_file file);
oc_file_size
u64 oc_file_size(oc_file file);
oc_file_open_with_request
oc_file oc_file_open_with_request(oc_str8 path, oc_file_access rights, oc_file_open_flags flags);
oc_file_listdir
oc_file_list oc_file_listdir(oc_arena* arena, oc_str8 directory);