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

📄 nandfind.c

📁 嵌入式系统中文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*************************************************************
File Name: findx.C (NAND_FLASH) 			     *
**************************************************************
Programmer: chilong
Last Modified Date: 2001/12/35
Compiler : GNU Cross-compiler/SDS
Platform : X86 protection mode, MIPS, Dragonball
Usage :
	int NAND_FFS_findfirst(unsigned char *target, struct ffblk *ffres, int attrib);
	int NAND_FFS_findnext(struct ffblk *ffres);
	int NAND_FFS_findclose(struct ffblk *ffres);	
*************************************************************/

/*************************************************************
 		  	Header Files
**************************************************************/
#include <sys/syscall.h>
//#include "myansi.h"

#include <stdio.h>
//#include "/rtos/driver/FileSys/FileSys.h"
#include "../include/FFS_NAND.h"
#include "../include/NANDlump.h"
#include "../include/NANDfind.h"

#ifdef NAND_FLASH_DISK_ID



/* FFS_NAND.c */
extern int NAND_FFS_SemaphoreID;
extern int NAND_FFS_Init_OK;
extern int NAND_FFS_Errno;

// NANDfile.c
extern char NAND_FFS_DebugString[80];

/*************************************************************
Function : NAND_FFS_findfirst
Description:
	find the first file that match the requirement
Input:
	target - a wildcard that specifies the directory and files to search
	ffres - a structure to hold the results of the search
	attrib - the target attribute
Output:
	0	if a match has been found
	-1	if the searching failed
**************************************************************/
int NAND_FFS_findfirst(unsigned char *target, struct ffblk *ffres, int attrib)
{
  int status;

   NAND_FFS_Errno = 0;

  if (NAND_FFS_Init_OK == FALSE)
  {
	ffres->ff_reserved = 0;
	NAND_FFS_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  sc_waitSemaphore(NAND_FFS_SemaphoreID);

  status = NAND_FFS_findfirst_r(target, ffres, attrib);

  sc_signalSemaphore(NAND_FFS_SemaphoreID);

  return status;
}


int NAND_FFS_findfirst_r(unsigned char *target, struct ffblk *ffres, int attrib)
{
  struct NAND_ffInternal *ffInfo;	/* internal information for findnext */
  struct NAND_diskLump *curLump;
  struct NAND_directory *curEntry;
  long dirSize;
  long i;
  long namePos = -1;
  unsigned char *subDirName;
  unsigned char *wildName;
  struct NAND_FFS_FILE *pfile;
  
  /**** added by chilong 12/25/2001 ****/
  int curLumpNo;
  /**** added by chilong 12/25/2001 ****/

#ifdef NAND_FFS_DEBUG
  sprintf(NAND_FFS_DebugString, "[NAND_FFS_findfirst] target: %s", target);
  SprintStringLn(NAND_FFS_DebugString);
#endif

  // this allocated buffer is actually used to store 2 data structures
  //   the first NAND_MAX_PATH_LENGTH bytes is used to store the sub-dir pathname
  //   and the remaining portion is used as a temporary file structure for RD_f_search
  subDirName = (unsigned char *)ap_malloc(NAND_MAX_PATH_LENGTH + sizeof(struct NAND_FFS_FILE) + 10);
  if (subDirName == NULL)
  {
	ffres->ff_reserved = 0;
	NAND_FFS_Errno = ERROR_ALLOC_MEM;
	return -1;
  }
  memoryZero((void *)subDirName, (unsigned int)(NAND_MAX_PATH_LENGTH + sizeof(struct NAND_FFS_FILE) + 10));

  // find the last '\\' in target
  i = 0;
  while (target[i] != '\0')
  {
  	if (target[i] == '\\' || target[i] == '/')
  		namePos = i;
  	subDirName[i] = target[i];
	i++;
	if (i >= NAND_MAX_PATH_LENGTH)
	{
		ffres->ff_reserved = 0;
		ap_free(subDirName);
		NAND_FFS_Errno = ERROR_PATHNAME_TOO_LONG;
		return -1;
	}
  }
  namePos++;
  wildName = &target[namePos];
  if (wildName[0] == '\0')
  {
	ffres->ff_reserved = 0;

	ap_free(subDirName);
	NAND_FFS_Errno = ERROR_FILE_NOT_EXIST;

	return -1;
  }

  subDirName[namePos] = '\0';

  namePos--;
  while (namePos >= 0 && (subDirName[namePos] == '\\' || subDirName[namePos] == '/'))
  {
	subDirName[namePos] = '\0';
	namePos--;
  }

  // if      target = "\\dir1\\dir2\\dir3\\*.*"
  // now   wildName = "*.*"
  //     subDirName = "\\dir1\\dir2\\dir3"

  if (subDirName[0] == '\0')
  {
	// searching in the root dir
	//curBlock = (struct diskBlock *)(((struct signature *)RD_BootBegin)->rootBegin);

	curLumpNo = 0;	
	if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
		return -1;
		
	if (curLump == NULL)
		return -1;
  }
  else
  {
	// searching in the sub-dir
	pfile = (struct NAND_FFS_FILE *)((unsigned long)subDirName + NAND_MAX_PATH_LENGTH);
	
  	// initialize some values before searching the file
  	pfile->subDirLumpNo = NAND_END_LUMP;
  	pfile->nThDirEntry = NAND_END_LUMP;
	
	if (NAND_FFS_fileSearch(pfile, subDirName) != 0)
	{
		// search failed, directory not found
		ffres->ff_reserved = 0;
		ap_free(subDirName);
		NAND_FFS_Errno = ERROR_DIR_NOT_EXIST;
		return -1;
	}

	// curBlock = (pfile->dirEntry)->startBlock;

	curLumpNo = (pfile->dirEntry).startLump;	
	if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
		return -1;
	if (curLump == NULL)
		return -1;
		
  }

  ap_free(subDirName);

  dirSize = sizeof(struct NAND_directory);

  // start looking for the file now
  //   curLump  is the starting lump of the directory to be searched
  //   wildName  contains only the wildcard name to be searched
  while (curLumpNo != NAND_END_LUMP)
  {
	curEntry = (struct NAND_directory *)((unsigned long)curLump + sizeof(struct NAND_diskLump));

	// compare entry by entry in the sector
	for (i = 0; i < NAND_LUMP_SIZE; i += dirSize)
	{
		// this entry and every following entry are empty => not found
		if (curEntry->name[0] == NAND_FREE_ENTRY)
		{
			ffres->ff_reserved = 0;
			NAND_FFS_Errno = ERROR_FILE_NOT_EXIST;
			return -1;
		}

		// skip this entry if it is an deleted entry or "." or ".."
		if (curEntry->name[0] == DELETED_ENTRY)
		{
			curEntry = (struct NAND_directory *)((unsigned long)curEntry + dirSize);
			continue;
		}

		// compare the attribute of this entry with the given attribute
		// DA_READONLY and DA_ARCHIVE files can always be seen
		// DA_HIDDEN, DA_SYSTEM, DA_VOLUME, and DA_DIR are not
		attrib = attrib | DA_READONLY;
		attrib = attrib | DA_ARCHIVE;
		// go to next entry if the attributes unmatch
		if ((curEntry->attribute & attrib) != curEntry->attribute)
		{
			curEntry = (struct NAND_directory *)((unsigned long)curEntry + dirSize);
			continue;
		}
		// DIR are shown only if DA_DIR is specified by user
		if ((attrib & DA_DIR) == DA_DIR)
			if ((curEntry->attribute & DA_DIR) != DA_DIR)
			{
				curEntry = (struct NAND_directory *)((unsigned long)curEntry + dirSize);
				continue;
			}

		// attributes match, compare filename with the wildcard name
		if (NAND_FFS_nameCompare(wildName, curEntry->name) == 1)
		{
			// allocate the internal findfile structure, used by findnext()
			// this structure is deallocated by findclose()
			ffInfo = (struct NAND_ffInternal *)ap_malloc(sizeof(struct NAND_ffInternal));
			if (ffInfo == NULL)
			{
				ffres->ff_reserved = 0;
				NAND_FFS_Errno = ERROR_ALLOC_MEM;
				return -1;
			}
			memoryZero((void *)ffInfo, (unsigned int)sizeof(struct NAND_ffInternal));

			// fill in the ffInternal structure
			ffInfo->attrib = attrib;
			// the entry number, counting from the 1st root sector
			ffInfo->nThEntry = i / dirSize;
			// the cluster number of the directory, 0 for root
			ffInfo->lumpNo = curLumpNo;
			// the wildcard name
			myStrCpy((char *)ffInfo->wildname, (const char *)wildName);

			// fill in the ffblk structure
			ffres->ff_reserved = (long)ffInfo;
			ffres->ff_attrib = curEntry->attribute;
			ffres->ff_ftime = curEntry->time;
			ffres->ff_fdate = curEntry->date;
			ffres->ff_fsize = curEntry->fsize;
			ffres->ff_drive = NAND_FLASH_DISK_ID;
			// copy the name from dir entry to result
			i = 0;
			while (curEntry->name[i] != '\0')
			{
				ffres->ff_name[i] = curEntry->name[i];
				i++;
			}
			ffres->ff_name[i] = '\0';

			return 0;
		} // if loop status
		// not found, try next entry
		
		curEntry = (struct NAND_directory *)((unsigned long)curEntry + dirSize);
	} // for loop i

	// not found in this cluster, try next cluster in the chain
	//curBlock = (struct diskBlock *)curBlock->nextBlock;
	curLumpNo = curLump->nextLump;
	
	if (curLumpNo == NAND_END_LUMP)
		break;
		
	if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
		return -1;
	if (curLump == NULL)
		return -1;
  }

  // all clusters in the chain have been searched, file not found
  ffres->ff_reserved = 0;

  NAND_FFS_Errno = ERROR_FILE_NOT_EXIST;
  return -1;
}



/*************************************************************
Function : NAND_FFS_findnext

⌨️ 快捷键说明

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