|
8 | 8 | #include <wasi/libc-find-relpath.h> |
9 | 9 | #include <wasi/libc.h> |
10 | 10 |
|
11 | | -char *__wasilibc_cwd = "/"; |
| 11 | +extern char *__wasilibc_cwd; |
12 | 12 | static int __wasilibc_cwd_mallocd = 0; |
13 | 13 |
|
14 | 14 | int chdir(const char *path) |
15 | 15 | { |
16 | | - // Find a preopen'd directory as well as a relative path we're anchored |
17 | | - // from which we're changing directories to. |
18 | | - char *relative; |
19 | | - const char *abs; |
20 | | - int parent_fd = __wasilibc_find_relpath(path, &abs, &relative); |
21 | | - if (parent_fd == -1) { |
22 | | - errno = ENOENT; |
23 | | - return -1; |
24 | | - } |
| 16 | + static char *relative_buf = NULL; |
| 17 | + static size_t relative_buf_len = 0; |
25 | 18 |
|
26 | | - // Make sure that this directory we're accessing is indeed a directory. |
27 | | - struct stat dirinfo; |
28 | | - int ret = fstatat(parent_fd, relative, &dirinfo, 0); |
29 | | - if (ret == -1) { |
30 | | - free(relative); |
31 | | - return -1; |
32 | | - } |
33 | | - if (!S_ISDIR(dirinfo.st_mode)) { |
34 | | - free(relative); |
35 | | - errno = ENOTDIR; |
36 | | - return -1; |
37 | | - } |
| 19 | + // Find a preopen'd directory as well as a relative path we're anchored |
| 20 | + // from which we're changing directories to. |
| 21 | + const char *abs; |
| 22 | + int parent_fd; |
| 23 | + while (1) { |
| 24 | + parent_fd = __wasilibc_find_relpath(path, &abs, relative_buf, relative_buf_len); |
| 25 | + if (parent_fd != -1) |
| 26 | + break; |
| 27 | + if (errno != ERANGE) |
| 28 | + return -1; |
| 29 | + size_t new_len = relative_buf_len == 0 ? 16 : 2 * relative_buf_len; |
| 30 | + relative_buf = realloc(relative_buf, new_len); |
| 31 | + if (relative_buf == NULL) { |
| 32 | + errno = ENOMEM; |
| 33 | + return -1; |
| 34 | + } |
| 35 | + relative_buf_len = new_len; |
| 36 | + } |
38 | 37 |
|
39 | | - // Copy over our new absolute path into `__wasilibc_cwd`. Only copy over |
40 | | - // the relative portion of the path if it's not equal to `.` |
41 | | - size_t len = strlen(abs); |
42 | | - int copy_relative = strcmp(relative, ".") != 0; |
43 | | - char *new_cwd = malloc(len + (copy_relative ? strlen(relative) : 0)); |
44 | | - if (new_cwd == NULL) { |
45 | | - errno = ENOMEM; |
46 | | - return -1; |
47 | | - } |
48 | | - strcpy(new_cwd, abs); |
49 | | - if (copy_relative) |
50 | | - strcpy(new_cwd + strlen(abs), relative); |
51 | | - free(relative); |
| 38 | + // Make sure that this directory we're accessing is indeed a directory. |
| 39 | + struct stat dirinfo; |
| 40 | + int ret = fstatat(parent_fd, relative_buf, &dirinfo, 0); |
| 41 | + if (ret == -1) { |
| 42 | + return -1; |
| 43 | + } |
| 44 | + if (!S_ISDIR(dirinfo.st_mode)) { |
| 45 | + errno = ENOTDIR; |
| 46 | + return -1; |
| 47 | + } |
52 | 48 |
|
53 | | - // And set our new malloc'd buffer into the global cwd, freeing the |
54 | | - // previous one if necessary. |
55 | | - char *prev_cwd = __wasilibc_cwd; |
56 | | - __wasilibc_cwd = new_cwd; |
57 | | - if (__wasilibc_cwd_mallocd) |
58 | | - free(prev_cwd); |
59 | | - __wasilibc_cwd_mallocd = 1; |
60 | | - return 0; |
| 49 | + // Copy over our new absolute path into `__wasilibc_cwd`. Only copy over |
| 50 | + // the relative portion of the path if it's not equal to `.` |
| 51 | + size_t len = strlen(abs); |
| 52 | + int copy_relative = strcmp(relative_buf, ".") != 0; |
| 53 | + char *new_cwd = malloc(len + (copy_relative ? strlen(relative_buf) : 0)); |
| 54 | + if (new_cwd == NULL) { |
| 55 | + errno = ENOMEM; |
| 56 | + return -1; |
| 57 | + } |
| 58 | + strcpy(new_cwd, abs); |
| 59 | + if (copy_relative) |
| 60 | + strcpy(new_cwd + strlen(abs), relative_buf); |
| 61 | + |
| 62 | + // And set our new malloc'd buffer into the global cwd, freeing the |
| 63 | + // previous one if necessary. |
| 64 | + char *prev_cwd = __wasilibc_cwd; |
| 65 | + __wasilibc_cwd = new_cwd; |
| 66 | + if (__wasilibc_cwd_mallocd) |
| 67 | + free(prev_cwd); |
| 68 | + __wasilibc_cwd_mallocd = 1; |
| 69 | + return 0; |
61 | 70 | } |
0 commit comments