mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-11 13:16:47 +00:00
🆕 新增fat文件名合法性检查
This commit is contained in:
parent
b79ba099df
commit
440b3c917f
@ -889,7 +889,9 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_
|
||||
// 父目录项的inode
|
||||
struct fat32_inode_info_t *parent_inode_info = (struct fat32_inode_info_t *)parent_inode->private_inode_info;
|
||||
// ======== todo:检验名称的合法性
|
||||
|
||||
retval = fat32_check_name_available(dEntry->name, dEntry->name_length, 0);
|
||||
if (retval != 0)
|
||||
return retval;
|
||||
// ====== 找一块连续的区域放置新的目录项 =====
|
||||
|
||||
// 计算总共需要多少个目录项
|
||||
@ -952,7 +954,10 @@ int64_t fat32_mkdir(struct vfs_index_node_t *parent_inode, struct vfs_dir_entry_
|
||||
{
|
||||
if (dEntry->name[tmp_index] == '.')
|
||||
break;
|
||||
empty_fat32_dentry->DIR_Name[tmp_index] = dEntry->name[tmp_index];
|
||||
if (fat32_check_char_available_in_short_name(dEntry->name[tmp_index], tmp_index))
|
||||
empty_fat32_dentry->DIR_Name[tmp_index] = dEntry->name[tmp_index];
|
||||
else
|
||||
empty_fat32_dentry->DIR_Name[tmp_index] = 0x20;
|
||||
}
|
||||
|
||||
// 不满的部分使用0x20填充
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <common/errno.h>
|
||||
#include <mm/slab.h>
|
||||
|
||||
static const char unavailable_character_in_short_name[] = {0x22, 0x2a, 0x2b, 0x2c, 0x2e, 0x2f, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x5b, 0x5c, 0x5d, 0x7c};
|
||||
/**
|
||||
* @brief 请求分配指定数量的簇
|
||||
*
|
||||
@ -93,7 +94,7 @@ done:;
|
||||
}
|
||||
else // 出现错误
|
||||
{
|
||||
kwarn("err in alloc clusters");
|
||||
kwarn("err in alloc clusters");
|
||||
if (clus_idx < num_clusters)
|
||||
fat32_free_clusters(inode, clusters[0]);
|
||||
return retval;
|
||||
@ -168,7 +169,6 @@ uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 在父亲inode的目录项簇中,寻找连续num个空的目录项
|
||||
*
|
||||
@ -254,4 +254,52 @@ struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *paren
|
||||
kfree(tmp_buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 检查文件名是否合法
|
||||
*
|
||||
* @param name 文件名
|
||||
* @param namelen 文件名长度
|
||||
* @param reserved 保留字段
|
||||
* @return int 合法:0, 其他:错误码
|
||||
*/
|
||||
int fat32_check_name_available(const char *name, int namelen, int8_t reserved)
|
||||
{
|
||||
if (namelen > 255 || namelen <= 0)
|
||||
return -ENAMETOOLONG;
|
||||
// 首个字符不能是空格或者'.'
|
||||
if (name[0] == 0x20 || name[0] == '.')
|
||||
return -EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 检查字符在短目录项中是否合法
|
||||
*
|
||||
* @param c 给定字符
|
||||
* @param index 字符在文件名中处于第几位
|
||||
* @return true 合法
|
||||
* @return false 不合法
|
||||
*/
|
||||
bool fat32_check_char_available_in_short_name(const char c, int index)
|
||||
{
|
||||
// todo: 严格按照fat规范完善合法性检查功能
|
||||
if (index == 0)
|
||||
{
|
||||
if (c < 0x20)
|
||||
{
|
||||
if (c != 0x05)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < sizeof(unavailable_character_in_short_name) / sizeof(char); ++i)
|
||||
{
|
||||
if (c == unavailable_character_in_short_name[i])
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
@ -2,10 +2,11 @@
|
||||
|
||||
#include "fat32.h"
|
||||
#include <filesystem/VFS/VFS.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* @brief 请求分配指定数量的簇
|
||||
*
|
||||
*
|
||||
* @param inode 要分配簇的inode
|
||||
* @param clusters 返回的被分配的簇的簇号结构体
|
||||
* @param num_clusters 要分配的簇的数量
|
||||
@ -15,12 +16,12 @@ int fat32_alloc_clusters(struct vfs_index_node_t *inode, uint32_t *clusters, int
|
||||
|
||||
/**
|
||||
* @brief 释放从属于inode的,从cluster开始的所有簇
|
||||
*
|
||||
*
|
||||
* @param inode 指定的文件的inode
|
||||
* @param cluster 指定簇
|
||||
* @return int 错误码
|
||||
*/
|
||||
int fat32_free_clusters(struct vfs_index_node_t * inode, int32_t cluster);
|
||||
int fat32_free_clusters(struct vfs_index_node_t *inode, int32_t cluster);
|
||||
|
||||
/**
|
||||
* @brief 读取指定簇的FAT表项
|
||||
@ -53,3 +54,23 @@ uint32_t fat32_write_FAT_entry(fat32_sb_info_t *fsbi, uint32_t cluster, uint32_t
|
||||
* @return struct fat32_Directory_t* 符合要求的entry的指针(指向地址高处的空目录项,也就是说,有连续num个≤这个指针的空目录项)
|
||||
*/
|
||||
struct fat32_Directory_t *fat32_find_empty_dentry(struct vfs_index_node_t *parent_inode, uint32_t num, uint32_t mode, uint32_t *res_sector, uint64_t *res_cluster, uint64_t *res_data_buf_base);
|
||||
|
||||
/**
|
||||
* @brief 检查文件名是否合法
|
||||
*
|
||||
* @param name 文件名
|
||||
* @param namelen 文件名长度
|
||||
* @param reserved 保留字段
|
||||
* @return int 合法:0, 其他:错误码
|
||||
*/
|
||||
int fat32_check_name_available(const char *name, int namelen, int8_t reserved);
|
||||
|
||||
/**
|
||||
* @brief 检查字符在短目录项中是否合法
|
||||
*
|
||||
* @param c 给定字符
|
||||
* @param index 字符在文件名中处于第几位
|
||||
* @return true 合法
|
||||
* @return false 不合法
|
||||
*/
|
||||
bool fat32_check_char_available_in_short_name(const char c, int index);
|
Loading…
x
Reference in New Issue
Block a user