summaryrefslogtreecommitdiff
path: root/Kernel/FileBackedDiskDevice.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-01-23 05:13:17 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-01-23 05:14:00 +0100
commit754037874c692769c703f86bfa7af641e1346139 (patch)
treed88a26977532a288eed2f61ef61526931713fa4f /Kernel/FileBackedDiskDevice.cpp
parent19104570cc286b608f6f8c22002ed4a8965b4648 (diff)
downloadserenity-754037874c692769c703f86bfa7af641e1346139.zip
Move VFS sources into Kernel/.
Diffstat (limited to 'Kernel/FileBackedDiskDevice.cpp')
-rw-r--r--Kernel/FileBackedDiskDevice.cpp82
1 files changed, 82 insertions, 0 deletions
diff --git a/Kernel/FileBackedDiskDevice.cpp b/Kernel/FileBackedDiskDevice.cpp
new file mode 100644
index 0000000000..df86e43cca
--- /dev/null
+++ b/Kernel/FileBackedDiskDevice.cpp
@@ -0,0 +1,82 @@
+#define _FILE_OFFSET_BITS 64
+
+#include "FileBackedDiskDevice.h"
+#include <cstring>
+#include <sys/stat.h>
+
+//#define FBBD_DEBUG
+#define IGNORE_FILE_LENGTH // Useful for e.g /dev/hda2
+
+RetainPtr<FileBackedDiskDevice> FileBackedDiskDevice::create(String&& image_path, unsigned block_size)
+{
+ return adopt(*new FileBackedDiskDevice(move(image_path), block_size));
+}
+
+FileBackedDiskDevice::FileBackedDiskDevice(String&& image_path, unsigned block_size)
+ : m_image_path(move(image_path))
+ , m_block_size(block_size)
+{
+ struct stat st;
+ int result = stat(m_image_path.characters(), &st);
+ ASSERT(result != -1);
+ m_file_length = st.st_size;
+ m_file = fopen(m_image_path.characters(), "r+");
+}
+
+FileBackedDiskDevice::~FileBackedDiskDevice()
+{
+}
+
+unsigned FileBackedDiskDevice::block_size() const
+{
+ return m_block_size;
+}
+
+bool FileBackedDiskDevice::read_block(unsigned index, byte* out) const
+{
+ DiskOffset offset = index * m_block_size;
+ return read_internal(offset, block_size(), out);
+}
+
+bool FileBackedDiskDevice::write_block(unsigned index, const byte* data)
+{
+ DiskOffset offset = index * m_block_size;
+ return write_internal(offset, block_size(), data);
+}
+
+bool FileBackedDiskDevice::read_internal(DiskOffset offset, unsigned length, byte* out) const
+{
+#ifndef IGNORE_FILE_LENGTH
+ if (offset + length >= m_fileLength)
+ return false;
+#endif
+#ifdef FBBD_DEBUG
+ printf("[FileBackedDiskDevice] Read device @ offset %llx, length %u\n", offset, length);
+#endif
+ fseeko(m_file, offset, SEEK_SET);
+ unsigned nread = fread(out, sizeof(byte), length, m_file);
+ ASSERT(nread == length);
+ return true;
+}
+
+bool FileBackedDiskDevice::write_internal(DiskOffset offset, unsigned length, const byte* data)
+{
+#ifndef IGNORE_FILE_LENGTH
+ if (offset + length >= m_fileLength)
+ return false;
+#endif
+#ifdef FBBD_DEBUG
+ printf("[FileBackedDiskDevice] Write device @ offset %llx, length %u\n", offset, length);
+#endif
+ fseeko(m_file, offset, SEEK_SET);
+ // size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+ unsigned nwritten = fwrite(data, sizeof(byte), length, m_file);
+ ASSERT(nwritten == length);
+ return true;
+}
+
+const char* FileBackedDiskDevice::class_name() const
+{
+ return "FileBackedDiskDevice";
+}
+