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

📄 fat_find.c

📁 ucFS的加强版,含完整代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	FS__fat_dentry_type *s;
	FS_u32 i;
	FS_u32 dsec;
	FS_i32 cluster;
	int len;
	int err;
	FS_u16 val;
	char *buffer;
	char realname[12];
	
	buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
	if (!buffer) {
		return -1;
	}
	len = FS__CLIB_strlen(pFileName);
	if (len > 11) {
		len = 11;
	}
	/* Read directory */
	for (i = 0; i < DirSize; i++) {
		dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, i);
		if (dsec == 0) {
			FS__fat_free(buffer);
			return -1;
		}
		err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
		if (err < 0) {
			FS__fat_free(buffer);
			return -1;
		}
		s = (FS__fat_dentry_type*)buffer;
		while (1) {
			if (s >= (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
				break;  /* End of sector reached */
			}
			if (s->data[0] == 0x00) {
				break;  /* Empty entry found */
			}
			if (s->data[0] == (unsigned char)0xe5) {
				break;  /* Deleted entry found */
			}
			s++;
		}
		if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) {
			FS__fat_make_realname(realname,pFileName);
			/* Free entry found. Make entry and return 1st block of the file */
			FS__CLIB_strncpy((char*)s->data, realname, 11);
			s->data[11] = FS_FAT_ATTR_ARCHIVE;
			/* Alloc block in FAT */
			cluster = FS__fat_FAT_alloc(Idx, Unit, -1);
			if (cluster >= 0) {
				s->data[12]     = 0x00;                           /* Res */
				s->data[13]     = 0x00;                           /* CrtTimeTenth (optional, not supported) */
				s->data[14]     = 0x00;                           /* CrtTime (optional, not supported) */
				s->data[15]     = 0x00;
				s->data[16]     = 0x00;                           /* CrtDate (optional, not supported) */
				s->data[17]     = 0x00;
				s->data[18]     = 0x00;                           /* LstAccDate (optional, not supported) */
				s->data[19]     = 0x00;
				val             = FS_X_OS_GetTime();
				s->data[22]     = (unsigned char)(val & 0xff);   /* WrtTime */
				s->data[23]     = (unsigned char)(val / 256);
				val             = FS_X_OS_GetDate();
				s->data[24]     = (unsigned char)(val & 0xff);   /* WrtDate */
				s->data[25]     = (unsigned char)(val / 256);
				s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
				s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
				s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
				s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
				s->data[28]     = 0x00;                           /* FileSize */
				s->data[29]     = 0x00;
				s->data[30]     = 0x00;
				s->data[31]     = 0x00;
				err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
				if (err < 0) {
					FS__fat_free(buffer);
					return -1;
				}
			}
			FS__fat_free(buffer);
			return cluster;
		}
	}
	FS__fat_free(buffer);
  return -2;      /* Directory is full */
}


FS_u32 _FS_fat_make_short_name(FS_u8 *pFileName,FS_u16 cnt)
{
	int last;
	int idx;
    FS_u8 *pstr;
	
	last = 11;
	
	do
	{
		idx = cnt%10;
		pFileName[--last] = idx + '0';
		cnt /= 10;
	} while(cnt);

	return 0;
}

//////////////////////////////////////////////////////////////////////////
FS_i32 _FS_fat_create_directory_ex(int Idx, FS_u32 Unit,  const char *pDirName,
							  FS_u32 DirStart, FS_u32 DirSize)
{
	
	FS_u16 len;
	char dname[12];
	FS_i32 cluster = 0;
	
	len = FS__CLIB_strlen(pDirName);
	
	if(len > 11 || FS__CLIB_check_wchar(pDirName))
	{
		//len = FS__CLIB_str2wstr(g_search_wide_name,pFileName,len);
		cluster = _FS_fat_create_directory_long(Idx,Unit,pDirName,len,DirStart,DirSize);
	}
	else
	{
		FS__fat_make_realname(dname,pDirName);
		cluster = _FS_fat_create_directory_short(Idx,Unit,dname,11,DirStart,DirSize);
	}
	
	return cluster;
}

