summaryrefslogtreecommitdiff
path: root/src/kernel/vfs/vfile.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/vfs/vfile.cpp')
-rw-r--r--src/kernel/vfs/vfile.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/kernel/vfs/vfile.cpp b/src/kernel/vfs/vfile.cpp
new file mode 100644
index 0000000..ff57c9e
--- /dev/null
+++ b/src/kernel/vfs/vfile.cpp
@@ -0,0 +1,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;
+}