From c63f340d90800895f007de64b7d2d14624263331 Mon Sep 17 00:00:00 2001 From: nthnluu Date: Sun, 28 Jan 2024 21:20:27 -0500 Subject: Created student weenix repository --- kernel/include/fs/file.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 kernel/include/fs/file.h (limited to 'kernel/include/fs/file.h') diff --git a/kernel/include/fs/file.h b/kernel/include/fs/file.h new file mode 100644 index 0000000..72caee4 --- /dev/null +++ b/kernel/include/fs/file.h @@ -0,0 +1,62 @@ +#pragma once + +#include "types.h" + +#define FMODE_READ 1 +#define FMODE_WRITE 2 +#define FMODE_APPEND 4 +#define FMODE_MAX_VALUE (FMODE_READ | FMODE_WRITE | FMODE_APPEND) + +struct vnode; + +typedef struct file +{ + /* + * The current position in the file. Can be modified by system calls + * like lseek(2), read(2), and write(2) (and possibly others) as + * described in the man pages of those calls. + */ + size_t f_pos; + + /* + * The mode in which this file was opened. This is a mask of the flags + * FMODE_READ, FMODE_WRITE, and FMODE_APPEND. It is set when the file + * is first opened, and use to restrict the operations that can be + * performed on the underlying vnode. + */ + unsigned int f_mode; + + /* + * The number of references to this struct. + */ + size_t f_refcount; + + /* + * The vnode which corresponds to this file. + */ + struct vnode *f_vnode; +} file_t; + +struct file *fcreate(int fd, struct vnode *vnode, unsigned int mode); + +/* + * Returns the file_t assiciated with the given file descriptor for the + * current process. If there is no associated file_t, returns NULL. + */ +struct file *fget(int fd); + +/* + * fref() increments the reference count on the given file. + */ +void fref(file_t *file); + +/* + * fput() decrements the reference count on the given file. + * + * If the refcount reaches 0, the storage for the given file_t will be + * released (f won't point to a valid memory address anymore), and the + * refcount on the associated vnode (if any) will be decremented. + * + * The vnode release operation will also be called if it exists. + */ +void fput(file_t **filep); -- cgit v1.2.3-70-g09d2