⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nandfile.424

📁 PDA上的CF CARD 文件系统的建立程式
💻 424
📖 第 1 页 / 共 4 页
字号:
	// no lump other than the current lump is found
	//   either the required lump is not found or the required lump
	//   is the currnet lump
	if (curLumpNo == NAND_END_LUMP)
	{
		// the required lump is not found
		pfile->currentLump = NAND_END_LUMP;
		pfile->lumpfpos = 0;
		NAND_FFS_Errno = ERROR_FILE_STRUCTURE;
		return 0;
	}

	// the required lump is already the current lump
  }

  // the lump to be written is now in pfile->currentLump
  curLumpNo = pfile->currentLump;
  remains = size;
  // part 1: copy the data from the user-provided buffer to handle buffer
  //   the handle buffer is dirty afterwards

  // the size of data from the file position to the end of the lump
  freeSize = LUMP_DATA_SIZE - (pfile->fpos - pfile->lumpfpos);
  if (remains <= freeSize)
  {
	// the current lump has enough data to satisfy the read operation
	freeSize = remains;
  }


  /* marked by chilong 01/13/2002 
  if (NAND_FFS_readLump(curLumpNo, (unsigned char **)&curLump) == -1)
  	return 0;
     marked by chilong 01/13/2002 */
     
  /**** modified by chilong 01/13/2002 ****/
  if (bNeed2ReadAgain)
  {
  	if (NAND_FFS_readLump(curLumpNo, (unsigned char **)&curLump) == -1)
  		return 0;
  }
  /**** modified by chilong 01/13/2002 ****/

#ifdef NAND_FFS_DEBUG
  sprintf(NAND_FFS_DebugString, "[NAND_FFS_write] part1 write starts: curLumpNo: %d", curLumpNo);
  SprintStringLn(NAND_FFS_DebugString);
#endif
  	
  des = (unsigned char *)((unsigned long)curLump + L_HEADER + (pfile->fpos - pfile->lumpfpos));
  // prevLumpNo = curLumpNo;
  
  // if both buffers are 4 byte-aligned
  if ((long)des % 4 == 0 && (long)buffer % 4 == 0)
  {
  	for (i = 0; i < freeSize / 4; i++)
	{
		*((int*)des) = *((int*)buffer);
		des += 4;
		buffer += 4;
	}
			
	for (i = 0; i < freeSize % 4; i++)
		*des++ = *buffer++;
  }
  else if ((long)des % 2 == 0 && (long)buffer % 2 == 0)// if both buffers are 2 byte-aligned
  {
	for (i = 0; i < freeSize / 2; i++)
	{
		*((short*)des) = *((short*)buffer);
		des += 2;
		buffer += 2;
	}
			
	for (i = 0; i < freeSize % 2; i++)
		*des++ = *buffer++;
  }
  else if ((long) des % 2 == 1 && (long) buffer % 2 == 1)
  {
	// move 1 byte first to make both des & buffer located on even addresses
	*des++ = *buffer++;
		
	if ((long)des % 4 == 0 && (long)buffer % 4 == 0)
	{
		for (i = 0; i < (freeSize-1) / 4; i++)
		{
			*((int*)des) = *((int*)buffer);
			des += 4;
			buffer += 4;
		}
		
		for (i = 0; i < (freeSize-1) % 4; i++)
			*des++ = *buffer++;
	}
	else
	{
		for (i = 0; i < (freeSize-1) / 2; i++)
		{
			*((short*)des) = *((short*)buffer);
			des += 2;
			buffer += 2;
		}
		
		for (i = 0; i < (freeSize-1) % 2; i++)
			*des++ = *buffer++;
	}
  }
  else
  {
  	for (i = 0; i < freeSize; i++)
		*des++ = *buffer++;
  }

  // update the positions
  remains -= freeSize;

  pfile->fpos += freeSize;

  
//jason
//******************** jason	force to update the data in NAND flash
//	For the bug to overwrite smaller sting into the exist file , will fail .
	pfile->dirEntry.fsize = pfile->fpos;
	curLump->actualSize = (pfile->fpos - pfile->lumpfpos);
	if (remains == 0 || curLump->nextLump != NAND_END_LUMP)
	{
		if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
			return freeSize;
		
		bCurLumpWritten = 1;
	}

