backported more fixes from 256boss
[bootcensus] / src / fs.h
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018-2019  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY, without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #ifndef FS_H_
19 #define FS_H_
20
21 #include <inttypes.h>
22
23 /* device ids for virtual filesystems */
24 enum {
25         DEV_FLOPPY0             = 0,
26         DEV_FLOPPY1             = 1,
27         DEV_HDD0                = 0x80,
28         DEV_HDD1                = 0x81,
29         DEV_MEMDISK             = 0x10000
30 };
31
32 enum {
33         FSTYPE_MEM,
34         FSTYPE_FAT,
35
36         NUM_FSTYPES
37 };
38
39 enum { FSNODE_FILE, FSNODE_DIR };
40
41 enum { FSSEEK_SET, FSSEEK_CUR, FSSEEK_END };
42
43 enum {
44         FSO_CREATE      = 1,
45         FSO_DIR         = 2,
46         FSO_EXCL        = 4
47 };
48
49 struct filesys;
50 struct fs_node;
51 struct fs_dirent;
52
53 struct fs_operations {
54         void (*destroy)(struct filesys *fs);
55
56         struct fs_node *(*open)(struct filesys *fs, const char *path, unsigned int flags);
57         void (*close)(struct fs_node *node);
58
59         long (*fsize)(struct fs_node *node);
60         int (*seek)(struct fs_node *node, int offs, int whence);
61         long (*tell)(struct fs_node *node);
62         int (*read)(struct fs_node *node, void *buf, int sz);
63         int (*write)(struct fs_node *node, void *buf, int sz);
64
65         int (*rewinddir)(struct fs_node *node);
66         struct fs_dirent *(*readdir)(struct fs_node *node);
67
68         int (*rename)(struct fs_node *node, const char *name);
69         int (*remove)(struct fs_node *node);
70 };
71
72 struct filesys {
73         int type;
74         char *name;
75         struct fs_operations *fsop;
76         void *data;
77 };
78
79 struct fs_node {
80         struct filesys *fs;
81         int type;
82         void *data;
83
84         struct filesys *mnt;
85 };
86
87 struct fs_dirent {
88         char *name;
89         void *data;
90         int type;
91         long fsize;
92 };
93
94 struct filesys *rootfs;
95 struct fs_node *cwdnode;        /* current working directory node */
96
97 struct filesys *fs_mount(int dev, uint64_t start, uint64_t size, struct fs_node *parent);
98
99 int fs_chdir(const char *path);
100 char *fs_getcwd(void);
101
102 struct fs_node *fs_open(const char *path, unsigned int flags);
103 int fs_close(struct fs_node *node);
104
105 int fs_rename(struct fs_node *node, const char *name);
106 int fs_remove(struct fs_node *node);
107
108 long fs_filesize(struct fs_node *node);
109 int fs_seek(struct fs_node *node, int offs, int whence);
110 long fs_tell(struct fs_node *node);
111 int fs_read(struct fs_node *node, void *buf, int sz);
112 int fs_write(struct fs_node *node, void *buf, int sz);
113
114 int fs_rewinddir(struct fs_node *node);
115 struct fs_dirent *fs_readdir(struct fs_node *node);
116
117 /* fs utility functions */
118 char *fs_path_skipsep(char *s);
119
120 /* copies the current name into the namebuf, and returns a pointer to the
121  * start of the next path component.
122  */
123 char *fs_path_next(char *s, char *namebuf, int bufsz);
124
125 #endif  /* FS_H_ */