Skip to content

Commit f848505

Browse files
Rollup merge of rust-lang#124304 - hermit-os:fuse, r=joboet
revise the interpretation of ReadDir for HermitOS HermitOS supports getdents64. As under Linux, the dirent64 entry `d_off` is not longer used, because its definition is not clear. Instead of `d_off` the entry `d_reclen` is used to determine the end of the dirent64 entry. In addition, take up `@workingjubilee` suggestion from the discussions in rust-lang#115984 to increase the readability. Hermit is a tier 3 platform and this PR changes only files, wich are related to the tier 3 platform.
2 parents 0f923a4 + 5aa779f commit f848505

File tree

10 files changed

+87
-90
lines changed

10 files changed

+87
-90
lines changed

library/std/src/os/hermit/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#[allow(unused_extern_crates)]
44
#[stable(feature = "rust1", since = "1.0.0")]
5-
pub extern crate hermit_abi as abi;
5+
pub extern crate hermit_abi;
66

77
pub mod ffi;
88
pub mod io;
+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
use super::abi;
1+
use super::hermit_abi;
22
use crate::alloc::{GlobalAlloc, Layout, System};
33
use crate::ptr;
44

55
#[stable(feature = "alloc_system_type", since = "1.28.0")]
66
unsafe impl GlobalAlloc for System {
77
#[inline]
88
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
9-
abi::malloc(layout.size(), layout.align())
9+
hermit_abi::malloc(layout.size(), layout.align())
1010
}
1111

1212
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
13-
let addr = abi::malloc(layout.size(), layout.align());
13+
let addr = hermit_abi::malloc(layout.size(), layout.align());
1414

1515
if !addr.is_null() {
1616
ptr::write_bytes(addr, 0x00, layout.size());
@@ -21,11 +21,11 @@ unsafe impl GlobalAlloc for System {
2121

2222
#[inline]
2323
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
24-
abi::free(ptr, layout.size(), layout.align())
24+
hermit_abi::free(ptr, layout.size(), layout.align())
2525
}
2626

2727
#[inline]
2828
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
29-
abi::realloc(ptr, layout.size(), layout.align(), new_size)
29+
hermit_abi::realloc(ptr, layout.size(), layout.align(), new_size)
3030
}
3131
}

library/std/src/sys/pal/hermit/fd.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![unstable(reason = "not public", issue = "none", feature = "fd")]
22

3-
use super::abi;
3+
use super::hermit_abi;
44
use crate::io::{self, Read};
55
use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd};
66
use crate::sys::cvt;
@@ -16,7 +16,8 @@ pub struct FileDesc {
1616

1717
impl FileDesc {
1818
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
19-
let result = cvt(unsafe { abi::read(self.fd.as_raw_fd(), buf.as_mut_ptr(), buf.len()) })?;
19+
let result =
20+
cvt(unsafe { hermit_abi::read(self.fd.as_raw_fd(), buf.as_mut_ptr(), buf.len()) })?;
2021
Ok(result as usize)
2122
}
2223

@@ -26,7 +27,8 @@ impl FileDesc {
2627
}
2728

2829
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
29-
let result = cvt(unsafe { abi::write(self.fd.as_raw_fd(), buf.as_ptr(), buf.len()) })?;
30+
let result =
31+
cvt(unsafe { hermit_abi::write(self.fd.as_raw_fd(), buf.as_ptr(), buf.len()) })?;
3032
Ok(result as usize)
3133
}
3234

@@ -49,8 +51,8 @@ impl FileDesc {
4951
unsupported()
5052
}
5153

52-
pub fn fstat(&self, stat: *mut abi::stat) -> io::Result<()> {
53-
cvt(unsafe { abi::fstat(self.fd.as_raw_fd(), stat) })?;
54+
pub fn fstat(&self, stat: *mut hermit_abi::stat) -> io::Result<()> {
55+
cvt(unsafe { hermit_abi::fstat(self.fd.as_raw_fd(), stat) })?;
5456
Ok(())
5557
}
5658
}

