-int
-__vfs_walk(struct v_dnode* start,
- const char* path,
- struct v_dnode** dentry,
- struct hstr* component,
- int walk_options)
-{
- int errno = 0;
- int i = 0, j = 0;
-
- if (path[0] == PATH_DELIM || !start) {
- if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
- start = start->super_block->root;
- } else {
- start = sysroot;
- }
- i++;
- }
-
- struct v_dnode* dnode;
- struct v_dnode* current_level = start;
-
- char name_content[VFS_NAME_MAXLEN];
- struct hstr name = HSTR(name_content, 0);
-
- char current = path[i++], lookahead;
- while (current && current_level) {
- lookahead = path[i++];
- if (current != PATH_DELIM) {
- if (j >= VFS_NAME_MAXLEN - 1) {
- return ENAMETOOLONG;
- }
- if (!VFS_VALID_CHAR(current)) {
- return EINVAL;
- }
- name_content[j++] = current;
- if (lookahead) {
- goto cont;
- }
- }
-
- // handling cases like /^.*(\/+).*$/
- if (lookahead == PATH_DELIM) {
- goto cont;
- }
-
- lock_dnode(current_level);
-
- name_content[j] = 0;
- name.len = j;
- hstr_rehash(&name, HSTR_FULL_HASH);
-
- if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
- if (component) {
- component->hash = name.hash;
- component->len = j;
- strcpy(component->value, name_content);
- }
- unlock_dnode(current_level);
- break;
- }
-
- dnode = vfs_dcache_lookup(current_level, &name);
-
- if (!dnode) {
- dnode = vfs_d_alloc(current_level, &name);
-
- if (!dnode) {
- errno = ENOMEM;
- goto error;
- }
-
- lock_inode(current_level->inode);
-
- errno =
- current_level->inode->ops.dir_lookup(current_level->inode, dnode);
-
- if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
- if (!current_level->inode->ops.mkdir) {
- errno = ENOTSUP;
- } else {
- errno = current_level->inode->ops.mkdir(
- current_level->inode, dnode);
- }
- }
-
- vfs_dcache_add(current_level, dnode);
- unlock_inode(current_level->inode);
-
- if (errno) {
- unlock_dnode(current_level);
- goto cleanup;
- }
- }
-
- unlock_dnode(current_level);
-
- j = 0;
- current_level = dnode;
- cont:
- current = lookahead;
- };
-
- *dentry = current_level;
- return 0;
-
-cleanup:
- vfs_d_free(dnode);
-error:
- *dentry = NULL;
- return errno;
-}
-
-#define VFS_MAX_SYMLINK 16
-
-int
-vfs_walk(struct v_dnode* start,
- const char* path,
- struct v_dnode** dentry,
- struct hstr* component,
- int options)
-{
- struct v_dnode* interim;
- const char* pathname = path;
- int errno = __vfs_walk(start, path, &interim, component, options);
- int counter = 0;
-
- while (!errno && interim->inode && (options & VFS_WALK_NOFOLLOW)) {
- if (counter >= VFS_MAX_SYMLINK) {
- errno = ELOOP;
- continue;
- }
- if ((interim->inode->itype & VFS_IFSYMLINK) &&
- interim->inode->ops.read_symlink) {
-
- lock_inode(interim->inode);
- errno = interim->inode->ops.read_symlink(interim->inode, &pathname);
- unlock_inode(interim->inode);
-
- if (errno) {
- break;
- }
- } else {
- break;
- }
- errno = __vfs_walk(start, pathname, &interim, component, options);
- counter++;
- }
-
- *dentry = errno ? 0 : interim;
-
- return errno;
-}
-