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

📄 filesys.c

📁 PDA上的CF CARD 文件系统的建立程式
💻 C
📖 第 1 页 / 共 5 页
字号:
		result = NOR_FFS_setStat2(handle, buffer);
		FileSys_Errno = NOR_FFS_Errno;
		return result;
	}
  #endif

  #ifdef NAND_FLASH_DISK_ID
	if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
	{
		//result = NAND_FFS_setStat2(handle, buffer);
		//FileSys_Errno = NAND_FFS_Errno;
		result = 0;
		return result;
	}
  #endif

  #ifdef RAMDISK_ID
	if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
	{
		/* marked by chilong 05/05/2002 
		result = RD_setStat2(handle, buffer);
		FileSys_Errno = RDerrno;
		return result;
		   marked by chilong 05/05/2002 */

		// modified by chilong 05/05/2002 temporarily
		return -1;

	}
  #endif

  #ifdef FAT_ID
	if (*(FileSysHandleTable[handle]) == FAT_ID)
	{
		result = FAT_setStat2(handle, buffer);
		FileSys_Errno = FATErrno;
		return result;
	}
  #endif

  FileSys_Errno = ERROR_INVALID_DEVICE;
  return -1;
}

/**** added by MSC 03/07/2002 ****/


/*************************************************************
Function: remove
Description:
	delete an existing file
Input:
	name - filename
Output:
	0:  Success
	-1: Failure
**************************************************************/

// chilong: we should add the access-right mechanism here

int fs_remove(const char *name)
{
  int result;
  short driveNO;

  /**** added by chilong 01/15/2002 ****/
  //unsigned char *Pathname1;
  char *Pathname1;        // by zhang xue ping
  /**** added by chilong 01/15/2002 ****/

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }
  
  /**** added by chilong 12/7/2001 ****/
  if (name == NULL)
  	return -1;
  /**** added by chilong 12/7/2001 ****/

  /**** added by chilong 01/15/2002 ****/
  if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
  	FileSys_Errno = ERROR_ALLOC_MEM;
  	return -1;
  }
  /**** added by chilong 01/15/2002 ****/

  FileSys_Errno = 0;

  driveNO = getDriveNO((char *)name, Pathname1);
  if (driveNO == -1)
  {
	ap_free(Pathname1);
	return -1;
  }
	
  #ifdef NOR_FLASH_DISK_ID
	if (driveNO == NOR_FLASH_DISK_ID)
	{
		result = NOR_FFS_remove(Pathname1);
		FileSys_Errno = NOR_FFS_Errno;

		ap_free(Pathname1);

		return result;
	}
  #endif

  #ifdef NAND_FLASH_DISK_ID
	if (driveNO == NAND_FLASH_DISK_ID)
	{
		result = NAND_FFS_remove((unsigned char *)Pathname1);
		FileSys_Errno = NAND_FFS_Errno;
		
		ap_free(Pathname1);
		return result;
	}
  #endif
  #ifdef RAMDISK_ID
	if (driveNO == RAMDISK_ID)
	{
		result = RD_remove((unsigned char *)Pathname1);
		FileSys_Errno = RDerrno;
	
		ap_free(Pathname1);
		
		return result;
	}
  #endif

  #ifdef FAT_ID
	if (driveNO == FAT_ID)
	{
		result = FAT_remove((unsigned char *)Pathname1);
		FileSys_Errno = FATErrno;
	
		ap_free(Pathname1);
		
		return result;
	}
  #endif

  FileSys_Errno = ERROR_INVALID_DEVICE;
  
  ap_free(Pathname1);
  
  return -1;
}



/*************************************************************
Function: truncate
Description:
	cut a file to the given size
Input:
	handle - the file handle of the target file
	size - the new size of the file in bytes
Output:
	0	SUCCESS
	-1	FAILURE
Note:
	Only file size reduction is supported now.
**************************************************************/
int fs_truncate(int handle, unsigned long size)
{
  int result;

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  if ((handle < 0) || (handle >= MAX_OPEN_FILE) || (FileSysHandleTable[handle] == NULL))
  {
	FileSys_Errno = ERROR_INVALID_HANDLE;
	return -1;
  }

  #ifdef NOR_FLASH_DISK_ID
	if (*(FileSysHandleTable[handle]) == NOR_FLASH_DISK_ID)
	{
		result = NOR_FFS_truncate(handle, size);
		FileSys_Errno = NOR_FFS_Errno;
		return result;
	}
  #endif


  #ifdef NAND_FLASH_DISK_ID
	if (*(FileSysHandleTable[handle]) == NAND_FLASH_DISK_ID)
	{
		result = NAND_FFS_truncate(handle, size);
		FileSys_Errno = NAND_FFS_Errno;
		return result;
	}
  #endif

  #ifdef RAMDISK_ID
	if (*(FileSysHandleTable[handle]) == RAMDISK_ID)
	{
		result = RD_truncate(handle, size);
		FileSys_Errno = RDerrno;
		return result;
	}
  #endif

  #ifdef FAT_ID
	if (*(FileSysHandleTable[handle]) == FAT_ID)
	{
		result = FAT_truncate(handle, size);
		FileSys_Errno = FATErrno;
		return result;
	}
  #endif

  FileSys_Errno = ERROR_INVALID_DEVICE;
  return -1;
}



