400 lines
8.4 KiB
C
400 lines
8.4 KiB
C
#include "posix.h"
|
|
|
|
#include <blue/ds/string.h>
|
|
#include <blue/io/file.h>
|
|
#include <blue/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>
|
|
|
|
/*** PRIVATE DATA *************************************************************/
|
|
|
|
struct b_path_p {
|
|
b_string *p_pathstr;
|
|
};
|
|
|
|
/*** PRIVATE FUNCTIONS ********************************************************/
|
|
|
|
static b_path *path_make_absolute(const struct b_path_p *in)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
static b_path *path_make_relative(const struct b_path_p *in)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
static b_path *path_make_canonical(const struct b_path_p *in)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
static bool path_is_absolute(const struct b_path_p *path)
|
|
{
|
|
const char *s = b_string_ptr(path->p_pathstr);
|
|
return s[0] == '/';
|
|
}
|
|
|
|
static const char *path_ptr(const struct b_path_p *path)
|
|
{
|
|
return b_string_ptr(path->p_pathstr);
|
|
}
|
|
|
|
static enum b_status path_stat(const struct b_path_p *path, struct b_file_info *out)
|
|
{
|
|
struct stat st;
|
|
int err = stat(path_ptr(path), &st);
|
|
|
|
if (err != 0) {
|
|
return b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
|
}
|
|
|
|
memset(out, 0x0, sizeof *out);
|
|
|
|
return b_file_info_from_stat(&st, out);
|
|
}
|
|
|
|
static bool path_exists(const struct b_path_p *path)
|
|
{
|
|
b_file_info info;
|
|
if (!B_OK(path_stat(path, &info))) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
static bool path_is_file(const struct b_path_p *path)
|
|
{
|
|
b_file_info info;
|
|
if (!B_OK(path_stat(path, &info))) {
|
|
return false;
|
|
}
|
|
|
|
return (info.attrib & B_FILE_ATTRIB_NORMAL) != 0;
|
|
}
|
|
|
|
static bool path_is_directory(const struct b_path_p *path)
|
|
{
|
|
b_file_info info;
|
|
if (!B_OK(path_stat(path, &info))) {
|
|
return false;
|
|
}
|
|
|
|
return (info.attrib & B_FILE_ATTRIB_DIRECTORY) != 0;
|
|
}
|
|
|
|
static void append_path(struct b_path_p *dest, const struct b_path_p *src)
|
|
{
|
|
if (path_is_absolute(src)) {
|
|
b_string_clear(dest->p_pathstr);
|
|
b_string_append_s(dest->p_pathstr, src->p_pathstr);
|
|
return;
|
|
}
|
|
|
|
if (b_string_get_size(dest->p_pathstr, B_STRLEN_NORMAL) > 0
|
|
&& b_string_back(dest->p_pathstr) != '/'
|
|
&& b_string_front(src->p_pathstr) != '/') {
|
|
char s[] = {'/', 0};
|
|
b_string_append_cstr(dest->p_pathstr, s);
|
|
}
|
|
|
|
b_string_append_s(dest->p_pathstr, src->p_pathstr);
|
|
}
|
|
|
|
static enum b_status path_unlink(const struct b_path_p *path)
|
|
{
|
|
int err = remove(b_string_ptr(path->p_pathstr));
|
|
return err == 0 ? B_SUCCESS : b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
|
}
|
|
|
|
static enum b_status path_get_directory(
|
|
const struct b_path_p *path, b_path **out_dir_path)
|
|
{
|
|
b_string *path_str = path->p_pathstr;
|
|
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
|
|
|
|
const char *path_cstr = b_string_ptr(path_str);
|
|
char *sep = strrchr(path_cstr, '/');
|
|
|
|
if (!sep) {
|
|
*out_dir_path = b_path_create();
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
size_t dir_path_len = (size_t)(sep - path_cstr);
|
|
b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len);
|
|
|
|
b_path *dir_path = b_path_create_from_cstr(b_string_ptr(dir_path_s));
|
|
b_string_unref(dir_path_s);
|
|
|
|
*out_dir_path = dir_path;
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static enum b_status path_get_filename(
|
|
const struct b_path_p *path, b_string *out_name)
|
|
{
|
|
b_string *path_str = path->p_pathstr;
|
|
long len = b_string_get_size(path_str, B_STRLEN_NORMAL);
|
|
|
|
char *sep = strrchr(b_string_ptr(path_str), '/');
|
|
|
|
if (!sep) {
|
|
b_string_append_s(out_name, path_str);
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
const char *filename = sep;
|
|
while (*filename == '/' && *filename != '\0') {
|
|
filename++;
|
|
}
|
|
|
|
if (*filename == '\0') {
|
|
b_string_clear(out_name);
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
b_string_append_cstr(out_name, filename);
|
|
|
|
return B_SUCCESS;
|
|
}
|
|
|
|
static size_t path_length(const struct b_path_p *path)
|
|
{
|
|
return b_string_get_size(path->p_pathstr, B_STRLEN_NORMAL);
|
|
}
|
|
|
|
/*** PUBLIC FUNCTIONS *********************************************************/
|
|
|
|
b_path *b_path_create_root()
|
|
{
|
|
const char *system_drive = "/";
|
|
size_t system_drive_len = strlen(system_drive);
|
|
|
|
b_path *path = b_path_create();
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH);
|
|
|
|
b_string_append_cstr(p->p_pathstr, system_drive);
|
|
if (system_drive[system_drive_len - 1] != '\\') {
|
|
b_string_append_c(p->p_pathstr, '\\');
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
b_path *b_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;
|
|
}
|
|
|
|
b_path *path = b_path_create();
|
|
if (!path) {
|
|
free(buf);
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH);
|
|
|
|
b_string_append_cstr(p->p_pathstr, buf);
|
|
free(buf);
|
|
|
|
return path;
|
|
}
|
|
|
|
b_path *b_path_create_from_cstr(const char *cstr)
|
|
{
|
|
b_path *path = b_path_create();
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path_p *p = b_object_get_private(path, B_TYPE_PATH);
|
|
|
|
char prev = 0;
|
|
|
|
for (size_t i = 0; cstr[i]; i++) {
|
|
char c = cstr[i];
|
|
if (c == '\\') {
|
|
c = '/';
|
|
}
|
|
|
|
if (prev == '/' && c == '/') {
|
|
continue;
|
|
}
|
|
|
|
b_string_append_c(p->p_pathstr, c);
|
|
prev = c;
|
|
}
|
|
|
|
while (b_string_back(p->p_pathstr) == '/') {
|
|
b_string_pop_back(p->p_pathstr);
|
|
}
|
|
|
|
return path;
|
|
}
|
|
|
|
b_path *b_path_duplicate(const b_path *path)
|
|
{
|
|
b_path *new_path = b_path_create();
|
|
if (!path) {
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path_p *old_p = b_object_get_private(path, B_TYPE_PATH);
|
|
struct b_path_p *new_p = b_object_get_private(new_path, B_TYPE_PATH);
|
|
|
|
new_p->p_pathstr = b_string_duplicate(old_p->p_pathstr);
|
|
if (!new_p->p_pathstr) {
|
|
b_path_unref(new_path);
|
|
return NULL;
|
|
}
|
|
|
|
return new_path;
|
|
}
|
|
|
|
b_path *b_path_join(const b_path *paths[], size_t nr_paths)
|
|
{
|
|
b_path *result = b_path_create();
|
|
if (!result) {
|
|
return NULL;
|
|
}
|
|
|
|
struct b_path_p *result_p = b_object_get_private(result, B_TYPE_PATH);
|
|
|
|
for (size_t i = 0; i < nr_paths; i++) {
|
|
if (paths[i]) {
|
|
struct b_path_p *path_p
|
|
= b_object_get_private(paths[i], B_TYPE_PATH);
|
|
append_path(result_p, path_p);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
b_path *b_path_make_absolute(const b_path *in)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_absolute, in);
|
|
}
|
|
|
|
b_path *b_path_make_relative(const b_path *in)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_relative, in);
|
|
}
|
|
|
|
b_path *b_path_make_canonical(const b_path *in)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_make_canonical, in);
|
|
}
|
|
|
|
bool b_path_is_absolute(const b_path *path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_absolute, path);
|
|
}
|
|
|
|
bool b_path_exists(const b_path *path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_exists, path);
|
|
}
|
|
|
|
bool b_path_is_file(const b_path *path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_file, path);
|
|
}
|
|
|
|
bool b_path_is_directory(const b_path *path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_is_directory, path);
|
|
}
|
|
|
|
enum b_status b_path_stat(const b_path *path, struct b_file_info *out)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_stat, path, out);
|
|
}
|
|
|
|
enum b_status b_path_unlink(const b_path *path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_unlink, path);
|
|
}
|
|
|
|
enum b_status b_path_get_directory(const b_path *path, b_path **out_dir_path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_directory, path, out_dir_path);
|
|
}
|
|
|
|
enum b_status b_path_get_filename(const b_path *path, b_string *out_name)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC(B_TYPE_PATH, path_get_filename, path, out_name);
|
|
}
|
|
|
|
const char *b_path_ptr(const b_path *path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_ptr, path);
|
|
}
|
|
|
|
size_t b_path_length(const b_path *path)
|
|
{
|
|
B_CLASS_DISPATCH_STATIC_0(B_TYPE_PATH, path_length, path);
|
|
}
|
|
|
|
/*** VIRTUAL FUNCTIONS ********************************************************/
|
|
|
|
static void path_init(b_object *obj, void *priv)
|
|
{
|
|
struct b_path_p *path = priv;
|
|
|
|
path->p_pathstr = b_string_create();
|
|
if (!path->p_pathstr) {
|
|
/* TODO return error */
|
|
}
|
|
}
|
|
|
|
void path_fini(b_object *obj, void *priv)
|
|
{
|
|
struct b_path_p *path = priv;
|
|
|
|
b_string_unref(path->p_pathstr);
|
|
}
|
|
|
|
void path_to_string(const b_object *obj, b_stream *out)
|
|
{
|
|
struct b_path_p *path = b_object_get_private(obj, B_TYPE_PATH);
|
|
|
|
b_stream_write_string(out, b_string_ptr(path->p_pathstr), NULL);
|
|
}
|
|
|
|
/*** CLASS DEFINITION *********************************************************/
|
|
|
|
B_TYPE_CLASS_DEFINITION_BEGIN(b_path)
|
|
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
|
B_INTERFACE_ENTRY(to_string) = path_to_string;
|
|
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
|
B_TYPE_CLASS_DEFINITION_END(b_path)
|
|
|
|
B_TYPE_DEFINITION_BEGIN(b_path)
|
|
B_TYPE_ID(0x56dc32eb, 0xea96, 0x46ed, 0x85d3, 0x760fa4ad61f4);
|
|
B_TYPE_CLASS(b_path_class);
|
|
B_TYPE_INSTANCE_PRIVATE(struct b_path_p);
|
|
B_TYPE_INSTANCE_INIT(path_init);
|
|
B_TYPE_INSTANCE_FINI(path_fini);
|
|
B_TYPE_DEFINITION_END(b_path)
|