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

vfile::vfile(node* parent, char* d, size_t s) : node(parent, FT_FILE) {
	size = 0;
	own_data = false;

	data = d;
	if (d != 0) size = s;
}

vfile::~vfile() {
	if (own_data) kfree(data);
}

int vfile::read(size_t offset, size_t len, char* buffer) {
	if (offset + len > size) len = size - offset;
	memcpy(buffer, data + offset, len);
	return len;
}

int vfile::write(size_t offset, size_t len, char* buffer) {
	size_t new_size = MAX(offset + len, size);
	if (!own_data || new_size > size) {
		char* new_data = (char*)kmalloc(new_size);
		memcpy(new_data, data, size);
		if (new_size > size) memset(new_data + size, 0, new_size - size);
		if (own_data) kfree(data);
		data = new_data;
		size = new_size;
		own_data = true;
	}
	memcpy(data + offset, buffer, len);
	return len;
}