summaryrefslogblamecommitdiff
path: root/src/kernel/vfs/vdir.cpp
blob: 00b67cb56ae8a88a752add97b5f53d657e2323ed (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                                                  



                              










































                                                                              
#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++;
	}
}

vdir::~vdir() {
	//TODO : free children
}

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;
}