FS_i32 _FS_fat_create_directory_long(int Idx, FS_u32 Unit,  FS_u8 *pDirName,
                                FS_u16 name_len,FS_u32 DirStart, FS_u32 DirSize)
{
	FS__fat_dentry_type *s;
	FS_u32 i;
	FS_u32 dsec;
	
	FS_u32 long_start;
    FS_u16 long_cnt;
	FS_u16 long_offs;
	FS_u16 long_state;

	int len;
	int err;
	int c;
	char *buffer;
	 
    FS_u16 str_count;
	
	FS_u16 *str_cur;
	FS_u16 *str_last;
	
	FS_u16 str_state;
	FS_u16 *buf;
	
	FS_u16 long_name_len;
	FS_u16 long_name_idx;
	FS_u8  fname[12];
	FS_i32 cluster;
	FS_u16 val_time;
	FS_u16 val_date;
	FS_u8 short_loop;
	FS_u8 short_checksum;

	str_state = 0;

	buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
	
	if (!buffer)
	{
		return -1;
	}

//////////////////////////////////////////////////////////////////////////
	long_name_len = FS__CLIB_str2wstr(g_search_wide_name,pDirName,name_len);
    if(long_name_len % 13)
	{
		FS__CLIB_memset(&g_search_wide_name[long_name_len + 1],0xFF, (13 - long_name_len%13 -1)*sizeof(FS_u16));
		long_name_len = long_name_len + 13 - long_name_len%13;
		g_search_wide_name[long_name_len] = 0;
	}

    FS__CLIB_strncpy(fname,pDirName,11);
    
	if(name_len < 11)
	{
        FS__CLIB_memset(fname,0x20,11 - name_len);
	   fname[11] = 0;
	}

	long_cnt = 0;

	do
	{
	  buffer[11] = 0;
      dsec = _FS_fat_find_file_short(Idx,Unit,fname,11,buffer,DirStart,DirSize);
	  
	  if(buffer[11] == 0)
		  break;
	  else
	  {
		  long_cnt++;
		  _FS_fat_make_short_name(fname,long_cnt);
	  }
	}while(1);
	

	long_name_idx = 0;

//////////////////////////////////////////////////////////////////////////
	/* Read directory */
	for (i = 0; i < DirSize; i++) 
	{
		dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, i);
		
		if (dsec == 0) 
		{
			FS__fat_free(buffer);
			return -1;
		}
		
		err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
		
		if (err < 0)
		{
			FS__fat_free(buffer);
			return -1;
		}
		
		s = (FS__fat_dentry_type*)buffer;
		
		for(; s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE); s++)
		{
			if(str_state == 0)
			{
				if(s->data[0] != 0x00 && s->data[0] != 0xE5)
					continue;

                if(s->data[11] != 0)
					continue;
				
				long_start = i;
                long_offs = s - buffer;
                
				long_name_idx = 0;
				str_state = 1;
			}
			
			if(str_state == 1)
			{
				if(s->data[0] != 0x00 && s->data[0] != 0xE5)
				{
					str_state = 0;
					continue;
				}
				
				long_name_idx += 13;

				
				if(long_name_idx == long_name_len) //first
				{
					str_state = 2;
					continue;
				}
			}
			
			if(str_state == 2)
			{
				break;
			}
		}
		
		if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE))
		    break;
	}
	
	if(i == DirSize)
	{
		FS__fat_free(buffer);
		return -1;
	}
	
    long_state = 0;
	
	short_loop = 0;
	short_checksum = 0;
				
	for (short_loop = 11; short_loop > 0; short_loop--)
		short_checksum = ((short_checksum & 1) ? 0x80 : 0) + (short_checksum >> 1) + fname[11 - short_loop];


	for (i = long_start; i < DirSize; i++) 
	{
		dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, i);
		
		if (dsec == 0) 
		{
			FS__fat_free(buffer);
			return -1;
		}
		
		err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
		
		if (err < 0)
		{
			FS__fat_free(buffer);
			return -1;
		}
		
		s = (FS__fat_dentry_type*)buffer;
		
		for(; s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE); s++)
		{
			if(long_state == 0)
			{
				s += long_offs;
				long_state = 1;
			}
			
			if(long_state == 1)
			{
				s->data[0] = long_name_idx/13;
				
				if(long_name_idx == long_name_len)
					s->data[0] |= 0x40;
				
				buf = &s->data[1];
				
				buf[0] = g_search_wide_name[long_name_idx - 13];
                buf[1] = g_search_wide_name[long_name_idx - 12];
				buf[2] = g_search_wide_name[long_name_idx - 11];
				buf[3] = g_search_wide_name[long_name_idx - 10];
				buf[4] = g_search_wide_name[long_name_idx - 9];
				
				buf = &s->data[14];
				buf[0] = g_search_wide_name[long_name_idx - 8];
                buf[1] = g_search_wide_name[long_name_idx - 7];
				buf[2] = g_search_wide_name[long_name_idx - 6];
				buf[3] = g_search_wide_name[long_name_idx - 5];
				buf[4] = g_search_wide_name[long_name_idx - 4];
				buf[5] = g_search_wide_name[long_name_idx - 3];
				
                buf = &s->data[28];
				buf[0] = g_search_wide_name[long_name_idx - 2];
				buf[1] = g_search_wide_name[long_name_idx - 1];
				
				s->data[11] = FS_FAT_ATTR_LONGNAME;
				s->data[13] = short_checksum;
				
				long_name_idx -= 13;
				
				if(!long_name_idx)
				{
					long_state = 2;
					continue;
				}
			}
			
			if(long_state == 2)
			{
				FS__CLIB_strncpy(s->data,fname,11);
				
				s->data[11] = FS_FAT_ATTR_DIRECTORY;
				
				cluster = FS__fat_FAT_alloc(Idx, Unit, -1);              /* Alloc block in FAT */
				
				if (cluster >= 0) 
				{
					s->data[12]     = 0x00;                                /* Res */
					s->data[13]     = 0x00;                                /* CrtTimeTenth (optional, not supported) */
					s->data[14]     = 0x00;                                /* CrtTime (optional, not supported) */
					s->data[15]     = 0x00;
					s->data[16]     = 0x00;                                /* CrtDate (optional, not supported) */
					s->data[17]     = 0x00;
					s->data[18]     = 0x00;                                /* LstAccDate (optional, not supported) */
					s->data[19]     = 0x00;
					val_time        = FS_X_OS_GetTime();
					s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
					s->data[23]     = (unsigned char)(val_time / 256);
					val_date        = FS_X_OS_GetDate();
					s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
					s->data[25]     = (unsigned char)(val_date / 256);
					s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
					s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
					s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
					s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
					s->data[28]     = 0x00;                                /* FileSize */
					s->data[29]     = 0x00;
					s->data[30]     = 0x00;
					s->data[31]     = 0x00;
					err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer); /* Write the modified directory sector */
					if (err < 0) {
						FS__fat_free(buffer);
						return -1;
					}
					/* Clear new directory and make '.' and '..' entries */
					/* Make "." entry */
					FS__CLIB_memset(buffer, 0x00, (FS_size_t)FS_FAT_SEC_SIZE);
					s = (FS__fat_dentry_type*)buffer;
					FS__CLIB_strncpy((char*)s->data, ".          ", 11);
					s->data[11]     = FS_FAT_ATTR_DIRECTORY;
					s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
					s->data[23]     = (unsigned char)(val_time / 256);
					s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
					s->data[25]     = (unsigned char)(val_date / 256);
					s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
					s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
					s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
					s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
					/* Make entry ".." */
					s++;
					FS__CLIB_strncpy((char*)s->data, "..         ", 11);
					s->data[11]     = FS_FAT_ATTR_DIRECTORY;
					s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
					s->data[23]     = (unsigned char)(val_time / 256);
					s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
					s->data[25]     = (unsigned char)(val_date / 256);
					s->data[26]     = (unsigned char)(DirStart & 0xff);    /* FstClusLo / FstClusHi */ 
					s->data[27]     = (unsigned char)((DirStart / 256) & 0xff);
					s->data[20]     = (unsigned char)((DirStart / 0x10000L) & 0xff);
					s->data[21]     = (unsigned char)((DirStart / 0x1000000L) & 0xff);
					dsec = FS__fat_dir_realsec(Idx, Unit, cluster, 0); /* Find 1st absolute sector of the new directory */
					
					if (dsec == 0) 
					{
						FS__fat_free(buffer);
						return -1;
					}
					
					/* Write "." & ".." entries into the new directory */
					err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
					
					if (err < 0) 
					{
						FS__fat_free(buffer);
						return -1;
					}
					
					FS__fat_free(buffer);
					
					return ((FS_i32)cluster);
				}
			}
		}

		err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
		
		if (err < 0)
		{
			FS__fat_free(buffer);
			return -1;
		}
	}

	//should not be here
	FS__fat_free(buffer);
	return -1;
}



