feat: (twimap) provide an easy way for mapping kernel objects into filesystem
[lunaix-os.git] / lunaix-os / includes / lunaix / fs / twifs.h
1 #ifndef __LUNAIX_TWIFS_H
2 #define __LUNAIX_TWIFS_H
3
4 #include <lunaix/fs.h>
5 #include <lunaix/spike.h>
6
7 struct twifs_node
8 {
9     struct hstr name;
10     inode_t ino_id;
11     void* data;
12     uint32_t itype;
13     char name_val[VFS_NAME_MAXLEN];
14     struct llist_header children;
15     struct llist_header siblings;
16     struct
17     {
18         int (*write)(struct v_inode* inode,
19                      void* buffer,
20                      size_t len,
21                      size_t fpos);
22         int (*read)(struct v_inode* inode,
23                     void* buffer,
24                     size_t len,
25                     size_t fpos);
26     } ops;
27 };
28
29 struct twimap
30 {
31     void* index;
32     void* buffer;
33     void* data;
34     size_t size_acc;
35     void (*read)(struct twimap* mapping);
36     int (*go_next)(struct twimap* mapping);
37     void (*reset)(struct twimap* mapping);
38 };
39
40 #define twinode_getdata(inode, type)                                           \
41     ({                                                                         \
42         struct twifs_node* twinode = (struct twifs_node*)(inode)->data;        \
43         assert(twinode);                                                       \
44         (type) twinode->data;                                                  \
45     })
46
47 void
48 twifs_init();
49
50 struct twifs_node*
51 twifs_file_node_vargs(struct twifs_node* parent, const char* fmt, va_list args);
52
53 struct twifs_node*
54 twifs_file_node(struct twifs_node* parent, const char* fmt, ...);
55
56 struct twifs_node*
57 twifs_dir_node(struct twifs_node* parent, const char* fmt, ...);
58
59 int
60 twifs_rm_node(struct twifs_node* node);
61
62 #define twimap_index(twimap, type) ((type)((twimap)->index))
63 #define twimap_data(twimap, type) ((type)((twimap)->data))
64
65 struct twimap*
66 twifs_mapping(struct twifs_node* parent, void* data, const char* fmt, ...);
67
68 void
69 twimap_printf(struct twimap* mapping, const char* fmt, ...);
70
71 int
72 twimap_memcpy(struct twimap* mapping, const void* src, const size_t len);
73
74 int
75 twimap_memappend(struct twimap* mapping, const void* src, const size_t len);
76
77 #endif /* __LUNAIX_TWIFS_H */