Untitled

 avatar
unknown
plain_text
15 days ago
3.9 kB
5
Indexable
#include "ext2.h"
#include "libk.h"
#include "ide.h"
#include "block_io.h"

Ext2::Ext2(StrongPtr<Ide> ide) {
    this->ide = ide;
    // char tempBuffer[1024]; no big buffers, will overflow

    sb = new Superblock();
    this->ide->read_all(1024, sizeof(Superblock), (char *) sb);

    root = get_inode(2);

    // BlockGD BGD; 
    // read all 
    // to get the gdt, make an array of bgd
    // buffer is the size of gd
    // that array is the size of the number of block groups
    // read into that array 
    // super block is 1024
    // numm block groups = block ocunts / blocks per group + 1
    // gdt is array of gd, size of block groups
    // read all
    // root is get inode (2)
    

    // get gdt

    // Superblock* tempSb = new Superblock();
    // tempSb = (Superblock*)(tempBuffer);

    // Superblock* sb = new Superblock();
    // sb = (Superblock*)(tempBuffer);

    // sb->s_inodes_count     = tempSb->s_inodes_count;
    // sb->s_blocks_count     = tempSb->s_blocks_count;
    // sb->s_first_data_block = tempSb->s_first_data_block;
    // sb->s_log_block_size   = tempSb->s_log_block_size;
    // sb->s_magic            = tempSb->s_magic;
    // sb->s_blocks_per_group = tempSb->s_blocks_per_group;
    // sb->s_inodes_per_group = tempSb->s_inodes_per_group;
    // sb->s_inode_size       = tempSb->s_inode_size;
}

///////////// Node /////////////

void Node::get_symbol(char* buffer) {
    if (!is_symlink()) {
        Debug::panic("not a symbolic link");
    }
    uint32_t size = size_in_bytes();

    // check if can be stored directly in i_block
    if (size <= 60) {
        for (uint32_t i = 0; i < size; i++) {
            buffer[i] = reinterpret_cast<char*>(currInode->i_block)[i];
        }
    }
    // if not then store in a data block
    else {
        read_all(0, size, buffer);
    }
    buffer[size] = '\0';
}

void Node::read_block(uint32_t index, char* buffer) {
    Debug::printf("*** Reading block %d for inode %d\n", index, number);
    if (index >= size_in_blocks()) {
        Debug::printf("*** ERROR: read_block() is accessing an out-of-range block! index=%d, max=%d\n", 
                      index, size_in_blocks());
        Debug::panic("Invalid block access in read_block()");
    }
    uint32_t num = 0;
    
    if (index < 12) {
        num = currInode->i_block[index];
    }
    // we know it is more than 12, womp womp
    // each block pointer is 4 bytes
    else {
        index -= 12;
        uint32_t indir_block[block_size / 4];

        // this means single indirect
        if (index < block_size / 4) {
            uint32_t offset = currInode->i_block[12] * block_size;
            currIde->read_all(offset, block_size, (char *) indir_block);
            num = indir_block[index];
        }
        // double indirect
    }
    uint32_t offset = num * block_size;
    currIde->read_all(offset, block_size, buffer);
}

uint32_t Node::entry_count() {
    Debug::printf("*** entry_count() checking inode %d, block %d\n", number, currInode->i_block[0]);
    if (!is_dir()) {
        Debug::panic("not a directory");
    }
    uint32_t count = 0;
    uint32_t total_size = size_in_bytes();
    uint32_t offset = currInode->i_block[0] * block_size;
    uint32_t pos = 0;

    char* buffer = new char[total_size];
    int64_t bytes_read = currIde->read_all(offset, total_size, buffer);

    while (bytes_read > pos) {
        uint32_t inode = *reinterpret_cast<uint32_t*> (buffer + pos);
        uint16_t len = *reinterpret_cast<uint16_t*> (buffer + pos + 4);
        char* name = buffer + pos + 8;
        uint8_t name_len = *(buffer + pos + 6);

        if (inode != 0) {
            count += 1;
            Debug::printf("*** Entry found: inode=%d, name=%.*s\n", inode, name_len, name);
        }
        pos += len;

        if (len == 0) {
            break;
        }
    }
    delete[] buffer;
    return count;
}


Editor is loading...
Leave a Comment