📄 fat_find.c
字号:
FS_u16 str_count;
FS_u16 *str_cur;
FS_u16 *str_last;
FS_u16 str_state;
FS_u16 *buf;
FS_u8 checksum;
FS_u8 short_checksum;
FS_i16 short_loop;
str_state = 0;
buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
if (!buffer)
{
return -1;
}
/* 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[11] != FS_FAT_ATTR_LONGNAME)
continue;
if(!(s->data[0] & 0x40)) //end idx
continue;
FS__CLIB_memset(g_search_tmp_name,0,sizeof(g_search_tmp_name));
str_count = 0;
str_last = str_cur = g_search_tmp_name;
checksum = s->data[13];
long_start = i;
long_cnt = 0;
long_offs = s - buffer;
str_state = 1;
}
if(str_state == 1)
{
if(s->data[11] != FS_FAT_ATTR_LONGNAME)
{
str_state = 0;
continue;
}
if(checksum != s->data[13])
{
str_state = 0;
continue;
}
buf = &s->data[28];
*str_cur++ = buf[1];
*str_cur++ = buf[0];
buf = &s->data[14];
*str_cur++ = buf[5];
*str_cur++ = buf[4];
*str_cur++ = buf[3];
*str_cur++ = buf[2];
*str_cur++ = buf[1];
*str_cur++ = buf[0];
buf = &s->data[1];
*str_cur++ = buf[4];
*str_cur++ = buf[3];
*str_cur++ = buf[2];
*str_cur++ = buf[1];
*str_cur++ = buf[0];
long_cnt++;
if(str_cur - g_search_tmp_name > 255)
{
str_state = 0;
continue;
}
if(s->data[0] & 0x01) //first
{
str_state = 2;
continue;
}
}
if(str_state == 2)
{
if(FS__CLIB_wstrncmp_r(pName,str_cur - 1,name_len) != 0)
{
str_state = 0;
continue;
}
if(s->data[0] == 0xE5 || s->data[0] == 0x00)
{
FS__fat_free(buffer);
return 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) + s->data[11 - short_loop];
if(checksum != short_checksum)
{
FS__fat_free(buffer);
return 0;
}
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;
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] = 0xE5;
s->data[11] = 0;
long_cnt --;
if(!long_cnt)
{
long_state = 2;
continue;
}
}
if(long_state == 2)
{
if (pDirEntry)
{
FS__CLIB_memcpy(pDirEntry, s, sizeof(FS__fat_dentry_type));
}
s->data[0] = 0xe5;
s->data[11] = 0;
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);
dsec = (FS_u32)s->data[26];
dsec += (FS_u32)s->data[27] * 0x100UL;
dsec += (FS_u32)s->data[20] * 0x10000UL;
dsec += (FS_u32)s->data[21] * 0x1000000UL;
return ((FS_i32)dsec);
}
}
err = FS__lb_write(FS__pDevInfo[Idx].devdriver, Unit, dsec, (void*)buffer);
if (err < 0)
{
FS__fat_free(buffer);
return -1;
}
}
/* Entry found. Return number of 1st block of the file */
}
static FS_i32 _FS_fat_del_unit_short(int Idx, FS_u32 Unit, const short *pName,
FS_u16 name_len, FS__fat_dentry_type *pDirEntry,
FS_u32 DirStart, FS_u32 DirSize,FS_u8 unit_attr)
{
FS__fat_dentry_type *s;
FS_u32 i;
FS_u32 dsec;
int len;
int err;
int c;
char *buffer;
buffer = FS__fat_malloc(FS_FAT_SEC_SIZE);
if (!buffer)
{
return -1;
}
/* 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(s->data[11] == FS_FAT_ATTR_LONGNAME)
continue;
if(s->data[0] == 0xE5)
continue;
if(s->data[0] == 0x00)
{
FS__fat_free(buffer);
return 0;
}
if(FS__CLIB_strncmp(pName,s->data,name_len) != 0)
continue;
break;
}
if (s < (FS__fat_dentry_type*)(buffer + FS_FAT_SEC_SIZE))
{
/* Entry found. Return number of 1st block of the file */
if (pDirEntry)
{
FS__CLIB_memcpy(pDirEntry, s, sizeof(FS__fat_dentry_type));
}
FS__fat_free(buffer);
dsec = (FS_u32)s->data[26];
dsec += (FS_u32)s->data[27] * 0x100UL;
dsec += (FS_u32)s->data[20] * 0x10000UL;
dsec += (FS_u32)s->data[21] * 0x1000000UL;
return ((FS_i32)dsec);
}
}
FS__fat_free(buffer);
return -1;
}
//////////////////////////////////////////////////////////////////////////
FS_i32 _FS_fat_create_file_ex(int Idx, FS_u32 Unit, const char *pFileName,
FS_u32 DirStart, FS_u32 DirSize)
{
FS_u16 len;
char dname[12];
FS_i32 cluster = 0;
len = FS__CLIB_strlen(pFileName);
if(len > 11 || FS__CLIB_check_wchar(pFileName))
{
//len = FS__CLIB_str2wstr(g_search_wide_name,pFileName,len);
cluster = _FS_fat_create_file_long(Idx,Unit,pFileName,len,DirStart,DirSize);
}
else
{
FS__fat_make_realname(dname,pFileName);
cluster = _FS_fat_create_file_short(Idx,Unit,g_search_wide_name,11,DirStart,DirSize);
}
return cluster;
}
FS_i32 _FS_fat_create_file_long(int Idx, FS_u32 Unit, FS_u8 *pFileName,
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;
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,pFileName,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,pFileName,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_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 ((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_file_short(int Idx, FS_u32 Unit, FS_u8 *pFileName,
FS_u16 name_len, FS_u32 DirStart, FS_u32 DirSize)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -