esint8 ls_openDir(DirList *dlist,FileSystem *fs,eint8* dirname) { FileLocation loc; euint32 fc; dlist->fs=fs; if(fs_findFile(dlist->fs,dirname,&loc,&fc)!=2) { return(-1); } fs_initClusterChain(dlist->fs,&(dlist->Cache),fc); memClr(&(dlist->currentEntry),sizeof(dlist->currentEntry)); dlist->rEntry=0; dlist->cEntry=0xFFFF; return(0); }
/* **************************************************************************** * esint8 dir_addCluster(FileSystem *fs,euint32 firstCluster) * This function extends a directory by 1 cluster + optional the number of * clusters you want pre-allocated. It will also delete the contents of that * cluster. (or clusters) * Return value: 0 on success, -1 on fail */ esint8 dir_addCluster(FileSystem *fs,euint32 firstCluster) { euint32 lastc,logicalc; ClusterChain cache; fs_initClusterChain(fs,&cache,firstCluster); if(fat_allocClusterChain(fs,&cache,1)) { return(-1); } lastc = fs_getLastCluster(fs,&cache); if(CLUSTER_PREALLOC_DIRECTORY) { if(fat_allocClusterChain(fs,&cache,CLUSTER_PREALLOC_DIRECTORY)) { return(-1); } logicalc = fat_DiscToLogicCluster(fs,firstCluster,lastc); while(!fat_LogicToDiscCluster(fs,&cache,++logicalc)) { fs_clearCluster(fs,cache.DiscCluster); } } else { fs_clearCluster(fs,lastc); } return(0); }
/* ***************************************************************************\ * signed eint8 file_fopen(FileSystem *fs,File* file,eint8* filename) * Description: This functions opens a file. * This function is about to be redesigned. No Docs. * Return value: */ esint8 file_fopen(File* file,FileSystem *fs,eint8* filename,eint8 mode) { FileLocation loc; FileRecord wtmp; eint8 fatfilename[LIST_MAXLENFILENAME]; euint32 sec; dir_getFatFileName(filename,fatfilename); switch(mode) { case MODE_READ: if(fs_findFile(fs,filename,&loc,0)==1) { dir_getFileStructure(fs,&(file->DirEntry), &loc); file_initFile(file,fs,&loc); file_setAttr(file,FILE_STATUS_OPEN,1); file_setAttr(file,FILE_STATUS_WRITE,0); return(0); } return(-1); break; case MODE_WRITE: if(fs_findFile(fs,filename,&loc,&sec)) /* File may NOT exist, but parent HAS to exist */ { return(-2); } if(sec==0){ /* Parent dir does not exist */ return(-4); } if(fs_findFreeFile(fs,filename,&loc,0)) { dir_createDefaultEntry(fs,&wtmp,fatfilename); dir_createDirectoryEntry(fs,&wtmp,&loc); memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp)); file_initFile(file,fs,&loc); sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs)); dir_setFirstCluster(file->fs,&(file->Location),sec); fs_setFirstClusterInDirEntry(&(file->DirEntry),sec); fs_initClusterChain(fs,&(file->Cache),sec); fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs)); file_setAttr(file,FILE_STATUS_OPEN,1); file_setAttr(file,FILE_STATUS_WRITE,1); return(0); } else { return(-3); } break; case MODE_APPEND: if(fs_findFile(fs,filename,&loc,0)==1) /* File exists */ { dir_getFileStructure(fs,&(file->DirEntry), &loc); file_initFile(file,fs,&loc); if(file->Cache.FirstCluster==0){ sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs)); dir_setFirstCluster(file->fs,&(file->Location),sec); fs_setFirstClusterInDirEntry(&(file->DirEntry),sec); fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs)); file_initFile(file,fs,&loc); } file_setpos(file,file->FileSize); file_setAttr(file,FILE_STATUS_OPEN,1); file_setAttr(file,FILE_STATUS_WRITE,1); } else /* File does not excist */ { if(fs_findFreeFile(fs,filename,&loc,0)) { dir_createDefaultEntry(fs,&wtmp,fatfilename); dir_createDirectoryEntry(fs,&wtmp,&loc); memCpy(&wtmp,&(file->DirEntry),sizeof(wtmp)); file_initFile(file,fs,&loc); sec=fs_getNextFreeCluster(file->fs,fs_giveFreeClusterHint(file->fs)); dir_setFirstCluster(file->fs,&(file->Location),sec); fs_setFirstClusterInDirEntry(&(file->DirEntry),sec); fs_initClusterChain(fs,&(file->Cache),sec); fat_setNextClusterAddress(fs,sec,fat_giveEocMarker(fs)); file_setAttr(file,FILE_STATUS_OPEN,1); file_setAttr(file,FILE_STATUS_WRITE,1); } else { return(-3); } } return(0); break; default: return(-4); break; } return(-5); }