From 30ffa8780e765c50ad2386e32e45223ee74e12e9 Mon Sep 17 00:00:00 2001 From: Michael Shaw Date: Mon, 24 Feb 2025 20:01:31 -0500 Subject: [PATCH] vfs: add `vfs_create_file` --- fs/vfs.c | 34 +++++++++++++++++++++++++++++----- include/fs/vfs.h | 1 + 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/fs/vfs.c b/fs/vfs.c index 4cbad7c..4b7f98c 100644 --- a/fs/vfs.c +++ b/fs/vfs.c @@ -207,6 +207,26 @@ ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count) return file->vnode->fs->ops->read(file->vnode, buf, off, count); } +struct vnode *vfs_create_file(struct vnode *parent, const char *path, mode_t mode) +{ + struct fs *fs = parent->fs; + + int res = fs->ops->creat(parent, path, mode); + if (res < 0) + return NULL; + + /* find the new vnode */ + struct vnode *vnode = NULL; + for (int i = 0; i < parent->num_dirents; i++) { + if (strcmp(parent->dirents[i].name, path) == 0) { + vnode = parent->dirents[i].vnode; + break; + } + } + + return vnode; +} + int vfs_statf(struct file *file, struct statbuf *statbuf) { statbuf->size = file->vnode->size; @@ -214,11 +234,6 @@ int vfs_statf(struct file *file, struct statbuf *statbuf) return 0; } -int unlink(const char *pathname) -{ - return -ENOSYS; -} - char *basename(char *path) { char *ret = path; @@ -277,6 +292,15 @@ struct vnode *vfs_mknod(const char *pathname, mode_t mode) return NULL; } + switch (mode & VFS_VTYPE_MASK) { + case VFS_VNO_FIFO: + case VFS_VNO_BLKDEV: + case VFS_VNO_CHARDEV: + break; + default: + return NULL; + } + vnode->refcount = 1; vnode->fs = dir_vnode->fs; vnode->flags = mode; diff --git a/include/fs/vfs.h b/include/fs/vfs.h index d32ed4b..89168ca 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -140,6 +140,7 @@ ssize_t vfs_write(struct file *file, void *buf, off_t off, size_t count); ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count); int vfs_statf(struct file *file, struct statbuf *statbuf); struct file *vfs_open(const char *pathname, int *err); +struct vnode *vfs_create_file(struct vnode *parent, const char *path, mode_t mode); struct vnode *vfs_mknod(const char *pathname, mode_t mode); int vfs_close(struct file *file); int unlink(const char *pathname);