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

📄 nandfile.424

📁 PDA上的CF CARD 文件系统的建立程式
💻 424
📖 第 1 页 / 共 4 页
字号:
  	{
  		if (NAND_FFS_HandleTable[i]->deviceID == NAND_FLASH_DISK_ID)
  		{
 			if ((pfile->dirEntry).startLump == (NAND_FFS_HandleTable[i]->dirEntry).startLump)
  			{
  				return 1;
  			}
  		}
  	}
  }
  
  return 0;
}

/*************************************************************
Function: NAND_FFS_mkdir
Desciption: Create a new directory
Input:
	dirname - path for new directory
Output:
	0 if the new directory was created.
	-1 if failed
Notes:
*************************************************************/
int NAND_FFS_mkdir(unsigned char *dirname)
{
  int status;

  if (NAND_FFS_Init_OK == FALSE)
  {
	NAND_FFS_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  if (myStrLen(dirname) >= NAND_MAX_PATH_LENGTH)
  {
	NAND_FFS_Errno = ERROR_PATHNAME_TOO_LONG;
	return -1;
  }

  NAND_FFS_Errno = 0;

  sc_waitSemaphore(NAND_FFS_SemaphoreID);

  status = NAND_FFS_mkdir_r(dirname);

  sc_signalSemaphore(NAND_FFS_SemaphoreID);

  return status;
}

//static char HHH[80];
int NAND_FFS_mkdir_r(unsigned char *dirname)
{
  struct NAND_FFS_FILE *pfile;
  struct NAND_diskLump *curLump;
  struct NAND_directory *curEntry;
  int result;
  int curLumpNo;

#ifdef NAND_FFS_DEBUG
  unsigned char *pData;
  long i;
#endif
  // initialize the file's structure
  pfile = (struct NAND_FFS_FILE *)ap_malloc(sizeof(struct NAND_FFS_FILE));
  if (pfile == NULL)
  {
	NAND_FFS_Errno = ERROR_ALLOC_MEM;
	return -1;
  }
  memoryZero((void *)pfile, (unsigned int)sizeof(struct NAND_FFS_FILE));

  // initialize some values before searching the file
  pfile->subDirLumpNo = NAND_END_LUMP;
  pfile->nThDirEntry = NAND_END_LUMP;

  result = NAND_FFS_fileSearch(pfile, dirname);
  
#ifdef NAND_FFS_DEBUG
  sprintf(NAND_FFS_DebugString, "[NAND_FFS_mkdir] lump: %d, entry: %d", 
  	pfile->subDirLumpNo, pfile->nThDirEntry);
  SprintStringLn(NAND_FFS_DebugString);
#endif  

  if (result == -1)
  {
	// the given name does not exist, make the directory
	result = NAND_FFS_fileCreate(pfile, dirname, DA_DIR);
	if (result == 0)
	{
		// directory entry creation succeeds
		// get a DIR lump for the new directory
		
		//curBlock = (struct diskBlock *)RD_getBlock((unsigned long)DIR_BLOCK_SIZE);
		
		curLumpNo = NAND_findFreeLump();
		
		if (NAND_FFS_readLump(pfile->subDirLumpNo, (unsigned char**)&curLump) == -1)
		{
			ap_free(pfile);
			return -1;
		}
			
/*
#ifdef NAND_FFS_DEBUG

 	SprintStringLn("before mkdir");
  	pData = (unsigned char *) curLump;
	if (pData != NULL)
	{
		for (i = 0; i < 512; i++)
		{
			if (i % 16 == 0)
				SprintStringLn("");
					
			sprintf(NAND_FFS_DebugString, "%.2x ", *(pData+i));
			SprintString(NAND_FFS_DebugString);
		}
	}
	SprintStringLn("");
		
#endif
*/				
		curEntry = (struct NAND_directory *) ((unsigned long)curLump +
			sizeof(struct NAND_diskLump) + 
			pfile->nThDirEntry * sizeof(struct NAND_directory));
		

		// curLumpNo is gotten from NAND_findFreeLump()
		if (curLumpNo == NAND_END_LUMP)
		{
			NAND_FFS_Errno = ERROR_DISK_FULL;
			
			curEntry->name[0] = DELETED_ENTRY;
			
			result = -1;
		}
		else
		{
			//(pfile->dirEntry)->startBlock = curBlock;
			curEntry->startLump = curLumpNo;
			NAND_FFS_Errno = 0;
		}

		if (NAND_FFS_writeLump(pfile->subDirLumpNo, (unsigned char*)curLump) == -1)
		{
			ap_free(pfile);
			return -1;
		}

/*
#ifdef NAND_FFS_DEBUG

	if (NAND_FFS_readLump(pfile->subDirLumpNo, (unsigned char**)&curLump) == -1)
	{
		ap_free(pfile);
		return -1;
	}

	if (curLump == NULL)
	{
		ap_free(pfile);
		return -1;
	}

 	SprintStringLn("after mkdir");
  	pData = (unsigned char *) curLump;
	if (pData != NULL)
	{
		for (i = 0; i < 512; i++)
		{
			if (i % 16 == 0)
				SprintStringLn("");
					
			sprintf(NAND_FFS_DebugString, "%.2x ", *(pData+i));
			SprintString(NAND_FFS_DebugString);
		}
	}
	SprintStringLn("");
		
#endif
*/

		// write info to the starting lump of dir
		if (NAND_FFS_readLump(curLumpNo, (unsigned char **)&curLump) == -1)
		{
			ap_free(pfile);
			return -1;
		}
		
  		curLump->type = NAND_LUMP_TYPE_DIR;
		curLump->prevLump = NAND_END_LUMP;
		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)
		{
			ap_free(pfile);
			return -1;
		}
	
#ifdef NAND_FFS_DEBUG
		if (NAND_FFS_readLump(pfile->subDirLumpNo, (unsigned char**)&curLump) == -1)
		{
			ap_free(pfile);
			return -1;
		}

#endif
	}
  }
  else
  {
	// the given name already exists, cannot make directory
	NAND_FFS_Errno = ERROR_DIR_ALREADY_EXIST;
	result = -1;
  }

  ap_free(pfile);

  return result;
}

