//Delete a given file in a given directory. BOOL DeleteFatFile(__FAT32_FS* pFat32Fs,CHAR* pszFileName) { __FAT32_SHORTENTRY ShortEntry; __FAT32_SHORTENTRY* pFileEntry = NULL; DWORD dwParentClus = 0; DWORD dwParentOffset = 0; DWORD dwStartClus = 0; DWORD dwSector = 0; BOOL bResult = FALSE; BYTE* pBuffer = NULL; if(!GetDirEntry(pFat32Fs, pszFileName, &ShortEntry, &dwParentClus, //Parent directory's cluster where this entry resides. &dwParentOffset)) //Cluster offset. { goto __TERMINAL; } //Release the cluster chain this file occupies. dwStartClus = (DWORD)(ShortEntry.wFirstClusHi) << 16; dwStartClus += (DWORD)ShortEntry.wFirstClusLow; ReleaseClusterChain(pFat32Fs,dwStartClus); //Remove the directory entry in it's parent directory. pBuffer = (BYTE*)KMemAlloc(pFat32Fs->dwClusterSize,KMEM_SIZE_TYPE_ANY); if(NULL == pBuffer) { goto __TERMINAL; } dwSector = GetClusterSector(pFat32Fs,dwParentClus); if(0 == dwSector) { goto __TERMINAL; } if(!ReadDeviceSector((__COMMON_OBJECT*)pFat32Fs->pPartition, dwSector, pFat32Fs->SectorPerClus, pBuffer)) { goto __TERMINAL; } pFileEntry = (__FAT32_SHORTENTRY*)(pBuffer + dwParentOffset); memzero(pFileEntry,sizeof(__FAT32_SHORTENTRY)); pFileEntry->FileName[0] = (CHAR)0xE5; //Empty this short entry. if(!WriteDeviceSector((__COMMON_OBJECT*)pFat32Fs->pPartition, dwSector, pFat32Fs->SectorPerClus, pBuffer)) { goto __TERMINAL; } bResult = TRUE; __TERMINAL: if(pBuffer) { KMemFree(pBuffer,KMEM_SIZE_TYPE_ANY,0); } return bResult; }
//Implementation of _SetEndOfFile. static BOOL _SetEndOfFile( __COMMON_OBJECT* lpDev) { __FAT32_FS* pFat32Fs = NULL; __FAT32_FILE* pFat32File = NULL; BYTE* pClusBuffer = NULL; __FAT32_SHORTENTRY* pfse = NULL; DWORD dwNextCluster = 0; DWORD dwSector = 0; BOOL bSetOk = FALSE; pFat32File = (__FAT32_FILE*)(((__DEVICE_OBJECT*)lpDev)->lpDevExtension); pFat32Fs = pFat32File->pFileSystem; pClusBuffer = (BYTE*)FatMem_Alloc(pFat32Fs->dwClusterSize); if(NULL == pClusBuffer) //Can not allocate buffer. { goto __TERMINAL; } //Now update the file's directory entry. dwSector = GetClusterSector(pFat32Fs,pFat32File->dwParentClus); if(0 == dwSector) { goto __TERMINAL; } if(!ReadDeviceSector((__COMMON_OBJECT*)pFat32Fs->pPartition, dwSector, pFat32Fs->SectorPerClus, pClusBuffer)) { goto __TERMINAL; } pfse = (__FAT32_SHORTENTRY*)(pClusBuffer + pFat32File->dwParentOffset); pFat32File->dwFileSize = pFat32File->dwCurrPos; pfse->dwFileSize = pFat32File->dwFileSize; WriteDeviceSector((__COMMON_OBJECT*)pFat32Fs->pPartition, dwSector, pFat32Fs->SectorPerClus, pClusBuffer); if(pFat32File->dwCurrPos < pFat32Fs->dwClusterSize ) { dwNextCluster = 1; } else { dwNextCluster = pFat32File->dwCurrPos/pFat32Fs->dwClusterSize; if((pFat32File->dwCurrPos+1)%pFat32Fs->dwClusterSize) { dwNextCluster ++; } } dwNextCluster ++; ReleaseClusterChain(pFat32Fs,dwNextCluster); bSetOk = TRUE; __TERMINAL: FatMem_Free(pClusBuffer); return bSetOk; }