Skip to content

Commit e276b6e

Browse files
misc(absolute_path): rename and return a PathBuf
* Rename `absolute_path_str` -> `absolute_path` * Make it return a `PathBuf` Signed-off-by: Anhad Singh <[email protected]>
1 parent ec720d2 commit e276b6e

File tree

8 files changed

+56
-37
lines changed

8 files changed

+56
-37
lines changed

src/aero_kernel/src/fs/cache.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use spin::Once;
3838
use crate::fs::inode::{DirEntry, INodeInterface};
3939
use crate::utils::sync::BMutex;
4040

41+
use super::path::PathBuf;
4142
use super::FileSystem;
4243

4344
pub static INODE_CACHE: Once<Arc<INodeCache>> = Once::new();
@@ -317,7 +318,7 @@ pub type DirCacheItem = CacheArc<CacheItem<DirCacheKey, DirEntry>>;
317318
impl Debug for DirCacheItem {
318319
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
319320
f.debug_tuple("DirCacheItem")
320-
.field(&self.absolute_path_str())
321+
.field(&self.absolute_path())
321322
.finish()
322323
}
323324
}
@@ -367,14 +368,14 @@ impl Cacheable<DirCacheKey> for DirEntry {
367368
}
368369

369370
pub trait DirCacheImpl {
370-
fn absolute_path_str(&self) -> String;
371+
fn absolute_path(&self) -> PathBuf;
371372
}
372373

