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

📄 ffindx.c

📁 嵌入式系统中文件系统源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
				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 ((entryAttr & attrib) != entryAttr)
				continue;
			// only DIR are shown if DA_DIR is specified by user
			if ((attrib & DA_DIR) == DA_DIR)
				if ((entryAttr & DA_DIR) != DA_DIR)
					continue;

			/* attributes match, compare filename with the wildcard name */
#ifdef FAT_LFN
			longInfo.startBlock = cluster;
			longInfo.startEntry = j;
			longInfo.endBlock = prevCluster;
			longInfo.endEntry = 0;
			status = FAT_getLongName(drive, 0, &longInfo, pBuffer + j, fileName);
			if (status == 1)
			{
				if (nameCompare(wildName, fileName, 1) == 1)
					status = 1;	// the long name (unicode) matches
				else
					status = -1;	// not match
			}
			if (status <= 0)
			{
				getShortName(pBuffer + j, fileName);
				if (nameCompare(wildName, fileName, 0) == 1)
					status = 0;	// the short name matches
				else
					status = -1;
			}	
#else
			getShortName(pBuffer + j, fileName);
			if (nameCompare(wildName, fileName, 0) == 1)
				status = 0;		// the short name matches
			else
				status = -1;
#endif

			if (status != -1)	/* found! */
			{
				// allocate the internal findfile structure, used by FAT_findnext()
				// this structure is deallocated by FAT_findclose()
				if ((ffInfo = (struct FAT_FFInternal *)ap_malloc(sizeof(struct FAT_FFInternal))) == NULL)
				{
					ap_free(blkBuffer);
					ap_free(pfile);
					ap_free(fileName);
					FATHandleTable[availHandler] = NULL;
					ffres->ff_reserved = 0;
					FATErrno = ERROR_ALLOC_MEM;
					return -1;
				}
				myMemSet(ffInfo, 0, sizeof(struct FAT_FFInternal));

				/* fill in the FAT_FFInternal structure */
				ffInfo->drive = drive;
				ffInfo->attrib = attrib;
				/* the entry number, counting from the 1st root sector */
				ffInfo->nThEntry = j / FAT_DIR_ENTRY_SIZE;
				/* the cluster number of the directory, 0 for root */
				ffInfo->cluster = cluster;
				/* the wildcard name */
				copyName(wildName, ffInfo->wildname, 0);

				// fill in the ffblk structure
				ffres->ff_reserved = (long)ffInfo;
//				pEntry = (struct FAT_directory *)(pBuffer + j);
				FAT_load_dir_info(pBuffer + j, &pEntry);
				ffres->ff_attrib = pEntry.attribute;
				ffres->ff_ftime = pEntry.time;
				ffres->ff_fdate = pEntry.date;
				ffres->ff_fsize = pEntry.fsize;
				ffres->ff_drive = FAT_ID;
				/* copy the name from dir entry to result */
				// note that if status = 1, the name is in unicode
				//   otherwise the name is in big5 & ascii
				if (status == 1)
					copyName(fileName, ffres->ff_name, 1);
				else
					copyName(fileName, ffres->ff_name, 0);

				ap_free(blkBuffer);
				ap_free(pfile);
				ap_free(fileName);
				FATHandleTable[availHandler] = NULL;
				return 0;
			} /* if loop status */
			/* not found, try next entry */
		} /* for loop j */

		/* not found in this cluster, try next cluster in the chain */
		prevCluster = cluster;
		cluster = FAT_read_fat_cluster(drive, cluster);
	}
	/* all clusters in the chain have been searched, file not found */
	ap_free(blkBuffer);
	ap_free(pfile);
	ap_free(fileName);
	FATHandleTable[availHandler] = NULL;
	ffres->ff_reserved = 0;
	FATErrno = ERROR_FILE_NOT_EXIST;
	return -1;
  }
}



