-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_manager.h
44 lines (40 loc) · 1.13 KB
/
fs_manager.h
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
#pragma once
#include "vfs.h"
#include <list>
#include <string>
#include <iostream>
#include <algorithm>
struct MountedFS {
VFS::FS *fs = nullptr;
std::string point; // 挂载点
MountedFS(VFS::FS *vfs, const std::string& p): fs(vfs), point(p) {}
};
class FSManager {
private:
std::list<MountedFS> fs_list;
VFS::DEntry *root = nullptr;
public:
void mount(VFS::FS *vfs, const std::string& p) {
for (auto &x: fs_list) {
if (x.point == p) {
std::cout << p << " is already mounted by another fs" << std::endl;
}
}
fs_list.emplace_back(vfs, p);
if (p == "/")
root = vfs->root;
}
void umount(const std::string& p) {
if (p == "/") {
std::cout << "Cannot umount root." << std::endl;
return;
}
auto res = std::find_if(fs_list.begin(), fs_list.end(), [&p](const MountedFS& x){x.point == p;});
if (res == fs_list.end()) {
std::cout << "No any filesystem mounted on " << p << std::endl;
return;
}
fs_list.erase(res);
delete res->fs;
}
};