+
+__DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
+{
+ char name_str[VFS_NAME_MAXLEN];
+ struct hstr name = HSTR(name_str, 0);
+ struct v_dnode *dentry, *file;
+ int errno, fd;
+ if ((errno = vfs_walk(NULL, path, &dentry, &name, VFS_WALK_PARENT))) {
+ return -1;
+ }
+
+ struct v_file* opened_file = 0;
+ if (!(file = vfs_dcache_lookup(dentry, &name)) && (options & FO_CREATE)) {
+ errno = dentry->inode->ops.create(dentry->inode, opened_file);
+ } else if (!file) {
+ errno = EEXIST;
+ } else {
+ errno = vfs_open(file, &opened_file);
+ }
+
+ __current->k_status = errno;
+
+ if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
+ struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
+ fd_s->file = opened_file;
+ fd_s->pos = file->inode->fsize & -((options & FO_APPEND) == 0);
+ __current->fdtable->fds[fd] = fd_s;
+ }
+
+ return SYSCALL_ESTATUS(errno);
+}
+
+__DEFINE_LXSYSCALL1(int, close, int, fd)
+{
+ struct v_fd* fd_s;
+ int errno;
+ if (fd < 0 || fd >= VFS_MAX_FD || !(fd_s = __current->fdtable->fds[fd])) {
+ errno = EBADF;
+ } else if (!(errno = vfs_close(fd_s->file))) {
+ vfree(fd_s);
+ }
+
+ __current->k_status = errno;
+
+ return SYSCALL_ESTATUS(errno);
+}
+
+void
+__vfs_readdir_callback(struct dir_context* dctx,
+ const char* name,
+ const int len,
+ const int dtype)
+{
+ struct dirent* dent = (struct dirent*)dctx->cb_data;
+ strcpy(dent->d_name, name);
+ dent->d_nlen = len;
+ dent->d_type = dtype;
+}
+
+__DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
+{
+ struct v_fd* fd_s;
+ int errno;
+ if (fd < 0 || fd >= VFS_MAX_FD || !(fd_s = __current->fdtable->fds[fd])) {
+ errno = EBADF;
+ } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
+ errno = ENOTDIR;
+ } else {
+ struct dir_context dctx =
+ (struct dir_context){ .cb_data = dent,
+ .index = dent->d_offset,
+ .read_complete_callback =
+ __vfs_readdir_callback };
+ if (!(errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
+ dent->d_offset++;
+ }
+ }
+
+ __current->k_status = errno;
+ return SYSCALL_ESTATUS(errno);
+}
+
+__DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
+{
+ struct v_dnode *parent, *dir;
+ struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
+ int errno =
+ vfs_walk(root_sb->root, path, &parent, &component, VFS_WALK_PARENT);
+ if (errno) {
+ goto done;
+ }
+
+ if (!parent->inode->ops.mkdir) {
+ errno = ENOTSUP;
+ } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
+ errno = ENOTDIR;
+ } else {
+ dir = vfs_d_alloc();
+ dir->name = component;
+ if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
+ llist_append(&parent->children, &dir->siblings);
+ } else {
+ vfs_d_free(dir);
+ vfree(component.value);
+ }
+ }
+
+done:
+ __current->k_status = errno;
+ return SYSCALL_ESTATUS(errno);
+}
\ No newline at end of file