Home | Info | Community | Development | myReactOS | Contact Us
Finds a Free Cluster and returns its number.
Definition at line 481 of file ff_fat.c.
Referenced by FF_CreateClusterChain(), FF_ExtendDirectory(), FF_ExtendFile(), and FF_MountPartition().
{ FF_BUFFER *pBuffer; FF_T_UINT32 i, x, nCluster = pIoman->pPartition->LastFreeCluster; FF_T_UINT32 FatOffset; FF_T_UINT32 FatSector; FF_T_UINT32 FatSectorEntry; FF_T_UINT32 EntriesPerSector; FF_T_UINT32 FatEntry = 1; *pError = FF_ERR_NONE; #ifdef FF_FAT12_SUPPORT if(pIoman->pPartition->Type == FF_T_FAT12) { // FAT12 tables are too small to optimise, and would make it very complicated! return FF_FindFreeClusterOLD(pIoman, pError); } #endif if(pIoman->pPartition->Type == FF_T_FAT32) { EntriesPerSector = pIoman->BlkSize / 4; FatOffset = nCluster * 4; } else { EntriesPerSector = pIoman->BlkSize / 2; FatOffset = nCluster * 2; } // HT addition: don't use non-existing clusters if (nCluster >= pIoman->pPartition->NumClusters) { *pError = FF_ERR_FAT_NO_FREE_CLUSTERS; return 0; } FatSector = (FatOffset / pIoman->pPartition->BlkSize); for(i = FatSector; i < pIoman->pPartition->SectorsPerFAT; i++) { pBuffer = FF_GetBuffer(pIoman, pIoman->pPartition->FatBeginLBA + i, FF_MODE_READ); { if(!pBuffer) { *pError = FF_ERR_DEVICE_DRIVER_FAILED; return 0; } for(x = nCluster % EntriesPerSector; x < EntriesPerSector; x++) { if(pIoman->pPartition->Type == FF_T_FAT32) { FatOffset = x * 4; FatSectorEntry = FatOffset % pIoman->pPartition->BlkSize; FatEntry = FF_getLong(pBuffer->pBuffer, (FF_T_UINT16)FatSectorEntry); FatEntry &= 0x0fffffff; // Clear the top 4 bits. } else { FatOffset = x * 2; FatSectorEntry = FatOffset % pIoman->pPartition->BlkSize; FatEntry = (FF_T_UINT32) FF_getShort(pBuffer->pBuffer, (FF_T_UINT16)FatSectorEntry); } if(FatEntry == 0x00000000) { FF_ReleaseBuffer(pIoman, pBuffer); pIoman->pPartition->LastFreeCluster = nCluster; return nCluster; } nCluster++; } } FF_ReleaseBuffer(pIoman, pBuffer); } return 0; }