⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
📖 第 1 页 / 共 2 页
字号:
    {
        base=
            fs->volumeId.ReservedSectorCount+
            fs->FatSectorCount*fs->volumeId.NumberOfFats;
    }
    else
    {
        base=
            fs->volumeId.ReservedSectorCount+
            fs->FatSectorCount*fs->volumeId.NumberOfFats+
            fs->volumeId.RootEntryCount/16;
    }
    return(((sector-base)-((sector-base)%fs->volumeId.SectorsPerCluster))/fs->volumeId.SectorsPerCluster+2 );
}
/*****************************************************************************/

/* ****************************************************************************
 * euint32 fs_getNextFreeCluster(FileSystem *fs,euint32 startingcluster)
 * Description: This functions searches for a free cluster, starting it's search at
 * cluster startingcluster. This allow to speed up searches and try to avoid
 * fragmentation. Implementing rollover search is still to be done.
 * Return value: If a free cluster is found it's number is returned. If none is
 * found 0 is returned.
*/
euint32 fs_getNextFreeCluster(FileSystem *fs,euint32 startingcluster)
{
    euint32 r;

    while(startingcluster<fs->DataClusterCount){
        r=fat_getNextClusterAddress(fs,startingcluster,0);
        if(r==0){
            return(startingcluster);
        }
        startingcluster++;
    }
    return(0);
}
/*****************************************************************************/

/* ****************************************************************************
 * euint32 fs_giveFreeClusterHint(FileSystem *fs)
 *
 * Description: This function should return a clusternumber that is free or
 * lies close before free clusters. The result MUST be checked to see if
 * it is free! Implementationhint: search the largest clusternumber in the
 * files in the rootdirectory.
 *
 * Return value: Returns it's best guess.
*/
euint32 fs_giveFreeClusterHint(FileSystem *fs)
{
    return(2); /* Now THIS is a hint ;) */
}
/*****************************************************************************/

/* ****************************************************************************
 * esint8 fs_findFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint32 *lastDir)
 *
 * Description: This function looks if the given filename is on the given fs
 * and, if found, fills in its location in loc.
 * The function will first check if the pathname starts with a slash. If so it will
 * set the starting directory to the rootdirectory. Else, it will take the firstcluster-
 * currentdir (That you can change with chdir()) as startingpoint.
 * The lastdir pointer will be the first cluster of the last directory fs_findfile
 * enters. It starts out at the root/current dir and then traverses the path along with
 * fs_findFile.
 * It is set to 0 in case of errors (like dir/dir/dir/file/dir/dir...)
 * Return value: Returns 0 when nothing was found, 1 when the thing found
 * was a file and 2 if the thing found was a directory.
*/

esint8 fs_findFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint32 *lastDir)
{
    euint32 fccd,tmpclus;
    eint8 ffname[11],*next,it=0,filefound=0;

    if(*filename=='/'){
        fccd = fs_getFirstClusterRootDir(fs);
        filename++;
        if(lastDir)*lastDir=fccd;
        if(!*filename){
            return(2);
        }
    }else{
        fccd = fs->FirstClusterCurrentDir;
        if(lastDir)*lastDir=fccd;
    }


    while((next=file_normalToFatName(filename,ffname))!=0){
        if((tmpclus=dir_findinDir(fs,ffname,fccd,loc,DIRFIND_FILE))==0){
            /* We didn't find what we wanted */
            /* We should check, to see if there is more after it, so that
             * we can invalidate lastDir
             */
            if((file_normalToFatName(next,ffname))!=0){
                if(lastDir)*lastDir=0;
            }
            return(0);
        }
        it++;
        if(loc->attrib&ATTR_DIRECTORY){
            fccd = tmpclus;
            filename = next;
            if(lastDir)*lastDir=fccd;
            if(filefound)*lastDir=0;
        }else{
            filefound=1;
            if((file_normalToFatName(next,ffname))!=0){
                if(lastDir)*lastDir=0;
                return(0);
            }else{
                filename=next;
            }
        }
    }

    if(it==0)return(0);
    if(loc->attrib&ATTR_DIRECTORY || !filefound)return(2);
    return(1);
}
/*****************************************************************************/

