Esempio n. 1
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);
}
Esempio n. 2
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);	
}