#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::open(process *proc, int mode) { if (mode & FM_TRUNC) { if (own_data) kfree(data); size = 0; own_data = false; data = 0; } return 0; } int vfile::read(size_t offset, size_t len, char* buffer) { if (size == 0) return 0; 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); if (size > 0) 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; }