esint16 fs_findFreeFile(FileSystem *fs,eint8* filename,FileLocation *loc,euint8 mode)
{
    euint32 targetdir=0;
    eint8 ffname[11];

    if(fs_findFile(fs,filename,loc,&targetdir))return(0);
    if(!dir_getFatFileName(filename,ffname))return(0);
    if(dir_findinDir(fs,ffname,targetdir,loc,DIRFIND_FREE)){
        return(1);
    }else{
        if(dir_addCluster(fs,targetdir)){
            return(0);
        }else{
            if(dir_findinDir(fs,ffname,targetdir,loc,DIRFIND_FREE)){
                return(1);
            }
        }
    }

    return(0);
}
/*****************************************************************************/

/* ****************************************************************************
 * euint32 fs_getLastCluster(FileSystem *fs,ClusterChain *Cache)
 * Description: This function searches the last cluster of a chain.
 * Return value: The LastCluster (also stored in cache);
*/
euint32 fs_getLastCluster(FileSystem *fs,ClusterChain *Cache)
{
    if(Cache->DiscCluster==0){
        Cache->DiscCluster=Cache->FirstCluster;
        Cache->LogicCluster=0;
    }

    if(Cache->LastCluster==0)
    {
        while(fat_getNextClusterChain(fs, Cache)==0)
        {
            Cache->LogicCluster+=Cache->Linear;
            Cache->DiscCluster+=Cache->Linear;
            Cache->Linear=0;
        }
    }
    return(Cache->LastCluster);
}
/*****************************************************************************/

euint32 fs_getFirstClusterRootDir(FileSystem *fs)
{
    switch(fs->type){
        case FAT32:
            return(fs->volumeId.RootCluster);
        default:
            return(1);
    }
}
/*****************************************************************************/

void fs_initClusterChain(FileSystem *fs,ClusterChain *cache,euint32 cluster_addr)
{
    cache->FirstCluster=cluster_addr;
    cache->DiscCluster=cluster_addr;
    cache->LogicCluster=0;
    cache->LastCluster=0; /* Warning flag here */
    cache->Linear=0;
    cache->ClusterCount=0; /* 0 means NOT known */
}
/*****************************************************************************/

void fs_setFirstClusterInDirEntry(FileRecord *rec,euint32 cluster_addr)
{
    rec->FirstClusterHigh=cluster_addr>>16;
    rec->FirstClusterLow=cluster_addr&0xFFFF;
}
/*****************************************************************************/

esint8 fs_flushFs(FileSystem *fs)
{
    return(part_flushPart(fs->part,0,fs->SectorCount));
}
/*****************************************************************************/

esint8 fs_umount(FileSystem *fs)
{
    return(fs_flushFs(fs));
}
/*****************************************************************************/

esint8 fs_clearCluster(FileSystem *fs,euint32 cluster)
{
    euint16 c;
    euint8* buf;

    for(c=0;c<(fs->volumeId.SectorsPerCluster);c++){
        buf = part_getSect(fs->part,fs_clusterToSector(fs,cluster)+c,IOM_MODE_READWRITE);
        memClr(buf,512);
        part_relSect(fs->part,buf);
    }
    return(0);
}

esint8 fs_getFsInfo(FileSystem *fs,euint8 force_update)
{
    euint8 *buf;

    if(!fs->type==FAT32)return(0);
    buf = part_getSect(fs->part,FS_INFO_SECTOR,IOM_MODE_READONLY);
    if(ex_getb32(buf,0)!=FSINFO_MAGIC_BEGIN || ex_getb32(buf,508)!=FSINFO_MAGIC_END){
        part_relSect(fs->part,buf);
        return(-1);
    }
    fs->FreeClusterCount = ex_getb32(buf,488);
    fs->NextFreeCluster  = ex_getb32(buf,492);
    part_relSect(fs->part,buf);
    if(force_update){
        fs->FreeClusterCount=fat_countFreeClusters(fs);
    }
    return(0);
}

esint8 fs_setFsInfo(FileSystem *fs)
{
    euint8* buf;

    if(!fs->type==FAT32)return(0);
    buf = part_getSect(fs->part,FS_INFO_SECTOR,IOM_MODE_READWRITE);
    if(ex_getb32(buf,0)!=FSINFO_MAGIC_BEGIN || ex_getb32(buf,508)!=FSINFO_MAGIC_END){
        part_relSect(fs->part,buf);
        return(-1);
    }
    ex_setb32(buf,488,fs->FreeClusterCount);
    ex_setb32(buf,492,fs->NextFreeCluster);
    part_relSect(fs->part,buf);
    return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -