MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / test_fd_readdir

Function test_fd_readdir

crates/test-programs/src/bin/p1_fd_readdir.rs:97–170  ·  view source on GitHub ↗
(dir_fd: wasip1::Fd)

Source from the content-addressed store, hash-verified

95}
96
97unsafe fn test_fd_readdir(dir_fd: wasip1::Fd) {
98 // Check the behavior in an empty directory
99 assert_empty_dir(dir_fd);
100
101 // Add a file and check the behavior
102 let file_fd = wasip1::path_open(
103 dir_fd,
104 0,
105 "file",
106 wasip1::OFLAGS_CREAT,
107 wasip1::RIGHTS_FD_READ | wasip1::RIGHTS_FD_WRITE,
108 0,
109 0,
110 )
111 .expect("failed to create file");
112 assert!(
113 file_fd > libc::STDERR_FILENO as wasip1::Fd,
114 "file descriptor range check",
115 );
116
117 let file_stat = wasip1::fd_filestat_get(file_fd).expect("failed filestat");
118 wasip1::fd_close(file_fd).expect("closing a file");
119
120 wasip1::path_create_directory(dir_fd, "nested").expect("create a directory");
121 let nested_fd = wasip1::path_open(dir_fd, 0, "nested", 0, 0, 0, 0)
122 .expect("failed to open nested directory");
123 let nested_stat = wasip1::fd_filestat_get(nested_fd).expect("failed filestat");
124
125 // Execute another readdir
126 let (mut dirs, eof) = exec_fd_readdir(dir_fd, 0);
127 assert!(eof, "expected to read the entire directory");
128 assert_eq!(dirs.len(), 4, "expected four entries");
129 // Save the data about the last entry. We need to do it before sorting.
130 let lastfile_cookie = dirs[2].dirent.d_next;
131 let lastfile_name = dirs[3].name.clone();
132 dirs.sort_by_key(|d| d.name.clone());
133 let mut dirs = dirs.into_iter();
134
135 let dir = dirs.next().expect("first entry is None");
136 assert_eq!(dir.name, ".", "first name");
137 let dir = dirs.next().expect("second entry is None");
138 assert_eq!(dir.name, "..", "second name");
139 let dir = dirs.next().expect("third entry is None");
140 // check the file info
141 assert_eq!(dir.name, "file", "file name doesn't match");
142 assert_eq!(
143 dir.dirent.d_type,
144 wasip1::FILETYPE_REGULAR_FILE,
145 "type for the real file"
146 );
147 assert_eq!(dir.dirent.d_ino, file_stat.ino);
148 let dir = dirs.next().expect("fourth entry is None");
149 // check the directory info
150 assert_eq!(dir.name, "nested", "nested directory name doesn't match");
151 assert_eq!(
152 dir.dirent.d_type,
153 wasip1::FILETYPE_DIRECTORY,
154 "type for the nested directory"

Callers 1

mainFunction · 0.85

Calls 13

path_openFunction · 0.85
fd_filestat_getFunction · 0.85
fd_closeFunction · 0.85
path_create_directoryFunction · 0.85
exec_fd_readdirFunction · 0.85
path_unlink_fileFunction · 0.85
path_remove_directoryFunction · 0.85
sort_by_keyMethod · 0.80
assert_empty_dirFunction · 0.70
expectMethod · 0.45
cloneMethod · 0.45
into_iterMethod · 0.45

Tested by

no test coverage detected