void std_fclose(FILE *handle) { if (DS::isGBAMPAvailable()) { FAT_fclose((FAT_FILE *) handle); return; } handle->used = false; }
bool savestate(const char *filename) #endif { int tag_number; int totalsize=0; int mysize; #if !MOVIEPLAYER save_ptr file = dest; #else bool retval=false; save_ptr file; file=FAT_fopen(filename,"r+b"); //indicate that no blocks have been saved (this is done for the "restore_variables" function) blocks_loaded = 0; if (file==NO_FILE) { file=FAT_fopen(filename,"wb"); } if (file!=NO_FILE) #endif { //Messes up the variables, but encodes them in a way that contains no internal pointers prepare_variables_for_savestate(); for (tag_number=0;tag_number<NUM_TAGS;tag_number++) { //inidcate that the block has been saved (so "restore_variables" doesn't try to guess values) blocks_loaded |= (1<<tag_number); mysize=dumpdata(&file,tag_number); totalsize+=mysize; } #if MOVIEPLAYER FAT_fclose(file); // build_chr_decode(); //BECAUSE OF POSSIBLE STACK OVERFLOW retval=true; #endif //Restores the variables so we can run the emulator again restore_variables_for_loadstate(); } #if !MOVIEPLAYER return totalsize; #else return retval; #endif }
int CreateTestFile (tIORes* pRes, tFATFile* pFile, const char* name, unsigned long size, int close) { unsigned long pos; size_t s; uchar c; FAT_fopen (&stdio_FATFS, pRes, pFile, name, "wb"); if (*pRes != ior_OK) return 0; for (pos = 0; pos < size; pos++) { c = (uchar)(pos % 251); s = FAT_fwrite (&stdio_FATFS, pRes, &c, 1, 1, pFile); if (*pRes != ior_OK) return 0; if (s != 1) { *pRes = ior_PROHIBITED; // $$$ return 0; } } if (close) { FAT_fclose (&stdio_FATFS, pRes, pFile); if (*pRes != ior_OK) return 0; } else { FAT_rewind (&stdio_FATFS, pRes, pFile); if (*pRes != ior_OK) return 0; } return 1; }
bool loadstate(const char* filename) #endif { //save old values of Crash detector and sound volume //crash detector will become enabled, and sound will be muted later int old_crash_disabled = _crash_disabled; int soundvol; #if !MOVIEPLAYER u8* limit=src+statesize; load_ptr file=src; #else File file; #endif bool do_load=true; u32 tag; int size; //enable crash detector - if loading doesn't complete within 5 seconds, it crashed _crash_disabled = 0; //Set that no block has loaded yet, this is changed later when loadblock is called for each tag blocks_loaded = 0; #if MOVIEPLAYER file=FAT_fopen(filename,"r"); if (file==NO_FILE) { do_load=false; } #endif if (do_load) { //Read old sound volume, and stop sound on GB channels soundvol=REG_SOUNDCNT_L; REG_SOUNDCNT_L=0; do { int tagid; //verify presence of VERS tag as first thing in file if (!read_u32(file,tag)) break; //look up tag tagid=tag_search(tag,tags,NUM_TAGS); if (tagid!=VERSION_TAG) { seek_ahead(file,-4); #if !MOVIEPLAYER //reboot game, but don't clear some stuff do_not_decompress=1; do_not_reset_all=1; loadcart(romnumber,emuflags,0); //Restore previous sound volume (fixme, we get audio playing while we re-decompress the game) REG_SOUNDCNT_L=soundvol; //Messes up variables so that the emulator won't run anymore (later we un-mess them) prepare_variables_for_savestate(); do_not_decompress=0; do_not_reset_all=0; #endif load_old_savestate(&file); break; } else { if (!read_u32(file,size)) break; //discard version block loadblock(&file,tag,size); #if !MOVIEPLAYER //reboot game to a blank slate do_not_decompress=1; loadcart(romnumber,emuflags,0); //Restore sound volume REG_SOUNDCNT_L=soundvol; do_not_decompress=0; #endif //Messes up variables so that the emulator won't run anymore (later we un-mess them) prepare_variables_for_savestate(); //load all other tags while (1) { if (!read_u32(file,tag)) break; if (!read_u32(file,size)) break; loadblock(&file,tag,size); } } } while (0); #if MOVIEPLAYER FAT_fclose(file); #endif //Now we restore the variables so the emulator runs again restore_variables_for_loadstate(); restore_more_variables_for_loadstate(); } //restore previous state of crash detector _crash_disabled = old_crash_disabled; #if MOVIEPLAYER return do_load; #endif }
uint TestDir10(void) { tIORes res; uint result = 1; uint i, cnt; char name[12+1+13]; tReadDirState ReadDirState; tFATDirectoryEntry DirEntry; tFATFSStatus FSStatus1, FSStatus2; tFATFile file; uint32 FreeSpace1, FreeSpace2; int subtest; // tbd: allocate almost all free space to big file(s), leave 1 free cluster // create directory ending up with disk full, create empty files in it // until it needs to grow and fails FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus1); CHK(res == ior_OK); FAT_mkdir (&stdio_FATFS, &res, "level1", mt_S_IWUSR); CHK(res == ior_OK); for (subtest = 0; subtest < 2; subtest++) { FAT_fopen (&stdio_FATFS, &res, &file, "huge0000", "wb"); CHK(res == ior_OK); FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); FreeSpace1 = FSStatus2.TotalFreeClusterCount * FSStatus2.ClusterSize; CHK(FreeSpace1 / FSStatus2.TotalFreeClusterCount == FSStatus2.ClusterSize); CHK(FreeSpace1 >= 2 * FSStatus2.ClusterSize); if (!subtest) { FreeSpace2 = 0; } else { FreeSpace1 -= FSStatus2.ClusterSize; FreeSpace2 = FSStatus2.ClusterSize; } FAT_fseek (&stdio_FATFS, &res, &file, FreeSpace1); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "huge0001", "wb"); CHK(res == ior_OK); FAT_fseek (&stdio_FATFS, &res, &file, FreeSpace2); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); if (subtest) { int idx; for (idx = 0; idx < 2; idx++) { FAT_unlink (&stdio_FATFS, &res, "huge0000"); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "huge0000", "wb"); CHK(res == ior_OK); FAT_fseek (&stdio_FATFS, &res, &file, FreeSpace1); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); } } for (cnt = i = 0; i < UINT_MAX; i++) { strcpy (name, "level1\\"); sprintf (name + strlen(name), "%u", i); FAT_fopen (&stdio_FATFS, &res, &file, name, "wb"); CHK((res == ior_OK) || (res == ior_NO_SPACE)); if (res == ior_OK) { FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); cnt++; } else break; } CHK(cnt == 16 - 2); FAT_opendir (&stdio_FATFS, &res, "level1", &ReadDirState); CHK(res == ior_OK); for (i = 0; i < cnt; i++) { char s[13]; sprintf (name, "%u", i); lSkipDotEntries: FAT_readdir (&stdio_FATFS, &res, &ReadDirState, &DirEntry); CHK(res == ior_OK); CHK(DirEntryNameToASCIIZ (&DirEntry, s) == s); if (s[0] == '.') goto lSkipDotEntries; CHK(!strcmp (name, s)); } FAT_readdir (&stdio_FATFS, &res, &ReadDirState, &DirEntry); CHK(res == ior_NO_MORE_FILES); FAT_closedir (&stdio_FATFS, &res, &ReadDirState); CHK(res == ior_OK); for (i = 0; i < cnt; i++) { strcpy (name, "level1\\"); sprintf (name + strlen(name), "%u", i); FAT_unlink (&stdio_FATFS, &res, name); CHK(res == ior_OK); } FAT_unlink (&stdio_FATFS, &res, "huge0001"); CHK(res == ior_OK); FAT_unlink (&stdio_FATFS, &res, "huge0000"); CHK(res == ior_OK); } FAT_rmdir (&stdio_FATFS, &res, "level1"); CHK(res == ior_OK); FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); result = 0; lend: return result; }
uint TestDirFileMixup1(void) { tIORes res; uint result = 1; tFATFile file; tFATFSStatus FSStatus1, FSStatus2; FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus1); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "wb"); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_chdir (&stdio_FATFS, &res, "file1", 0); CHK(res == ior_FILE_FOUND); FAT_mkdir (&stdio_FATFS, &res, "file1", mt_S_IWUSR); CHK(res == ior_FILE_FOUND); FAT_rmdir (&stdio_FATFS, &res, "file1"); CHK(res == ior_FILE_FOUND); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb"); CHK(res == ior_NOT_FOUND); FAT_mkdir (&stdio_FATFS, &res, "level1", mt_S_IWUSR); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "level1", "rb"); CHK(res == ior_DIR_FOUND); FAT_unlink (&stdio_FATFS, &res, "level1"); CHK(res == ior_DIR_FOUND); FAT_chdir (&stdio_FATFS, &res, "level1", 0); CHK(res == ior_OK); FAT_chdir (&stdio_FATFS, &res, "..", 0); CHK(res == ior_OK); FAT_rmdir (&stdio_FATFS, &res, "level1"); CHK(res == ior_OK); FAT_chdir (&stdio_FATFS, &res, "..", 0); CHK(res == ior_NOT_FOUND); FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); result = 0; lend: return result; }
uint TestFile6(void) { tIORes res; uint result = 1; tFATFSStatus FSStatus1, FSStatus2; tpOpenFileSearchFxn pOldOpenFileSearch; pOldOpenFileSearch = stdio_FATFS.pOpenFileSearch; stdio_FATFS.pOpenFileSearch = &TestFile6OpenFileSearch; memset (&TestFile6File1, 0, sizeof(TestFile6File1)); memset (&TestFile6File2, 0, sizeof(TestFile6File2)); FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus1); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &TestFile6File1, "file1", "wb"); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &TestFile6File2, "file1", "wb"); CHK(res == ior_PROHIBITED); FAT_fopen (&stdio_FATFS, &res, &TestFile6File2, "file1", "rb"); CHK(res == ior_PROHIBITED); FAT_utime (&stdio_FATFS, &res, "file1", 1980, 1, 1, 0, 0, 0); CHK(res == ior_PROHIBITED); FAT_chmod (&stdio_FATFS, &res, "file1", mt_S_IRWXU); CHK(res == ior_PROHIBITED); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_PROHIBITED); FAT_fclose (&stdio_FATFS, &res, &TestFile6File1); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &TestFile6File1, "file1", "rb"); CHK(res == ior_OK); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_PROHIBITED); FAT_fopen (&stdio_FATFS, &res, &TestFile6File2, "file1", "wb"); CHK(res == ior_PROHIBITED); FAT_utime (&stdio_FATFS, &res, "file1", 1980, 1, 1, 0, 0, 0); CHK(res == ior_PROHIBITED); FAT_chmod (&stdio_FATFS, &res, "file1", mt_S_IRWXU); CHK(res == ior_PROHIBITED); FAT_fopen (&stdio_FATFS, &res, &TestFile6File2, "file1", "rb"); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &TestFile6File1); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &TestFile6File2); CHK(res == ior_OK); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_OK); FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); result = 0; lend: stdio_FATFS.pOpenFileSearch = pOldOpenFileSearch; return result; }
uint TestFile5(void) { tIORes res; uint result = 1; tFATFile file; tFATFSStatus FSStatus1, FSStatus2; tFATFileStatus FStatus; size_t s; FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus1); CHK(res == ior_OK); FAT_utime (&stdio_FATFS, &res, "file1", 2107, 12, 31, 23, 59, 58); CHK(res == ior_NOT_FOUND); FAT_chmod (&stdio_FATFS, &res, "file1", mt_S_IRWXU); CHK(res == ior_NOT_FOUND); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK); CHK(res == ior_NOT_FOUND); FAT_stat (&stdio_FATFS, &res, "file1", &FStatus); CHK(res == ior_NOT_FOUND); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "wb"); CHK(res == ior_OK); s = FAT_fwrite (&stdio_FATFS, &res, "Blah", 1, strlen("Blah"), &file); CHK((res == ior_OK) && (s == strlen("Blah"))); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_W_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_X_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_F_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK | am_X_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK | am_W_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_X_OK | am_W_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK | am_W_OK | am_X_OK); CHK(res == ior_OK); FAT_stat (&stdio_FATFS, &res, "file1", &FStatus); CHK(res == ior_OK); CHK(FStatus.Attribute == dea_ARCHIVE); CHK(FStatus.FileSize == strlen("Blah")); FAT_chmod (&stdio_FATFS, &res, "file1", mt_S_IRUSR | mt_S_IXUSR); CHK(res == ior_OK); FAT_stat (&stdio_FATFS, &res, "file1", &FStatus); CHK(res == ior_OK); CHK(FStatus.Attribute == (dea_ARCHIVE | dea_READ_ONLY)); CHK(FStatus.FileSize == strlen("Blah")); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb"); CHK(res == ior_OK); s = FAT_fwrite (&stdio_FATFS, &res, "Blah", 1, strlen("Blah"), &file); CHK(res == ior_PROHIBITED); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "wb"); CHK(res == ior_PROHIBITED); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb+"); CHK(res == ior_PROHIBITED); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_PROHIBITED); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_W_OK); CHK(res == ior_PROHIBITED); FAT_access (&stdio_FATFS, &res, "file1", am_X_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_F_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK | am_X_OK); CHK(res == ior_OK); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK | am_W_OK); CHK(res == ior_PROHIBITED); FAT_access (&stdio_FATFS, &res, "file1", am_X_OK | am_W_OK); CHK(res == ior_PROHIBITED); FAT_access (&stdio_FATFS, &res, "file1", am_R_OK | am_W_OK | am_X_OK); CHK(res == ior_PROHIBITED); FAT_utime (&stdio_FATFS, &res, "file1", 2107, 12, 31, 23, 59, 58); CHK(res == ior_PROHIBITED); FAT_chmod (&stdio_FATFS, &res, "file1", mt_S_IRWXU); CHK(res == ior_OK); FAT_utime (&stdio_FATFS, &res, "file1", 2107, 12, 31, 23, 59, 58); CHK(res == ior_OK); FAT_stat (&stdio_FATFS, &res, "file1", &FStatus); CHK(res == ior_OK); CHK(FStatus.Attribute == dea_ARCHIVE); CHK(FStatus.FileSize == strlen("Blah")); CHK(FStatus.Year == 2107); CHK(FStatus.Month == 12); CHK(FStatus.Day == 31); CHK(FStatus.Hour == 23); CHK(FStatus.Minute == 59); CHK(FStatus.Second == 58); FAT_utime (&stdio_FATFS, &res, "file1", 1980, 1, 1, 0, 0, 0); CHK(res == ior_OK); FAT_stat (&stdio_FATFS, &res, "file1", &FStatus); CHK(res == ior_OK); CHK(FStatus.Attribute == dea_ARCHIVE); CHK(FStatus.FileSize == strlen("Blah")); CHK(FStatus.Year == 1980); CHK(FStatus.Month == 1); CHK(FStatus.Day == 1); CHK(FStatus.Hour == 0); CHK(FStatus.Minute == 0); CHK(FStatus.Second == 0); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_OK); FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); result = 0; lend: return result; }
uint TestFile4(void) { tIORes res; uint result = 1; tFATFile file; tFATFSStatus FSStatus1, FSStatus2; uint32 aFileSize[]= {0, 1, // ~= 0 FAT_SECTOR_SIZE-1, FAT_SECTOR_SIZE, FAT_SECTOR_SIZE+1, // ~= sector -1, 0, 1, // ~= cluster -1, 0, 1, // ~= 2 clusters -1, 0, 1}; // ~= 4 clusters int FileSizeCount = 5; int FileSizeIndex = 0; int CloseIndex; int ReadSizeIndex = 0; FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus1); CHK(res == ior_OK); if (FSStatus1.ClusterSize != FAT_SECTOR_SIZE) { aFileSize[FileSizeCount++] = FSStatus1.ClusterSize - 1; aFileSize[FileSizeCount++] = FSStatus1.ClusterSize; aFileSize[FileSizeCount++] = FSStatus1.ClusterSize + 1; } aFileSize[FileSizeCount++] = 2 * FSStatus1.ClusterSize - 1; aFileSize[FileSizeCount++] = 2 * FSStatus1.ClusterSize; aFileSize[FileSizeCount++] = 2 * FSStatus1.ClusterSize + 1; aFileSize[FileSizeCount++] = 4 * FSStatus1.ClusterSize - 1; aFileSize[FileSizeCount++] = 4 * FSStatus1.ClusterSize; aFileSize[FileSizeCount++] = 4 * FSStatus1.ClusterSize + 1; for (CloseIndex = 0; CloseIndex <= 1; CloseIndex++) { for (FileSizeIndex = 0; FileSizeIndex < FileSizeCount; FileSizeIndex++) { for (ReadSizeIndex = 0; ReadSizeIndex < FileSizeCount; ReadSizeIndex++) { uint32 size, pos=0, ReadPos; size_t ReadSize; size = (size_t)aFileSize[FileSizeIndex]; ReadSize = aFileSize[ReadSizeIndex]; CreateTestFile (&res, &file, "file1", size, CloseIndex); CHK(res == ior_OK); if (!CloseIndex) { FAT_rewind (&stdio_FATFS, &res, &file); CHK(res == ior_OK); } else { FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb"); CHK(res == ior_OK); } FAT_ftell (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(pos == ReadPos); while (pos < size) { FAT_fseek (&stdio_FATFS, &res, &file, pos); CHK(res == ior_OK); FAT_ftell (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(pos == ReadPos); pos += MIN(size - pos, ReadSize); if (!ReadSize) break; } FAT_fseek (&stdio_FATFS, &res, &file, 0); CHK(res == ior_OK); FAT_ftell (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(ReadPos == 0); FAT_fseek (&stdio_FATFS, &res, &file, size); CHK(res == ior_OK); FAT_ftell (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(ReadPos == size); if (CloseIndex) { FAT_fseek (&stdio_FATFS, &res, &file, size + 1); CHK(res == ior_ARGUMENT_OUT_OF_RANGE); FAT_ftell (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(ReadPos == size); } FAT_fsize (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(size == ReadPos); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_OK); } } } FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); result = 0; lend: return result; }
uint TestFile3(void) { tIORes res; uint result = 1; tFATFile file; tFATFSStatus FSStatus1, FSStatus2; uchar buf[4UL*MAX_CLUSTER_SIZE+1]; uint32 aFileSize[]= {0, 1, // ~= 0 FAT_SECTOR_SIZE-1, FAT_SECTOR_SIZE, FAT_SECTOR_SIZE+1, // ~= sector -1, 0, 1, // ~= cluster -1, 0, 1, // ~= 2 clusters -1, 0, 1}; // ~= 4 clusters int FileSizeCount = 5; int FileSizeIndex = 0; int CloseIndex; int ReadSizeIndex = 0; FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus1); CHK(res == ior_OK); if (FSStatus1.ClusterSize != FAT_SECTOR_SIZE) { aFileSize[FileSizeCount++] = FSStatus1.ClusterSize - 1; aFileSize[FileSizeCount++] = FSStatus1.ClusterSize; aFileSize[FileSizeCount++] = FSStatus1.ClusterSize + 1; } aFileSize[FileSizeCount++] = 2 * FSStatus1.ClusterSize - 1; aFileSize[FileSizeCount++] = 2 * FSStatus1.ClusterSize; aFileSize[FileSizeCount++] = 2 * FSStatus1.ClusterSize + 1; aFileSize[FileSizeCount++] = 4 * FSStatus1.ClusterSize - 1; aFileSize[FileSizeCount++] = 4 * FSStatus1.ClusterSize; aFileSize[FileSizeCount++] = 4 * FSStatus1.ClusterSize + 1; for (CloseIndex = 0; CloseIndex <= 1; CloseIndex++) { for (FileSizeIndex = 0; FileSizeIndex < FileSizeCount; FileSizeIndex++) { for (ReadSizeIndex = 0; ReadSizeIndex < FileSizeCount; ReadSizeIndex++) { uint32 size, pos=0, ReadPos; size_t ReadSize, s; int IsEof; size = (size_t)aFileSize[FileSizeIndex]; ReadSize = aFileSize[ReadSizeIndex]; CreateTestFile (&res, &file, "file1", size, CloseIndex); CHK(res == ior_OK); if (!CloseIndex) { FAT_rewind (&stdio_FATFS, &res, &file); CHK(res == ior_OK); } else { FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb"); CHK(res == ior_OK); } FAT_fsize (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(size == ReadPos); FAT_feof (&stdio_FATFS, &res, &file, &IsEof); CHK(res == ior_OK); CHK(size ? !IsEof : IsEof); FAT_ftell (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(pos == ReadPos); while (pos < size) { uint32 i; s = FAT_fread (&stdio_FATFS, &res, buf, 1, ReadSize, &file); CHK(res == ior_OK); CHK(s == MIN(size - pos, ReadSize)); if (!ReadSize) break; for (i = 0; i < s; i++) { CHK(buf[i] == (pos + i) % 251); } pos += s; FAT_ftell (&stdio_FATFS, &res, &file, &ReadPos); CHK(res == ior_OK); CHK(pos == ReadPos); } if (ReadSize) { s = FAT_fread (&stdio_FATFS, &res, buf, 1, ReadSize, &file); CHK(res == ior_OK); CHK(s == 0); CHK(pos == size); FAT_feof (&stdio_FATFS, &res, &file, &IsEof); CHK(res == ior_OK); CHK(IsEof); } FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_OK); } } } FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); result = 0; lend: return result; }
uint TestFile2(void) { tIORes res; uint result = 1; tFATFile file; tFATFSStatus FSStatus1, FSStatus2; char c; size_t s; uint disk; for (disk = 'a'; disk <= 'z'; disk++) { char DiskPath[]="a:\\"; char FilePath[128]; DiskPath[0] = disk; FAT_statvfs (&stdio_FATFS, &res, DiskPath, &FSStatus1); if (((res == ior_ERR_IO) && (disk < 'c')) || (res == ior_BAD_NAME)) continue; CHK(res == ior_OK); strcpy (FilePath, "a:file1"); FilePath[0] = disk; FAT_fopen (&stdio_FATFS, &res, &file, FilePath, "wb"); CHK(res == ior_OK); s = FAT_fwrite (&stdio_FATFS, &res, "A", 1, 1, &file); CHK((res == ior_OK) && (s == 1)); FAT_rewind (&stdio_FATFS, &res, &file); CHK(res == ior_OK); c = 'z'; s = FAT_fread (&stdio_FATFS, &res, &c, 1, 1, &file); CHK((res == ior_OK) && (s == 1) && (c == 'A')); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); strcpy (FilePath, "a:\\.\\file1"); FilePath[0] = disk; FAT_fopen (&stdio_FATFS, &res, &file, FilePath, "rb"); CHK(res == ior_OK); c = 'z'; s = FAT_fread (&stdio_FATFS, &res, &c, 1, 1, &file); CHK((res == ior_OK) && (s == 1) && (c == 'A')); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); strcpy (FilePath, "a:file1"); FilePath[0] = disk; FAT_fopen (&stdio_FATFS, &res, &file, FilePath, "wb"); CHK(res == ior_OK); c = 'z'; s = FAT_fread (&stdio_FATFS, &res, &c, 1, 1, &file); CHK((res == ior_OK) && (s == 0) && (c == 'z')); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); strcpy (FilePath, "a:file1"); FilePath[0] = disk; FAT_unlink (&stdio_FATFS, &res, FilePath); CHK(res == ior_OK); FAT_statvfs (&stdio_FATFS, &res, DiskPath, &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); } result = 0; lend: return result; }
uint TestFile1(void) { tIORes res; uint result = 1; tFATFile file; tFATFSStatus FSStatus1, FSStatus2; FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus1); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, ".", "rb"); CHK(res == ior_DIR_FOUND); FAT_unlink (&stdio_FATFS, &res, "."); CHK(res == ior_DIR_FOUND); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_NOT_FOUND); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb"); CHK(res == ior_NOT_FOUND); FAT_fopen (&stdio_FATFS, &res, &file, "level1\\file1", "wb"); CHK(res == ior_BAD_NAME); FAT_fopen (&stdio_FATFS, &res, &file, "\xE5", "wb"); CHK(res == ior_BAD_NAME); FAT_fopen (&stdio_FATFS, &res, &file, "z:\\file1", "rb"); CHK(res == ior_BAD_NAME); FAT_fopen (&stdio_FATFS, &res, &file, NULL, "rb"); CHK(res == ior_INVALID_ARGUMENT); FAT_fopen (&stdio_FATFS, &res, &file, "file1", NULL); CHK(res == ior_INVALID_ARGUMENT); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "blah"); CHK(res == ior_INVALID_ARGUMENT); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "wb"); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb"); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "wb"); CHK(res == ior_OK); FAT_fclose (&stdio_FATFS, &res, &file); CHK(res == ior_OK); FAT_fopen (&stdio_FATFS, &res, &file, "file1\\", "rb"); CHK(res == ior_BAD_NAME); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_OK); FAT_unlink (&stdio_FATFS, &res, "file1"); CHK(res == ior_NOT_FOUND); FAT_fopen (&stdio_FATFS, &res, &file, "file1", "rb"); CHK(res == ior_NOT_FOUND); FAT_statvfs (&stdio_FATFS, &res, "\\", &FSStatus2); CHK(res == ior_OK); CHK(FSStatus2.TotalFreeClusterCount == FSStatus1.TotalFreeClusterCount); result = 0; lend: return result; }