ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
filesystem.h
Go to the documentation of this file.
1#pragma once
2
3#include <libplatform/filesystem.h>
4
5#if 0
6
47
48#include <stddef.h>
49#include <stdbool.h>
50#include <sys/types.h>
51#include "../asciichat_errno.h"
52
53/* ============================================================================
54 * Cross-platform I/O headers
55 * ============================================================================
56 * Consolidates file descriptor and I/O operations across platforms.
57 */
58#ifdef _WIN32
59#include <io.h> /* Windows I/O functions */
60#include <fcntl.h> /* File control options */
61#else
62#include <unistd.h> /* POSIX standard I/O and file descriptor functions */
63#include <fcntl.h> /* POSIX file control options */
64#endif
65
66#ifdef __cplusplus
67extern "C" {
68#endif
69
70// ============================================================================
71// File Mode Constants (for consistency with fcntl.h)
72// ============================================================================
73
74#ifdef _WIN32
75#include <fcntl.h>
76#define PLATFORM_O_RDONLY _O_RDONLY
77#define PLATFORM_O_WRONLY _O_WRONLY
78#define PLATFORM_O_RDWR _O_RDWR
79#define PLATFORM_O_CREAT _O_CREAT
80#define PLATFORM_O_EXCL _O_EXCL
81#define PLATFORM_O_TRUNC _O_TRUNC
82#define PLATFORM_O_APPEND _O_APPEND
83#define PLATFORM_O_BINARY _O_BINARY
84#else
85#include <fcntl.h>
86#define PLATFORM_O_RDONLY O_RDONLY
87#define PLATFORM_O_WRONLY O_WRONLY
88#define PLATFORM_O_RDWR O_RDWR
89#define PLATFORM_O_CREAT O_CREAT
90#define PLATFORM_O_EXCL O_EXCL
91#define PLATFORM_O_TRUNC O_TRUNC
92#define PLATFORM_O_APPEND O_APPEND
93#define PLATFORM_O_BINARY 0 // Not needed on POSIX
94#endif
95
96// ============================================================================
97// Path Separator and Length Constants
98// ============================================================================
99
118#ifdef _WIN32
119#define PATH_DELIM '\\'
120#define PATH_SEPARATOR_STR "\\"
121#else
122#define PATH_DELIM '/'
123#define PATH_SEPARATOR_STR "/"
124#endif
125
134#ifdef _WIN32
135#define PATH_ENV_SEPARATOR ";"
136#else
137#define PATH_ENV_SEPARATOR ":"
138#endif
139
153#ifdef _WIN32
154#define PLATFORM_MAX_PATH_LENGTH 32767
155#elif defined(__linux__)
156#ifndef PATH_MAX
157#define PLATFORM_MAX_PATH_LENGTH 4096
158#else
159#define PLATFORM_MAX_PATH_LENGTH PATH_MAX
160#endif
161#elif defined(__APPLE__)
162#ifndef PATH_MAX
163#define PLATFORM_MAX_PATH_LENGTH 1024
164#else
165#define PLATFORM_MAX_PATH_LENGTH PATH_MAX
166#endif
167#else
168#define PLATFORM_MAX_PATH_LENGTH 4096
169#endif
170
181#define PLATFORM_MAX_ENV_VALUE_LENGTH 32768
182
183// ============================================================================
184// File Permission Constants
185// ============================================================================
186
197#define FILE_PERM_PRIVATE 0600
198
209#define DIR_PERM_PRIVATE 0700
210
219#define FILE_PERM_PUBLIC_READ 0644
220
229#define FILE_PERM_MASK 0777
230
231// ============================================================================
232// File Statistics
233// ============================================================================
234
242typedef struct {
243 size_t size;
244 int mode;
245 int is_regular_file;
246 int is_directory;
247 int is_symlink;
248} platform_stat_t;
249
256typedef struct {
257 char *path;
258 int mode;
259} platform_directory_candidate_t;
260
261typedef struct {
262 platform_directory_candidate_t *candidates;
263 size_t count;
264} platform_directory_list_t;
265
294asciichat_error_t platform_stat(const char *path, platform_stat_t *stat_out);
295
311int platform_is_regular_file(const char *path);
312
328int platform_is_directory(const char *path);
329
330// ============================================================================
331// Directory Management
332// ============================================================================
333
361asciichat_error_t platform_mkdir(const char *path, int mode);
362
390asciichat_error_t platform_mkdir_recursive(const char *path, int mode);
391
410asciichat_error_t platform_mkdtemp(char *path_out, size_t path_size, const char *prefix);
411
429asciichat_error_t platform_rmdir_recursive(const char *path);
430
435typedef struct {
436 const char *name;
437 bool is_dir;
438} platform_dir_entry_t;
439
446typedef bool (*platform_dir_foreach_cb)(const platform_dir_entry_t *entry, void *user_data);
447
456asciichat_error_t platform_dir_foreach(const char *path, platform_dir_foreach_cb callback, void *user_data);
457
458// ============================================================================
459// Temporary Files
460// ============================================================================
461
478int platform_create_temp_file(char *path_out, size_t path_size, const char *prefix, int *fd);
479
488int platform_delete_temp_file(const char *path);
489
510asciichat_error_t platform_temp_file_open(const char *name, const char *path, int *fd_out);
511
512// ============================================================================
513// File Truncation
514// ============================================================================
515
534asciichat_error_t platform_truncate_file(const char *path, size_t size);
535
536// ============================================================================
537// Path Utilities
538// ============================================================================
539
552bool platform_path_is_absolute(const char *path);
553
561char platform_path_get_separator(void);
562
575asciichat_error_t platform_path_normalize(const char *input, char *output, size_t output_size);
576
577// ============================================================================
578// Key File Security
579// ============================================================================
580
596asciichat_error_t platform_validate_key_file_permissions(const char *key_path);
597
598// ============================================================================
599// Config File Search
600// ============================================================================
601
608typedef struct {
609 char *path;
610 uint8_t priority;
611 bool exists;
612 bool is_system_config;
613} config_file_result_t;
614
621typedef struct {
622 config_file_result_t *files;
623 size_t count;
624 size_t capacity;
625} config_file_list_t;
626
683asciichat_error_t platform_find_config_file(const char *filename, config_file_list_t *list_out);
684
699void config_file_list_destroy(config_file_list_t *list);
700
701// ============================================================================
702// Home and Config Directory Discovery
703// ============================================================================
704
719const char *platform_get_home_dir(void);
720
747char *platform_get_config_dir(void);
748
774char *platform_get_data_dir(void);
775
796asciichat_error_t platform_get_data_dir_candidates(const char *install_prefix, platform_directory_list_t *list_out);
797
803void platform_directory_list_destroy(platform_directory_list_t *list);
804
805// ============================================================================
806// File I/O Operations
807// ============================================================================
808
817int platform_open(const char *name, const char *pathname, int flags, ...);
818
826FILE *platform_fopen(const char *name, const char *filename, const char *mode);
827
832FILE *platform_tmpfile(void);
833
841FILE *platform_fdopen(const char *name, int fd, const char *mode);
842
850ssize_t platform_read(int fd, void *buf, size_t count);
851
859ssize_t platform_write(int fd, const void *buf, size_t count);
860
866int platform_close(int fd);
867
873int platform_unlink(const char *pathname);
874
881int platform_chmod(const char *pathname, int mode);
882
883// ============================================================================
884// Platform Path Utilities
885// ============================================================================
886
904const char *platform_path_skip_absolute_prefix(const char *path);
905
922void platform_normalize_path_separators(char *path);
923
940int platform_path_strcasecmp(const char *a, const char *b, size_t n);
941
956const char *file_read_error_message(const char *path);
957
974const char *file_write_error_message(const char *path);
975
986bool file_is_readable(const char *path);
987
999bool file_is_writable(const char *path);
1000
1001/* ============================================================================
1002 * Path Utilities
1003 * ============================================================================ */
1004
1033bool platform_get_executable_path(char *exe_path, size_t path_size);
1034
1066bool platform_get_temp_dir(char *temp_dir, size_t path_size);
1067
1078bool platform_get_cwd(char *cwd, size_t path_size);
1079
1085#define PLATFORM_ACCESS_EXISTS 0
1086#define PLATFORM_ACCESS_WRITE 2
1087#define PLATFORM_ACCESS_READ 4
1088
1116int platform_access(const char *path, int mode);
1117
1145bool platform_is_binary_in_path(const char *bin_name);
1146
1158void platform_cleanup_binary_path_cache(void);
1159
1169int platform_fsync(int fd);
1170
1171/* ============================================================================
1172 * Platform-Specific Internal Functions (not part of public API)
1173 * ============================================================================ */
1174
1184bool check_binary_in_path_uncached(const char *bin_name);
1185
1186#ifdef __cplusplus
1187}
1188#endif
1189
1191#endif
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
unsigned char uint8_t
Definition common.h:60
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
ssize_t platform_write(int fd, const void *buf, size_t count)
Platform-safe write function.
Capture stdout/stderr and redirect to logging system.