CS350 COS
COS
Loading...
Searching...
No Matches
vfsuio.c
Go to the documentation of this file.
1
2#include <stdbool.h>
3#include <stdint.h>
4#include <string.h>
5#include <errno.h>
6
7#include <sys/kassert.h>
8#include <sys/kdebug.h>
9#include <sys/spinlock.h>
10#include <sys/disk.h>
11#include <sys/vfs.h>
12#include <sys/handle.h>
13#include <sys/vfsuio.h>
14
15static int
17{
18 ASSERT(handle->type == HANDLE_TYPE_FILE);
19
20 // XXX: Need to pin memory
21
22 return VFS_Read(handle->vnode, buf, len, off);
23}
24
25static int
27{
28 ASSERT(handle->type == HANDLE_TYPE_FILE);
29
30 // XXX: Need to pin memory
31
32 return VFS_Write(handle->vnode, buf, len, off);
33}
34
35static int
37{
38 ASSERT(handle->type == HANDLE_TYPE_FILE);
39 return -EINVAL;
40}
41
42static int
44{
45 int status;
46
47 ASSERT(handle->type == HANDLE_TYPE_FILE);
48
49 status = VFS_Close(handle->vnode);
50 Handle_Free(handle);
51
52 return status;
53}
54
55int
56VFSUIO_Open(const char *path, Handle **handle)
57{
58 int status;
59
60 Handle *hdl = Handle_Alloc();
61 if (!hdl) {
62 return -ENOMEM;
63 }
64
65 VNode *vn = VFS_Lookup(path);
66 if (!vn) {
67 Handle_Free(hdl);
68 return -ENOENT;
69 }
70
71 status = VFS_Open(vn);
72 if (status != 0) {
73 // XXX: Release VNode
74 Handle_Free(hdl);
75 return status;
76 }
77
78 hdl->vnode = vn;
80 hdl->read = VFSUIO_Read;
81 hdl->write = VFSUIO_Write;
82 hdl->flush = VFSUIO_Flush;
83 hdl->close = VFSUIO_Close;
84
85 *handle = hdl;
86
87 return 0;
88}
89
90
#define ENOENT
Definition: errno.h:15
#define EINVAL
Definition: errno.h:12
#define ENOMEM
Definition: errno.h:14
static char buf[4096]
Definition: ethdump.c:10
#define HANDLE_TYPE_FILE
Definition: handle.h:12
#define ASSERT(_x)
Definition: kassert.h:8
uint64_t len
Definition: multiboot.h:2
Definition: handle.h:17
uint64_t type
Definition: handle.h:19
int(* close)(Handle *)
Definition: handle.h:26
VNode * vnode
Definition: handle.h:21
int(* write)(Handle *, void *, uint64_t, uint64_t)
Definition: handle.h:24
int(* read)(Handle *, void *, uint64_t, uint64_t)
Definition: handle.h:23
int(* flush)(Handle *)
Definition: handle.h:25
unsigned long uint64_t
Definition: types.h:13
int VFS_Read(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.c:180
VNode * VFS_Lookup(const char *path)
Definition: vfs.c:63
int VFS_Open(VNode *fn)
Definition: vfs.c:147
int VFS_Write(VNode *fn, void *buf, uint64_t off, uint64_t len)
Definition: vfs.c:198
int VFS_Close(VNode *fn)
Definition: vfs.c:162
Definition: vfs.h:24
static int VFSUIO_Write(Handle *handle, void *buf, uint64_t len, uint64_t off)
Definition: vfsuio.c:26
static int VFSUIO_Read(Handle *handle, void *buf, uint64_t len, uint64_t off)
Definition: vfsuio.c:16
static int VFSUIO_Flush(Handle *handle)
Definition: vfsuio.c:36
static int VFSUIO_Close(Handle *handle)
Definition: vfsuio.c:43
int VFSUIO_Open(const char *path, Handle **handle)
Definition: vfsuio.c:56