vfs: add vfs_creat
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
2534c317f2
commit
0007d8cea3
1 changed files with 60 additions and 0 deletions
60
fs/vfs/vfs.c
60
fs/vfs/vfs.c
|
|
@ -172,6 +172,66 @@ ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset)
|
|||
return ret;
|
||||
}
|
||||
|
||||
int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out)
|
||||
{
|
||||
if (!parent || !name || !out)
|
||||
return -EINVAL;
|
||||
|
||||
inode_t *ino = parent->inode;
|
||||
size_t len = strlen(name);
|
||||
int res;
|
||||
int ret = 0;
|
||||
if (len > 255)
|
||||
return -ENAMETOOLONG;
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
dirent_t *test;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
|
||||
if (res == 0) {
|
||||
/* exists */
|
||||
ret = -EEXIST;
|
||||
goto out_err;
|
||||
} else if (res != -ENOENT) {
|
||||
/* various other error codes */
|
||||
ret = res;
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
/* create inode */
|
||||
fs_t *fs = parent->fs;
|
||||
inode_t *new_ino;
|
||||
dirent_t *new_dirent;
|
||||
|
||||
res = fs->fs_ops.alloc_inode(fs, mode, &new_ino);
|
||||
if (res < 0) {
|
||||
ret = res;
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
res = ino->i_ops.link(parent, new_ino, name, &new_dirent);
|
||||
|
||||
if (res < 0) {
|
||||
ret = res;
|
||||
|
||||
res = fs->fs_ops.free_inode(fs, new_ino);
|
||||
if (res < 0)
|
||||
kerr("failed to link inode, then failed to free it!\n");
|
||||
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
*out = new_dirent;
|
||||
|
||||
out_err:
|
||||
if (test)
|
||||
dirent_dec_ref(test);
|
||||
write_unlock(&ino->rwlock);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
|
||||
{
|
||||
if (!mount_point || !strcmp(mount_point, "/")) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue