yaffs2写文件操作

本文详细解析了YAFFS2文件系统中int yaffs_write()函数的实现过程,包括追加文件、读取缓存、写入数据到Flash等步骤。通过对不同情况的判断,如是否覆盖现有内容、是否需要写入缓存,实现对文件的高效写入管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int yaffs_write(int fd, const void *buf, unsigned int nbyte)
{
 yaffsfs_Handle *h = NULL;
 yaffs_Object *obj = NULL;
 int pos = 0;
 int nWritten = -1;
 int writeThrough = 0;
 
 yaffsfs_Lock();
 h = yaffsfs_GetHandlePointer(fd);
 obj = yaffsfs_GetHandleObject(fd);
 
 if(!h || !obj)
 {
  // bad handle
  yaffsfs_SetError(-EBADF);  
 }
 else if( h && obj && h->readOnly)
 {
  // todo error
 }
 else if( h && obj)
 {
  if(h->append)
//如果是追加方式

  {
   pos =  yaffs_GetObjectFileLength(obj);
  }
  else
  {
   pos = h->position;
  }
  
  nWritten = yaffs_WriteDataToFile(obj,(__u8 const *)buf,pos,nbyte,writeThrough);
//如果writeThrough=1, 直接写入flash
  
  if(nWritten >= 0)
  {
   h->position = pos + nWritten;
  }
  else
  {
   //todo error
  }
  
 }
 
 yaffsfs_Unlock();
 
 
 return (nWritten >= 0) ? nWritten : -1;

}
----------------
int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
			  int nBytes, int writeThrough)
{

	int chunk;
	__u32 start;
	int nToCopy;
	int n = nBytes;
	int nDone = 0;
	int nToWriteBack;
	int startOfWrite = offset;
	int chunkWritten = 0;
	int nBytesRead;

	yaffs_Device *dev;

	dev = in->myDev;

	while (n > 0 && chunkWritten >= 0) {
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值