|
| 1 | +#include "test-common.h" |
| 2 | +#include "uv.h" |
| 3 | +#include "uvwasi.h" |
| 4 | +#include "wasi_serdes.h" |
| 5 | +#include <assert.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | + |
| 10 | +#define TEST_TMP_DIR "./out/tmp" |
| 11 | +#define TEST_PATH_READDIR TEST_TMP_DIR "/test_readdir_cookie" |
| 12 | + |
| 13 | +#if !defined(_WIN32) && !defined(__ANDROID__) |
| 14 | +static void touch_file(const char *name) { |
| 15 | + uv_fs_t req; |
| 16 | + int r; |
| 17 | + |
| 18 | + r = uv_fs_open(NULL, &req, name, O_WRONLY | O_CREAT | O_TRUNC, |
| 19 | + S_IWUSR | S_IRUSR, NULL); |
| 20 | + uv_fs_req_cleanup(&req); |
| 21 | + assert(r >= 0); |
| 22 | + r = uv_fs_close(NULL, &req, r, NULL); |
| 23 | + uv_fs_req_cleanup(&req); |
| 24 | + assert(r == 0); |
| 25 | +} |
| 26 | +#endif /* !defined(_WIN32) && !defined(__ANDROID__) */ |
| 27 | + |
| 28 | +/* |
| 29 | + * This is a test case for https://github.com/nodejs/node/issues/47193. |
| 30 | + */ |
| 31 | + |
| 32 | +int main(void) { |
| 33 | +#if !defined(_WIN32) && !defined(__ANDROID__) |
| 34 | + uvwasi_t uvwasi; |
| 35 | + uvwasi_options_t init_options; |
| 36 | + uvwasi_dircookie_t cookie; |
| 37 | + uvwasi_dirent_t dirent; |
| 38 | + uvwasi_size_t buf_size; |
| 39 | + uvwasi_size_t buf_used; |
| 40 | + uvwasi_errno_t err; |
| 41 | + uv_fs_t req; |
| 42 | + uvwasi_fd_t tmp_fd = 3; |
| 43 | + char buf[64]; |
| 44 | + int r; |
| 45 | + int cnt; |
| 46 | + |
| 47 | + setup_test_environment(); |
| 48 | + |
| 49 | + r = uv_fs_mkdir(NULL, &req, TEST_TMP_DIR, 0777, NULL); |
| 50 | + uv_fs_req_cleanup(&req); |
| 51 | + assert(r == 0 || r == UV_EEXIST); |
| 52 | + |
| 53 | + r = uv_fs_mkdir(NULL, &req, TEST_PATH_READDIR, 0777, NULL); |
| 54 | + uv_fs_req_cleanup(&req); |
| 55 | + assert(r == 0 || r == UV_EEXIST); |
| 56 | + |
| 57 | + for (int i = 0; i < 10; i++) { |
| 58 | + const char *format = TEST_PATH_READDIR "/test_file_" |
| 59 | + "%d"; |
| 60 | + int len = strlen(format) + 3; |
| 61 | + char file_name[len]; |
| 62 | + snprintf(file_name, len, format, i); |
| 63 | + touch_file(file_name); |
| 64 | + } |
| 65 | + |
| 66 | + uvwasi_options_init(&init_options); |
| 67 | + init_options.preopenc = 1; |
| 68 | + init_options.preopens = calloc(1, sizeof(uvwasi_preopen_t)); |
| 69 | + init_options.preopens[0].mapped_path = "/var"; |
| 70 | + init_options.preopens[0].real_path = TEST_PATH_READDIR; |
| 71 | + |
| 72 | + err = uvwasi_init(&uvwasi, &init_options); |
| 73 | + assert(err == 0); |
| 74 | + |
| 75 | + buf_size = 64; |
| 76 | + memset(buf, 0, buf_size); |
| 77 | + buf_used = -1; |
| 78 | + cookie = UVWASI_DIRCOOKIE_START; |
| 79 | + cnt = 0; |
| 80 | + |
| 81 | + // For simplicity, we read entries one by one |
| 82 | + while (buf_used == -1 || buf_used == buf_size) { |
| 83 | + memset(buf, 0, buf_size); |
| 84 | + err = uvwasi_fd_readdir(&uvwasi, tmp_fd, &buf, buf_size, cookie, &buf_used); |
| 85 | + uvwasi_serdes_read_dirent_t(buf, 0, &dirent); |
| 86 | + assert(err == UVWASI_ESUCCESS); |
| 87 | + |
| 88 | + cookie = dirent.d_next; |
| 89 | + cnt += 1; |
| 90 | + |
| 91 | + // There are only 10 files |
| 92 | + assert(cnt <= 10); |
| 93 | + } |
| 94 | + |
| 95 | + assert(cnt == 10); |
| 96 | + uvwasi_destroy(&uvwasi); |
| 97 | + free(init_options.preopens); |
| 98 | + |
| 99 | +#endif /* !defined(_WIN32) && !defined(__ANDROID__) */ |
| 100 | + return 0; |
| 101 | +} |
0 commit comments