Example #1
0
void
gdbWriteFreeBlockList(GDatabase *db, GdbFreeBlock *blocks, uint32_t count)
{
    uint32_t listSize;
    uint8_t *buffer;
    uint32_t i, counter = 0;
    offset_t __offset;

    if (db == NULL || blocks == NULL)
    {
        return;
    }
    /* Get the total size of the list. */
    listSize = sizeof(uint32_t) + count * (sizeof(uint16_t) + sizeof(offset_t));

    /* Allocate the buffer for the block list. */
    MEM_CHECK(buffer = (uint8_t *)SAFE_MALLOC(listSize, LOC_DB_0006));

    gdbPut32(buffer, &counter, count);

    for (i = 0; i < count; i++)
    {
        gdbPut16(buffer, &counter, blocks[i].size);
        gdbPutOffset(buffer, &counter, blocks[i].offset);
    }

    rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET, SEEK_SET);
    __offset = DB_FREE_BLOCK_LIST_OFFSET;

    rawFileWrite(db->idxRawFile, __offset, buffer, listSize, 1, LOC_DB_0007);

    SAFE_FREE(buffer, LOC_DB_0008);
}
Example #2
0
void
btreeEraseNode(BTreeNode *node)
{
    GdbBlock *block;

    if (node == NULL || node->block->offset == 0)
    {
        return;
    }
    block = node->block;

    gdbFreeBlock(block->db, block->offset, block->type);

#if 0
    rawFileSeek(block->db->fp, block->offset, SEEK_SET);
    gdbPad(block->db->fp, block->size);
#endif
}
Example #3
0
void
gdbWriteHeader(GDatabase *db)
{
    uint8_t version[2];
    uint8_t type;
    offset_t offset;

    if (db == NULL || db->idxRawFile == NULL)
    {
        return;
    }

    version[0] = DB_MAJOR_VER;
    version[1] = DB_MINOR_VER;

    type = db->type;

    rawFileSeek(db->idxRawFile, 0, SEEK_SET);
    offset = 0;

    /* Write the magic string. */

    rawFilePuts(db->idxRawFile, offset, DB_MAGIC);
    offset += strlen(DB_MAGIC);

    rawFileWrite(db->idxRawFile, offset, version, sizeof(uint8_t), 2, LOC_DB_0011);
    offset += (sizeof(uint8_t) * 2);

    rawFileWrite(db->idxRawFile, offset, &type,   sizeof(uint8_t), 1, LOC_DB_0012);
    offset += sizeof(uint8_t);

    if (DB_HEADER_BLOCK_SIZE > DB_HEADER_DATA_SIZE)
    {
        gdbPad(db->idxRawFile, offset, DB_HEADER_BLOCK_SIZE - DB_HEADER_DATA_SIZE);
    }
}
Example #4
0
uint8_t
gdbGetFreeBlockList(GDatabase *db, GdbFreeBlock **blocks, uint32_t *count)
{
    GdbFreeBlock *blockList;
    uint32_t listSize;
    uint8_t *buffer;
    size_t   s;
    uint32_t i, counter = 0;
    offset_t __offset;

    if (blocks == NULL || count == NULL)
    {
        return 0;
    }
    *blocks = NULL;

    /* Seek to the start of the block list. */
    rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET, SEEK_SET);
    __offset = DB_FREE_BLOCK_LIST_OFFSET;
    if (rawFileRead(db->idxRawFile, __offset, &db->freeBlockCount, sizeof(uint32_t), 1, LOC_DB_0001) != 1)
    {
        db->freeBlockCount = 0;
    }
    else
    {
        __offset += sizeof(uint32_t);
    }
    db->freeBlockCount = gdb_ntoh_uint32(db->freeBlockCount);

    *count = db->freeBlockCount;

    if (db->freeBlockCount == 0)
    {
        return 0;
    }
    /* Get the total size of the free blocks list. */
    listSize = db->freeBlockCount * (sizeof(uint16_t) + sizeof(offset_t));

    /* Allocate the buffer. */
    MEM_CHECK(buffer = (uint8_t *)SAFE_MALLOC(listSize, LOC_DB_0002));

    /* Read in the list. */
    //rawFileSeek(db->idxRawFile, DB_FREE_BLOCK_LIST_OFFSET + sizeof(uint32_t), SEEK_SET);
    if ((s = rawFileRead(db->idxRawFile, __offset, buffer, 1, listSize, LOC_DB_0003)) != listSize)
    {
        dbg_log(SEC_0131_DB, 0)(LOGSTDOUT,"error:gdbGetFreeBlockList: Truncated block list.\n"
                          "Expected %d bytes, got %d bytes. Block list offset = %d\n"
                          "Free block count = %d. Filename = %s\n",
                          listSize, s, DB_FREE_BLOCK_LIST_OFFSET, db->freeBlockCount,
                          db->filename);
        abort();
    }

    MEM_CHECK(blockList = (GdbFreeBlock *)SAFE_MALLOC(db->freeBlockCount * sizeof(GdbFreeBlock), LOC_DB_0004));

    for (i = 0; i < db->freeBlockCount; i++)
    {
        blockList[i].size   = gdbGet16(buffer, &counter);
        blockList[i].offset = gdbGetOffset(buffer, &counter);
    }

    *blocks = blockList;

    SAFE_FREE(buffer, LOC_DB_0005);

    return 1;
}
Example #5
0
uint8_t
gdbReadHeader(GDatabase *db)
{
    uint8_t  version[2];
    uint8_t  buffer[DB_HEADER_DATA_SIZE];
    uint32_t counter;

    if (db == NULL || db->idxRawFile == NULL)
    {
        return 0;
    }

    rawFileSeek(db->idxRawFile, 0, SEEK_SET);
    if (rawFileRead(db->idxRawFile, 0, buffer, DB_HEADER_DATA_SIZE, 1, LOC_DB_0009) != 1)
    {
        sys_log(LOGSTDOUT, "error:gdbReadHeader: Truncated database.\n");
        return 0;
    }

    if(0)/*debug*/
    {
        int idx;
        sys_log(LOGSTDOUT, "[DEBUG]gdbReadHeader: header: ");
        for(idx = 0; idx < DB_HEADER_DATA_SIZE; idx ++)
        {
            sys_log(LOGSTDOUT, "%02x ", *(buffer + idx));
        }
        sys_log(LOGSTDOUT, "\n");
    }

    /* Check the magic string. */
    if (0 != __safe_strncmp(buffer + DB_OFFSET_MAGIC, (const uint8_t *)DB_MAGIC, 8, LOC_DB_0010))
    {
        sys_log(LOGSTDOUT, "error:gdbReadHeader: Invalid database signature.\n");
        return 0;
    }

    counter = 8;

    version[0] = gdbGet8(buffer, &counter);
    version[1] = gdbGet8(buffer, &counter);

    if (version[0] != DB_MAJOR_VER || version[1] != DB_MINOR_VER)
    {
        sys_log(LOGSTDOUT, "error:gdbReadHeader: Unsupported database version %d.%d\n",
                            version[0], version[1]);

        return 0;
    }

    db->type = gdbGet8(buffer, &counter);

    if (GDB_INDEX_FILE != db->type && GDB_DATA_FILE != db->type)
    {
        sys_log(LOGSTDOUT, "error:gdbReadHeader: Unsupported database type.\n");

        return 0;
    }

    return 1;
}