/*************************************************************
Function : access
Description:
	determine the file access permission
Input:
	pathname - file or directory path name
	mode - permission setting
		Value	Meaning
		-----   -------
		00	check for existence only
		02	check for write permission
		04	check for read permission
		06	check for read and write permission
Output:
	0	the file has the given mode
	-1	the file does not exist or is not accessible in the given mode
	-2	file system error
Note:
**************************************************************/
int fs_access(char *name, int mode)
{
  int result;
  short driveNO;

  /**** added by chilong 01/15/2002 ****/
  //unsigned char *Pathname1;
  char *Pathname1;
  /**** added by chilong 01/15/2002 ****/

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }
  
  /**** added by chilong 12/7/2001 ****/
  if (name == NULL)
  	return -1;
  /**** added by chilong 12/7/2001 ****/

  /**** added by chilong 01/15/2002 ****/
  if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
  	FileSys_Errno = ERROR_ALLOC_MEM;
  	return -1;
  }
  /**** added by chilong 01/15/2002 ****/

  FileSys_Errno = 0;

  driveNO = getDriveNO(name, Pathname1);
  if (driveNO == -1)
  {
	ap_free(Pathname1);
	return -1;
  }

  #ifdef NOR_FLASH_DISK_ID
	if (driveNO == NOR_FLASH_DISK_ID)
	{
		result = NOR_FFS_access(Pathname1, mode);
		FileSys_Errno = NOR_FFS_Errno;

		ap_free(Pathname1);

		return result;
	}
  #endif

  #ifdef NAND_FLASH_DISK_ID
	if (driveNO == NAND_FLASH_DISK_ID)
	{
		result = NAND_FFS_access((unsigned char *)Pathname1, mode);
		FileSys_Errno = NAND_FFS_Errno;
		
		ap_free(Pathname1);
		return result;
	}
  #endif

  #ifdef RAMDISK_ID
	if (driveNO == RAMDISK_ID)
	{
		result = RD_access((unsigned char *)Pathname1, mode);
		FileSys_Errno = RDerrno;
	
		ap_free(Pathname1);
		
		return result;
	}
  #endif

  #ifdef FAT_ID
	if (driveNO == FAT_ID)
	{
		result = FAT_access((unsigned char *)Pathname1, mode);
		FileSys_Errno = FATErrno;
		
		ap_free(Pathname1);
		return result;
	}
  #endif

  FileSys_Errno = ERROR_INVALID_DEVICE;

  ap_free(Pathname1);
  
  return -1;
}



/*************************************************************
Function : rename
Description:
	rename a file
Input:
	oldName - original filename
	newName - new filename
Output:
	0	rename succeeded
	-1	rename failed
Note:
**************************************************************/
int fs_rename(const char *oldName, const char *newName)
{
  int result;
  short drive1, drive2;

  /**** added by chilong 01/15/2002 ****/
  //unsigned char *Pathname1;
  //unsigned char *Pathname2;
  char *Pathname1;
  char *Pathname2;

  /**** added by chilong 01/15/2002 ****/

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }
  
  /**** added by chilong 12/7/2001 ****/
  if (oldName == NULL || newName == NULL)
  	return -1;
  /**** added by chilong 12/7/2001 ****/

  /**** added by chilong 01/15/2002 ****/
  if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
  	FileSys_Errno = ERROR_ALLOC_MEM;
  	return -1;
  }

  if ((Pathname2 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
  	FileSys_Errno = ERROR_ALLOC_MEM;
  	ap_free(Pathname1);
  	return -1;
  }
  /**** added by chilong 01/15/2002 ****/


  FileSys_Errno = 0;

  drive1 = getDriveNO((char *)oldName, Pathname1);
  if (drive1 == -1)
  {
	FileSys_Errno = ERROR_INVALID_DRIVE;
  	
	ap_free(Pathname1);
	ap_free(Pathname2);
	return -1;
  }

  drive2 = getDriveNO((char *)newName, Pathname2);
  if (drive2 == -1)
  {
	FileSys_Errno = ERROR_INVALID_DRIVE;
	
	ap_free(Pathname1);
	ap_free(Pathname2);
	return -1;
  }

  if (drive1 != drive2)
  {
	FileSys_Errno = ERROR_NOT_SAME_DRIVE;

	ap_free(Pathname1);
	ap_free(Pathname2);
	return -1;
  }

  #ifdef NOR_FLASH_DISK_ID
	if (drive1 == NOR_FLASH_DISK_ID)
	{
		result = NOR_FFS_rename(Pathname1, Pathname2);
		FileSys_Errno = NOR_FFS_Errno;

		ap_free(Pathname1);
		ap_free(Pathname2);
		return result;
	}
  #endif

  #ifdef NAND_FLASH_DISK_ID
	if (drive1 == NAND_FLASH_DISK_ID)
	{
		result = NAND_FFS_rename((unsigned char *)Pathname1, (unsigned char *)Pathname2);
		FileSys_Errno = NAND_FFS_Errno;

		ap_free(Pathname1);
		ap_free(Pathname2);

		return result;
	}
  #endif
  #ifdef RAMDISK_ID
	if (drive1 == RAMDISK_ID)
	{
		result = RD_rename((unsigned char *)Pathname1, (unsigned char *)Pathname2);
		FileSys_Errno = RDerrno;

		ap_free(Pathname2);
		ap_free(Pathname1);
		
		return result;
	}
  #endif

  #ifdef FAT_ID
	if (drive1 == FAT_ID)
	{
		result = FAT_rename((unsigned char *)Pathname1, (unsigned char *)Pathname2);
		FileSys_Errno = FATErrno;

		ap_free(Pathname1);
		ap_free(Pathname2);
		return result;
	}
  #endif

  FileSys_Errno = ERROR_INVALID_DEVICE;

  ap_free(Pathname1);
  ap_free(Pathname2);

  return -1;
}



