Skip to content

Commit eb50440

Browse files
authored
Merge pull request #18 from hermit-os/fs
feat(fs): add `readdir` test
2 parents f3667b6 + 3ed6608 commit eb50440

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/fs.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,44 @@ static int print_getdents64(const char *path) {
216216
return 0;
217217
}
218218

219+
static int print_readdir(const char *path) {
220+
fprintf(stderr, "print_readdir(\"%s\")\n", path);
221+
222+
fprintf(stderr, "opendir(\"%s\")\n", path);
223+
DIR *dir = opendir(path);
224+
if (dir == NULL) {
225+
perror("opendir() failed");
226+
return -1;
227+
}
228+
229+
fputc('\n', stderr);
230+
for (;;) {
231+
errno = 0;
232+
// NOLINTNEXTLINE(concurrency-mt-unsafe)
233+
struct dirent *dirent = readdir(dir);
234+
if (dirent == NULL) {
235+
break;
236+
}
237+
238+
fprintf(stderr, "%s\n", dirent->d_name);
239+
}
240+
if (errno != 0) {
241+
perror("readdir() failed");
242+
return -1;
243+
}
244+
fputc('\n', stderr);
245+
246+
fprintf(stderr, "closedir(\"%s\")\n", path);
247+
int closed = closedir(dir);
248+
if (closed == -1) {
249+
perror("closedir() failed");
250+
return -1;
251+
}
252+
253+
fputc('\n', stderr);
254+
return 0;
255+
}
256+
219257
int main(void) {
220258
#ifdef __hermit__
221259
const char *dir_path = "/my-dir";
@@ -240,6 +278,11 @@ int main(void) {
240278
return EXIT_FAILURE;
241279
}
242280

281+
int readdir_status = print_readdir(dir_path);
282+
if (readdir_status == -1) {
283+
return EXIT_FAILURE;
284+
}
285+
243286
int unlink_status = unlink_files(dir_path, file_count);
244287
if (unlink_status == -1) {
245288
return EXIT_FAILURE;

0 commit comments

Comments
 (0)