/*************************************************************
Function: NAND_FFS_rmdir()
Desciption: Delete a directory
Input:
	dirname - path of directory to be removed
Output:
	0 if the removal is succeeded
	-1 if failed
Notes:
*************************************************************/
int NAND_FFS_rmdir(unsigned char *dirname)
{
  int status;

  if (NAND_FFS_Init_OK == FALSE)
  {
	NAND_FFS_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  NAND_FFS_Errno = 0;

  sc_waitSemaphore(NAND_FFS_SemaphoreID);

  status = NAND_FFS_killEntry(dirname, DA_DIR);

  sc_signalSemaphore(NAND_FFS_SemaphoreID);

  return status;
}



/*************************************************************
Function : NAND_FFS_read
Description:
	read data from file to user buffer
Inputs :
	fhandle - file handle
	buffer - user specified buffer
	size - size of data to be read
Outputs :
	the size of data read from disk
**************************************************************/
long NAND_FFS_read(int fhandle, void *buffer, unsigned long size)
{
  long result;
  sysTime now;
  
  if (NAND_FFS_Init_OK == FALSE)
  {
	NAND_FFS_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return 0;
  }

  if ((fhandle < 0) || (fhandle >= MAX_OPEN_FILE) || (NAND_FFS_HandleTable[fhandle] == NULL))
  {
	NAND_FFS_Errno = ERROR_INVALID_HANDLE;
	return 0;
  }

  if (NAND_FFS_HandleTable[fhandle]->deviceID != NAND_FLASH_DISK_ID)
  {
	NAND_FFS_Errno = ERROR_INVALID_DEVICE;
	return 0;
  }

  NAND_FFS_Errno = 0;

  if (size == 0)
	return 0;

  if (sc_waitSemaphore(NAND_FFS_SemaphoreID) == -1)
  {
	NAND_FFS_Errno = ERROR_WAIT_SEMAPHORE;
	return 0;
  }

  result = NAND_FFS_read_r(NAND_FFS_HandleTable[fhandle], (unsigned char *)buffer, size);
  
  NAND_FFS_updateFileHandleTable(NAND_FFS_HandleTable[fhandle], 0);
  
  if (sc_signalSemaphore(NAND_FFS_SemaphoreID) == -1)
  {
	NAND_FFS_Errno = ERROR_SIGNAL_SEMAPHORE;
	return 0;
  }

  return result;
}


/*************************************************************
Function: NAND_FFS_read_r
Description:
	read data from file to user buffer
Input:
	pfile - the target file structure
	buffer - the user buffer
	size - the size of data to be read
Output:
	size of data read from the disk
**************************************************************/
long NAND_FFS_read_r(struct NAND_FFS_FILE *pfile, unsigned char *buffer, unsigned long size)
{
  struct NAND_diskLump *curLump;
  struct NAND_diskLump *prevLump;
  struct NAND_diskLump *nextLump;
  
  unsigned char *src;
  int found = 0;
  long readCount = 0;
  long remains;
  long curSize;
  long i;
  
  int prevLumpNo;
  int curLumpNo;
  int nextLumpNo;
  
#ifdef NAND_FFS_DEBUG
  sprintf(NAND_FFS_DebugString, "[NAND_FFS_read] handle: %d, size: %ld", pfile->handle, size);
  SprintStringLn(NAND_FFS_DebugString);
#endif 
  
  // check if the end of the file has already been reached
  if (pfile->fpos >= pfile->dirEntry.fsize)
  {
	NAND_FFS_Errno = ERROR_EOF;
	
#ifdef NAND_FFS_DEBUG
  sprintf(NAND_FFS_DebugString, "[NAND_FFS_read] fsize is zero");
  SprintStringLn(NAND_FFS_DebugString);
#endif 
	return 0;
  }

  if (pfile->currentLump == NAND_END_LUMP)
  {
	// there is no current block
	curLumpNo = pfile->dirEntry.startLump;
	
	pfile->lumpfpos = 0;

	if (curLumpNo == NAND_END_LUMP)
	{
		// block not found, must be error in file structure
		pfile->currentLump = NAND_END_LUMP;
		return 0;
	}
	else
	{
		pfile->currentLump = curLumpNo;
		found = 1;
	}
  }

  // there is a current block
  curLumpNo = pfile->currentLump;

  if (NAND_checkLump(curLumpNo) == -1)
  {
	// invalid block location
	NAND_FFS_Errno = ERROR_FILE_SYSTEM;
	return 0;
  }

  if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
  	return 0;
		
  // check if the file position matches this block
  if (pfile->fpos < pfile->lumpfpos)
  {
	// file position is before the beginning of the current lump
	// find the lump that is pointed to by the file position by going backward
	while (curLumpNo != NAND_END_LUMP)
	{
		curLumpNo = curLump->prevLump;
		if (curLumpNo != NAND_END_LUMP)
		{
			pfile->lumpfpos -= LUMP_DATA_SIZE;
			if ((pfile->fpos >= pfile->lumpfpos) && (pfile->fpos < pfile->lumpfpos + LUMP_DATA_SIZE))
			{
				found = 1;
				break;
			}
			
			if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
				return 0;
		}
	}

	if (curLumpNo == NAND_END_LUMP)
		found = 0;
	
  }
  else if (pfile->fpos >= pfile->lumpfpos + curLump->actualSize)
  {
	// file position is behind the current lump
	// find the lump that is pointed to by the file position by going forward
	while (curLumpNo != NAND_END_LUMP)
	{
		pfile->lumpfpos += LUMP_DATA_SIZE;
		
		curLumpNo = curLump->nextLump;
		if (curLumpNo != NAND_END_LUMP)
		{
			if ((pfile->fpos >= pfile->lumpfpos) && (pfile->fpos < pfile->lumpfpos + LUMP_DATA_SIZE))
			{
				found = 1;
				break;
			}
			
			if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
				return 0;
		}
	}
	
	if (curLumpNo == NAND_END_LUMP)
		found = 0;
  }
  
  if (found == 1)
  {
	// a lump other than the current lump is found
	pfile->currentLump = curLumpNo;
  }
  else
  {
	// no lump other than the current lump is found
	//   either the required lump is not found or the required block
	//   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 block is already the current block
  }

  // the file position now points to the current lump which is uncompressed
  curLumpNo = pfile->currentLump;
  remains = size;
  // part 1: copy the data from the current lump to the user-provided buffer

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

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


  src = (unsigned char *)((unsigned long)curLump + L_HEADER + (pfile->fpos - pfile->lumpfpos));
  
  //prevLumpNo = curLumpNo;
  
  // found stores curLump->actualSize temporarily for
  // later curSize
  found = curLump->actualSize;
  nextLumpNo = curLump->nextLump;
  
  // if both buffers are 4 byte-aligned
  if ( (long)buffer % 4 == 0 && (long)src % 4 == 0)
  {
	for (i = 0; i < curSize / 4; i++)
	{
		*((int*)buffer) = *((int*)src);
		buffer += 4;
		src += 4;
	}
			
	for (i = 0; i < curSize % 4; i++)
		*buffer++ = *src++;
  }
  else if ( (long)buffer % 2 == 0 && (long)src % 2 == 0)// if both buffers are 2 byte-aligned
  {
	for (i = 0; i < curSize / 2; i++)
	{
		*((short*)buffer) = *((short*)src);
		buffer += sizeof(short);
		src += sizeof(short);
	}
			
	for (i = 0; i < curSize % 2; i++)
		*buffer++ = *src++;
  }
  else if ((long) buffer % 2 == 1 && (long) src % 2 == 1)
  {
	// move 1 byte first
	*buffer++ = *src++;
		
	if ((long)buffer % 4 == 0 && (long)src % 4 == 0)
	{
		// copy data short by short
		for (i = 0; i < (curSize-1) / 4; i++)
		{
			*((int*)buffer) = *((int*)src);
			buffer += 4;
			src += 4;
		}
		
		for (i = 0; i < (curSize-1) % 4; i++)
			*buffer++ = *src++;

	}
	else
	{
		// copy data short by short
		for (i = 0; i < (curSize-1) / 2; i++)
		{
			*((short*)buffer) = *((short*)src);
			buffer += 2;
			src += 2;
		}
		
		for (i = 0; i < (curSize-1) % 2; i++)
			*buffer++ = *src++;
	}
  }
  else
  {
  	for (i = 0; i < curSize; i++)
		*buffer++ = *src++;
  }
  
//  pfile->currentLump = curLumpNo = prevLumpNo;
  
  // update the positions

⌨️ 快捷键说明

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