/*************************************************************
Function : FAT_findnext
Description:
	find the next file that match the requirement
Input:
	ffres - a structure to hold the results of the search
Output:
	0	if a match has been found
	-1	if the searching failed
**************************************************************/
int FAT_findnext(struct ffblk *ffres)
{
  int status;

#ifdef FAT1X_DEBUG
  SprintStringLn("[FAT_findnext]");
#endif

  FATErrno = 0;

  if (InitFAT == FALSE)
  {
	FATErrno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  if (ffres->ff_reserved == 0)
  {
	FATErrno = ERROR_FILE_NOT_EXIST;
	return -1;
  }

  sc_waitSemaphore(FATDirSemaphoreID);

  status = FAT_findnext_r(ffres);

  sc_signalSemaphore(FATDirSemaphoreID);

  return status;
}


int FAT_findnext_r(struct ffblk *ffres)
{
  int i, j;
  int status;
  unsigned short blkSize;	/* the size of a block (sector in root, cluster in sub-dir) */
  unsigned short *blkBuffer;	/* a buffer for root sectors */
  unsigned char *pBuffer;	/* a pointer for reading sector/cluster from disk */
  short nThEntry;		/* the n-th entry in root sector or dir cluster */
  unsigned short sector;	/* searching is started from this root sector  */
  short entryInSector;		/* searching is started from this entry in the sector */
  unsigned short cluster;	/* the target sub-dir cluster for wildcard name search */
  unsigned short prevCluster;	/* the previous sub-dir cluster for wildcard name search */
  struct FAT_directory pEntry;	/* the dir entry with name matched the wildcard name */
  unsigned char entryAttr;	/* the attribute of the dir entry */
  short drive;			/* the target drive */
  unsigned char *fileName;
  struct FAT_FFInternal *ffInfo;	/* internal information for FAT_findnext */
#ifdef FAT_LFN
  struct FAT_LFNinfo longInfo;
#endif

#ifdef FAT1X_DEBUG
  SprintStringLn("[FAT_findnext_r]");
#endif

  ffInfo = (struct FAT_FFInternal *)ffres->ff_reserved;
  drive = ffInfo->drive;
  /* start searching from this cluster (0 = root dir, non-zero = sub-dir) */
  cluster = ffInfo->cluster;
  nThEntry = ffInfo->nThEntry;

#ifdef FAT_LFN
  if ((fileName = (unsigned char *)ap_malloc(FAT_MAX_LFN_LENGTH * 2)) == NULL)
  {
	FATErrno = ERROR_ALLOC_MEM;
	return -1;
  }
  myMemSet(fileName, 0, FAT_MAX_LFN_LENGTH * 2);
#else
  if ((fileName = (unsigned char *)ap_malloc(13)) == NULL)
  {
	FATErrno = ERROR_ALLOC_MEM;
	return -1;
  }
  myMemSet(fileName, 0, 13);
#endif

  /* starting comparing attributes and filenames */
  if (cluster == 0)	/* find first in the root directory */
  {
	/* start searching for the filename in the root directory */
	/* no need to search for the sub-dir part */
	blkSize = SECTOR_SIZE;	/* root dir is handled sector by sector */
	if ((blkBuffer = (unsigned short *)ap_malloc(blkSize)) == NULL)
	{
		ap_free(fileName);
		FATErrno = ERROR_ALLOC_MEM;
		return -1;
	}

	/* check all root sectors */
	sector = nThEntry / (SECTOR_SIZE / FAT_DIR_ENTRY_SIZE);
	entryInSector = nThEntry % (SECTOR_SIZE / FAT_DIR_ENTRY_SIZE);
	i = sector - 1;

	for (;;)
	{
		i++;
		/* if all root sectors have been searched, then no match file is found */
		if (i == FATSecPerROOT[drive])
		{
			ap_free(blkBuffer);
			ap_free(fileName);
			FATErrno = ERROR_FILE_NOT_EXIST;
			return -1;
		}			
		/* read the current root sector */
		FAT_read_root_sector(drive, FATRootBegin[drive] + i, blkBuffer);
		pBuffer = (unsigned char *)blkBuffer;

		/* compare entry by entry in the sector */
		for (j = (entryInSector + 1) * FAT_DIR_ENTRY_SIZE; j < blkSize; j += FAT_DIR_ENTRY_SIZE)
		{
			/* this entry and every following entry are empty => not found */
			if (*(pBuffer + j) == '\0')
			{
				ap_free(blkBuffer);
				ap_free(fileName);
				FATErrno = ERROR_FILE_NOT_EXIST;
				return -1;
			}

			/* skip this entry if it an deleted entry */
			if (*(pBuffer + j) == 0xe5 || *(pBuffer + j) == '.')
				continue;

			/* compare the attribute of this entry with the given attribute */
			entryAttr = *(pBuffer + j + 11);

			// skip long directory entry
			if ((entryAttr & DA_VFAT) == DA_VFAT)
				continue;

			/* go to next entry if the attributes unmatch */
			if ((entryAttr & ffInfo->attrib) != entryAttr)
				continue;

			/* marked by chilong 03/07/2002
			// only DIR are shown if DA_DIR is specified by user
			if ((ffInfo->attrib & DA_DIR) == DA_DIR)
				if ((entryAttr & DA_DIR) != DA_DIR)
					continue;
			   marked by chilong 03/07/2002 */

			/**** modified by MSC 03/07/2002 ****/
			if ((ffInfo->attrib & FF_DA_DIR_OR) == 0)
			{
				// only DIR are shown if DA_DIR is specified by user
				if ((ffInfo->attrib & DA_DIR) == DA_DIR)
					if ((entryAttr & DA_DIR) != DA_DIR)
						continue;
			}
			/**** modified by MSC 03/07/2002 ****/

			/* attributes match, compare filename with the wildcard name */
#ifdef FAT_LFN
			longInfo.startBlock = FATRootBegin[drive] + i;
			longInfo.startEntry = j;
			if (i == 0)
				longInfo.endBlock = 0;
			else
				longInfo.endBlock = FATRootBegin[drive] + i - 1;
			longInfo.endEntry = 0;
			status = FAT_getLongName(drive, 1, &longInfo, pBuffer + j, fileName);
			if (status == 1)
			{
				if (nameCompare(ffInfo->wildname, fileName, 1) == 1)
					status = 1;	// the long name (unicode) matches
				else
					status = -1;	// not match
			}
			if (status <= 0)
			{
				getShortName(pBuffer + j, fileName);
				if (nameCompare(ffInfo->wildname, fileName, 0) == 1)
					status = 0;	// the short name matches
				else
					status = -1;
			}	
#else
			getShortName(pBuffer + j, fileName);
			if (nameCompare(ffInfo->wildname, fileName, 0) == 1)
				status = 0;		// the short name matches
			else
				status = -1;
#endif

			if (status != -1)	/* found! */
			{
				/* update the FAT_FFInternal structure */
				ffInfo->nThEntry = (i * SECTOR_SIZE + j) / FAT_DIR_ENTRY_SIZE;
				// update the ffblk structure
				/* copy field from dir entry to result */
//				pEntry = (struct FAT_directory *)(pBuffer + j);
				FAT_load_dir_info(pBuffer + j, &pEntry);
				ffres->ff_attrib = pEntry.attribute;
				ffres->ff_ftime = pEntry.time;
				ffres->ff_fdate = pEntry.date;
				ffres->ff_fsize = pEntry.fsize;
				
				ffres->ff_reserved = (long)ffInfo;
				ffres->ff_drive = FAT_ID;
				
				/* copy the name from dir entry to result */
				// note that if status = 1, the name is in unicode
				//   otherwise the name is in big5 & ascii
				if (status == 1)
					copyName(fileName, ffres->ff_name, 1);
				else
					copyName(fileName, ffres->ff_name, 0);

				ap_free(blkBuffer);
				ap_free(fileName);
				return 0;
			} /* if loop status */
			/* not found, try next entry */
		} /* for loop j */
		/* not found in this sector, try next sector */
		entryInSector = -1;
	} /* for loop infinite */
  } /* if loop index */
  else			/* find first in the sub-directory */
  {
	/* the sub-dir is found, start searching for the filename from the 1st entry */
	blkSize = FATBytesPerCluster[drive];	/* sub-dir is handled cluster by cluster */
	if ((blkBuffer = (unsigned short *)ap_malloc(blkSize)) == NULL)
	{
		ap_free(fileName);
		FATErrno = ERROR_ALLOC_MEM;
		return -1;
	}

	prevCluster = 0;

	while (cluster < FATEndOfChain[FATType[drive]])
	{
		FAT_read_cluster(drive, cluster, blkBuffer);
		pBuffer = (unsigned char *)blkBuffer;

		/* compare entry by entry in the sector */
		for (j = (nThEntry + 1) * FAT_DIR_ENTRY_SIZE; j < blkSize; j += FAT_DIR_ENTRY_SIZE)
		{
			/* this entry and every following entry are empty => not found */
			if (*(pBuffer + j) == '\0')
			{
				ap_free(blkBuffer);
				ap_free(fileName);
				FATErrno = ERROR_FILE_NOT_EXIST;
				return -1;
			}

			/* skip this entry if it an deleted entry */
			if (*(pBuffer + j) == 0xe5 || *(pBuffer + j) == '.')
				continue;

			/* compare the attribute of this entry with the given attribute */
			entryAttr = *(pBuffer + j + 11);

			// skip long directory entry
			if ((entryAttr & DA_VFAT) == DA_VFAT)
				continue;

			/* go to next entry if the attributes unmatch */
			if ((entryAttr & ffInfo->attrib) != entryAttr)
				continue;
			// only DIR are shown if DA_DIR is specified by user
			if ((ffInfo->attrib & DA_DIR) == DA_DIR)
				if ((entryAttr & DA_DIR) != DA_DIR)
					continue;

			/* attributes match, compare filename with the wildcard name */
#ifdef FAT_LFN
			longInfo.startBlock = cluster;
			longInfo.startEntry = j;
			longInfo.endBlock = prevCluster;
			longInfo.endEntry = 0;
			status = FAT_getLongName(drive, 0, &longInfo, pBuffer + j, fileName);
			if (status == 1)
			{
				if (nameCompare(ffInfo->wildname, fileName, 1) == 1)
					status = 1;	// the long name (unicode) matches
				else
					status = -1;	// not match
			}
			if (status <= 0)
			{
				getShortName(pBuffer + j, fileName);
				if (nameCompare(ffInfo->wildname, fileName, 0) == 1)
					status = 0;	// the short name matches
				else
					status = -1;
			}	
#else
			getShortName(pBuffer + j, fileName);
			if (nameCompare(ffInfo->wildname, fileName, 0) == 1)
				status = 0;		// the short name matches
			else
				status = -1;
#endif

			if (status != -1)	/* found! */
			{
				/* update the FAT_FFInternal structure */
				ffInfo->nThEntry = j / FAT_DIR_ENTRY_SIZE;
				ffInfo->cluster = cluster;
				// update the ffblk structure
				/* copy field from dir entry to result */
//				pEntry = (struct FAT_directory *)(pBuffer + j);
				FAT_load_dir_info(pBuffer + j, &pEntry);
				ffres->ff_attrib = pEntry.attribute;
				ffres->ff_ftime = pEntry.time;
				ffres->ff_fdate = pEntry.date;
				ffres->ff_fsize = pEntry.fsize;
				ffres->ff_reserved = (long)ffInfo;
				ffres->ff_drive = FAT_ID;
				/* copy the name from dir entry to result */
				// note that if status = 1, the name is in unicode
				//   otherwise the name is in big5 & ascii
				if (status == 1)
					copyName(fileName, ffres->ff_name, 1);
				else

⌨️ 快捷键说明

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