summaryrefslogtreecommitdiff
path: root/Source/Kernel/VFS/VFS.ns.cpp
blob: cf686533b6325ad5415134d0d4d35c32a1467ed1 (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "VFS.ns.h"
#include <VFS/FileNode.class.h>
#include <Vector.class.h>
#include <VTManager/VirtualTerminal.proto.h>

#include <VFS/Part.ns.h>
#include <FileSystems/RamFS/RamFS.class.h>
#include <FileSystems/FAT/FATFS.class.h>

struct local_fs_t {
	const char* name;
	mount_callback_t cb;
} fileSystems[] = {
	{"fat", FATFS::mount},
	{0, 0}
};

FileSystem::~FileSystem() { delete m_rootNode; }

namespace VFS {

DirectoryNode *rootNode = 0;
Vector<FileSystem*> filesystems;

DirectoryNode* getRootNode() {
	return rootNode;
}

void registerFilesystem(FileSystem* fs) {
	unregisterFilesystem(fs);
	filesystems.push(fs);
	if (rootNode == 0) rootNode = fs->getRootNode();
}

void unregisterFilesystem(FileSystem* fs) {
	for (u32int i = 0; i < filesystems.size(); i++) {
		if (filesystems[i] == fs) {
			filesystems[i] = filesystems.back();
			filesystems.pop();
			break;
		}
	}
}

bool unmount(FileSystem* fs) {
	if (!fs->getRootNode()->unmountable()) return false;
	if (fs->getRootNode() == rootNode) return false;
	if (!fs->unmount()) return false;
	delete fs;	//Will automatically delete the root node (destructor is in this file);
	return true;
}

bool mount(String str, VirtualTerminal* vt, multiboot_info_t *mbd) {
	Vector<String> fs = str.split(":");
	DirectoryNode* root;
	if (fs[0] == "/") {
		root = NULL;
	} else {
		FSNode* n = VFS::find(fs[0]);
		if (n == NULL) {
			*vt << "Mountpoint does not exist : " << fs[0] << "\n";
			return false;
		}
		if (n->type() != NT_DIRECTORY) {
			*vt << "Mountpoint is not a directory : " << fs[0] << "\n";
			return false;
		}
		root = (DirectoryNode*)n;
	}
	if (fs[1] == "ramfs") {
		if (fs.size() > 2) {
			if (mbd != 0) {
				module_t *mods = (module_t*)mbd->mods_addr;
				if (fs[2].toInt() >= mbd->mods_count) {
					*vt << "Invalid module number for filesystem to mount on " << fs[0] << "\n";
					return false;
				}
				FileSystem* wat = RamFS::mount((u8int*)mods[fs[2].toInt()].mod_start, 1024 * 1024, root);
				if (wat != NULL) {
					wat->setIdentifier(str);
					return true;
				}
				return false;
			} else {
				*vt << "Cannot mount kernel modules outside of kernel command line.\n";
				return false;
			}
		} else {
			FileSystem* wat = RamFS::mount(1024 * 1024, root);
			if (wat != NULL) {
				wat->setIdentifier(str);
				return true;
			}
			return false;
		}
	} else {
		if (fs.size() < 4) {
			*vt << "Syntax: <mountpoint>:[<dev_class>]:<dev_id>:<part_id>[:<fs_type>[:[ro|rw]]]\n";
			return false;
		}
		if (fs.size() < 5) fs.push("");
		if (fs.size() < 6) fs.push("ro");	//By default, mount file systems read-only
		BlockDevice* d = Part::dev(fs[1], fs[2].toInt());
		Partition* p = Part::part(d, fs[3].toInt());
		for (u32int i = 0; i < filesystems.size(); i++) {
			if (filesystems[i]->getPart() == p) {
				*vt << "Cannot mount " << str << " : partition already mounted.\n";
				return false;
			}
		}
		for (u32int i = 0; fileSystems[i].cb != 0; i++) {
			if (fs[4] == fileSystems[i].name or fs[4] == "") {
				FileSystem* mounted = fileSystems[i].cb(p, root, (fs[5] == "rw"));
				if (mounted != NULL) {
					mounted->setIdentifier(str);
					return true;
				} else if (fs[4] != "") {
					*vt << "Could not mount filesystem on " << fs[0] << "\n";
					if (root == NULL) PANIC("Error while mounting root filesystem.");
					return false;
				}
			}
		}
		*vt << "Unknown filesystem type for filesystem to mount on " << fs[0] << "\n";
		if (root == NULL) PANIC("Unknown filesystem type for root file system.");
		return false;
	}
}

FSNode* find(const String& path, FSNode* start) {
	if (start == 0) start = rootNode;

	Vector<String> p = path.split("/");

	FSNode* node = start;
	if (p[0].empty()) node = rootNode;	//Empty first element means string starts with "/"
	for (u32int i = 0; i < p.size(); i++) {
		if (p[i] == "..") {
			node = node->getParent();
		} else if (!p[i].empty() and p[i] != ".") {
			if (node->type() == NT_DIRECTORY) {
				node = ((DirectoryNode*)node)->getChild(p[i]);
			} else {
				node = NULL;
			}
		}
		if (node == NULL) return node;
	}

	return node;
}

FSNode* createFile(const String& path, FSNode* start, bool vrfyperm) {
	if (find(path, start) != NULL) return NULL;	//Something already has that name.
	if (start == 0) start = rootNode;

	Vector<String> p = path.split("/");
	String name = p.back();
	p.pop();

	FSNode* node = start;
	if (!path.empty()) {
		if (p[0].empty()) node = rootNode;
		for (u32int i = 0; i < p.size(); i++) {
			if (p[i] == "..") {
				node = node->getParent();
			} else if (!p[i].empty() and p[i] != ".") {
				if (node->type() == NT_DIRECTORY) {
					node = ((DirectoryNode*)node)->getChild(p[i]);
				} else {
					node = NULL;
				}
			}
			if (node == NULL) return node;
		}
	}

	if (node->type() == NT_DIRECTORY) {
		if ((vrfyperm && node->writable()) or !vrfyperm) return ((DirectoryNode*)node)->createFile(name);
	}
	return NULL;
}

FSNode* createDirectory(const String& path, FSNode* start, bool vrfyperm) {
	if (find(path, start) != NULL) return NULL;	//Something already has that name.
	if (start == 0) start = rootNode;

	Vector<String> p = path.split("/");
	String name = p.back();
	p.pop();

	FSNode* node = start;
	if (!path.empty()) {
		if (p[0].empty()) node = rootNode;
		for (u32int i = 0; i < p.size(); i++) {
			if (p[i] == "..") {
				node = node->getParent();
			} else if (!p[i].empty() and p[i] != ".") {
				if (node->type() == NT_DIRECTORY) {
					node = ((DirectoryNode*)node)->getChild(p[i]);
				} else {
					node = NULL;
				}
			}
			if (node == NULL) return node;
		}
	}

	if (node->type() == NT_DIRECTORY) {
		if ((vrfyperm && node->writable()) or !vrfyperm) return ((DirectoryNode*)node)->createDirectory(name);
	}
	return NULL;
}

bool remove(FSNode* node) {
	FSNode* parent = node->getParent();
	if (parent == NULL) return false;

	if (parent->type() == NT_DIRECTORY) {
		return ((DirectoryNode*)parent)->remove(node);
	} else {
		return false;	//Something wierd happenned
	}
}

bool remove(const String& path, FSNode* start) {
	FSNode* node = find(path, start);
	if (node == NULL) return false;
	return remove(node);
}

String path(FSNode* node) {
	String path;

	while (node != NULL) {
		String t = "/";
		t += node->getName();
		if (t != "//") {
			t += path;
			path = t;
		}
		node = node->getParent();
	}

	if (path.empty()) path = "/";

	return path;
}

}