Files

400 lines
8.7 KiB
C
Raw Permalink Normal View History

#include "posix.h"
2026-03-16 10:35:43 +00:00
#include <fx/ds/string.h>
#include <fx/io/file.h>
#include <fx/io/path.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
2025-10-19 21:00:26 +01:00
/*** PRIVATE DATA *************************************************************/
2026-03-16 10:35:43 +00:00
struct fx_path_p {
fx_string *p_pathstr;
};
2025-10-19 21:00:26 +01:00
/*** PRIVATE FUNCTIONS ********************************************************/
2026-03-16 10:35:43 +00:00
static fx_path *path_make_absolute(const struct fx_path_p *in)
2025-10-19 21:00:26 +01:00
{
return NULL;
}
2026-03-16 10:35:43 +00:00
static fx_path *path_make_relative(const struct fx_path_p *in)
{
2025-10-19 21:00:26 +01:00
return NULL;
}
2026-03-16 10:35:43 +00:00
static fx_path *path_make_canonical(const struct fx_path_p *in)
2025-10-19 21:00:26 +01:00
{
return NULL;
}
2026-03-16 10:35:43 +00:00
static bool path_is_absolute(const struct fx_path_p *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
const char *s = fx_string_ptr(path->p_pathstr);
2025-10-19 21:00:26 +01:00
return s[0] == '/';
}
2026-03-16 10:35:43 +00:00
static const char *path_ptr(const struct fx_path_p *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
return fx_string_ptr(path->p_pathstr);
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
static enum fx_status path_stat(const struct fx_path_p *path, struct fx_file_info *out)
2025-10-19 21:00:26 +01:00
{
struct stat st;
int err = stat(path_ptr(path), &st);
if (err != 0) {
2026-03-16 10:35:43 +00:00
return fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
}
2025-10-19 21:00:26 +01:00
memset(out, 0x0, sizeof *out);
2026-03-16 10:35:43 +00:00
return fx_file_info_from_stat(&st, out);
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
static bool path_exists(const struct fx_path_p *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
fx_file_info info;
if (!FX_OK(path_stat(path, &info))) {
2025-10-19 21:00:26 +01:00
return false;
}
2025-10-19 21:00:26 +01:00
return true;
}
2026-03-16 10:35:43 +00:00
static bool path_is_file(const struct fx_path_p *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
fx_file_info info;
if (!FX_OK(path_stat(path, &info))) {
2025-10-19 21:00:26 +01:00
return false;
}
2026-03-16 10:35:43 +00:00
return (info.attrib & FX_FILE_ATTRIB_NORMAL) != 0;
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
static bool path_is_directory(const struct fx_path_p *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
fx_file_info info;
if (!FX_OK(path_stat(path, &info))) {
2025-10-19 21:00:26 +01:00
return false;
}
2026-03-16 10:35:43 +00:00
return (info.attrib & FX_FILE_ATTRIB_DIRECTORY) != 0;
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
static void append_path(struct fx_path_p *dest, const struct fx_path_p *src)
2025-10-19 21:00:26 +01:00
{
if (path_is_absolute(src)) {
2026-03-16 10:35:43 +00:00
fx_string_clear(dest->p_pathstr);
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
2025-10-19 21:00:26 +01:00
return;
}
2026-03-16 10:35:43 +00:00
if (fx_string_get_size(dest->p_pathstr, FX_STRLEN_NORMAL) > 0
&& fx_string_back(dest->p_pathstr) != '/'
&& fx_string_front(src->p_pathstr) != '/') {
2025-10-19 21:00:26 +01:00
char s[] = {'/', 0};
2026-03-16 10:35:43 +00:00
fx_string_append_cstr(dest->p_pathstr, s);
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
fx_string_append_s(dest->p_pathstr, src->p_pathstr);
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
static enum fx_status path_unlink(const struct fx_path_p *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
int err = remove(fx_string_ptr(path->p_pathstr));
return err == 0 ? FX_SUCCESS : fx_status_from_errno(errno, FX_ERR_IO_FAILURE);
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
static enum fx_status path_get_directory(
const struct fx_path_p *path, fx_path **out_dir_path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
const char *path_cstr = fx_string_ptr(path_str);
2025-10-19 21:00:26 +01:00
char *sep = strrchr(path_cstr, '/');
if (!sep) {
2026-03-16 10:35:43 +00:00
*out_dir_path = fx_path_create();
return FX_SUCCESS;
2025-10-19 21:00:26 +01:00
}
size_t dir_path_len = (size_t)(sep - path_cstr);
2026-03-16 10:35:43 +00:00
fx_string *dir_path_s = fx_string_substr(path_str, 0, dir_path_len);
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
fx_path *dir_path = fx_path_create_from_cstr(fx_string_ptr(dir_path_s));
fx_string_unref(dir_path_s);
2025-10-19 21:00:26 +01:00
*out_dir_path = dir_path;
2026-03-16 10:35:43 +00:00
return FX_SUCCESS;
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
static enum fx_status path_get_filename(
const struct fx_path_p *path, fx_string *out_name)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
fx_string *path_str = path->p_pathstr;
long len = fx_string_get_size(path_str, FX_STRLEN_NORMAL);
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
char *sep = strrchr(fx_string_ptr(path_str), '/');
2025-10-19 21:00:26 +01:00
if (!sep) {
2026-03-16 10:35:43 +00:00
fx_string_append_s(out_name, path_str);
return FX_SUCCESS;
2025-10-19 21:00:26 +01:00
}
const char *filename = sep;
while (*filename == '/' && *filename != '\0') {
filename++;
}
if (*filename == '\0') {
2026-03-16 10:35:43 +00:00
fx_string_clear(out_name);
return FX_SUCCESS;
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
fx_string_append_cstr(out_name, filename);
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
return FX_SUCCESS;
}
2026-03-16 10:35:43 +00:00
static size_t path_length(const struct fx_path_p *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
return fx_string_get_size(path->p_pathstr, FX_STRLEN_NORMAL);
2025-10-19 21:00:26 +01:00
}
/*** PUBLIC FUNCTIONS *********************************************************/
2026-03-16 10:35:43 +00:00
fx_path *fx_path_create_root()
{
const char *system_drive = "/";
size_t system_drive_len = strlen(system_drive);
2026-03-16 10:35:43 +00:00
fx_path *path = fx_path_create();
if (!path) {
return NULL;
}
2026-03-16 10:35:43 +00:00
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
fx_string_append_cstr(p->p_pathstr, system_drive);
if (system_drive[system_drive_len - 1] != '\\') {
2026-03-16 10:35:43 +00:00
fx_string_append_c(p->p_pathstr, '\\');
}
return path;
}
2026-03-16 10:35:43 +00:00
fx_path *fx_path_create_cwd()
{
const long buf_len = 2048;
char *buf = malloc(buf_len);
if (!buf) {
return NULL;
}
if (!getcwd(buf, buf_len)) {
free(buf);
return NULL;
}
2026-03-16 10:35:43 +00:00
fx_path *path = fx_path_create();
if (!path) {
free(buf);
return NULL;
}
2026-03-16 10:35:43 +00:00
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
fx_string_append_cstr(p->p_pathstr, buf);
free(buf);
return path;
}
2026-03-16 10:35:43 +00:00
fx_path *fx_path_create_from_cstr(const char *cstr)
{
2026-03-16 10:35:43 +00:00
fx_path *path = fx_path_create();
if (!path) {
return NULL;
}
2026-03-16 10:35:43 +00:00
struct fx_path_p *p = fx_object_get_private(path, FX_TYPE_PATH);
2025-10-19 21:00:26 +01:00
char prev = 0;
for (size_t i = 0; cstr[i]; i++) {
char c = cstr[i];
if (c == '\\') {
c = '/';
}
if (prev == '/' && c == '/') {
continue;
}
2026-03-16 10:35:43 +00:00
fx_string_append_c(p->p_pathstr, c);
prev = c;
}
2026-03-16 10:35:43 +00:00
while (fx_string_back(p->p_pathstr) == '/') {
fx_string_pop_back(p->p_pathstr);
}
return path;
}
2026-03-16 10:35:43 +00:00
fx_path *fx_path_duplicate(const fx_path *path)
2025-07-28 22:18:52 +01:00
{
2026-03-16 10:35:43 +00:00
fx_path *new_path = fx_path_create();
2025-07-28 22:18:52 +01:00
if (!path) {
return NULL;
}
2026-03-16 10:35:43 +00:00
struct fx_path_p *old_p = fx_object_get_private(path, FX_TYPE_PATH);
struct fx_path_p *new_p = fx_object_get_private(new_path, FX_TYPE_PATH);
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
new_p->p_pathstr = fx_string_duplicate(old_p->p_pathstr);
2025-10-19 21:00:26 +01:00
if (!new_p->p_pathstr) {
2026-03-16 10:35:43 +00:00
fx_path_unref(new_path);
2025-07-28 22:18:52 +01:00
return NULL;
}
return new_path;
}
2026-03-16 10:35:43 +00:00
fx_path *fx_path_join(const fx_path *paths[], size_t nr_paths)
{
2026-03-16 10:35:43 +00:00
fx_path *result = fx_path_create();
if (!result) {
return NULL;
}
2026-03-16 10:35:43 +00:00
struct fx_path_p *result_p = fx_object_get_private(result, FX_TYPE_PATH);
2025-10-19 21:00:26 +01:00
for (size_t i = 0; i < nr_paths; i++) {
if (paths[i]) {
2026-03-16 10:35:43 +00:00
struct fx_path_p *path_p
= fx_object_get_private(paths[i], FX_TYPE_PATH);
2025-10-19 21:00:26 +01:00
append_path(result_p, path_p);
}
}
return result;
}
2026-03-16 10:35:43 +00:00
fx_path *fx_path_make_absolute(const fx_path *in)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_absolute, in);
}
2026-03-16 10:35:43 +00:00
fx_path *fx_path_make_relative(const fx_path *in)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_relative, in);
}
2026-03-16 10:35:43 +00:00
fx_path *fx_path_make_canonical(const fx_path *in)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_make_canonical, in);
}
2026-03-16 10:35:43 +00:00
bool fx_path_is_absolute(const fx_path *path)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_absolute, path);
}
2026-03-16 10:35:43 +00:00
bool fx_path_exists(const fx_path *path)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_exists, path);
}
2026-03-16 10:35:43 +00:00
bool fx_path_is_file(const fx_path *path)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_file, path);
}
2026-03-16 10:35:43 +00:00
bool fx_path_is_directory(const fx_path *path)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_is_directory, path);
}
2026-03-16 10:35:43 +00:00
enum fx_status fx_path_stat(const fx_path *path, struct fx_file_info *out)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_stat, path, out);
}
2026-03-16 10:35:43 +00:00
enum fx_status fx_path_unlink(const fx_path *path)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_unlink, path);
}
2026-03-16 10:35:43 +00:00
enum fx_status fx_path_get_directory(const fx_path *path, fx_path **out_dir_path)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_directory, path, out_dir_path);
}
2026-03-16 10:35:43 +00:00
enum fx_status fx_path_get_filename(const fx_path *path, fx_string *out_name)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC(FX_TYPE_PATH, path_get_filename, path, out_name);
2025-10-19 21:00:26 +01:00
}
2026-03-16 10:35:43 +00:00
const char *fx_path_ptr(const fx_path *path)
2025-10-19 21:00:26 +01:00
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_ptr, path);
}
2026-03-16 10:35:43 +00:00
size_t fx_path_length(const fx_path *path)
{
2026-03-16 10:35:43 +00:00
FX_CLASS_DISPATCH_STATIC_0(FX_TYPE_PATH, path_length, path);
}
2025-10-19 21:00:26 +01:00
/*** VIRTUAL FUNCTIONS ********************************************************/
2026-03-16 10:35:43 +00:00
static void path_init(fx_object *obj, void *priv)
{
2026-03-16 10:35:43 +00:00
struct fx_path_p *path = priv;
2025-10-19 21:00:26 +01:00
2026-03-16 10:35:43 +00:00
path->p_pathstr = fx_string_create();
2025-10-19 21:00:26 +01:00
if (!path->p_pathstr) {
/* TODO return error */
}
}
2026-03-16 10:35:43 +00:00
void path_fini(fx_object *obj, void *priv)
{
2026-03-16 10:35:43 +00:00
struct fx_path_p *path = priv;
2026-03-16 10:35:43 +00:00
fx_string_unref(path->p_pathstr);
}
2026-03-16 10:35:43 +00:00
void path_to_string(const fx_object *obj, fx_stream *out)
{
2026-03-16 10:35:43 +00:00
struct fx_path_p *path = fx_object_get_private(obj, FX_TYPE_PATH);
2026-03-16 10:35:43 +00:00
fx_stream_write_string(out, fx_string_ptr(path->p_pathstr), NULL);
}
2025-10-19 21:00:26 +01:00
/*** CLASS DEFINITION *********************************************************/
2026-03-16 10:35:43 +00:00
FX_TYPE_CLASS_DEFINITION_BEGIN(fx_path)
FX_TYPE_CLASS_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT)
FX_INTERFACE_ENTRY(to_string) = path_to_string;
FX_TYPE_CLASS_INTERFACE_END(fx_object, FX_TYPE_OBJECT)
FX_TYPE_CLASS_DEFINITION_END(fx_path)
FX_TYPE_DEFINITION_BEGIN(fx_path)
FX_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
FX_TYPE_CLASS(fx_path_class);
FX_TYPE_INSTANCE_PRIVATE(struct fx_path_p);
FX_TYPE_INSTANCE_INIT(path_init);
FX_TYPE_INSTANCE_FINI(path_fini);
FX_TYPE_DEFINITION_END(fx_path)