summaryrefslogtreecommitdiff
path: root/include/fs/file.h
blob: a92d8f83122d353404f75aa710473cd1769df491 (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
#pragma once
#include "common/types.h"
#include "fs/fs.h"

enum FileType
{
    FD_NONE,
    FD_INODE,
    FD_DEVICE,
    FD_PIPE
};

class Pipe;

struct file
{
    enum FileType type;
    int ref;
    char readable;
    char writable;

    Pipe *pipe; // pipeline pointer
    Inode *ip;         // VFS Inode
    uint32 off;        // Current Offset
};

class Pipe
{
public:
    static constexpr uint32_t SIZE = 512;

    Pipe() : nread(0), nwrite(0), read_open(true), write_open(true)
    {
        lock.init("pipe");
    }

    // forbid copy
    Pipe(const Pipe &) = delete;
    Pipe &operator=(const Pipe &) = delete;

    ~Pipe()
    {
        //
    }

    // creates a pair of connected file objects
    static int create_pair(struct file **f0, struct file **f1);

    int read(uint64_t addr, int n);
    int write(uint64_t addr, int n);
    void close(bool is_writer);

private:
    Spinlock lock;
    char data[SIZE];
    uint32_t nread;  // read
    uint32_t nwrite; // write
    bool read_open;
    bool write_open;
};

namespace FileTable
{
    void init();
    struct file *alloc();             // Allocate a free file handle
    struct file *dup(struct file *f); // Duplicate handle (sys_dup)
    void close(struct file *f);       // Close handle

    int stat(struct file *f, uint64 addr);
    int read(struct file *f, uint64 addr, int n);
    int write(struct file *f, uint64 addr, int n);
    int lseek(struct file *f, int offset, int whence);
} // namespace FileTable