@@ -8,13 +8,84 @@ import std;
88
99export namespace fs ::procfs
1010{
11- using gen_fn = std::string (*)(sched:: process_t *) ;
11+ enum class node_type { file, dir, symlink } ;
1212
13- // /proc/<name>
14- bool register_global (std::string name, gen_fn gen, mode_t mode, bool is_symlink = false );
13+ struct node_ops ;
14+ struct node_t
15+ {
16+ std::string name;
17+ mode_t mode;
18+ node_type type;
19+ std::shared_ptr<node_ops> ops;
20+ };
1521
16- // /proc/[pid]/<name>
17- bool register_per_pid (std::string name, gen_fn gen, mode_t mode, bool is_symlink = false );
22+ struct node_ops
23+ {
24+ virtual ~node_ops () = default ;
25+
26+ virtual bool can_trunc () { return false ; }
27+
28+ virtual lib::expect<std::string> generate (sched::process_t *proc)
29+ {
30+ lib::unused (proc);
31+ return std::unexpected { lib::err::not_supported };
32+ }
33+
34+ virtual lib::expect<void > write (sched::process_t *proc, std::string_view data)
35+ {
36+ lib::unused (proc, data);
37+ return std::unexpected { lib::err::read_only_fs };
38+ }
39+
40+ virtual lib::expect<lib::path> readlink (sched::process_t *proc)
41+ {
42+ lib::unused (proc);
43+ return std::unexpected { lib::err::not_supported };
44+ }
45+
46+ virtual lib::expect<lib::list<node_t >> readdir (sched::process_t *proc)
47+ {
48+ lib::unused (proc);
49+ return std::unexpected { lib::err::not_supported };
50+ }
51+
52+ virtual lib::expect<node_t > lookup (sched::process_t *proc, std::string_view name)
53+ {
54+ lib::unused (proc, name);
55+ return std::unexpected { lib::err::not_supported };
56+ }
57+
58+ virtual bool revalidate (sched::process_t *proc)
59+ {
60+ lib::unused (proc);
61+ return true ;
62+ }
63+ };
64+
65+ // I hate myself
66+ using gen_fn = std::function<lib::expect<std::string> (sched::process_t *)>;
67+ using write_fn = std::function<lib::expect<void > (sched::process_t *, std::string_view)>;
68+ using readlink_fn = std::function<lib::expect<lib::path> (sched::process_t *)>;
69+ using lookup_fn = std::function<lib::expect<node_t > (sched::process_t *, std::string_view)>;
70+ using readdir_fn = std::function<lib::expect<lib::list<node_t >> (sched::process_t *)>;
71+ using revalidate_fn = std::function<bool (sched::process_t *)>;
72+
73+ std::shared_ptr<node_ops> make_file_ops (gen_fn gfn, write_fn wfn = nullptr );
74+ std::shared_ptr<node_ops> make_symlink_ops (readlink_fn rdlfn, revalidate_fn rvfn = nullptr );
75+
76+ std::shared_ptr<node_ops> make_dir_ops (lookup_fn lfn = nullptr , readdir_fn rfn = nullptr );
77+
78+ // /proc/<path>
79+ bool register_global (
80+ lib::path path, std::shared_ptr<node_ops> ops,
81+ node_type type, mode_t mode
82+ );
83+
84+ // /proc/[pid]/<path>
85+ bool register_per_pid (
86+ lib::path path, std::shared_ptr<node_ops> ops,
87+ node_type type, mode_t mode
88+ );
1889
1990 lib::initgraph::stage *registered_stage ();
2091 lib::initgraph::stage *mounted_stage ();
0 commit comments