static int32_t recover_log_checkpoint()

in Sources/aliyun-log-c-sdk/log_persistent_manager.c [20:54]


static int32_t recover_log_checkpoint(log_persistent_manager * manager)
{
    FILE * tmpFile = fopen(manager->checkpoint_file_path, "rb");
    if (tmpFile == NULL)
    {
        if (errno == ENOENT)
        {
            return 0;
        }
        return -1;
    }
    fseek(tmpFile, 0, SEEK_END);
    long pos = ftell(tmpFile);
    if (pos == 0)
    {
        // empty file
        return 0;
    }
    long fixedPos = pos - pos%sizeof(log_persistent_checkpoint);
    long lastPos = fixedPos == 0? 0 : fixedPos - sizeof(log_persistent_checkpoint);
    fseek(tmpFile, lastPos, SEEK_SET);
    if (1 != fread((void *)&(manager->checkpoint), sizeof(log_persistent_checkpoint), 1, tmpFile))
    {
        fclose(tmpFile);
        return -2;
    }
    if (!is_valid_log_checkpoint(&(manager->checkpoint)))
    {
        fclose(tmpFile);
        return -3;
    }
    fclose(tmpFile);
    manager->checkpoint_file_size = pos;
    return 0;
}