/* **************************************************************************** * euint32 file_fread(File *file,euint32 offset, euint32 size,euint8 *buf) * Description: This function reads 'size' bytes from 'file' starting at * 'offset' and puts the result in '*buf'. * Return value: amount of bytes actually read (can differ from the given * size when the file was smaller */ euint32 file_fread(File *file,euint32 offset, euint32 size,euint8 *buf) { //OSTaskSuspend(1); euint32 bytes_read=0,size_left=size,coffset=offset; euint32 cclus,csec,cbyte; euint32 rclus,rsec; euint32 btr; euint8 *tbuf; if(!file_getAttr(file,FILE_STATUS_OPEN))return(0); if(offset>=file->FileSize) size_left=0; /* Offset check */ if( (offset+size > file->FileSize) && size_left!=0) size_left=file->FileSize-offset; while(size_left>0){ //printf("size_left = %d\n", size_left); cclus = coffset/(512*file->fs->volumeId.SectorsPerCluster); csec = (coffset/(512))%file->fs->volumeId.SectorsPerCluster; cbyte = coffset%512; if(cbyte!=0 || size_left<512){ btr = 512-(coffset%512)>=size_left?size_left:512-(coffset%512); }else{ btr = 512; } if((fat_LogicToDiscCluster(file->fs,&(file->Cache),cclus))!=0){ return(0); } rclus=file->Cache.DiscCluster; rsec=fs_clusterToSector(file->fs,rclus); if(btr==512){ /*part_readBuf(file->fs->part,rsec+csec,buf+bytes_read);*/ part_directSectorRead(file->fs->part,rsec+csec,buf+bytes_read); }else{ /*part_readBuf(file->fs->part,rsec+csec,tbuf);*/ tbuf = part_getSect(file->fs->part,rsec+csec,IOM_MODE_READONLY); memCpy(tbuf+(coffset%512),buf+bytes_read,btr); part_relSect(file->fs->part,tbuf); } coffset+=btr; bytes_read+=btr; size_left-=btr; } //OSTaskResume(1); return(bytes_read); }
/* **************************************************************************** * unsigned long file_fread(File *file,unsigned long offset, unsigned long size,unsigned char *buf) * Description: This function reads 'size' bytes from 'file' starting at * 'offset' and puts the result in '*buf'. * Return value: amount of bytes actually read (can differ from the given * size when the file was smaller */ unsigned long file_fread(File *file,unsigned long offset, unsigned long size,unsigned char *buf) { unsigned long bytes_read=0,size_left=size,coffset=offset; unsigned long cclus,csec,cbyte; unsigned long rclus,rsec; unsigned long btr; unsigned char *tbuf; if(!file_getAttr(file,FILE_STATUS_OPEN))return(0); if(offset>=file->FileSize) size_left=0; /* Offset check */ if( (offset+size > file->FileSize) && size_left!=0) size_left=file->FileSize-offset; while(size_left>0){ cclus = coffset/(512*file->fs->volumeId.SectorsPerCluster); csec = (coffset/(512))%file->fs->volumeId.SectorsPerCluster; cbyte = coffset%512; if(cbyte!=0 || size_left<512){ btr = 512-(coffset%512)>=size_left?size_left:512-(coffset%512); }else{ btr = 512; } if((fat_LogicToDiscCluster(file->fs,&(file->Cache),cclus))!=0){ return(0); } rclus=file->Cache.DiscCluster; rsec=fs_clusterToSector(file->fs,rclus); if(btr==512){ /*part_readBuf(file->fs->part,rsec+csec,buf+bytes_read);*/ part_directSectorRead(file->fs->part,rsec+csec,buf+bytes_read); }else{ /*part_readBuf(file->fs->part,rsec+csec,tbuf);*/ tbuf = part_getSect(file->fs->part,rsec+csec,IOM_MODE_READONLY); memCpy(tbuf+(coffset%512),buf+bytes_read,btr); part_relSect(file->fs->part,tbuf); } coffset+=btr; bytes_read+=btr; size_left-=btr; } return(bytes_read); }
/* **************************************************************************** * euint32 dir_findinDir(FileSystem *fs, eint8* fatname,euint32 firstcluster, FileLocation *loc, euint8 mode) * This function will search for an existing (fatname) or free directory entry * in a directory, following the clusterchains. * Return value: 0 on failure, firstcluster on finding file, and 1 on finding free spot. */ euint32 dir_findinDir(FileSystem *fs, eint8* fatname,euint32 firstcluster, FileLocation *loc, euint8 mode) { euint32 c=0,cluster; ClusterChain Cache; Cache.DiscCluster = Cache.FirstCluster = firstcluster; Cache.LogicCluster = Cache.LastCluster = Cache.Linear = 0; if(firstcluster <= 1) { return(dir_findinRootArea(fs,fatname,loc,mode)); } while(!fat_LogicToDiscCluster(fs,&Cache,c++)) { if((cluster=dir_findinCluster(fs,Cache.DiscCluster,fatname,loc,mode))) { return(cluster); } } 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); }
esint8 ls_getRealDirEntry(DirList *dlist) { euint8* buf; if(dlist->Cache.FirstCluster<=1)return(-1); if(fat_LogicToDiscCluster(dlist->fs, &(dlist->Cache), (dlist->rEntry)/(16 * dlist->fs->volumeId.SectorsPerCluster))){ return(-1); } buf = part_getSect(dlist->fs->part, fs_clusterToSector(dlist->fs,dlist->Cache.DiscCluster) + (dlist->rEntry/16)%dlist->fs->volumeId.SectorsPerCluster, IOM_MODE_READONLY); /*memCpy(buf+(dlist->rEntry%16)*32,&(dlist->currentEntry),32);*/ ls_fileEntryToDirListEntry(dlist,buf,32*(dlist->rEntry%16)); part_relSect(dlist->fs->part,buf); return(0); }
/* **************************************************************************** * euint32 file_fwrite(File* file,euint32 offset,euint32 size,euint8* buf) * Description: This function writes to a file, at offset 'offset' and size 'size'. * It also updates the FileSize in the object, and discstructure. * Return value: Bytes actually written. */ euint32 file_fwrite(File* file,euint32 offset,euint32 size,euint8* buf) { euint32 need_cluster; euint32 cclus,csec,cbyte; euint32 size_left=size,bytes_written=0; euint32 rclus,rsec; euint32 coffset=offset; euint16 btr; euint8 *tbuf; if(!file_getAttr(file,FILE_STATUS_OPEN) || !file_getAttr(file,FILE_STATUS_WRITE))return(0); if(offset>file->FileSize){ offset=file->FileSize; } need_cluster = file_requiredCluster(file,offset,size); if(need_cluster){ if(fat_allocClusterChain(file->fs,&(file->Cache),need_cluster+CLUSTER_PREALLOC_FILE)!=0){ return(0); } } while(size_left>0){ cclus = coffset/(512*file->fs->volumeId.SectorsPerCluster); csec = (coffset/(512))%file->fs->volumeId.SectorsPerCluster; cbyte = coffset%512; if(cbyte!=0 || size_left<512){ btr = 512-(coffset%512)>=size_left?size_left:512-(coffset%512); }else{ btr = 512; } if((fat_LogicToDiscCluster(file->fs,&(file->Cache),cclus))!=0){ file->FileSize+=bytes_written; dir_setFileSize(file->fs,&(file->Location),file->FileSize); return(bytes_written); } rclus=file->Cache.DiscCluster; rsec=fs_clusterToSector(file->fs,rclus); if(btr==512){ /*part_writeBuf(file->fs->part,rsec+csec,buf+bytes_written);*/ part_directSectorWrite(file->fs->part,rsec+csec,buf+bytes_written); }else{ /*part_readBuf(file->fs->part,rsec+csec,tbuf);*/ tbuf = part_getSect(file->fs->part,rsec+csec,IOM_MODE_READWRITE); memCpy(buf+bytes_written,tbuf+(coffset%512),btr); /*part_writeBuf(file->fs->part,rsec+csec,tbuf);*/ part_relSect(file->fs->part,tbuf); } coffset+=btr; bytes_written+=btr; size_left-=btr; } if(bytes_written>file->FileSize-offset){ file->FileSize+=bytes_written-(file->FileSize-offset); } return(bytes_written); }