From 547baa9ecd20510efd1331f248a0faf45646f05b Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Sat, 23 Mar 2019 15:47:44 +0800 Subject: [PATCH] Implement symlink following --- kernel/Cargo.lock | 4 ++-- kernel/src/fs/mod.rs | 2 ++ kernel/src/process/structs.rs | 6 ++++-- kernel/src/syscall/fs.rs | 4 ++-- kernel/src/syscall/proc.rs | 4 ++-- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/kernel/Cargo.lock b/kernel/Cargo.lock index 02a7ef8..5fa45bd 100644 --- a/kernel/Cargo.lock +++ b/kernel/Cargo.lock @@ -338,12 +338,12 @@ dependencies = [ [[package]] name = "rcore-fs" version = "0.1.0" -source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#47df4e1cc97f061fa6eadf66eaacf774d22bb806" +source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#c9322710b4ba67c086f500befb8208c6ae353493" [[package]] name = "rcore-fs-sfs" version = "0.1.0" -source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#47df4e1cc97f061fa6eadf66eaacf774d22bb806" +source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#c9322710b4ba67c086f500befb8208c6ae353493" dependencies = [ "bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/kernel/src/fs/mod.rs b/kernel/src/fs/mod.rs index 342871f..0fad557 100644 --- a/kernel/src/fs/mod.rs +++ b/kernel/src/fs/mod.rs @@ -56,6 +56,8 @@ lazy_static! { }; } +pub const FOLLOW_MAX_DEPTH: usize = 1; + pub trait INodeExt { fn read_as_vec(&self) -> Result>; } diff --git a/kernel/src/process/structs.rs b/kernel/src/process/structs.rs index c3f06aa..9998bdd 100644 --- a/kernel/src/process/structs.rs +++ b/kernel/src/process/structs.rs @@ -6,11 +6,12 @@ use spin::{Mutex, RwLock}; use xmas_elf::{ElfFile, header, program::{Flags, Type}}; use rcore_memory::PAGE_SIZE; use rcore_thread::Tid; +use rcore_fs::vfs::FileType; use core::str; use crate::arch::interrupt::{Context, TrapFrame}; use crate::memory::{ByFrame, GlobalFrameAlloc, KernelStack, MemoryAttr, MemorySet}; -use crate::fs::{FileHandle, OpenOptions, INodeExt}; +use crate::fs::{FileHandle, OpenOptions, INodeExt, FOLLOW_MAX_DEPTH}; use crate::sync::Condvar; use crate::net::{SocketWrapper, SOCKETS}; @@ -204,7 +205,8 @@ impl Thread { let size = interp.file_size() as usize - 1; // skip last '\0' if offset + size < data.len() { if let Ok(loader_path) = str::from_utf8(&data[offset..(offset+size)]) { - if let Ok(inode) = crate::fs::ROOT_INODE.lookup(&loader_path[1..]) { + // assuming absolute path + if let Ok(inode) = crate::fs::ROOT_INODE.lookup_follow(&loader_path[1..], FOLLOW_MAX_DEPTH) { if let Ok(buf) = inode.read_as_vec() { debug!("using loader {}", &loader_path); // Elf loader should not have INTERP diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index f2863cd..055dd1d 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -683,12 +683,12 @@ impl Process { if path.len() > 0 && path.as_bytes()[0] == b'/' { // absolute path let abs_path = path.split_at(1).1; // skip start '/' - let inode = ROOT_INODE.lookup(abs_path)?; + let inode = ROOT_INODE.lookup_follow(abs_path, FOLLOW_MAX_DEPTH)?; Ok(inode) } else { // relative path let cwd = self.cwd.split_at(1).1; // skip start '/' - let inode = ROOT_INODE.lookup(cwd)?.lookup(path)?; + let inode = ROOT_INODE.lookup(cwd)?.lookup_follow(path, FOLLOW_MAX_DEPTH)?; Ok(inode) } } diff --git a/kernel/src/syscall/proc.rs b/kernel/src/syscall/proc.rs index 01cf63f..ed0ccfc 100644 --- a/kernel/src/syscall/proc.rs +++ b/kernel/src/syscall/proc.rs @@ -1,7 +1,7 @@ //! Syscalls for process use super::*; -use crate::fs::INodeExt; +use crate::fs::{INodeExt, FOLLOW_MAX_DEPTH}; /// Fork the current process. Return the child's PID. pub fn sys_fork(tf: &TrapFrame) -> SysResult { @@ -120,7 +120,7 @@ pub fn sys_exec(name: *const u8, argv: *const *const u8, envp: *const *const u8, // Read program file let path = args[0].as_str(); - let inode = crate::fs::ROOT_INODE.lookup(path)?; + let inode = crate::fs::ROOT_INODE.lookup_follow(path, FOLLOW_MAX_DEPTH)?; let buf = inode.read_as_vec()?; // Make new Thread