delete.c
来自「ST5518机顶盒系统文件系统源代码!绝对超值!」· C语言 代码 · 共 355 行 · 第 1/2 页
C
355 行
} Result = OpenPartition(0, 1, "Dev1", &DiskHandle); if (TEST_PASSED == Result) { STAVFS_FileHandle_t FileHandle; U64 FileSize; U32 DataWritten, DataRead; int i, j; /* Create four files; cyclic mode needs a declared size one byte bigger than the amount of data we actually want to write */ I64_SetValue (OpenMode & STAVFS_CYCLIC_MODE ? 1 + sizeof (StdData) : sizeof (StdData), 0, FileSize); for (i = 0; (i < 4); ++i) { if (ST_NO_ERROR != STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FNames[i], OpenMode, FileSize, &FileHandle)) { STTBX_Print (("Error opening %s file\n", OpenDescr)); Result = TEST_FAILED; --i; /* don't try to delete this file below */ } else { if (ST_NO_ERROR != STAVFS_WriteFile (FileHandle, StdData, sizeof(StdData), &DataWritten)) { STTBX_Print (("Error writing %s file\n", OpenDescr)); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_CloseFile (FileHandle)) { STTBX_Print (("Error closing %s file\n", OpenDescr)); Result = TEST_FAILED; } } if (Result == TEST_FAILED) { /* delete files that were opened; the last one has index i (-1 for none) */ for ( ; i >= 0; --i) { if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FNames[i])) { STTBX_Print (("Error deleting %s file\n", OpenDescr)); } } break; /* don't try to create any more */ } } if (Result == TEST_PASSED) { /* delete files 1 and 3 */ if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FNames[1])) { STTBX_Print (("Error deleting %s file\n", OpenDescr)); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FNames[3])) { STTBX_Print (("Error deleting %s file\n", OpenDescr)); Result = TEST_FAILED; } /* run defrag if appropriate */ if ((Result == TEST_PASSED) && DoDefrag && (ST_NO_ERROR != STAVFS_FileSystemDefragmentation (DiskHandle))) { STTBX_Print (("Error defragmenting file system\n")); Result = TEST_FAILED; } /* check which files still exist by attempting to open them for reading (only) */ if (TEST_PASSED == Result) { OpenMode = OpenMode & ~STAVFS_WRITE_MODE; I64_SetValue (0, 0, FileSize); /* provides between failed delete and incorrect open for read of nonexistant file: in the latter case the new file size will be zero and so a write next time around will fail */ for (i = 0; (i < 4); ++i) { Error = STAVFS_OpenFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FNames[i], OpenMode, FileSize, &FileHandle); switch(i) { case 1: case 3: if(Error != STAVFS_ERROR_NO_SUCH_FILE) { STTBX_Print (("Managed to reopen deleted %s file\n", OpenDescr)); Result = TEST_FAILED; } break; case 0: case 2: if(Error != ST_NO_ERROR) { STTBX_Print (("Error opening %s file\n", OpenDescr)); Result = TEST_FAILED; } /* for all cases except cyclic files (which do not currently support reading data after reopening), check the data is still what it should be. We don't check the exact amount available, but note STAVFS_ERROR_EOF is returned if there is less than we request */ if ((OpenMode & STAVFS_CYCLIC_MODE) == 0) { if (ST_NO_ERROR != STAVFS_ReadFile (FileHandle, Buffer, sizeof(StdData), &DataRead)) { STTBX_Print (("Error reading %s file\n", OpenDescr)); Result = TEST_FAILED; } else if (memcmp(StdData, Buffer, sizeof(StdData))) { for (j = 0; j < sizeof(StdData); ++j) { if (StdData[j] != Buffer[j]) { Result = TEST_FAILED; STTBX_Print(("Write-Read mismatch on %s file #%i: first error is at byte offset %d within block\n", OpenDescr, i, j)); break; } } } } break; } /* attempt to close any file we opened (but later only bother trying to delete those we didn't try to delete before) */ if ((Error == ST_NO_ERROR) && (ST_NO_ERROR != STAVFS_CloseFile (FileHandle))) { STTBX_Print (("Error closing %s file\n", OpenDescr)); } /* try all four files in all cases */ } } /* delete the remaining two files in all cases where we created them */ if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FNames[0])) { STTBX_Print (("Error deleting %s file\n", OpenDescr)); Result = TEST_FAILED; } if (ST_NO_ERROR != STAVFS_DeleteFile (DiskHandle, STAVFS_NULL_FILE_HANDLE, FNames[2])) { STTBX_Print (("Error deleting %s file\n", OpenDescr)); Result = TEST_FAILED; } } if (TEST_PASSED != ClosePartition("Dev1", DiskHandle)) { Result = TEST_FAILED; /* message already displayed */ } } return(Result);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?