FS_i32 _FS_fat_create_directory_short(int Idx, FS_u32 Unit,  FS_u8 *pDirName,
                                 FS_u16 name_len, FS_u32 DirStart, FS_u32 DirSize)
{
  char *buffer;
  FS__fat_dentry_type *s;
  FS_u32 dirindex;
  FS_u32 dsec;
  FS_i32 cluster;
  FS_u16 val_time;
  FS_u16 val_date;
  
  int err;
  int len;
  int j;

  buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
  
  if (!buffer)
  {
	  return -1;
  }
  
  /* Read directory */
  for (dirindex = 0; dirindex < DirSize; dirindex++) 
  {
    dsec = FS__fat_dir_realsec(Idx, Unit, DirStart, dirindex);
    
	if (dsec == 0) 
	{
      /* Translation of relativ directory sector to an absolute sector failed */
      FS__fat_free(buffer);
      return -1;
    }
    
	err = FS__lb_read(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer); /* Read directory sector */
    
	if (err < 0)
	{
      /* Read error */
      FS__fat_free(buffer);
      return -1;
    }
    
	/* Scan the directory sector for a free or deleted entry */
    s = (FS__fat_dentry_type*)buffer;
    
	while (1)
	{
		if (s >= (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) 
		{
			break;  /* End of sector reached */
		}
      
	  if (s->data[0] == 0x00) 
	  {
        break;  /* Found a free entry */
      }
      
	  if (s->data[0] == (unsigned char)0xe5) 
	  {
        break;  /* Found a deleted entry */
      }
      s++;
    }
    
	if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE)) 
	{
      /* Free entry found. Make entry and return 1st block of the file. */
      FS__CLIB_strncpy((char*)s->data, pDirName, len);
       
	  s->data[11] = FS_FAT_ATTR_DIRECTORY;
      
	  cluster = FS__fat_FAT_alloc(Idx, Unit, -1);              /* Alloc block in FAT */
      
	  if (cluster >= 0) 
	  {
        s->data[12]     = 0x00;                                /* Res */
        s->data[13]     = 0x00;                                /* CrtTimeTenth (optional, not supported) */
        s->data[14]     = 0x00;                                /* CrtTime (optional, not supported) */
        s->data[15]     = 0x00;
        s->data[16]     = 0x00;                                /* CrtDate (optional, not supported) */
        s->data[17]     = 0x00;
        s->data[18]     = 0x00;                                /* LstAccDate (optional, not supported) */
        s->data[19]     = 0x00;
        val_time        = FS_X_OS_GetTime();
        s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
        s->data[23]     = (unsigned char)(val_time / 256);
        val_date        = FS_X_OS_GetDate();
        s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
        s->data[25]     = (unsigned char)(val_date / 256);
        s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
        s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
        s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
        s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
        s->data[28]     = 0x00;                                /* FileSize */
        s->data[29]     = 0x00;
        s->data[30]     = 0x00;
        s->data[31]     = 0x00;
        err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer); /* Write the modified directory sector */
        
		if (err < 0) 
		{
          FS__fat_free(buffer);
          return -1;
        }
        
		/* Clear new directory and make '.' and '..' entries */
        /* Make "." entry */
        
		FS__CLIB_memset(buffer, 0x00, (FS_size_t)FS_FAT_SEC_SIZE);
        s = (FS__fat_dentry_type*)buffer;
        FS__CLIB_strncpy((char*)s->data, ".          ", 11);
        s->data[11]     = FS_FAT_ATTR_DIRECTORY;
        s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
        s->data[23]     = (unsigned char)(val_time / 256);
        s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
        s->data[25]     = (unsigned char)(val_date / 256);
        s->data[26]     = (unsigned char)(cluster & 0xff);    /* FstClusLo / FstClusHi */ 
        s->data[27]     = (unsigned char)((cluster / 256) & 0xff);
        s->data[20]     = (unsigned char)((cluster / 0x10000L) & 0xff);
        s->data[21]     = (unsigned char)((cluster / 0x1000000L) & 0xff);
        /* Make entry ".." */
        s++;
        FS__CLIB_strncpy((char*)s->data, "..         ", 11);
        s->data[11]     = FS_FAT_ATTR_DIRECTORY;
        s->data[22]     = (unsigned char)(val_time & 0xff);   /* WrtTime */
        s->data[23]     = (unsigned char)(val_time / 256);
        s->data[24]     = (unsigned char)(val_date & 0xff);   /* WrtDate */
        s->data[25]     = (unsigned char)(val_date / 256);
        s->data[26]     = (unsigned char)(DirStart & 0xff);    /* FstClusLo / FstClusHi */ 
        s->data[27]     = (unsigned char)((DirStart / 256) & 0xff);
        s->data[20]     = (unsigned char)((DirStart / 0x10000L) & 0xff);
        s->data[21]     = (unsigned char)((DirStart / 0x1000000L) & 0xff);
        dsec = FS__fat_dir_realsec(Idx, Unit, cluster, 0); /* Find 1st absolute sector of the new directory */
        
		if (dsec == 0) 
		{
			FS__fat_free(buffer);
			return -1;
        }
        /* Write "." & ".." entries into the new directory */
        err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
        
		if (err < 0) 
		{
			FS__fat_free(buffer);
			return -1;
        }
        /* Clear rest of the directory cluster */
        FS__CLIB_memset(buffer, 0x00, (FS_size_t)FS_FAT_SEC_SIZE);
        
		for (j = 1; j < FS__FAT_aBPBUnit[Idx][Unit].SecPerClus; j++) 
		{
			dsec = FS__fat_dir_realsec(Idx, Unit, cluster, j);
			err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
			
			if (err < 0) 
			{
				FS__fat_free(buffer);
				return -1;
			}
        }
        FS__fat_free(buffer);
        return 1;
		
      }
      FS__fat_free(buffer);
      return -1;
    }
  }
  FS__fat_free(buffer);
  return -2;  /* Directory is full */
}

⌨️ 快捷键说明

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