aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/fs/vfs.c
blob: 0098d3bc24b1e6a19e7a147ac8d5539bb15c5505 (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
#include <stdint.h>
#include <stddef.h>
#include <kernel/fs/vfs.h>
#include <kernel/fs/ext2.h>
#include <kernel/lib/stdlib.h>
#include <kernel/memory/alloc.h>
#include <kernel/system.h>

struct tree *fs_tree;
struct fs_node *fs_root = NULL;

uint32_t fs_read(struct fs_node *node, uint32_t offset, uint32_t size, char *buf)
{
	if (node->read != NULL)
		return node->read(node, offset, size, buf);
	else
		return 0;
}

uint32_t fs_write(struct fs_node *node, uint32_t offset, uint32_t size, char *buf)
{
	if (node->write != NULL)
		return node->write(node, offset, size, buf);
	else
		return 0;
}

void fs_open(struct fs_node *node)
{
	if (node->open != NULL)
		node->open(node);
	else // TODO: Better ext2 default open workaround
		fs_root->open(node);
}

void fs_close(struct fs_node *node)
{
	if (node->close != NULL)
		node->close(node);
	else
		fs_root->open(node);
}

struct dirent *fs_read_dir(struct fs_node *node, uint32_t index)
{
	if ((node->type & DIR_NODE) != 0 && node->find_dir != NULL)
		return node->read_dir(node, index);
	else
		return (struct dirent *)NULL;
}

struct fs_node *fs_find_dir(struct fs_node *node, char *name)
{
	if ((node->type & DIR_NODE) != 0 && node->find_dir != NULL)
		return node->find_dir(node, name);
	else
		return (struct fs_node *)NULL;
}

// TODO: This should be somewhere else ig
char *next_str(char *str)
{
	str = &str[strlen(str)];

	int i = 0;
	while (str[i] == 0)
		i++;

	return &str[i];
}

// By far not the fastest algorithm
struct fs_node *fs_path(struct fs_node *node, char *name)
{
	if (name[0] == '/')
		return node;

	char *cpy = (char *)kmalloc(strlen(name) + 2);
	strcpy(cpy, name);
	cpy[strlen(name) + 1] = 1;

	name = cpy;
	char *end = &name[strlen(name)];

	int len = strlen(name);
	for (int i = 0; i < len; i++)
		if (name[i] == '/')
			name[i] = 0;

	if (strlen(name) == 0)
		name = next_str(name);

	do {
		node = node->node_ptr;

		while (node != NULL) {
			if (strcmp(node->name, name) == 0)
				break;

			node = node->link;
		}

		if (node == NULL) {
			kfree(cpy);
			return NULL;
		}

		name = next_str(name);
	} while ((uint32_t)name < (uint32_t)end);

	kfree(cpy);
	return node;
}

void vfs_ls(char *path)
{
	struct fs_node *dir = fs_path(fs_root, path);
	if (dir == NULL) {
		warn("Invalid path");
		return;
	}

	struct fs_node *link = dir->node_ptr;

	log("Listing for %s", path);
	while (link != NULL) {
		log(link->name);
		link = link->link;
	}
}

struct fs_node *vfs_get_dir(struct fs_node *node, char *name)
{
	char *cpy = (char *)kmalloc(strlen(name) + 2);
	char *ref = cpy;
	strcpy(cpy, name);
	cpy[strlen(name) + 1] = 1;
	name = cpy;

	for (int i = strlen(name); i >= 0; i--)
		if (name[i] == '/') {
			name[i] = 0;
			break;
		}

	struct fs_node *output;
	if (strlen(name) == 0)
		output = fs_root;
	else
		output = fs_path(node, name);

	kfree(ref);
	return output;
}

char *basename(char *name)
{
	for (int i = strlen(name); i >= 0; i--)
		if (name[i] == '/')
			return &name[i + 1];

	return name;
}

// TODO: Implement file touching in ext2
struct fs_node *vfs_touch(struct fs_node *node, char *name)
{
	struct fs_node *dir = vfs_get_dir(node, name);

	if (dir == NULL)
		return NULL;

	struct fs_node *file = (struct fs_node *)kmalloc(sizeof(struct fs_node));
	ext2_node_init(file);
	file->type = FILE_NODE;
	strcpy(file->name, basename(name));

	//ext2_file *entry = ext2_new_file();
	//strcpy(file->name, entry->name);

	file->link = dir->node_ptr;
	dir->node_ptr = file;

	return file;
}

struct fs_node *vfs_mkdir(struct fs_node *node, char *name)
{
	struct fs_node *dir = vfs_get_dir(node, name);
	if (dir == NULL) {
		warn("Invalid path");
		return NULL;
	}

	struct fs_node *file = (struct fs_node *)kmalloc(sizeof(struct fs_node));
	ext2_node_init(file);
	file->type = DIR_NODE;
	strcpy(file->name, basename(name));

	file->link = dir->node_ptr;
	dir->node_ptr = file;

	return file;
}