vfs_open: split into two functions
This commit is contained in:
parent
30ffa8780e
commit
eccc30372e
1 changed files with 49 additions and 39 deletions
84
fs/vfs.c
84
fs/vfs.c
|
|
@ -1,4 +1,6 @@
|
||||||
/* SDPX-License-Identifier: GPL-2.0-only */
|
/* SDPX-License-Identifier: GPL-2.0-only */
|
||||||
|
#include "kernel/common.h"
|
||||||
|
#include "kernel/pio.h"
|
||||||
#include <kernel/slab.h>
|
#include <kernel/slab.h>
|
||||||
#include <kernel/block.h>
|
#include <kernel/block.h>
|
||||||
#include <kernel/rbtree.h>
|
#include <kernel/rbtree.h>
|
||||||
|
|
@ -62,35 +64,13 @@ static void vfs_vnode_dec_ref(struct vnode *vnode)
|
||||||
vfs_vnode_dealloc(vnode);
|
vfs_vnode_dealloc(vnode);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct file *vfs_open(const char *pathname, int *err)
|
struct vnode *vfs_lookup(const char *name, int *err)
|
||||||
{
|
{
|
||||||
/* allocate a file */
|
struct vnode *cur_vnode = rootfs->root;
|
||||||
struct file *file = slab_alloc(file_slab);
|
|
||||||
|
|
||||||
if (!file) {
|
|
||||||
kprintf(LOG_ERROR "Failed to allocate file struct\n");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
memset(file, 0, sizeof(struct file));
|
|
||||||
|
|
||||||
int len = strlen(pathname);
|
|
||||||
int inc = 0;
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
if (pathname[i] == '/')
|
|
||||||
inc++;
|
|
||||||
else
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inc < len)
|
|
||||||
pathname += inc;
|
|
||||||
|
|
||||||
/* start at root */
|
|
||||||
struct fs *fs = rootfs;
|
struct fs *fs = rootfs;
|
||||||
struct vnode *cur_vnode = fs->root;
|
|
||||||
|
|
||||||
char *path_copy = kmalloc(strlen(pathname) + 1, ALLOC_KERN);
|
char *path_copy = kzalloc(strlen(name) + 1, ALLOC_KERN);
|
||||||
strcpy(path_copy, pathname);
|
strcpy(path_copy, name);
|
||||||
|
|
||||||
char *tok_last = NULL;
|
char *tok_last = NULL;
|
||||||
char *tok = strtok(path_copy, "/", &tok_last);
|
char *tok = strtok(path_copy, "/", &tok_last);
|
||||||
|
|
@ -119,7 +99,8 @@ struct file *vfs_open(const char *pathname, int *err)
|
||||||
struct vnode *new_vnode = slab_alloc(vnode_slab);
|
struct vnode *new_vnode = slab_alloc(vnode_slab);
|
||||||
if (!new_vnode) {
|
if (!new_vnode) {
|
||||||
ATTEMPT_WRITE(err, -ENOMEM);
|
ATTEMPT_WRITE(err, -ENOMEM);
|
||||||
goto out_2;
|
kfree(path_copy);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
memset(new_vnode, 0, sizeof(struct vnode));
|
memset(new_vnode, 0, sizeof(struct vnode));
|
||||||
new_vnode->refcount = 1;
|
new_vnode->refcount = 1;
|
||||||
|
|
@ -129,7 +110,8 @@ struct file *vfs_open(const char *pathname, int *err)
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
ATTEMPT_WRITE(err, res);
|
ATTEMPT_WRITE(err, res);
|
||||||
slab_free(vnode_slab, new_vnode);
|
slab_free(vnode_slab, new_vnode);
|
||||||
goto out_2;
|
kfree(path_copy);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
cur_vnode = new_vnode;
|
cur_vnode = new_vnode;
|
||||||
|
|
@ -143,26 +125,54 @@ struct file *vfs_open(const char *pathname, int *err)
|
||||||
|
|
||||||
if (!found) {
|
if (!found) {
|
||||||
ATTEMPT_WRITE(err, -ENOENT);
|
ATTEMPT_WRITE(err, -ENOENT);
|
||||||
|
|
||||||
/* clean up */
|
|
||||||
vfs_vnode_dec_ref(cur_vnode);
|
vfs_vnode_dec_ref(cur_vnode);
|
||||||
goto out_2;
|
kfree(path_copy);
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get next token */
|
/* get next token */
|
||||||
tok = strtok(NULL, "/", &tok_last);
|
tok = strtok(NULL, "/", &tok_last);
|
||||||
}
|
}
|
||||||
|
|
||||||
file->vnode = cur_vnode;
|
kfree(path_copy);
|
||||||
file->type = cur_vnode->flags & VFS_VTYPE_MASK;
|
return cur_vnode;
|
||||||
file->size = cur_vnode->size;
|
}
|
||||||
file->ino_num = cur_vnode->ino_num;
|
|
||||||
|
|
||||||
return file;
|
struct file *vfs_open(const char *pathname, int *err)
|
||||||
|
{
|
||||||
|
/* allocate a file */
|
||||||
|
struct file *file = slab_alloc(file_slab);
|
||||||
|
|
||||||
out_2:
|
if (!file) {
|
||||||
|
kprintf(LOG_ERROR "Failed to allocate file struct\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
memset(file, 0, sizeof(struct file));
|
||||||
|
|
||||||
|
int len = strlen(pathname);
|
||||||
|
int inc = 0;
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (pathname[i] == '/')
|
||||||
|
inc++;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inc < len)
|
||||||
|
pathname += inc;
|
||||||
|
|
||||||
|
struct vnode *vno = vfs_lookup(pathname, err);
|
||||||
|
if (!vno) {
|
||||||
slab_free(file_slab, file);
|
slab_free(file_slab, file);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
file->vnode = vno;
|
||||||
|
file->type = vno->flags & VFS_VTYPE_MASK;
|
||||||
|
file->size = vno->size;
|
||||||
|
file->ino_num = vno->ino_num;
|
||||||
|
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
int vfs_close(struct file *file)
|
int vfs_close(struct file *file)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue