Skip to content

Commit 857f078

Browse files
authored
add support of getdents64 to read directory entries (#519)
* add support of getdents64 to read directory entries - remove readdir support - add comment to describe file attributes - This PR depends on hermit-os/kernel#1013 * add latest version of libhermit * add test to open a file from the proc filesystem /etc is not longer mounted and should not be used for a test * rename some data structures to improve the backward compatability * minor changes to pass the format check * add test to read the directory entries * using hexadecimal numbers instead decimal numbers - improves the readability * update kernel module
1 parent dbb6aa6 commit 857f078

File tree

5 files changed

+69
-38
lines changed

5 files changed

+69
-38
lines changed

examples/demo/src/main.rs

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ fn main() {
4848
stringify!(read_file),
4949
test_result(read_file())
5050
);
51+
println!(
52+
"Test {} ... {}",
53+
stringify!(read_dir),
54+
test_result(read_dir())
55+
);
5156
println!(
5257
"Test {} ... {}",
5358
stringify!(create_file),

examples/demo/src/tests/mod.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,20 @@ pub fn pi_parallel(num_steps: u64) -> Result<(), ()> {
108108
}
109109

110110
pub fn read_file() -> Result<(), std::io::Error> {
111-
let mut file = File::open("/etc/hostname")?;
111+
let mut file = File::open("/proc/version")?;
112112
let mut contents = String::new();
113113
file.read_to_string(&mut contents)?;
114114

115-
println!("Hostname: {contents}");
115+
println!("Version: {contents}");
116+
117+
Ok(())
118+
}
119+
120+
pub fn read_dir() -> Result<(), std::io::Error> {
121+
for entry in std::fs::read_dir("/proc")? {
122+
let entry = entry?;
123+
println!("Found {:?} in /proc", entry.file_name());
124+
}
116125

117126
Ok(())
118127
}

hermit-abi/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hermit-abi"
3-
version = "0.3.3"
3+
version = "0.3.4"
44
authors = ["Stefan Lankes"]
55
license = "MIT OR Apache-2.0"
66
edition = "2021"

hermit-abi/src/lib.rs

+51-34
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ pub mod errno;
1010
pub mod tcplistener;
1111
pub mod tcpstream;
1212

13-
use core::ffi::{c_int, c_void};
13+
pub use self::errno::*;
14+
pub use core::ffi::{c_int, c_short, c_void};
1415

1516
/// A thread handle type
1617
pub type Tid = u32;
@@ -54,6 +55,12 @@ pub const O_CREAT: i32 = 0o100;
5455
pub const O_EXCL: i32 = 0o200;
5556
pub const O_TRUNC: i32 = 0o1000;
5657
pub const O_APPEND: i32 = 0o2000;
58+
pub const F_DUPFD: i32 = 0;
59+
pub const F_GETFD: i32 = 1;
60+
pub const F_SETFD: i32 = 2;
61+
pub const F_GETFL: i32 = 3;
62+
pub const F_SETFL: i32 = 4;
63+
pub const FD_CLOEXEC: i32 = 1;
5764

5865
/// returns true if file descriptor `fd` is a tty
5966
pub fn isatty(_fd: c_int) -> bool {
@@ -145,6 +152,8 @@ pub const POLLHUP: i16 = 0x10;
145152
pub const POLLNVAL: i16 = 0x20;
146153
pub const POLLRDNORM: i16 = 0x040;
147154
pub const POLLRDBAND: i16 = 0x080;
155+
pub const POLLWRNORM: u16 = 0x0100;
156+
pub const POLLWRBAND: u16 = 0x0200;
148157
pub const POLLRDHUP: i16 = 0x2000;
149158
pub type sa_family_t = u8;
150159
pub type socklen_t = u32;
@@ -253,40 +262,49 @@ pub struct pollfd {
253262
}
254263

255264
#[repr(C)]
256-
pub struct dirent {
257-
pub d_ino: u64,
258-
pub d_off: u64,
259-
pub d_namelen: u32,
260-
pub d_type: u32,
261-
pub d_name: [u8; 0],
262-
}
263-
264-
#[repr(C)]
265-
#[derive(Copy, Clone, Debug)]
266-
pub enum DirectoryEntry {
267-
Invalid(i32),
268-
Valid(*const dirent),
269-
}
270-
271-
#[repr(C)]
272-
#[derive(Debug, Copy, Clone)]
265+
#[derive(Debug, Default, Copy, Clone)]
273266
pub struct stat {
274267
pub st_dev: u64,
275268
pub st_ino: u64,
276269
pub st_nlink: u64,
270+
/// access permissions
277271
pub st_mode: u32,
272+
/// user id
278273
pub st_uid: u32,
274+
/// group id
279275
pub st_gid: u32,
276+
/// device id
280277
pub st_rdev: u64,
281-
pub st_size: i64,
278+
/// size in bytes
279+
pub st_size: u64,
280+
/// block size
282281
pub st_blksize: i64,
282+
/// size in blocks
283283
pub st_blocks: i64,
284-
pub st_atime: i64,
285-
pub st_atime_nsec: i64,
286-
pub st_mtime: i64,
287-
pub st_mtime_nsec: i64,
288-
pub st_ctime: i64,
289-
pub st_ctime_nsec: i64,
284+
/// time of last access
285+
pub st_atime: u64,
286+
pub st_atime_nsec: u64,
287+
/// time of last modification
288+
pub st_mtime: u64,
289+
pub st_mtime_nsec: u64,
290+
/// time of last status change
291+
pub st_ctime: u64,
292+
pub st_ctime_nsec: u64,
293+
}
294+
295+
#[repr(C)]
296+
#[derive(Debug, Clone, Copy)]
297+
pub struct dirent64 {
298+
/// 64-bit inode number
299+
pub d_ino: u64,
300+
/// 64-bit offset to next structure
301+
pub d_off: i64,
302+
/// Size of this dirent
303+
pub d_reclen: u16,
304+
/// File type
305+
pub d_type: u8,
306+
/// Filename (null-terminated)
307+
pub d_name: core::marker::PhantomData<u8>,
290308
}
291309

292310
pub const DT_UNKNOWN: u32 = 0;
@@ -299,10 +317,10 @@ pub const DT_LNK: u32 = 10;
299317
pub const DT_SOCK: u32 = 12;
300318
pub const DT_WHT: u32 = 14;
301319

302-
pub const S_IFDIR: u32 = 16384;
303-
pub const S_IFREG: u32 = 32768;
304-
pub const S_IFLNK: u32 = 40960;
305-
pub const S_IFMT: u32 = 61440;
320+
pub const S_IFDIR: u32 = 0x4000;
321+
pub const S_IFREG: u32 = 0x8000;
322+
pub const S_IFLNK: u32 = 0xA000;
323+
pub const S_IFMT: u32 = 0xF000;
306324

307325
// sysmbols, which are part of the library operating system
308326
extern "C" {
@@ -559,11 +577,10 @@ extern "C" {
559577
#[link_name = "sys_read"]
560578
pub fn read(fd: i32, buf: *mut u8, len: usize) -> isize;
561579

562-
/// 'readdir' returns a pointer to a dirent structure
563-
/// representing the next directory entry in the directory stream
564-
/// pointed to by the file descriptor
565-
#[link_name = "sys_readdir"]
566-
pub fn readdir(fd: i32) -> DirectoryEntry;
580+
/// `getdents64` reads directory entries from the directory referenced
581+
/// by the file descriptor `fd` into the buffer pointed to by `buf`.
582+
#[link_name = "sys_getdents64"]
583+
pub fn getdents64(fd: i32, dirp: *mut dirent64, count: usize) -> i64;
567584

568585
/// 'mkdir' attempts to create a directory,
569586
/// it returns 0 on success and -1 on error

0 commit comments

Comments
 (0)