//***************************
#if 0
  if (pfile->fpos > pfile->dirEntry.fsize)
  {
	// file size grows
	pfile->dirEntry.fsize = pfile->fpos;
  }

  if (curLump->actualSize < (pfile->fpos - pfile->lumpfpos))
  {
	curLump->actualSize = (pfile->fpos - pfile->lumpfpos);
	
	/* marked by chilong 01/17/2002
	if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
		return freeSize;
	   marked by chilong 01/17/2002 */
	   
	/**** modified by chilong 01/17/2002 ****/
	// chilong: if curLump->nextLump == NAND_END_LUMP
	//	we'll write curLump later when we invoke NAND_findFreeLump()
	if (remains == 0 || curLump->nextLump != NAND_END_LUMP)
	{
		if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
			return freeSize;
		
		bCurLumpWritten = 1;
	}
	/**** modified by chilong 01/17/2002 ****/
  }
#endif

  if (remains == 0)
  {
  	/*  moved to NAND_FFS_write()
	sc_getTime(&now);
	pfile->dirEntry.time = now.hour * 2048 + now.minute * 32 + now.second / 2;
	pfile->dirEntry.date = (now.year-1980) * 512 + now.month * 32 + now.day;
	*/
	
  #ifdef NAND_FFS_DEBUG
  	SprintStringLn("[NAND_FFS_write] finished at the first part");
  #endif
	return freeSize;
  }

  writeCount += freeSize;

  // part 2: write not done yet

  // continue with next block till the last block
  while (remains > LUMP_DATA_SIZE)
  {
  	/**** added by chilong 01/13/2002 ****/
  	bNeed2ReadAgain = 1;
  	/**** added by chilong 01/13/2002 ****/
  	
	// update the file position of the 1st byte of the current block
	pfile->lumpfpos += LUMP_DATA_SIZE;

	// get next block
	prevLumpNo = curLumpNo;
	
	/**** added by chilong 01/16/2002 ****/
	prevLump = curLump;
	/**** added by chilong 01/16/2002 ****/
	
	/* marked by chilong 01/16/2001 
	// chilong: we've read this lump before
	if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
		return writeCount;
	   marked by chilong 01/16/2001 */
		
	curLumpNo = curLump->nextLump;
	if (curLumpNo == NAND_END_LUMP)
	{
		// a new lump is needed, go get it
		curLumpNo = NAND_findFreeLump();
		if (curLumpNo == NAND_END_LUMP)
		{
			pfile->currentLump = NAND_END_LUMP;
			pfile->lumpfpos = 0;
			NAND_FFS_Errno = ERROR_ALLOC_LUMP;

			/* moved to NAND_FFS_write()
			sc_getTime(&now);
			pfile->dirEntry.time = now.hour * 2048 + now.minute * 32 + now.second / 2;
			pfile->dirEntry.date = (now.year-1980) * 512 + now.month * 32 + now.day;
			*/ 
			
			/**** added by chilong 01/17/2002 ****/
  			if (bCurLumpWritten == 0)
  			{
				if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
					return writeCount;
  			}
			/**** added by chilong 01/17/2002 ****/
			
			return writeCount;
		}
		   
		// write previous lump header back to flash
		if (prevLumpNo != NAND_END_LUMP)
		{
			/* marked by chilong 01/16/2002
			
			// chilong: prevLump is the same as curLump right now
			//	and we've read curLump
			if (NAND_FFS_readLump(prevLumpNo, (unsigned char **)&prevLump) == -1)
				return writeCount;

			   marked by chilong 01/16/2002 */
			
			prevLump->nextLump = curLumpNo;
			
			if (NAND_FFS_writeLump(prevLumpNo, (unsigned char*)prevLump) == -1)
				return writeCount;
		}

		if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
			return writeCount;
		// link the new block to the file block link
		// write current lump
		curLump->type = NAND_LUMP_TYPE_FILE;
		//curLump->userCount = 0;
		curLump->prevLump = prevLumpNo;
		curLump->nextLump = NAND_END_LUMP;
		curLump->size = NAND_LUMP_SIZE - sizeof(struct NAND_diskLump);
		curLump->actualSize = 0;
		
	//	if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
	//		return writeCount;
		
		bNeed2ReadAgain = 0;
			
	}
	
	// the current lump is the new lump
	pfile->currentLump = curLumpNo;

   	// store the original curLump temporarily
	//prevLumpNo = curLumpNo;
	
	/* marked by chilong 01/13/2002
	if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
		return writeCount;
	   marked by chilong 01/13/2002 */
	
	/**** modified by chilong 01/13/2002 ****/
	if (bNeed2ReadAgain)
	{
		if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
			return writeCount;
	}
	/**** modified by chilong 01/13/2002 ****/

	/**** added by chilong 01/17/2002 ****/
	bCurLumpWritten = 0;
	/**** added by chilong 01/17/2002 ****/
	
	des = (unsigned char *)((unsigned long)curLump + L_HEADER);
	
	// if both buffers are 4 byte-aligned
	if ((long)des % 4 == 0 && (long)buffer % 4 == 0)
	{
		for (i = 0; i < LUMP_DATA_SIZE / 4; i++)
		{
			*((int*)des) = *((int*)buffer);
			des += 4;
			buffer += 4;
		}
			
		for (i = 0; i < LUMP_DATA_SIZE % 4; i++)
			*des++ = *buffer++;
	}
	else if ((long)des % 2 == 0 && (long)buffer % 2 == 0)// if both buffers are 2 byte-aligned
	{
		for (i = 0; i < LUMP_DATA_SIZE / 2; i++)
		{
			*((short*)des) = *((short*)buffer);
			des += 2;
			buffer += 2;
		}
			
		for (i = 0; i < LUMP_DATA_SIZE % 2; i++)
			*des++ = *buffer++;
	}
	else if ((long)des % 2 == 1 && (long) buffer % 2 == 1)
	{
		// move 1 byte first to make both des & buffer located on even addresses
		*des++ = *buffer++;
			
		if ((long)des % 4 == 0 && (long)buffer % 4 == 0)
		{
			for (i = 0; i < (LUMP_DATA_SIZE-1) / 4; i++)
			{
				*((int*)des) = *((int*)buffer);
				des += 4;
				buffer += 4;
			}
			
			for (i = 0; i < (LUMP_DATA_SIZE-1) % 4; i++)
				*des++ = *buffer++;
		}
		else
		{
			for (i = 0; i < (LUMP_DATA_SIZE-1) / 2; i++)
			{
				*((short*)des) = *((short*)buffer);
				des += 2;
				buffer += 2;
			}
			
			for (i = 0; i < (LUMP_DATA_SIZE-1) % 2; i++)
				*des++ = *buffer++;
		}
	}
	else
	{
  		for (i = 0; i < LUMP_DATA_SIZE; i++)
			*des++ = *buffer++;
	}
	
	remains -= LUMP_DATA_SIZE;
	pfile->fpos += LUMP_DATA_SIZE;
	
	if (pfile->fpos > pfile->dirEntry.fsize)
	{
		// file size grows
		pfile->dirEntry.fsize = pfile->fpos;
	}
	
	curLump->actualSize = LUMP_DATA_SIZE;
	
	/* marked by chilong 01/17/2002
	if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
		return (writeCount + LUMP_DATA_SIZE);
	   marked by chilong 01/17/2002 */
	   
	/**** modified by chilong 01/17/2002 ****/
	if (curLump->nextLump != NAND_END_LUMP)
	{
		if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
			return (writeCount + LUMP_DATA_SIZE);
			
		bCurLumpWritten = 1;
	}
	/**** modified by chilong 01/17/2002 ****/
		
	writeCount += LUMP_DATA_SIZE;
  }

  // part 3: transfer data from the last block to user buffer
  
  // update the file position of the 1st byte of the current block
  pfile->lumpfpos += LUMP_DATA_SIZE;

  // get next block
  prevLumpNo = curLumpNo;
  
  /**** added by chilong 01/15/2002 ****/
  prevLump = curLump;
  /**** added by chilong 01/15/2002 ****/
  
  curLumpNo = curLump->nextLump;

  /**** added by chilong 01/13/2002 ****/
  bNeed2ReadAgain = 1;
  /**** added by chilong 01/13/2002 ****/
  
  if (curLumpNo == NAND_END_LUMP)
  {
	// a new lump is needed, go get it
	curLumpNo = NAND_findFreeLump();
	if (curLumpNo == NAND_END_LUMP)
	{
		pfile->currentLump = NAND_END_LUMP;
		pfile->lumpfpos = 0;
		NAND_FFS_Errno = ERROR_ALLOC_LUMP;

		/* moved to NAND_FFS_write()
		sc_getTime(&now);
		pfile->dirEntry.time = now.hour * 2048 + now.minute * 32 + now.second / 2;
		pfile->dirEntry.date = (now.year-1980) * 512 + now.month * 32 + now.day;
		*/

		/**** added by chilong 01/17/2002 ****/
  		if (bCurLumpWritten == 0)
  		{
			if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
				return writeCount;
  		}
		/**** added by chilong 01/17/2002 ****/

		return writeCount;
	}

	// write previous lump header back to flash
	if (prevLumpNo != NAND_END_LUMP)
	{
		/* marked by chilong 01/16/2002
		// chilong: prevLump is the same as curLump right now
		//	and we've read curLump
		if (NAND_FFS_readLump(prevLumpNo, (unsigned char **)&prevLump) == -1)
			return writeCount;
			
		   marked by chilong 01/16/2002 */
			
		prevLump->nextLump = curLumpNo;
		
		if (NAND_FFS_writeLump(prevLumpNo, (unsigned char *)prevLump) == -1)
			return writeCount;
	}
	
	if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
		return writeCount;
		
	// link the new lump to the file lump link
	curLump->type = NAND_LUMP_TYPE_FILE;
	//curLump->userCount = 0;
	curLump->prevLump = prevLumpNo;
	curLump->nextLump = NAND_END_LUMP;
	curLump->size = NAND_LUMP_SIZE - sizeof(struct NAND_diskLump);
	curLump->actualSize = 0;
	
	//if (NAND_FFS_writeLump(curLumpNo, (unsigned char *)curLump) == -1)
	//	return writeCount;
	
	bNeed2ReadAgain = 0;
  }
  
  // the current lump is the new lump
  pfile->currentLump = curLumpNo;

  /* marked by chilong 01/13/2002
  if (NAND_FFS_readLump(curLumpNo, (unsigned char **)&curLump) == -1)
  	return writeCount;
     marekd by chilong 01/13/2002 */
     
  /**** added by chilong 01/13/2002 ****/
  if (bNeed2ReadAgain)
  {
  	if (NAND_FFS_readLump(curLumpNo, (unsigned char **)&curLump) == -1)
  		return writeCount;
  }
  /**** added by chilong 01/13/2002 ****/

  des = (unsigned char *)((unsigned long)curLump + L_HEADER);
  
  prevLumpNo = curLumpNo;
  
  if ((long)des % 4 == 0 && (long)buffer % 4 == 0)
  {
	for (i = 0; i < remains / 4; i++)
	{
		*((int*)des) = *((int*)buffer);
		des += 4;
		buffer += 4;
	}
			
	for (i = 0; i < remains % 4; i++)
		*des++ = *buffer++;
  }
  else if ((long)des % 2 == 0 && (long)buffer % 2 == 0)// if both buffers are 2 byte-aligned
  {
	for (i = 0; i < remains / 2; i++)
	{
		*((short*)des) = *((short*)buffer);
		des += 2;
		buffer += 2;
	}
			
	for (i = 0; i < remains % 2; i++)
		*des++ = *buffer++;
  }
  else if ((long) des % 2 == 1 && (long) buffer % 2 == 1)
  {
	// move one byte first to make both des & buffer located on even addresses
	*des++ = *buffer++;
		
	if ((long)des % 4 == 0 && (long)buffer % 4 == 0)
	{
		for (i = 0; i < (remains-1) / 4; i++)
		{
			*((int*)des) = *((int*)buffer);
			des += 4;
			buffer += 4;
		}
			
		for (i = 0; i < (remains-1) % 4; i++)
			*des++ = *buffer++;
	}
	else
	{
		for (i = 0; i < (remains-1) / 2; i++)
		{
			*((short*)des) = *((short*)buffer);
			des += 2;
			buffer += 2;
		}
			
		for (i = 0; i < (remains-1) % 2; i++)
			*des++ = *buffer++;
	}
		
  }
  else
  {
  	for (i = 0; i < remains; i++)
		*des++ = *buffer++;
  }
  
  pfile->fpos += remains;
  if (pfile->fpos > pfile->dirEntry.fsize)
  {
	// file size grows
	pfile->dirEntry.fsize = pfile->fpos;
  }

  writeCount += remains;

  if (curLump->actualSize < (pfile->fpos - pfile->lumpfpos))
  {
	curLump->actualSize = (pfile->fpos - pfile->lumpfpos);

	if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
		return freeSize;
  }
  
  remains = 0;
  /*
  sc_getTime(&now);
  pfile->dirEntry.time = now.hour * 2048 + now.minute * 32 + now.second / 2;
  pfile->dirEntry.date = (now.year-1980) * 512 + now.month * 32 + now.day;
  */
  return writeCount;
}

/*************************************************************
Function: NAND_FFS_update_file_info
Description:
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -