io: add lots of directory and path manipulation functions
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include "status.h"
|
||||
#include "posix.h"
|
||||
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/io/file.h>
|
||||
@@ -216,27 +216,65 @@ enum b_status b_path_stat(const b_path *path, struct b_file_info *out)
|
||||
|
||||
memset(out, 0x0, sizeof *out);
|
||||
|
||||
out->length = st.st_size;
|
||||
return b_file_info_from_stat(&st, out);
|
||||
}
|
||||
|
||||
if (S_ISREG(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_NORMAL;
|
||||
enum b_status b_path_unlink(const b_path *path)
|
||||
{
|
||||
int err = unlink(b_string_ptr(path->pathstr));
|
||||
return err == 0 ? B_SUCCESS : b_status_from_errno(errno, B_ERR_IO_FAILURE);
|
||||
}
|
||||
|
||||
enum b_status b_path_get_directory(
|
||||
const struct b_path *path, struct b_path **out_dir_path)
|
||||
{
|
||||
struct b_string *path_str = path->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;
|
||||
}
|
||||
|
||||
if (S_ISDIR(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_DIRECTORY;
|
||||
size_t dir_path_len = (size_t)(sep - path_cstr);
|
||||
struct b_string *dir_path_s = b_string_substr(path_str, 0, dir_path_len);
|
||||
|
||||
struct b_path *dir_path
|
||||
= b_path_create_from_cstr(b_string_ptr(dir_path_s));
|
||||
b_string_release(dir_path_s);
|
||||
|
||||
*out_dir_path = dir_path;
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
enum b_status b_path_get_filename(
|
||||
const struct b_path *path, struct b_string *out_name)
|
||||
{
|
||||
struct b_string *path_str = path->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;
|
||||
}
|
||||
|
||||
if (S_ISBLK(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_BLOCK_DEVICE;
|
||||
const char *filename = sep;
|
||||
while (*filename == '/' && *filename != '\0') {
|
||||
filename++;
|
||||
}
|
||||
|
||||
if (S_ISCHR(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_CHAR_DEVICE;
|
||||
if (*filename == '\0') {
|
||||
b_string_clear(out_name);
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
if (S_ISLNK(st.st_flags)) {
|
||||
out->attrib |= B_FILE_ATTRIB_SYMLINK;
|
||||
}
|
||||
b_string_append_cstr(out_name, filename);
|
||||
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user