library/std/src/sys/pal/hermit/fs.rs

+23-28
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use super::abi::{
1+
use super::fd::FileDesc;
2+
use super::hermit_abi::{
23
self, dirent64, stat as stat_struct, DT_DIR, DT_LNK, DT_REG, DT_UNKNOWN, O_APPEND, O_CREAT,
34
O_EXCL, O_RDONLY, O_RDWR, O_TRUNC, O_WRONLY, S_IFDIR, S_IFLNK, S_IFMT, S_IFREG,
45
};
5-
use super::fd::FileDesc;
66
use crate::ffi::{CStr, OsStr, OsString};
77
use crate::fmt;
88
use crate::io::{self, Error, ErrorKind};
@@ -47,7 +47,7 @@ impl InnerReadDir {
4747

4848
pub struct ReadDir {
4949
inner: Arc<InnerReadDir>,
50-
pos: i64,
50+
pos: usize,
5151
}
5252

5353
impl ReadDir {
@@ -197,38 +197,31 @@ impl Iterator for ReadDir {
197197

198198
fn next(&mut self) -> Option<io::Result<DirEntry>> {
199199
let mut counter: usize = 0;
200-
let mut offset: i64 = 0;
200+
let mut offset: usize = 0;
201201

202202
// loop over all directory entries and search the entry for the current position
203203
loop {
204204
// leave function, if the loop reaches the of the buffer (with all entries)
205-
if offset >= self.inner.dir.len().try_into().unwrap() {
205+
if offset >= self.inner.dir.len() {
206206
return None;
207207
}
208208

209-
let dir = unsafe {
210-
&*(self.inner.dir.as_ptr().offset(offset.try_into().unwrap()) as *const dirent64)
211-
};
209+
let dir = unsafe { &*(self.inner.dir.as_ptr().add(offset) as *const dirent64) };
212210

213-
if counter == self.pos.try_into().unwrap() {
211+
if counter == self.pos {
214212
self.pos += 1;
215213

216214
// After dirent64, the file name is stored. d_reclen represents the length of the dirent64
217215
// plus the length of the file name. Consequently, file name has a size of d_reclen minus
218216
// the size of dirent64. The file name is always a C string and terminated by `\0`.
219217
// Consequently, we are able to ignore the last byte.
220-
let name_bytes = unsafe {
221-
core::slice::from_raw_parts(
222-
&dir.d_name as *const _ as *const u8,
223-
dir.d_reclen as usize - core::mem::size_of::<dirent64>() - 1,
224-
)
225-
.to_vec()
226-
};
218+
let name_bytes =
219+
unsafe { CStr::from_ptr(&dir.d_name as *const _ as *const i8).to_bytes() };
227220
let entry = DirEntry {
228221
root: self.inner.root.clone(),
229222
ino: dir.d_ino,
230223
type_: dir.d_type as u32,
231-
name: OsString::from_vec(name_bytes),
224+
name: OsString::from_vec(name_bytes.to_vec()),
232225
};
233226

234227
return Some(Ok(entry));
@@ -237,7 +230,7 @@ impl Iterator for ReadDir {
237230
counter += 1;
238231

239232
// move to the next dirent64, which is directly stored after the previous one
240-
offset = offset + dir.d_off;
233+
offset = offset + usize::from(dir.d_reclen);
241234
}
242235
}
243236
}
@@ -365,7 +358,7 @@ impl File {
365358
mode = 0;
366359
}
367360

368-
let fd = unsafe { cvt(abi::open(path.as_ptr(), flags, mode))? };
361+
let fd = unsafe { cvt(hermit_abi::open(path.as_ptr(), flags, mode))? };
369362
Ok(File(unsafe { FileDesc::from_raw_fd(fd as i32) }))
370363
}
371364

@@ -446,7 +439,7 @@ impl DirBuilder {
446439

447440
pub fn mkdir(&self, path: &Path) -> io::Result<()> {
448441
run_path_with_cstr(path, &|path| {
449-
cvt(unsafe { abi::mkdir(path.as_ptr(), self.mode) }).map(|_| ())
442+
cvt(unsafe { hermit_abi::mkdir(path.as_ptr(), self.mode) }).map(|_| ())
450443
})
451444
}
452445

@@ -508,7 +501,8 @@ impl FromRawFd for File {
508501
}
509502

510503
pub fn readdir(path: &Path) -> io::Result<ReadDir> {
511-
let fd_raw = run_path_with_cstr(path, &|path| cvt(unsafe { abi::opendir(path.as_ptr()) }))?;
504+
let fd_raw =
505+
run_path_with_cstr(path, &|path| cvt(unsafe { hermit_abi::opendir(path.as_ptr()) }))?;
512506
let fd = unsafe { FileDesc::from_raw_fd(fd_raw as i32) };
513507
let root = path.to_path_buf();
514508

@@ -519,8 +513,9 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
519513
// reserve memory to receive all directory entries
520514
vec.resize(sz, 0);
521515

522-
let readlen =
523-
unsafe { abi::getdents64(fd.as_raw_fd(), vec.as_mut_ptr() as *mut dirent64, sz) };
516+
let readlen = unsafe {
517+
hermit_abi::getdents64(fd.as_raw_fd(), vec.as_mut_ptr() as *mut dirent64, sz)
518+
};
524519
if readlen > 0 {
525520
// shrink down to the minimal size
526521
vec.resize(readlen.try_into().unwrap(), 0);
@@ -529,7 +524,7 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
529524

530525
// if the buffer is too small, getdents64 returns EINVAL
531526
// otherwise, getdents64 returns an error number
532-
if readlen != (-abi::errno::EINVAL).into() {
527+
if readlen != (-hermit_abi::errno::EINVAL).into() {
533528
return Err(Error::from_raw_os_error(readlen.try_into().unwrap()));
534529
}
535530

@@ -547,7 +542,7 @@ pub fn readdir(path: &Path) -> io::Result<ReadDir> {
547542
}
548543

549544
pub fn unlink(path: &Path) -> io::Result<()> {
550-
run_path_with_cstr(path, &|path| cvt(unsafe { abi::unlink(path.as_ptr()) }).map(|_| ()))
545+
run_path_with_cstr(path, &|path| cvt(unsafe { hermit_abi::unlink(path.as_ptr()) }).map(|_| ()))
551546
}
552547

553548
pub fn rename(_old: &Path, _new: &Path) -> io::Result<()> {
@@ -559,7 +554,7 @@ pub fn set_perm(_p: &Path, _perm: FilePermissions) -> io::Result<()> {
559554
}
560555

561556
pub fn rmdir(path: &Path) -> io::Result<()> {
562-
run_path_with_cstr(path, &|path| cvt(unsafe { abi::rmdir(path.as_ptr()) }).map(|_| ()))
557+
run_path_with_cstr(path, &|path| cvt(unsafe { hermit_abi::rmdir(path.as_ptr()) }).map(|_| ()))
563558
}
564559

565560
pub fn remove_dir_all(_path: &Path) -> io::Result<()> {
@@ -581,15 +576,15 @@ pub fn link(_original: &Path, _link: &Path) -> io::Result<()> {
581576
pub fn stat(path: &Path) -> io::Result<FileAttr> {
582577
run_path_with_cstr(path, &|path| {
583578
let mut stat_val: stat_struct = unsafe { mem::zeroed() };
584-
cvt(unsafe { abi::stat(path.as_ptr(), &mut stat_val) })?;
579+
cvt(unsafe { hermit_abi::stat(path.as_ptr(), &mut stat_val) })?;
585580
Ok(FileAttr::from_stat(stat_val))
586581
})
587582
}
588583

589584
pub fn lstat(path: &Path) -> io::Result<FileAttr> {
590585
run_path_with_cstr(path, &|path| {
591586
let mut stat_val: stat_struct = unsafe { mem::zeroed() };
592-
cvt(unsafe { abi::lstat(path.as_ptr(), &mut stat_val) })?;
587+
cvt(unsafe { hermit_abi::lstat(path.as_ptr(), &mut stat_val) })?;
593588
Ok(FileAttr::from_stat(stat_val))
594589
})
595590
}
+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::abi;
1+
use super::hermit_abi;
22
use crate::ptr::null;
33
use crate::sync::atomic::AtomicU32;
44
use crate::time::Duration;
@@ -8,32 +8,32 @@ pub fn futex_wait(futex: &AtomicU32, expected: u32, timeout: Option<Duration>) -
88
//
99
// Overflows are rounded up to an infinite timeout (None).
1010
let timespec = timeout.and_then(|dur| {
11-
Some(abi::timespec {
11+
Some(hermit_abi::timespec {
1212
tv_sec: dur.as_secs().try_into().ok()?,
1313
tv_nsec: dur.subsec_nanos().into(),
1414
})
1515
});
1616

1717
let r = unsafe {
18-
abi::futex_wait(
18+
hermit_abi::futex_wait(
1919
futex.as_ptr(),
2020
expected,
21-
timespec.as_ref().map_or(null(), |t| t as *const abi::timespec),
22-
abi::FUTEX_RELATIVE_TIMEOUT,
21+
timespec.as_ref().map_or(null(), |t| t as *const hermit_abi::timespec),
22+
hermit_abi::FUTEX_RELATIVE_TIMEOUT,
2323
)
2424
};
2525

26-
r != -abi::errno::ETIMEDOUT
26+
r != -hermit_abi::errno::ETIMEDOUT
2727
}
2828

2929
#[inline]
3030
pub fn futex_wake(futex: &AtomicU32) -> bool {
31-
unsafe { abi::futex_wake(futex.as_ptr(), 1) > 0 }
31+
unsafe { hermit_abi::futex_wake(futex.as_ptr(), 1) > 0 }
3232
}
3333

3434
#[inline]
3535
pub fn futex_wake_all(futex: &AtomicU32) {
3636
unsafe {
37-
abi::futex_wake(futex.as_ptr(), i32::MAX);
37+
hermit_abi::futex_wake(futex.as_ptr(), i32::MAX);
3838
}
3939
}

library/std/src/sys/pal/hermit/mod.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub mod thread_local_key;
3939
pub mod time;
4040

4141
use crate::io::ErrorKind;
42-
use crate::os::hermit::abi;
42+
use crate::os::hermit::hermit_abi;
4343

4444
pub fn unsupported<T>() -> crate::io::Result<T> {
4545
Err(unsupported_err())
@@ -54,15 +54,15 @@ pub fn unsupported_err() -> crate::io::Error {
5454

5555
pub fn abort_internal() -> ! {
5656
unsafe {
57-
abi::abort();
57+
hermit_abi::abort();
5858
}
5959
}
6060

6161
pub fn hashmap_random_keys() -> (u64, u64) {
6262
let mut buf = [0; 16];
6363
let mut slice = &mut buf[..];
6464
while !slice.is_empty() {
65-
let res = cvt(unsafe { abi::read_entropy(slice.as_mut_ptr(), slice.len(), 0) })
65+
let res = cvt(unsafe { hermit_abi::read_entropy(slice.as_mut_ptr(), slice.len(), 0) })
6666
.expect("failed to generate random hashmap keys");
6767
slice = &mut slice[res as usize..];
6868
}
@@ -109,31 +109,31 @@ pub unsafe extern "C" fn runtime_entry(
109109
let result = main(argc as isize, argv);
110110

111111
run_dtors();
112-
abi::exit(result);
112+
hermit_abi::exit(result);
113113
}
114114

115115
#[inline]
116116
pub(crate) fn is_interrupted(errno: i32) -> bool {
117-
errno == abi::errno::EINTR
117+
errno == hermit_abi::errno::EINTR
118118
}
119119

120120
pub fn decode_error_kind(errno: i32) -> ErrorKind {
121121
match errno {
122-
abi::errno::EACCES => ErrorKind::PermissionDenied,
123-
abi::errno::EADDRINUSE => ErrorKind::AddrInUse,
124-
abi::errno::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
125-
abi::errno::EAGAIN => ErrorKind::WouldBlock,
126-
abi::errno::ECONNABORTED => ErrorKind::ConnectionAborted,
127-
abi::errno::ECONNREFUSED => ErrorKind::ConnectionRefused,
128-
abi::errno::ECONNRESET => ErrorKind::ConnectionReset,
129-
abi::errno::EEXIST => ErrorKind::AlreadyExists,
130-
abi::errno::EINTR => ErrorKind::Interrupted,
131-
abi::errno::EINVAL => ErrorKind::InvalidInput,
132-
abi::errno::ENOENT => ErrorKind::NotFound,
133-
abi::errno::ENOTCONN => ErrorKind::NotConnected,
134-
abi::errno::EPERM => ErrorKind::PermissionDenied,
135-
abi::errno::EPIPE => ErrorKind::BrokenPipe,
136-
abi::errno::ETIMEDOUT => ErrorKind::TimedOut,
122+
hermit_abi::errno::EACCES => ErrorKind::PermissionDenied,
123+
hermit_abi::errno::EADDRINUSE => ErrorKind::AddrInUse,
124+
hermit_abi::errno::EADDRNOTAVAIL => ErrorKind::AddrNotAvailable,
125+
hermit_abi::errno::EAGAIN => ErrorKind::WouldBlock,
126+
hermit_abi::errno::ECONNABORTED => ErrorKind::ConnectionAborted,
127+
hermit_abi::errno::ECONNREFUSED => ErrorKind::ConnectionRefused,
128+
hermit_abi::errno::ECONNRESET => ErrorKind::ConnectionReset,
129+
hermit_abi::errno::EEXIST => ErrorKind::AlreadyExists,
130+
hermit_abi::errno::EINTR => ErrorKind::Interrupted,
131+
hermit_abi::errno::EINVAL => ErrorKind::InvalidInput,
132+
hermit_abi::errno::ENOENT => ErrorKind::NotFound,
133+
hermit_abi::errno::ENOTCONN => ErrorKind::NotConnected,
134+
hermit_abi::errno::EPERM => ErrorKind::PermissionDenied,
135+
hermit_abi::errno::EPIPE => ErrorKind::BrokenPipe,
136+
hermit_abi::errno::ETIMEDOUT => ErrorKind::TimedOut,
137137
_ => ErrorKind::Uncategorized,
138138
}
139139
}

library/std/src/sys/pal/hermit/os.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::abi;
1+
use super::hermit_abi;
22
use crate::collections::HashMap;
33
use crate::error::Error as StdError;
44
use crate::ffi::{CStr, OsStr, OsString};
@@ -14,11 +14,11 @@ use crate::vec;
1414
use core::slice::memchr;
1515

1616
pub fn errno() -> i32 {
17-
unsafe { abi::get_errno() }
17+
unsafe { hermit_abi::get_errno() }
1818
}
1919

2020
pub fn error_string(errno: i32) -> String {
21-
abi::error_string(errno).to_string()
21+
hermit_abi::error_string(errno).to_string()
2222
}
2323

2424
pub fn getcwd() -> io::Result<PathBuf> {
@@ -197,10 +197,10 @@ pub fn home_dir() -> Option<PathBuf> {
197197

198198
pub fn exit(code: i32) -> ! {
199199
unsafe {
200-
abi::exit(code);
200+
hermit_abi::exit(code);
201201
}
202202
}
203203

204204
pub fn getpid() -> u32 {
205-
unsafe { abi::getpid() }
205+
unsafe { hermit_abi::getpid() }
206206
}

0 commit comments

Comments
 (0)