/*************************************************************
Function : 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 fs_findfirst(unsigned char *target, struct ffblk *ffres, int attrib)
int fs_findfirst(char *target, struct ffblk *ffres, int attrib)
{
  int result;
  short driveNO;

  /**** added by chilong 01/15/2002 ****/
  //unsigned char *Pathname1;
  char *Pathname1;      // by zhang xue ping
  /**** added by chilong 01/15/2002 ****/

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }
  
  /**** added by chilong 12/7/2001 ****/
  if (target == NULL || ffres == NULL)
  	return -1;
  /**** added by chilong 12/7/2001 ****/

  /**** added by chilong 01/15/2002 ****/
  if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
  	FileSys_Errno = ERROR_ALLOC_MEM;
  	return -1;
  }
  /**** added by chilong 01/15/2002 ****/

  FileSys_Errno = 0;

  driveNO = getDriveNO(target, Pathname1);
  if (driveNO == -1)
  {
	ap_free(Pathname1);
	return -1;
  }

  #ifdef NOR_FLASH_DISK_ID
	if (driveNO == NOR_FLASH_DISK_ID)
	{
		result = NOR_FFS_findfirst(Pathname1, ffres, attrib);
		FileSys_Errno = NOR_FFS_Errno;
		
		ap_free(Pathname1);
		return result;
	}
  #endif

  #ifdef NAND_FLASH_DISK_ID
	if (driveNO == NAND_FLASH_DISK_ID)
	{
		result = NAND_FFS_findfirst((unsigned char *)Pathname1, ffres, attrib);
		FileSys_Errno = NAND_FFS_Errno;
		
		ap_free(Pathname1);
		return result;
	}
  #endif
  #ifdef RAMDISK_ID
	if (driveNO == RAMDISK_ID)
	{
		result = RD_findfirst((unsigned char *)Pathname1, ffres, attrib);
		FileSys_Errno = RDerrno;
		
		ap_free(Pathname1);
		return result;
	}
  #endif

  #ifdef FAT_ID
	if (driveNO == FAT_ID)
	{
		result = FAT_findfirst((unsigned char *)Pathname1, ffres, attrib);
		FileSys_Errno = FATErrno;

		ap_free(Pathname1);
		return result;
	}
  #endif

  FileSys_Errno = ERROR_INVALID_DEVICE;

  ap_free(Pathname1);

  return -1;
}



/*************************************************************
Function : 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 fs_findnext(struct ffblk *ffres)
{
  int result;

  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }
  
  /**** added by chilong 12/7/2001 ****/
  if (ffres == NULL)
  	return -1;
  /**** added by chilong 12/7/2001 ****/

  FileSys_Errno = 0;

  #ifdef NOR_FLASH_DISK_ID
	if (ffres->ff_drive == NOR_FLASH_DISK_ID)
	{
		result = NOR_FFS_findnext(ffres);
		FileSys_Errno = NOR_FFS_Errno;
		return result;
	}
  #endif


  #ifdef NAND_FLASH_DISK_ID
	if (ffres->ff_drive == NAND_FLASH_DISK_ID)
	{
		result = NAND_FFS_findnext(ffres);
		FileSys_Errno = NAND_FFS_Errno;
		return result;
	}

⌨️ 快捷键说明

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