⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 position.c

📁 ST5518机顶盒系统文件系统源代码!绝对超值!
💻 C
📖 第 1 页 / 共 5 页
字号:
    STAVFS_OpenMode_t   OpenMode = STAVFS_WRITE_MODE | STAVFS_READ_MODE;    switch (Variant)    {    case TEST_VARIANT_A:        /* regular files; no special settings */        break;            case TEST_VARIANT_B:        /* cyclic files; just to check they do the same thing when not wrapping */                OpenMode |= STAVFS_CYCLIC_MODE;        break;            default:        return TEST_NOT_WRITTEN;    }        Result = OpenPartition(0, 1, "Dev1", &DiskHandle); /* 1 sector per cluster */    if (TEST_PASSED == Result)    {        STAVFS_FileHandle_t FileHandle;                U64 FileSize, Pos;        U32 DataWritten, DataRead;        U32 WriteSeekPos = 250, FirstReadSize = 400;        U32 FirstWriteSize = 500, WriteChunkSize = 100, WritePos;        int i;                I64_SetValue (1000, 0, FileSize);        if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME,                                            OpenMode, FileSize, &FileHandle))        {            STTBX_Print (("Error opening file\n"));            Result = TEST_FAILED;        }        else        {            I64_SetValue (WriteSeekPos, 0, Pos); /* for SeekFile below */            /* write some data */            if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, StdData, FirstWriteSize, &DataWritten))            {                STTBX_Print (("Error writing initial data to file\n"));                Result = TEST_FAILED;            }            else if (ST_NO_ERROR != STAVFS_SeekFile (FileHandle, Pos, STAVFS_START, TRUE))            {                STTBX_Print (("Error write-seeking\n"));                Result = TEST_FAILED;            }            else if (TEST_PASSED != CheckPos (FileHandle, 0, WriteSeekPos, "After write seek: "))            {                Result = TEST_FAILED; /* message already displayed */            }                        WritePos = WriteSeekPos;                        if (Result == TEST_PASSED)            {                for (i = 0; i < 2; ++i)                {                    if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, StdData, WriteChunkSize, &DataWritten))                    {                        STTBX_Print (("Error writing chunk %i to file\n", i));                        Result = TEST_FAILED;                        break;                    }                    WritePos += WriteChunkSize;                    if (TEST_PASSED != CheckPos (FileHandle, 0, WritePos, "After writing a chunk: "))                    {                        Result = TEST_FAILED; /* message already displayed */                        break;                    }                }            }            /* read back in a 400 byte chunk, checking those positionnings are correct. It should count up to              WriteSeekPos, and then fall into 100 byte chunks */                        if (Result == TEST_PASSED)            {                if (ST_NO_ERROR != STAVFS_ReadFile (FileHandle, Buffer, FirstReadSize, &DataRead)                    || DataRead != FirstReadSize)                {                    STTBX_Print (("Error reading file\n"));                    Result = TEST_FAILED;                }                            for (i = 0; (i < WriteSeekPos) && (Result == TEST_PASSED); ++i)                {                    if (Buffer[i] != i)                    {                        STTBX_Print (("Write-read mismatch in originally written data, position %i\n", i));                        Result = TEST_FAILED;                    }                }                for (i = WriteSeekPos; (i < FirstReadSize) && (Result == TEST_PASSED); ++i)                {                    if (Buffer[i] != (i - WriteSeekPos) % WriteChunkSize)                    {                        STTBX_Print (("Write-read mismatch in rewritten data, position %i\n", i));                        Result = TEST_FAILED;                    }                }            }            if (Result == TEST_PASSED)            {                /* write a further 100 bytes, and check they are read back correctly */                                if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, StdData, WriteChunkSize, &DataWritten))                {                    STTBX_Print (("Error writing chunk 3 to file\n"));                    Result = TEST_FAILED;                }                else                {                    WritePos += WriteChunkSize;                    if (TEST_PASSED != CheckPos (FileHandle, FirstReadSize, WritePos, "After read and extra write: "))                    {                        Result = TEST_FAILED; /* message already displayed */                    }                    else if (ST_NO_ERROR != STAVFS_ReadFile (FileHandle, Buffer, WritePos - FirstReadSize, &DataRead)                        || DataRead != WritePos - FirstReadSize)                    {                        STTBX_Print (("Error reading file\n"));                        Result = TEST_FAILED;                    }                                        for (i = 0; (i < WritePos - FirstReadSize)  && (Result == TEST_PASSED); ++i)                    {                        if (Buffer[i] != (FirstReadSize + i - WriteSeekPos) % WriteChunkSize)                        {                            STTBX_Print (("Write-read mismatch, position %i\n", FirstReadSize + i));                            Result = TEST_FAILED;                        }                    }                }            }                        if (Result == TEST_PASSED)            {                /* confirm that further attempt to read returns EOF and no data */                                if (ST_NO_ERROR == STAVFS_ReadFile (FileHandle, Buffer, 100, &DataRead)                    || DataRead != 0)                {                    STTBX_Print (("Managed to read data past EOF\n"));                    Result = TEST_FAILED;                }            }                        /* close and delete file in all cases where we sucessfully created it */                        if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle))            {                STTBX_Print (("Error closing file\n"));                Result = TEST_FAILED;            }                        if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME))            {                STTBX_Print (("Error deleting file\n"));                Result = TEST_FAILED;            }        }                if (TEST_PASSED != ClosePartition("Dev1", DiskHandle))        {            Result = TEST_FAILED; /* message already displayed */        }    }    return (Result);}/******************************************************************************Function Name : CyclicSeek  Description : Check the seekable positions after wrap-around in a cyclic file   Parameters :******************************************************************************/static TestResult_t CyclicSeek(int TestNo, TestVariant_t Variant){    TestResult_t        Result;    STAVFS_Handle_t     DiskHandle;    STAVFS_OpenMode_t   OpenMode = STAVFS_WRITE_MODE | STAVFS_READ_MODE;    switch (Variant)    {    case TEST_VARIANT_A:        /* test checks a behaviour applicable to cyclic files only;          it does not apply to regular files */        return TEST_NOT_APPLICABLE;            case TEST_VARIANT_B:        /* cyclic files */                OpenMode |= STAVFS_CYCLIC_MODE;        break;            default:        return TEST_NOT_WRITTEN;    }        Result = OpenPartition(0, 2, "Dev2", &DiskHandle); /* 2 sectors per cluster */    if (TEST_PASSED == Result)    {        STAVFS_FileHandle_t FileHandle;                U64 FileSize;        S64 Pos;        U32 AllocSize = 2 * DISK_SECTOR_SIZE; /* file size as U32 */        U32 DataSize = 1200;                  /* total amount of data we will write */        U32 ChunkSize = 300, ReadSize = 550;  /* size of write chunks, and mid-cycle read */        U32 DataWritten, DataRead;            /* for STAVFS_Write/ReadFile */        int i, j;                /* list of positions to try seek and read from below */        S32 SeekReadList[] = {            1100, /* current cycle */            1000, /* previous cycle, other sector */            300,  /* previous cycle, current sector */            1023, /* last byte of previous cycle */            1024, /* first byte of new cycle */            177,  /* earliest byte that is still valid */        };        U32 SeekReadChunkSize = 10;                /* list of bad positions to attempt to seek to later */        S32 BadSeeks[] = {            1250,   /* past end */            1201,   /* second byte not yet written; usual EOF rule now with wrapping */            0, 100, /* overwritten after wrapping */            -100,   /* never valid; just check wrapping hasn't affected this */            176,    /* last byte written that is now invalid */        };        I64_SetValue (AllocSize, 0, FileSize); /* 1 cluster */                if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FILE_NAME,                                            OpenMode, FileSize, &FileHandle))        {            STTBX_Print (("Error opening file\n"));            Result = TEST_FAILED;        }        else        {            /* write four 300-byte chunks to the file, with a read in the middle to prevent overflow              when we wrap around. This is all standard stuff by now */                          for (i = 0; i < DataSize/ChunkSize; ++i)            {                if (i == 2) /* insert a read before third write */                {                    if (ST_NO_ERROR != STAVFS_ReadFile (FileHandle, Buffer, ReadSize, &DataRead))                    {                        STTBX_Print (("Error reading file\n"));                        Result = TEST_FAILED;                        break;                    }                }                                if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, StdData, ChunkSize, &DataWritten))                {                    STTBX_Print (("Error writing file\n"));                    Result = TEST_FAILED;                    break;                }                            }                        if (Result == TEST_PASSED)            {                                /* quick check that the wrap-around has been properly recognised */                Result = CheckPos (FileHandle, ReadSize, DataSize, "Data written: ");                                /* seek to the positions listed above and check we can read from them correctly. Each chunk                  consists of a byte counter incrementing from zero (see StdData definition above), so this                  is easy to check */                for (i = 0; (i < TABLE_LEN(SeekReadList)) && (Result == TEST_PASSED); ++i)                {                    I64_SetValue (SeekReadList[i], 0, Pos);                    if (ST_NO_ERROR != STAVFS_SeekFile (FileHandle, Pos, STAVFS_START, FALSE))                    {                        STTBX_Print (("Error read-seeking to position %i\n", SeekReadList[i]));                        Result = TEST_FAILED;                    }                    if (ST_NO_ERROR != STAVFS_ReadFile (FileHandle, Buffer, SeekReadChunkSize, &DataRead))                    {                        STTBX_Print (("Error reading from position %i\n", SeekReadList[i]));                        Result = TEST_FAILED;                    }                    for (j = 0; j < SeekReadChunkSize; ++j)                    {                        if (Buffer[j] != StdData[(SeekReadList[i] + j) % ChunkSize])                        {                            STTBX_Print (("Seek to position %i led to wrong data read at %i\n",                                SeekReadList[i], SeekReadList[i] + j));                                                        Result = TEST_FAILED;                        }                    }                }            }                        if (Result == TEST_PASSED)            {                /* check last byte (1199) and reading over EOF behaviour */                memset (Buffer, 0, 10);                                I64_SetValue (DataSize - 1, 0, Pos);                if (ST_NO_ERROR != STAVFS_SeekFile (FileHandle, Pos, STAVFS_START, FALSE))                {                    STTBX_Print (("Error read-seeking to last byte written (%i)\n", DataSize - 1));                    Result = TEST_FAILED;                }                else if (STAVFS_ERROR_UNDERFLOW != STAVFS_ReadFile (FileHandle, Buffer, SeekReadChunkSize, &DataRead)                    || DataRead != 1)                {                    STTBX_Print (("Request to read %i bytes from position %i should return 1 byte only\n",                        SeekReadChunkSize, DataSize - 1));                    Result = TEST_FAILED;                }                else if (Buffer[0] != StdData[ChunkSize-1]) /* the last byte is the final byte in a chunk */                {                    STTBX_Print (("Wrong final byte read back after seeking to it\n"));                    Result = TEST_FAILED;                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -