/* **************************************************************************** * euint32 dir_findinCluster(FileSystem *fs,euint32 cluster,eint8 *fatname, FileLocation *loc, euint8 mode) * This function will search for an existing (fatname) or free directory entry * in a full cluster. * Return value: 0 on failure, firstcluster on finding file, and 1 on finding free spot. */ euint32 dir_findinCluster(FileSystem *fs,euint32 cluster,eint8 *fatname, FileLocation *loc, euint8 mode) { euint8 c,*buf=0; euint32 fclus; for(c=0; c<fs->volumeId.SectorsPerCluster; c++) { buf = part_getSect(fs->part,fs_clusterToSector(fs,cluster)+c,IOM_MODE_READONLY); if((fclus=dir_findinBuf(buf,fatname,loc,mode))) { if(loc)loc->Sector=fs_clusterToSector(fs,cluster)+c; part_relSect(fs->part,buf); return(fclus); } } part_relSect(fs->part,buf); return(0); }
/* **************************************************************************** * 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); }
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); }