BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / commit / 648c56b1573240b17ce0a6ab62746ab30e41e126 / vfs / ext2fs / block.c

// Related

Nucleus

Barry Read-only Ext2 file system driver 648c56b (3 years, 2 months ago)
diff --git a/vfs/ext2fs/block.c b/vfs/ext2fs/block.c
new file mode 100644
index 0000000..65ffdae
--- /dev/null
+++ b/vfs/ext2fs/block.c
@@ -0,0 +1,49 @@
+#include <stdint.h>
+#include <nucleus/panic.h>
+#include <nucleus/vfs.h>
+#include "ext2fs.h"
+
+/* Read a block of data */
+void
+ext2fs_read_block(SuperBlock *super, uint32_t index, char *buf)
+{
+	struct Ext2Super *rsuper = super->data;
+	super->back->pos = index * (1024 << rsuper->blockSize);
+	file_read(super->back, buf, 1024 << rsuper->blockSize);
+}
+
+/* Get the data block address from inode block index */
+uint32_t
+ext2fs_get_data_addr(SuperBlock *super, struct Ext2Inode *inode, uint32_t index)
+{
+	uint32_t tmp;
+	char block[PAGE_SIZE];
+	struct Ext2Super *rsuper = super->data;
+	/* Blocks per indirect block */
+	uint32_t bpib = (1024 << rsuper->blockSize) / sizeof(uint32_t);
+
+	/* Direct blocks */
+	if (index < 12)
+		return inode->directBlock[index];
+	index -= 12;
+
+	/* Single indirect block */
+	if (index < bpib) {
+		ext2fs_read_block(super, inode->singleBlock, block);
+		return *((uint32_t *) block + index);
+	}
+	index -= bpib;
+
+	/* Double indirect block */
+	if (index < bpib*bpib) {
+		panic("Attempted to read from double indirect block");
+	}
+	index -= bpib*bpib;
+
+	/* Triple indirect block */
+	if (index < bpib*bpib*bpib) {
+		panic("Attempted to read from triple indirect block");
+	}
+
+	return 0;
+}