vfs: add vfs_create_file

This commit is contained in:
Michael Shaw 2025-02-24 20:01:31 -05:00
commit 30ffa8780e
2 changed files with 30 additions and 5 deletions

View file

@ -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); 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) int vfs_statf(struct file *file, struct statbuf *statbuf)
{ {
statbuf->size = file->vnode->size; statbuf->size = file->vnode->size;
@ -214,11 +234,6 @@ int vfs_statf(struct file *file, struct statbuf *statbuf)
return 0; return 0;
} }
int unlink(const char *pathname)
{
return -ENOSYS;
}
char *basename(char *path) char *basename(char *path)
{ {
char *ret = path; char *ret = path;
@ -277,6 +292,15 @@ struct vnode *vfs_mknod(const char *pathname, mode_t mode)
return NULL; 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->refcount = 1;
vnode->fs = dir_vnode->fs; vnode->fs = dir_vnode->fs;
vnode->flags = mode; vnode->flags = mode;

View file

@ -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); ssize_t vfs_read(struct file *file, void *buf, off_t off, size_t count);
int vfs_statf(struct file *file, struct statbuf *statbuf); int vfs_statf(struct file *file, struct statbuf *statbuf);
struct file *vfs_open(const char *pathname, int *err); 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); struct vnode *vfs_mknod(const char *pathname, mode_t mode);
int vfs_close(struct file *file); int vfs_close(struct file *file);
int unlink(const char *pathname); int unlink(const char *pathname);