summaryrefslogtreecommitdiff
path: root/src/fs/file.cc
blob: 48af49ee4984a19beef231961d6c7b829f085f4d (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
#include "fs/file.h"
#include "kernel/spinlock.h"
#include "kernel/mm.h"
#include "drivers/uart.h"
#include "kernel/proc.h"

#define NFILE 100

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;
    }
} // namespace FileTable