Попытка получить доступ к суперблоку файла .vdi - PullRequest
0 голосов
/ 28 апреля 2019

Я пытаюсь получить доступ к суперблоку, который хранится в файле .vdi, с помощью некоторых созданных мной vdiTools.

Инструменты, которые я написал, являются просто функциями для файла .vdi, который отражает системные вызовы UNIX. То есть vdiOpen () использует UNIX open (и выводит все в структуру vdi с помощью чтения UNIX), vdiRead () использует UNIX-чтение, vdiWrite использует Unix-запись и так далее для закрытия и поиска. Кажется, все работает правильно, но мои данные смещены.

Вот результаты, которые моя функция printSuperblock () выкладывает в терминал из двух разных файлов vdi.

vdi1 17GBs 
>------------File System info---------------
>Total file system size in bytes: 1140850688 
>Size available for files, used and unused: 
>Amount of space currently used: 2509353121 
>Number of possible files and directories: 1569648350 
>Number of existing files: 
>Number of existing directories: 0 
>Number of block groups: 0 
>Block size in bytes: 16777216 
>Magic number: 9330 
>Error with fileSysState number. 28582 

vdi2 37GBs
>------------File System info---------------
>Total file system size in bytes: 42082304 
>Size available for files, used and unused: 
>Amount of space currently used: 1518474772 
>Number of possible files and directories: 352321745 
>Number of existing files: 
>Number of existing directories: 0 
>Number of block groups: 0 
>Block size in bytes: 1024 
>Magic number: 510 
>Error with fileSysState number. 5120 

Вот моя функция получения суперблока.

void getSuperblock(struct vdiFile *vdi_ptr, struct superblockStructure *superblock_ptr){//ask if you how to pass in the struct itself as the buffer its reading into.
    vdiSeek(vdi_ptr, 1024, 0);
    vdiRead(vdi_ptr, superblock_ptr, 84);

     //if(superblock_ptr->revLevel >= 1){
        //vdiSeek(vdi_ptr, 1024+83, SEEK_START);
        //vdiRead(vdi_ptr, superblock_ptr, 
//}
}

вот мой vdiRead

ssize_t vdiRead(struct vdiFile *vdi_ptr, void *buffer, size_t length){

    int blockNum = vdi_ptr->cursor / vdi_ptr->vdiStruct.blockSize;
    int offset = vdi_ptr->cursor % vdi_ptr->vdiStruct.blockSize;
    int pos = (vdi_ptr->vdiStruct.offsetBlocks + blockNum) * vdi_ptr->vdiStruct.blockSize;

    lseek(vdi_ptr->fileDesc, pos, SEEK_CUR);

    int realBlockNum;
    read(vdi_ptr->fileDesc, &realBlockNum, 1);

    int start = (vdi_ptr->vdiStruct.offsetBlocks + realBlockNum) * vdi_ptr->vdiStruct.blockSize + offset;   // location of the data to start

    lseek(vdi_ptr->fileDesc, start, SEEK_CUR);

    // read in the data to the buffer from start to length
    vdi_ptr->cursor += length;
    return read(vdi_ptr->fileDesc, buffer, length);
}

Вот структура суперблока

typedef struct __attribute__((__packed__)) superblockStructure{ // superblock is always located at byte 1024
    uint32_t iNodeCount,
            blocksCount,
            superUserBlocks,
            freeBlocks,
            freeInodes,
            firstDataBlock,
            logBlockSize,
            logFragSize,
            blocksPerGroup,
            fragsPerGroup,
            iNodesPerGroup,
            mountTime,
            writeTime;
    uint16_t mountCount,
            maxMountCount,
            magicNum,
            fileSysState, //1: clean; 2: error
            errors, // 1: continue, 2:remount as read only, 3: kernel panic
            minorRevLevel;
    uint32_t lastCheck,
            checkInterval,
            creatorOS,
            revLevel;
    uint16_t defaultUserIdReservedBlocks,
            defaultGroupIdReservedBlocks;
}superblockStructure;

Главное, что выделяется для меня, это то, что размер блока сильно отличается. Я точно знаю, что оба моих файла .vdi используют размер блока 1 КБ. Кроме того, я заметил, что оба их магических номера выключены. Я просто сбит с толку тем, что у одного правильный размер блока, а у другого - другой.

...