@@ -65,11 +65,10 @@ unsafe fn exec_fd_readdir(fd: wasi::Fd, cookie: wasi::Dircookie) -> (Vec<DirEntr
6565 ( dirs, eof)
6666}
6767
68- unsafe fn test_fd_readdir ( dir_fd : wasi:: Fd ) {
69- let stat = wasi:: fd_filestat_get ( dir_fd ) . expect ( "failed filestat" ) ;
68+ unsafe fn assert_empty_dir ( fd : wasi:: Fd ) {
69+ let stat = wasi:: fd_filestat_get ( fd ) . expect ( "failed filestat" ) ;
7070
71- // Check the behavior in an empty directory
72- let ( mut dirs, eof) = exec_fd_readdir ( dir_fd, 0 ) ;
71+ let ( mut dirs, eof) = exec_fd_readdir ( fd, 0 ) ;
7372 assert ! ( eof, "expected to read the entire directory" ) ;
7473 dirs. sort_by_key ( |d| d. name . clone ( ) ) ;
7574 assert_eq ! ( dirs. len( ) , 2 , "expected two entries in an empty directory" ) ;
@@ -91,6 +90,11 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
9190 dirs. next( ) . is_none( ) ,
9291 "the directory should be seen as empty"
9392 ) ;
93+ }
94+
95+ unsafe fn test_fd_readdir ( dir_fd : wasi:: Fd ) {
96+ // Check the behavior in an empty directory
97+ assert_empty_dir ( dir_fd) ;
9498
9599 // Add a file and check the behavior
96100 let file_fd = wasi:: path_open (
@@ -111,16 +115,33 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
111115 "file descriptor range check" ,
112116 ) ;
113117
114- let stat = wasi:: fd_filestat_get ( file_fd) . expect ( "failed filestat" ) ;
118+ let file_stat = wasi:: fd_filestat_get ( file_fd) . expect ( "failed filestat" ) ;
115119 wasi:: fd_close ( file_fd) . expect ( "closing a file" ) ;
116120
121+ wasi:: path_create_directory ( dir_fd, "nested" ) . expect ( "create a directory" ) ;
122+ let nested_fd = wasi:: path_open (
123+ dir_fd,
124+ 0 ,
125+ "nested" ,
126+ 0 ,
127+ wasi:: RIGHTS_FD_READ | wasi:: RIGHTS_FD_READDIR | wasi:: RIGHTS_FD_FILESTAT_GET ,
128+ 0 ,
129+ 0 ,
130+ )
131+ . expect ( "failed to open nested directory" ) ;
132+ assert ! (
133+ nested_fd > file_fd,
134+ "nested directory file descriptor range check" ,
135+ ) ;
136+ let nested_stat = wasi:: fd_filestat_get ( nested_fd) . expect ( "failed filestat" ) ;
137+
117138 // Execute another readdir
118139 let ( mut dirs, eof) = exec_fd_readdir ( dir_fd, 0 ) ;
119140 assert ! ( eof, "expected to read the entire directory" ) ;
120- assert_eq ! ( dirs. len( ) , 3 , "expected three entries" ) ;
141+ assert_eq ! ( dirs. len( ) , 4 , "expected four entries" ) ;
121142 // Save the data about the last entry. We need to do it before sorting.
122- let lastfile_cookie = dirs[ 1 ] . dirent . d_next ;
123- let lastfile_name = dirs[ 2 ] . name . clone ( ) ;
143+ let lastfile_cookie = dirs[ 2 ] . dirent . d_next ;
144+ let lastfile_name = dirs[ 3 ] . name . clone ( ) ;
124145 dirs. sort_by_key ( |d| d. name . clone ( ) ) ;
125146 let mut dirs = dirs. into_iter ( ) ;
126147
@@ -136,15 +157,29 @@ unsafe fn test_fd_readdir(dir_fd: wasi::Fd) {
136157 wasi:: FILETYPE_REGULAR_FILE ,
137158 "type for the real file"
138159 ) ;
139- assert_eq ! ( dir. dirent. d_ino, stat. ino) ;
160+ assert_eq ! ( dir. dirent. d_ino, file_stat. ino) ;
161+ let dir = dirs. next ( ) . expect ( "fourth entry is None" ) ;
162+ // check the directory info
163+ assert_eq ! ( dir. name, "nested" , "nested directory name doesn't match" ) ;
164+ assert_eq ! (
165+ dir. dirent. d_type,
166+ wasi:: FILETYPE_DIRECTORY ,
167+ "type for the nested directory"
168+ ) ;
169+ assert_eq ! ( dir. dirent. d_ino, nested_stat. ino) ;
140170
141171 // check if cookie works as expected
142172 let ( dirs, eof) = exec_fd_readdir ( dir_fd, lastfile_cookie) ;
143173 assert ! ( eof, "expected to read the entire directory" ) ;
144174 assert_eq ! ( dirs. len( ) , 1 , "expected one entry" ) ;
145175 assert_eq ! ( dirs[ 0 ] . name, lastfile_name, "name of the only entry" ) ;
146176
177+ // check if nested directory shows up as empty
178+ assert_empty_dir ( nested_fd) ;
179+ wasi:: fd_close ( nested_fd) . expect ( "closing a nested directory" ) ;
180+
147181 wasi:: path_unlink_file ( dir_fd, "file" ) . expect ( "removing a file" ) ;
182+ wasi:: path_remove_directory ( dir_fd, "nested" ) . expect ( "removing a nested directory" ) ;
148183}
149184
150185unsafe fn test_fd_readdir_lots ( dir_fd : wasi:: Fd ) {
0 commit comments