aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/fs/file.h
blob: 72caee4fce0985d21a631f0f499253524b487ea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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);