summaryrefslogtreecommitdiff
path: root/src/fs/file.cc
blob: 99bb65d09be2d18a00d47e311a3676632344675c (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "fs/file.h"
#include "kernel/spinlock.h"
#include "kernel/mm.h"
#include "drivers/uart.h"
#include "kernel/proc.h"

#define NFILE 100
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2

namespace FileTable
{
    struct
    {
        Spinlock lock;
        struct file files[NFILE];
    } ftable;

    void init()
    {
        ftable.lock.init("file_table");
    }

    struct file *alloc()
    {
        struct file *f;
        ftable.lock.acquire();
        for (f = ftable.files; f < ftable.files + NFILE; f++)
        {
            if (f->ref == 0)
            {
                f->ref = 1;
                ftable.lock.release();
                return f;
            }
        }
        ftable.lock.release();
        return nullptr;
    }

    struct file *dup(struct file *f)
    {
        ftable.lock.acquire();
        if (f->ref < 1)
        {
            Drivers::uart_puts("panic: file dup\n");
            while (1)
                ;
        }
        f->ref++;
        ftable.lock.release();
        return f;
    }

    void close(struct file *f)
    {
        struct file ff;

        ftable.lock.acquire();
        if(f->ref < 1)
        {
            Drivers::uart_puts("panic: file close\n");
            while (1)
                ;
        }
        f->ref--;
        if (f->ref > 0)
        {
            ftable.lock.release();
            return;
        }

        ff = *f;
        f->ref = 0;
        f->type = FD_NONE;
        ftable.lock.release();
        if (ff.type == FD_INODE)
        {
            // Release the corresponding Inode
            VFS::iput(ff.ip);
        }
        else if (ff.type == FD_PIPE)
        {
            // pipeclose(ff.pipe, ff.writable);
        }
    }

    int read(struct file *f, uint64 addr, int n)
    {
        if (f->readable == 0)
            return -1;
        if (f->type == FD_INODE)
        {
            VFS::ilock(f->ip);
            int r = f->ip->read((char*)addr, f->off, n, 1);
            if(r > 0)
            {
                f->off += r;
            }
            VFS::iunlock(f->ip);
            return r;
        }
        return -1;
    }
    
    int write(struct file *f, uint64 addr, int n)
    {
        if (f->writable == 0)
            return -1;
        if (f->type == FD_INODE)
        {
            VFS::ilock(f->ip);
            int r = f->ip->write((char *)addr, f->off, n, 1);
            if (r > 0)
            {
                f->off += r;
            }
            VFS::iunlock(f->ip);
            return r;
        }
        // else if (f->type == FD_PIPE) ...

        return -1;
    }

    int stat(struct file *f, uint64 addr)
    {
        if (f->type == FD_INODE)
        {
            struct kstat st;
            VFS::ilock(f->ip);
            f->ip->getattr(&st);
            VFS::iunlock(f->ip);

            struct Proc *p = myproc();
            if (VM::copyout(p->pagetable, addr, (char *)&st, sizeof(st)) < 0)
                return -1;
            return 0;
        }
        return -1;
    }

    int lseek(struct file *f, int offset, int whence)
    {
        if (f->type != FD_INODE)
            return -1;

        VFS::ilock(f->ip);

        int new_off = f->off;
        int size = f->ip->size;

        switch (whence)
        {
        case SEEK_SET:
            new_off = offset;
            break;
        case SEEK_CUR:
            new_off += offset;
            break;
        case SEEK_END:
            new_off = size + offset;
            break;
        default:
            VFS::iunlock(f->ip);
            return -1;
        }

        if (new_off < 0)
        {
            VFS::iunlock(f->ip);
            return -1;
        }

        f->off = new_off;
        VFS::iunlock(f->ip);
        return new_off;
    }
} // namespace FileTable