mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 12:16:31 +00:00
feat:添加symlink系统调用 (#984)
* 添加symlink系统调用 * 修改FATInode的dname的获取逻辑 * 修改fat对Dname的处理,分离dname和inode缓存的key --------- Co-authored-by: sparkzky <sparkhhhhhhhhh@outlook.com> Co-authored-by: longjin <longjin@DragonOS.org>
This commit is contained in:
@ -2,11 +2,11 @@ use alloc::string::ToString;
|
||||
use core::cmp::Ordering;
|
||||
use core::intrinsics::unlikely;
|
||||
use core::{any::Any, fmt::Debug};
|
||||
use hashbrown::HashMap;
|
||||
use log::error;
|
||||
use system_error::SystemError;
|
||||
|
||||
use alloc::{
|
||||
collections::BTreeMap,
|
||||
string::String,
|
||||
sync::{Arc, Weak},
|
||||
vec::Vec,
|
||||
@ -36,6 +36,7 @@ use crate::{
|
||||
};
|
||||
|
||||
use super::entry::FATFile;
|
||||
use super::utils::{to_search_name, to_search_name_string};
|
||||
use super::{
|
||||
bpb::{BiosParameterBlock, FATType},
|
||||
entry::{FATDir, FATDirEntry, FATDirIter, FATEntry},
|
||||
@ -106,9 +107,9 @@ pub struct FATInode {
|
||||
parent: Weak<LockedFATInode>,
|
||||
/// 指向自身的弱引用
|
||||
self_ref: Weak<LockedFATInode>,
|
||||
/// 子Inode的B树. 该数据结构用作缓存区。其中,它的key表示inode的名称。
|
||||
/// 子Inode的map. 该数据结构用作缓存区。其中,它的key表示inode的名称。
|
||||
/// 请注意,由于FAT的查询过程对大小写不敏感,因此我们选择让key全部是大写的,方便统一操作。
|
||||
children: BTreeMap<DName, Arc<LockedFATInode>>,
|
||||
children: HashMap<String, Arc<LockedFATInode>>,
|
||||
/// 当前inode的元数据
|
||||
metadata: Metadata,
|
||||
/// 指向inode所在的文件系统对象的指针
|
||||
@ -148,24 +149,25 @@ impl FATInode {
|
||||
fn find(&mut self, name: &str) -> Result<Arc<LockedFATInode>, SystemError> {
|
||||
match &self.inode_type {
|
||||
FATDirEntry::Dir(d) => {
|
||||
let dname = DName::from(name.to_uppercase());
|
||||
let search_name = to_search_name(name);
|
||||
// 尝试在缓存区查找
|
||||
if let Some(entry) = self.children.get(&dname) {
|
||||
if let Some(entry) = self.children.get(&search_name) {
|
||||
return Ok(entry.clone());
|
||||
}
|
||||
// 在缓存区找不到
|
||||
// 在磁盘查找
|
||||
let fat_entry: FATDirEntry =
|
||||
d.find_entry(name, None, None, self.fs.upgrade().unwrap())?;
|
||||
let dname = DName::from(name);
|
||||
// 创建新的inode
|
||||
let entry_inode: Arc<LockedFATInode> = LockedFATInode::new(
|
||||
dname.clone(),
|
||||
dname,
|
||||
self.fs.upgrade().unwrap(),
|
||||
self.self_ref.clone(),
|
||||
fat_entry,
|
||||
);
|
||||
// 加入缓存区, 由于FAT文件系统的大小写不敏感问题,因此存入缓存区的key应当是全大写的
|
||||
self.children.insert(dname, entry_inode.clone());
|
||||
self.children.insert(search_name, entry_inode.clone());
|
||||
return Ok(entry_inode);
|
||||
}
|
||||
FATDirEntry::UnInit => {
|
||||
@ -197,7 +199,7 @@ impl LockedFATInode {
|
||||
let inode: Arc<LockedFATInode> = Arc::new(LockedFATInode(SpinLock::new(FATInode {
|
||||
parent,
|
||||
self_ref: Weak::default(),
|
||||
children: BTreeMap::new(),
|
||||
children: HashMap::new(),
|
||||
fs: Arc::downgrade(&fs),
|
||||
inode_type,
|
||||
metadata: Metadata {
|
||||
@ -348,7 +350,7 @@ impl FATFileSystem {
|
||||
let root_inode: Arc<LockedFATInode> = Arc::new(LockedFATInode(SpinLock::new(FATInode {
|
||||
parent: Weak::default(),
|
||||
self_ref: Weak::default(),
|
||||
children: BTreeMap::new(),
|
||||
children: HashMap::new(),
|
||||
fs: Weak::default(),
|
||||
inode_type: FATDirEntry::UnInit,
|
||||
metadata: Metadata {
|
||||
@ -1559,14 +1561,15 @@ impl IndexNode for LockedFATInode {
|
||||
for ent in dir_iter {
|
||||
ret.push(ent.name());
|
||||
|
||||
// ====== 生成inode缓存,存入B树
|
||||
let name = DName::from(ent.name().to_uppercase());
|
||||
// ====== 生成inode缓存
|
||||
let search_name = to_search_name_string(ent.name());
|
||||
// debug!("name={name}");
|
||||
|
||||
if !guard.children.contains_key(&name)
|
||||
&& name.as_ref() != "."
|
||||
&& name.as_ref() != ".."
|
||||
if !guard.children.contains_key(&search_name)
|
||||
&& search_name != "."
|
||||
&& search_name != ".."
|
||||
{
|
||||
let name = DName::from(ent.name());
|
||||
// 创建新的inode
|
||||
let entry_inode: Arc<LockedFATInode> = LockedFATInode::new(
|
||||
name.clone(),
|
||||
@ -1575,7 +1578,7 @@ impl IndexNode for LockedFATInode {
|
||||
ent,
|
||||
);
|
||||
// 加入缓存区, 由于FAT文件系统的大小写不敏感问题,因此存入缓存区的key应当是全大写的
|
||||
guard.children.insert(name, entry_inode.clone());
|
||||
guard.children.insert(search_name, entry_inode.clone());
|
||||
}
|
||||
}
|
||||
return Ok(ret);
|
||||
@ -1611,7 +1614,7 @@ impl IndexNode for LockedFATInode {
|
||||
// 对目标inode上锁,以防更改
|
||||
let target_guard: SpinLockGuard<FATInode> = target.0.lock();
|
||||
// 先从缓存删除
|
||||
let nod = guard.children.remove(&DName::from(name.to_uppercase()));
|
||||
let nod = guard.children.remove(&to_search_name(name));
|
||||
|
||||
// 若删除缓存中为管道的文件,则不需要再到磁盘删除
|
||||
if nod.is_some() {
|
||||
@ -1646,7 +1649,7 @@ impl IndexNode for LockedFATInode {
|
||||
// 对目标inode上锁,以防更改
|
||||
let target_guard: SpinLockGuard<FATInode> = target.0.lock();
|
||||
// 先从缓存删除
|
||||
guard.children.remove(&DName::from(name.to_uppercase()));
|
||||
guard.children.remove(&to_search_name(name));
|
||||
|
||||
let dir = match &guard.inode_type {
|
||||
FATDirEntry::File(_) | FATDirEntry::VolId(_) => {
|
||||
@ -1669,9 +1672,7 @@ impl IndexNode for LockedFATInode {
|
||||
Err(r) => {
|
||||
if r == SystemError::ENOTEMPTY {
|
||||
// 如果要删除的是目录,且不为空,则删除动作未发生,重新加入缓存
|
||||
guard
|
||||
.children
|
||||
.insert(DName::from(name.to_uppercase()), target.clone());
|
||||
guard.children.insert(to_search_name(name), target.clone());
|
||||
drop(target_guard);
|
||||
}
|
||||
return Err(r);
|
||||
@ -1695,7 +1696,6 @@ impl IndexNode for LockedFATInode {
|
||||
let old_inode_guard: SpinLockGuard<FATInode> = old_inode.0.lock();
|
||||
let fs = old_inode_guard.fs.upgrade().unwrap();
|
||||
// 从缓存删除
|
||||
let _nod = guard.children.remove(&DName::from(old_name.to_uppercase()));
|
||||
let old_dir = match &guard.inode_type {
|
||||
FATDirEntry::File(_) | FATDirEntry::VolId(_) => {
|
||||
return Err(SystemError::ENOTDIR);
|
||||
@ -1710,6 +1710,7 @@ impl IndexNode for LockedFATInode {
|
||||
// old_dir.check_existence(old_name, Some(false), guard.fs.upgrade().unwrap())?;
|
||||
|
||||
old_dir.rename(fs, old_name, new_name)?;
|
||||
let _nod = guard.children.remove(&to_search_name(old_name));
|
||||
} else {
|
||||
let mut old_guard = self.0.lock();
|
||||
let other: &LockedFATInode = target
|
||||
@ -1721,10 +1722,7 @@ impl IndexNode for LockedFATInode {
|
||||
// 对目标inode上锁,以防更改
|
||||
let old_inode_guard: SpinLockGuard<FATInode> = old_inode.0.lock();
|
||||
let fs = old_inode_guard.fs.upgrade().unwrap();
|
||||
// 从缓存删除
|
||||
let _nod = old_guard
|
||||
.children
|
||||
.remove(&DName::from(old_name.to_uppercase()));
|
||||
|
||||
let old_dir = match &old_guard.inode_type {
|
||||
FATDirEntry::File(_) | FATDirEntry::VolId(_) => {
|
||||
return Err(SystemError::ENOTDIR);
|
||||
@ -1748,6 +1746,8 @@ impl IndexNode for LockedFATInode {
|
||||
// 检查文件是否存在
|
||||
old_dir.check_existence(old_name, Some(false), old_guard.fs.upgrade().unwrap())?;
|
||||
old_dir.rename_across(fs, new_dir, old_name, new_name)?;
|
||||
// 从缓存删除
|
||||
let _nod = old_guard.children.remove(&to_search_name(old_name));
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
@ -1806,9 +1806,9 @@ impl IndexNode for LockedFATInode {
|
||||
return self.create(filename, FileType::File, mode);
|
||||
}
|
||||
|
||||
let filename = DName::from(filename.to_uppercase());
|
||||
let dname = DName::from(filename);
|
||||
let nod = LockedFATInode::new(
|
||||
filename.clone(),
|
||||
dname,
|
||||
inode.fs.upgrade().unwrap(),
|
||||
inode.self_ref.clone(),
|
||||
FATDirEntry::File(FATFile::default()),
|
||||
@ -1830,7 +1830,7 @@ impl IndexNode for LockedFATInode {
|
||||
return Err(SystemError::EINVAL);
|
||||
}
|
||||
|
||||
inode.children.insert(filename, nod.clone());
|
||||
inode.children.insert(to_search_name(filename), nod.clone());
|
||||
Ok(nod)
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
use alloc::string::String;
|
||||
use core::char::REPLACEMENT_CHARACTER;
|
||||
|
||||
/// FAT文件系统保留开头的2个簇
|
||||
@ -5,7 +6,7 @@ pub const RESERVED_CLUSTERS: u32 = 2;
|
||||
|
||||
/// @brief 将u8转为ascii字符。
|
||||
/// 当转码成功时,返回对应的ascii字符,否则返回Unicode占位符
|
||||
pub fn decode_u8_ascii(value: u8) -> char {
|
||||
pub(super) fn decode_u8_ascii(value: u8) -> char {
|
||||
if value <= 0x7f {
|
||||
return value as char;
|
||||
} else {
|
||||
@ -13,3 +14,16 @@ pub fn decode_u8_ascii(value: u8) -> char {
|
||||
return REPLACEMENT_CHARACTER;
|
||||
}
|
||||
}
|
||||
|
||||
/// 把名称转为inode缓存里面的key
|
||||
#[inline(always)]
|
||||
pub(super) fn to_search_name(name: &str) -> String {
|
||||
name.to_ascii_uppercase()
|
||||
}
|
||||
|
||||
/// 把名称转为inode缓存里面的key(输入为string,原地替换)
|
||||
#[inline(always)]
|
||||
pub(super) fn to_search_name_string(mut name: String) -> String {
|
||||
name.make_ascii_uppercase();
|
||||
name
|
||||
}
|
||||
|
@ -12,9 +12,13 @@ use crate::{
|
||||
procfs::procfs_init,
|
||||
ramfs::RamFS,
|
||||
sysfs::sysfs_init,
|
||||
vfs::{mount::MountFS, syscall::ModeType, AtomicInodeId, FileSystem, FileType},
|
||||
vfs::{
|
||||
mount::MountFS, syscall::ModeType, AtomicInodeId, FileSystem, FileType, MAX_PATHLEN,
|
||||
},
|
||||
},
|
||||
libs::spinlock::SpinLock,
|
||||
process::ProcessManager,
|
||||
syscall::user_access::check_and_clone_cstr,
|
||||
};
|
||||
|
||||
use super::{
|
||||
@ -23,7 +27,7 @@ use super::{
|
||||
mount::{init_mountlist, MOUNT_LIST},
|
||||
syscall::UmountFlag,
|
||||
utils::{rsplit_path, user_path_at},
|
||||
IndexNode, InodeId, VFS_MAX_FOLLOW_SYMLINK_TIMES,
|
||||
FilePrivateData, IndexNode, InodeId, VFS_MAX_FOLLOW_SYMLINK_TIMES,
|
||||
};
|
||||
|
||||
/// 当没有指定根文件系统时,尝试的根文件系统列表
|
||||
@ -248,6 +252,48 @@ pub fn do_unlink_at(dirfd: i32, path: &str) -> Result<u64, SystemError> {
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
pub fn do_symlinkat(from: *const u8, newdfd: i32, to: *const u8) -> Result<usize, SystemError> {
|
||||
let oldname = check_and_clone_cstr(from, Some(MAX_PATHLEN))?
|
||||
.into_string()
|
||||
.map_err(|_| SystemError::EINVAL)?;
|
||||
let newname = check_and_clone_cstr(to, Some(MAX_PATHLEN))?
|
||||
.into_string()
|
||||
.map_err(|_| SystemError::EINVAL)?;
|
||||
let from = oldname.as_str().trim();
|
||||
let to = newname.as_str().trim();
|
||||
|
||||
// TODO: 添加权限检查,确保进程拥有目标路径的权限
|
||||
|
||||
let pcb = ProcessManager::current_pcb();
|
||||
let (old_begin_inode, old_remain_path) = user_path_at(&pcb, AtFlags::AT_FDCWD.bits(), from)?;
|
||||
// info!("old_begin_inode={:?}", old_begin_inode.metadata());
|
||||
let _ =
|
||||
old_begin_inode.lookup_follow_symlink(&old_remain_path, VFS_MAX_FOLLOW_SYMLINK_TIMES)?;
|
||||
|
||||
// 得到新创建节点的父节点
|
||||
let (new_begin_inode, new_remain_path) = user_path_at(&pcb, newdfd, to)?;
|
||||
let (new_name, new_parent_path) = rsplit_path(&new_remain_path);
|
||||
let new_parent = new_begin_inode
|
||||
.lookup_follow_symlink(new_parent_path.unwrap_or("/"), VFS_MAX_FOLLOW_SYMLINK_TIMES)?;
|
||||
// info!("new_parent={:?}", new_parent.metadata());
|
||||
|
||||
if new_parent.metadata()?.file_type != FileType::Dir {
|
||||
return Err(SystemError::ENOTDIR);
|
||||
}
|
||||
|
||||
let new_inode = new_parent.create_with_data(
|
||||
new_name,
|
||||
FileType::SymLink,
|
||||
ModeType::from_bits_truncate(0o777),
|
||||
0,
|
||||
)?;
|
||||
|
||||
let buf = old_remain_path.as_bytes();
|
||||
let len = buf.len();
|
||||
new_inode.write_at(0, len, buf, SpinLock::new(FilePrivateData::Unused).lock())?;
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
/// # do_mount - 挂载文件系统
|
||||
///
|
||||
/// 将给定的文件系统挂载到指定的挂载点。
|
||||
|
@ -8,6 +8,7 @@ use alloc::{
|
||||
collections::BTreeMap,
|
||||
string::{String, ToString},
|
||||
sync::{Arc, Weak},
|
||||
vec::Vec,
|
||||
};
|
||||
use system_error::SystemError;
|
||||
|
||||
@ -215,12 +216,29 @@ impl MountFSInode {
|
||||
.ok_or(SystemError::ENOENT);
|
||||
}
|
||||
|
||||
fn do_absolute_path(&self, len: usize) -> Result<String, SystemError> {
|
||||
if self.metadata()?.inode_id == ROOT_INODE().metadata()?.inode_id {
|
||||
return Ok(String::with_capacity(len));
|
||||
fn do_absolute_path(&self) -> Result<String, SystemError> {
|
||||
let mut path_parts = Vec::new();
|
||||
let mut current = self.self_ref.upgrade().unwrap();
|
||||
|
||||
while current.metadata()?.inode_id != ROOT_INODE().metadata()?.inode_id {
|
||||
let name = current.dname()?;
|
||||
path_parts.push(name.0);
|
||||
current = current.do_parent()?;
|
||||
}
|
||||
let name = self.dname()?;
|
||||
return Ok(self.do_parent()?.do_absolute_path(len + name.0.len() + 1)? + "/" + &name.0);
|
||||
|
||||
// 由于我们从叶子节点向上遍历到根节点,所以需要反转路径部分
|
||||
path_parts.reverse();
|
||||
|
||||
// 构建最终的绝对路径字符串
|
||||
let mut absolute_path = String::with_capacity(
|
||||
path_parts.iter().map(|s| s.len()).sum::<usize>() + path_parts.len(),
|
||||
);
|
||||
for part in path_parts {
|
||||
absolute_path.push('/');
|
||||
absolute_path.push_str(&part);
|
||||
}
|
||||
|
||||
Ok(absolute_path)
|
||||
}
|
||||
}
|
||||
|
||||
@ -469,7 +487,7 @@ impl IndexNode for MountFSInode {
|
||||
}
|
||||
|
||||
fn absolute_path(&self) -> Result<String, SystemError> {
|
||||
self.do_absolute_path(0)
|
||||
self.do_absolute_path()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -20,6 +20,7 @@ use crate::{
|
||||
time::{syscall::PosixTimeval, PosixTimeSpec},
|
||||
};
|
||||
|
||||
use super::core::do_symlinkat;
|
||||
use super::{
|
||||
core::{do_mkdir_at, do_remove_dir, do_unlink_at},
|
||||
fcntl::{AtFlags, FcntlCommand, FD_CLOEXEC},
|
||||
@ -980,6 +981,18 @@ impl Syscall {
|
||||
return do_unlink_at(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize);
|
||||
}
|
||||
|
||||
pub fn symlink(oldname: *const u8, newname: *const u8) -> Result<usize, SystemError> {
|
||||
return do_symlinkat(oldname, AtFlags::AT_FDCWD.bits(), newname);
|
||||
}
|
||||
|
||||
pub fn symlinkat(
|
||||
oldname: *const u8,
|
||||
newdfd: i32,
|
||||
newname: *const u8,
|
||||
) -> Result<usize, SystemError> {
|
||||
return do_symlinkat(oldname, newdfd, newname);
|
||||
}
|
||||
|
||||
/// # 修改文件名
|
||||
///
|
||||
///
|
||||
|
@ -346,6 +346,20 @@ impl Syscall {
|
||||
Self::unlinkat(dirfd, path, flags)
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
SYS_SYMLINK => {
|
||||
let oldname = args[0] as *const u8;
|
||||
let newname = args[1] as *const u8;
|
||||
Self::symlink(oldname, newname)
|
||||
}
|
||||
|
||||
SYS_SYMLINKAT => {
|
||||
let oldname = args[0] as *const u8;
|
||||
let newdfd = args[1] as i32;
|
||||
let newname = args[2] as *const u8;
|
||||
Self::symlinkat(oldname, newdfd, newname)
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
SYS_RMDIR => {
|
||||
let path = args[0] as *const u8;
|
||||
|
Reference in New Issue
Block a user