|
solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
|
Cross-platform file handling API with synchronous and asynchronous I/O support. More...
#include "platform.h"#include <stdbool.h>#include <stddef.h>#include <stdint.h>#include <stdio.h>#include <fcntl.h>#include <sys/mman.h>#include <sys/stat.h>#include <unistd.h>Go to the source code of this file.
Classes | |
| struct | FileAttributes |
| struct | file_t |
Typedefs | |
| typedef enum FileAttrFlags | FileAttrFlags |
| typedef struct FileAttributes | FileAttributes |
Enumerations | |
| enum | FileAttrFlags { FATTR_NONE = 0 , FATTR_FILE = 1 << 0 , FATTR_DIR = 1 << 1 , FATTR_SYMLINK = 1 << 2 , FATTR_CHARDEV = 1 << 3 , FATTR_BLOCKDEV = 1 << 4 , FATTR_FIFO = 1 << 5 , FATTR_SOCKET = 1 << 6 , FATTR_HIDDEN = 1 << 7 , FATTR_EXECUTABLE = 1 << 8 } |
| enum | file_result_t { FILE_SUCCESS = 0 , FILE_ERROR_INVALID_ARGS , FILE_ERROR_OPEN_FAILED , FILE_ERROR_IO_FAILED , FILE_ERROR_LOCK_FAILED , FILE_ERROR_MEMORY_FAILED , FILE_ERROR_SYSTEM_ERROR } |
Functions | |
| int | populate_file_attrs (const char *path, FileAttributes *attr) |
| file_result_t | file_open (file_t *file, const char *filename, const char *mode) |
| void | file_close (file_t *file) |
| file_result_t | file_truncate (file_t *file, int64_t length) |
| file_result_t | filesize_tostring (uint64_t size, char *buf, size_t len) |
| size_t | file_read (const file_t *file, void *buffer, size_t size, size_t count) |
| size_t | file_write (file_t *file, const void *buffer, size_t size, size_t count) |
| size_t | file_write_string (file_t *file, const char *str) |
| ssize_t | file_pread (const file_t *file, void *buffer, size_t size, int64_t offset) |
| ssize_t | file_pwrite (file_t *file, const void *buffer, size_t size, int64_t offset) |
| void * | file_readall (file_t *file, size_t *size_out) |
| file_result_t | file_lock (const file_t *file) |
| file_result_t | file_unlock (const file_t *file) |
| file_result_t | file_copy (const file_t *src, file_t *dst) |
| void * | file_mmap (const file_t *file, size_t length, bool read_access, bool write_access) |
| file_result_t | file_munmap (void *addr, size_t length) |
| file_result_t | file_flush (file_t *file) |
| int64_t | file_tell (const file_t *file) |
| file_result_t | file_seek (file_t *file, int64_t offset, int whence) |
Cross-platform file handling API with synchronous and asynchronous I/O support.
Definition in file file.h.
| typedef enum FileAttrFlags FileAttrFlags |
File attribute flags bitmask
| typedef struct FileAttributes FileAttributes |
Represents file attributes for directory traversal
| enum file_result_t |
File operation result codes.
| enum FileAttrFlags |
File attribute flags bitmask
| void file_close | ( | file_t * | file | ) |
Closes the file and releases all associated resources. Safe to call multiple times or on uninitialized structures.
| file | Pointer to the file_t structure. |
Definition at line 234 of file file.c.
References file_t::native_handle, and file_t::stream.
| file_result_t file_copy | ( | const file_t * | src, |
| file_t * | dst | ||
| ) |
Copies content from source file to destination file. Reads from current position of source until EOF. Flushes destination after copying.
| src | Source file (must be open for reading). |
| dst | Destination file (must be open for writing). |
Definition at line 471 of file file.c.
References FILE_ERROR_IO_FAILED, file_read(), FILE_SUCCESS, file_write(), and file_t::stream.
| file_result_t file_flush | ( | file_t * | file | ) |
Flushes any buffered data to the underlying file.
| file | Pointer to the opened file_t structure. |
Definition at line 544 of file file.c.
References FILE_ERROR_IO_FAILED, FILE_SUCCESS, and file_t::stream.
| file_result_t file_lock | ( | const file_t * | file | ) |
Attempts to acquire an exclusive advisory lock on the entire file. The lock is non-blocking and will fail immediately if unavailable.
| file | Pointer to the opened file_t structure. |
Definition at line 424 of file file.c.
References FILE_ERROR_LOCK_FAILED, FILE_ERROR_SYSTEM_ERROR, FILE_SUCCESS, and file_t::native_handle.
| void * file_mmap | ( | const file_t * | file, |
| size_t | length, | ||
| bool | read_access, | ||
| bool | write_access | ||
| ) |
Memory maps a portion of the file starting from the beginning.
| file | Pointer to the opened file_t structure. |
| length | Number of bytes to map. |
| read_access | Request read access to the mapping. |
| write_access | Request write access to the mapping. |
Definition at line 498 of file file.c.
References file_t::native_handle.
| file_result_t file_munmap | ( | void * | addr, |
| size_t | length | ||
| ) |
Unmaps a previously memory-mapped region.
| addr | Pointer to the mapped region (from file_mmap). |
| length | Length of the mapping (must match file_mmap parameter). |
Definition at line 531 of file file.c.
References FILE_ERROR_INVALID_ARGS, FILE_ERROR_SYSTEM_ERROR, and FILE_SUCCESS.
| file_result_t file_open | ( | file_t * | file, |
| const char * | filename, | ||
| const char * | mode | ||
| ) |
Opens a file and populates the file_t structure.
| file | Pointer to file_t structure to initialize. |
| filename | Path to the file to open. |
| mode | Mode string (e.g., "r", "wb+", "a"). See fopen documentation. |
Definition at line 198 of file file.c.
References FILE_ERROR_INVALID_ARGS, FILE_ERROR_OPEN_FAILED, FILE_SUCCESS, file_t::native_handle, populate_file_attrs(), and file_t::stream.
| ssize_t file_pread | ( | const file_t * | file, |
| void * | buffer, | ||
| size_t | size, | ||
| int64_t | offset | ||
| ) |
Performs positioned read without affecting the file pointer. This is atomic on POSIX systems and simulated on Windows.
| file | Pointer to the opened file_t structure. |
| buffer | Buffer to store read data. |
| size | Number of bytes to read. |
| offset | File offset to read from. |
Definition at line 323 of file file.c.
References file_t::native_handle.
| ssize_t file_pwrite | ( | file_t * | file, |
| const void * | buffer, | ||
| size_t | size, | ||
| int64_t | offset | ||
| ) |
Performs positioned write without affecting the file pointer. This is atomic on POSIX systems and simulated on Windows.
| file | Pointer to the opened file_t structure. |
| buffer | Data to write. |
| size | Number of bytes to write. |
| offset | File offset to write to. |
Definition at line 347 of file file.c.
References file_t::native_handle.
| size_t file_read | ( | const file_t * | file, |
| void * | buffer, | ||
| size_t | size, | ||
| size_t | count | ||
| ) |
Reads data from the file using buffered I/O.
| file | Pointer to the opened file_t structure. |
| buffer | Buffer to store read data. |
| size | Size of each element. |
| count | Number of elements to read. |
Definition at line 301 of file file.c.
References file_t::stream.
Referenced by file_copy(), and file_readall().
| void * file_readall | ( | file_t * | file, |
| size_t * | size_out | ||
| ) |
Reads the entire file content into a newly allocated buffer. The file position is reset to the beginning before reading.
| file | Pointer to the opened file_t structure. |
| size_out | Optional pointer to store the file size read. |
Definition at line 367 of file file.c.
References file_read(), file_tell(), FileAttributes::size, and file_t::stream.
| file_result_t file_seek | ( | file_t * | file, |
| int64_t | offset, | ||
| int | whence | ||
| ) |
Sets the file position.
| file | Pointer to the opened file_t structure. |
| offset | Offset relative to whence. |
| whence | Position reference (SEEK_SET, SEEK_CUR, SEEK_END). |
Definition at line 561 of file file.c.
References FILE_ERROR_INVALID_ARGS, FILE_ERROR_IO_FAILED, FILE_SUCCESS, file_t::native_handle, and file_t::stream.
| int64_t file_tell | ( | const file_t * | file | ) |
Gets the current file position.
| file | Pointer to the opened file_t structure. |
Definition at line 546 of file file.c.
References file_t::native_handle.
Referenced by file_readall().
| file_result_t file_truncate | ( | file_t * | file, |
| int64_t | length | ||
| ) |
Truncates or extends the file to the specified length. File must be opened with write permissions.
| file | Pointer to the opened file_t structure. |
| length | Desired file length in bytes. |
Definition at line 242 of file file.c.
References FILE_ERROR_INVALID_ARGS, FILE_ERROR_IO_FAILED, FILE_SUCCESS, file_t::native_handle, and file_t::stream.
| file_result_t file_unlock | ( | const file_t * | file | ) |
Releases an advisory lock on the file. Safe to call on files that are not locked.
| file | Pointer to the opened file_t structure. |
Definition at line 453 of file file.c.
References FILE_ERROR_SYSTEM_ERROR, FILE_SUCCESS, and file_t::native_handle.
| size_t file_write | ( | file_t * | file, |
| const void * | buffer, | ||
| size_t | size, | ||
| size_t | count | ||
| ) |
Writes data to the file using buffered I/O. Does not automatically flush the stream.
| file | Pointer to the opened file_t structure. |
| buffer | Data to write. |
| size | Size of each element. |
| count | Number of elements to write. |
Definition at line 308 of file file.c.
References file_t::stream.
Referenced by file_copy().
| size_t file_write_string | ( | file_t * | file, |
| const char * | str | ||
| ) |
Writes a null-terminated string to the file (excluding null terminator).
| file | Pointer to the opened file_t structure. |
| str | Null-terminated string to write. |
Definition at line 315 of file file.c.
References file_t::stream.
| file_result_t filesize_tostring | ( | uint64_t | size, |
| char * | buf, | ||
| size_t | len | ||
| ) |
Converts a file size to a human-readable string representation.
| size | File size in bytes. |
| buf | Buffer to store the formatted string. |
| len | Size of the buffer. |
Definition at line 268 of file file.c.
References FILE_ERROR_INVALID_ARGS, and FILE_SUCCESS.
| int populate_file_attrs | ( | const char * | path, |
| FileAttributes * | attr | ||
| ) |
Populates FileAttributes structure from a file path (POSIX implementation).
| path | Full path to the file. |
| name | Basename of the file. |
| attr | Output FileAttributes structure to populate. |
Cross-platform file information retrieval. On Unix: uses lstat to detect symlinks without following them. On Windows: uses GetFileAttributesEx for basic info.
Definition at line 124 of file file.c.
References FileAttributes::attrs, FATTR_BLOCKDEV, FATTR_CHARDEV, FATTR_DIR, FATTR_EXECUTABLE, FATTR_FIFO, FATTR_FILE, FATTR_HIDDEN, FATTR_NONE, FATTR_SOCKET, FATTR_SYMLINK, populate_file_attrs(), and FileAttributes::size.
Referenced by dir_walk(), dir_walk_depth_first(), file_open(), and populate_file_attrs().