summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/vdir.cpp
blob: 299c0e740e7d3d35085709af25e9025517db2e62 (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
#include "vdir.h"

vdir::vdir(node* parent) : node(parent, FT_DIR), children(8, 64) {
	children.add(new vdir_child(".", this));
	child_count = 1;

	if (parent != 0) {
		children.add(new vdir_child("..", parent));
		child_count++;
	}
}

node* vdir::get_child(char* name) {
	for (unsigned i = 0; i < child_count; i++) {
		vdir_child *c = children.at(i);
		ASSERT(c != 0);
		if (strcmp(c->name, name) == 0) return c->el;
	}
	return 0;
}

int vdir::read(size_t offset, size_t len, char* buffer) {
	if (offset >= child_count) return 0;
	vdir_child *c = children.at(offset);
	ASSERT(c != 0);
	size_t nlen = strlen(c->name);
	if (len <= nlen) {
		return E_TOO_SHORT;
	}
	strcpy(buffer, c->name);
	return nlen;
}

int vdir::write(size_t offset, size_t len, char* buffer) {
	// rename file
	if (offset >= child_count) return E_INVALID_RANGE;
	vdir_child *c = children.at(offset);
	ASSERT(c != 0);
	if (len == 0) return E_INVALID;
	kfree(c->name);
	c->name = (char*)kmalloc(len + 1);
	for (unsigned i = 0; i < len; i++) c->name[i] = buffer[i];
	c->name[len] = 0;
	return len;
}

size_t vdir::get_size() {
	return child_count;
}

int vdir::add_child(char* name, node *child) {
	ASSERT((int)child_count == children.add(new vdir_child(name, child)));
	child_count++;
	return 0;
}