ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

FF_T_UINT32 FF_FindFreeCluster ( FF_IOMAN pIoman,
FF_ERROR pError 
)

Finds a Free Cluster and returns its number.

Parameters:
pIomanIOMAN Object.
Returns:
The number of the cluster found to be free.
0 on error.

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;
}

Generated on Fri May 25 2012 05:54:40 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.