373374
impl DirCacheImpl for DirCacheItem {
374-
fn absolute_path_str(&self) -> String {
375+
fn absolute_path(&self) -> PathBuf {
375376
let mut current_entry = Some(self.clone());
376377
let mut path_nodes = Vec::new();
377-
let mut result = String::new();
378+
let mut result = PathBuf::new();
378379

379380
// We need to collect all of the path nodes, reverse them and then join them
380381
// with the path separator.
@@ -384,12 +385,13 @@ impl DirCacheImpl for DirCacheItem {
384385
}
385386

386387
for node in path_nodes.iter().rev() {
387-
result.push_str(node);
388+
result.push(node.as_str());
389+
// result.push_str(node);
388390

389-
// If we are not at the root node, we need to add the path separator.
390-
if node != "/" {
391-
result.push('/');
392-
}
391+
// // If we are not at the root node, we need to add the path separator.
392+
// if node != "/" {
393+
// result.push('/');
394+
// }
393395
}
394396

395397
result

src/aero_kernel/src/fs/file_table.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,7 @@ impl FileHandle {
6868
let offset = self.offset.load(Ordering::SeqCst);
6969
let new_offset = self.inode.inode().read_at(offset, buffer)?;
7070

71-
if self.inode.absolute_path_str().contains("/tmp/ccAAAAAA.s") {
72-
log::debug!(
73-
"read {} bytes from {} ----- hi mom ----- {:?}",
74-
new_offset,
75-
self.inode.absolute_path_str(),
76-
buffer
77-
);
78-
}
79-
8071
self.offset.fetch_add(new_offset, Ordering::SeqCst);
81-
8272
Ok(new_offset)
8373
}
8474

@@ -87,7 +77,6 @@ impl FileHandle {
8777
let new_offset = self.inode.inode().write_at(offset, buffer)?;
8878

8979
self.offset.fetch_add(new_offset, Ordering::SeqCst);
90-
9180
Ok(new_offset)
9281
}
9382

@@ -223,7 +212,7 @@ impl FileTable {
223212
log::debug!(
224213
"file handle: (fd={}, path=`{}`)",
225214
handle.fd,
226-
handle.inode.absolute_path_str()
215+
handle.inode.absolute_path()
227216
)
228217
}
229218
}

src/aero_kernel/src/fs/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ pub fn lookup_path_with(
223223
) {
224224
Ok(x) => x,
225225
Err(e) => {
226-
dbg!(component, cwd.absolute_path_str());
226+
dbg!(component, cwd.absolute_path());
227227
return Err(dbg!(e));
228228
}
229229
};

src/aero_kernel/src/fs/path.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ impl Path {
6666
pub fn as_bytes(&self) -> &[u8] {
6767
self.0.as_bytes()
6868
}
69+
70+
/// Returns the byte length of the path.
71+
pub fn len(&self) -> usize {
72+
self.0.len()
73+
}
6974
}
7075

7176
impl Borrow<Path> for PathBuf {
@@ -91,11 +96,24 @@ impl AsRef<Path> for Path {
9196
}
9297
}
9398

99+
impl AsRef<Path> for &str {
100+
#[inline]
101+
fn as_ref(&self) -> &Path {
102+
Path::new(self)
103+
}
104+
}
105+
94106
/// An owned, mutable path (akin to [`String`]).
95107
#[derive(Debug, Clone, PartialEq, Eq)]
96108
pub struct PathBuf(String);
97109

98110
impl PathBuf {
111+
/// Allocates an empty `PathBuf`.
112+
#[inline]
113+
pub fn new() -> Self {
114+
Self(String::new())
115+
}
116+
99117
#[inline]
100118
fn as_mut_vec(&mut self) -> &mut Vec<u8> {
101119
// TODO: safety?
@@ -154,6 +172,13 @@ impl AsRef<Path> for PathBuf {
154172
}
155173
}
156174

175+
impl Into<String> for PathBuf {
176+
#[inline]
177+
fn into(self) -> String {
178+
self.0
179+
}
180+
}
181+
157182
impl Display for PathBuf {
158183
#[inline]
159184
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {

src/aero_kernel/src/syscall/fs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl FileDescriptor {
5555
impl fmt::Display for FileDescriptor {
5656
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5757
if let Ok(file_handle) = self.handle() {
58-
let path = file_handle.inode.absolute_path_str();
58+
let path = file_handle.inode.absolute_path();
5959
write!(f, "{{ {} -> {} }}", self.0, path)
6060
} else {
6161
// invalid file descriptor
@@ -470,7 +470,7 @@ pub fn fstat(fd: usize, path: &Path, flags: usize, stat: &mut Stat) -> Result<us
470470
return Ok(0);
471471
}
472472

473-
log::debug!("{}", at.absolute_path_str());
473+
log::debug!("{}", at.absolute_path());
474474

475475
let ent = fs::lookup_path_with(at, path, LookupMode::None, true)?;
476476
*stat = ent.inode().stat()?;
@@ -502,7 +502,7 @@ pub fn read_link(path: &Path, buffer: &mut [u8]) -> Result<usize, SyscallError>
502502
let resolved_path = if resolved_path.is_absolute() {
503503
resolved_path
504504
} else {
505-
Path::new(&cwd.absolute_path_str()).join(resolved_path)
505+
cwd.absolute_path().join(resolved_path)
506506
};
507507

508508
let size = core::cmp::min(resolved_path.as_str().len(), buffer.len());

src/aero_kernel/src/userland/scheduler/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,14 @@ impl Scheduler {
131131

132132
pub fn log_ptable(&self) {
133133
self.tasks.0.lock().iter().for_each(|(pid, task)| {
134+
let path: String = task
135+
.path()
136+
.map(|path| path.into())
137+
.unwrap_or("<unknown>".into());
138+
134139
log::info!(
135140
"task(pid={pid:?}, path={:?}, state={:?})",
136-
task.path().unwrap_or(String::from("<unknown>")),
141+
path,
137142
task.state()
138143
)
139144
});

src/aero_kernel/src/userland/task/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use core::ops::Range;
2828
use core::sync::atomic::{AtomicBool, AtomicU8, AtomicUsize, Ordering};
2929

3030
use crate::fs::cache::{DirCacheImpl, DirCacheItem};
31+
use crate::fs::path::PathBuf;
3132
use crate::fs::{self, FileSystem};
3233
use crate::mem::paging::*;
3334

@@ -510,11 +511,8 @@ impl Task {
510511
}
511512
}
512513

513-
pub fn path(&self) -> Option<String> {
514-
self.executable
515-
.lock()
516-
.as_ref()
517-
.map(|e| e.absolute_path_str())
514+
pub fn path(&self) -> Option<PathBuf> {
515+
self.executable.lock().as_ref().map(|e| e.absolute_path())
518516
}
519517

520518
pub fn exec(
@@ -648,8 +646,8 @@ impl Task {
648646
self.cwd.read().as_ref().unwrap().inode.clone()
649647
}
650648

651-
pub fn get_cwd(&self) -> String {
652-
self.cwd.read().as_ref().unwrap().inode.absolute_path_str()
649+
pub fn get_cwd(&self) -> PathBuf {
650+
self.cwd.read().as_ref().unwrap().inode.absolute_path()
653651
}
654652

655653
pub fn set_cwd(&self, cwd: DirCacheItem) {

src/aero_kernel/src/userland/vm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ impl VmProtected {
872872
protection,
873873
flags,
874874
offset,
875-
z.map(|f| f.absolute_path_str())
875+
z.map(|f| f.absolute_path())
876876
);
877877

878878
crate::unwind::unwind_stack_trace();
@@ -915,19 +915,19 @@ impl VmProtected {
915915
if let Some(shebang) = parse_shebang(bin.clone())? {
916916
log::debug!(
917917
"shebang: (interpreter={}, argument={})",
918-
shebang.interpreter.absolute_path_str(),
918+
shebang.interpreter.absolute_path(),
919919
shebang.argument
920920
);
921921

922922
let mut largv = ExecArgs::default();
923923

924-
largv.push(shebang.interpreter.absolute_path_str().as_bytes());
924+
largv.push(shebang.interpreter.absolute_path().as_bytes());
925925

926926
if !shebang.argument.is_empty() {
927927
largv.push(shebang.argument.as_bytes());
928928
}
929929

930-
largv.push(bin.absolute_path_str().as_bytes());
930+
largv.push(bin.absolute_path().as_bytes());
931931

932932
if let Some(argv) = argv {
933933
largv.extend(&argv.inner[1..])

0 commit comments

Comments
 (0)