46 lines
1.3 KiB
C
46 lines
1.3 KiB
C
#include "src/path.h"
|
|
#include "src/fs.h"
|
|
#include "src/memory.h"
|
|
#include "src/string.h"
|
|
#include "src/util.h"
|
|
|
|
path_t *path_open(const fs_node_t *root, const path_t *source, char *path) {
|
|
path_t *result = memory_allocate(sizeof(path_t));
|
|
|
|
char *path_components[PATH_DEPTH];
|
|
uint8_t path_length = (uint8_t)string_split(path, '/', PATH_DEPTH, path_components);
|
|
if (!string_empty(path_components[0])) {
|
|
for (uint8_t i = 0; i < source->depth; i++) {
|
|
result->stack[result->depth++] = fs_open_again(source->stack[i]);
|
|
}
|
|
}
|
|
|
|
for (uint8_t i = 0; i < path_length; i++) {
|
|
if (string_empty(path_components[i]) || string_equal(path_components[i], ".")) {
|
|
continue;
|
|
} else if (string_equal(path_components[i], "..")) {
|
|
if (result->depth) {
|
|
fs_close(result->stack[--result->depth]);
|
|
}
|
|
} else {
|
|
const fs_node_t *prev = result->depth ? result->stack[result->depth - 1] : root;
|
|
fs_node_t *next = prev->is_dir ? fs_open_by(prev, path_components[i]) : NUL;
|
|
if (!next || result->depth >= PATH_DEPTH) {
|
|
path_close(result);
|
|
return NUL;
|
|
} else {
|
|
result->stack[result->depth++] = next;
|
|
}
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void path_close(path_t *path) {
|
|
for (uint64_t i = 0; i < path->depth; i++) {
|
|
fs_close(path->stack[i]);
|
|
}
|
|
memory_free(path);
|
|
}
|