authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-12-05 21:06:33-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-06 12:27:28-07:00
log793db278050973e00d71d6789b8d439a7bbbcaa9
tree505e19c453babe2e9f0b3a10b80d7dd9cb27757a
parent98338358ede66020458e83b1ec7cda79eac2698b

wasi: add support for windows paths


1 files changed, 27 insertions(+), 8 deletions(-)

stage1/wasi.c+27-8
......@@ -237,7 +237,7 @@ int main(int argc, char **argv) {
237237
238238 des[3].filetype = wasi_filetype_directory;
239239 des[3].guest_path = dupe("/lib", sizeof("/lib"));
240 des[3].host_path = dupe(argv[1], strlen(argv[1]));
240 des[3].host_path = dupe(argv[1], strlen(argv[1]) + 1);
241241
242242 fd_len = 6;
243243 fds = calloc(sizeof(struct FileDescriptor), fd_len);
......@@ -252,8 +252,27 @@ int main(int argc, char **argv) {
252252 wasm__start();
253253}
254254
255static bool isLetter(char c) {
256 return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
257}
258static bool isPathSep(char c) {
259 return c == '/' || c == '\\';
260}
261static bool isAbsPath(const char *path, uint32_t path_len) {
262 if (path_len >= 1 && isPathSep(path[0])) return true;
263 if (path_len >= 3 && isLetter(path[0]) && path[1] == ':' && isPathSep(path[2])) return true;
264 return false;
265}
266static bool isSamePath(const char *a, const char *b, uint32_t len) {
267 for (uint32_t i = 0; i < len; i += 1) {
268 if (isPathSep(a[i]) && isPathSep(b[i])) continue;
269 if (a[i] != b[i]) return false;
270 }
271 return true;
272}
273
255274static enum wasi_errno DirEntry_create(uint32_t dir_fd, const char *path, uint32_t path_len, enum wasi_filetype filetype, time_t tim, uint32_t *res_de) {
256 if (path[0] != '/') {
275 if (isAbsPath(path, path_len)) {
257276 if (dir_fd >= fd_len || fds[dir_fd].de >= de_len) return wasi_errno_badf;
258277 if (des[fds[dir_fd].de].filetype != wasi_filetype_directory) return wasi_errno_notdir;
259278 }
......@@ -267,7 +286,7 @@ static enum wasi_errno DirEntry_create(uint32_t dir_fd, const char *path, uint32
267286 de->atim = tim;
268287 de->mtim = tim;
269288 de->ctim = tim;
270 if (path[0] == '/') {
289 if (isAbsPath(path, path_len)) {
271290 de->guest_path = malloc(path_len + 1);
272291 if (de->guest_path == NULL) return wasi_errno_nomem;
273292 memcpy(&de->guest_path[0], path, path_len);
......@@ -307,10 +326,10 @@ static enum wasi_errno DirEntry_create(uint32_t dir_fd, const char *path, uint32
307326
308327static enum wasi_errno DirEntry_lookup(uint32_t dir_fd, uint32_t flags, const char *path, uint32_t path_len, uint32_t *res_de) {
309328 (void)flags;
310 if (path[0] == '/') {
329 if (isAbsPath(path, path_len)) {
311330 for (uint32_t de = 0; de < de_len; de += 1) {
312331 if (des[de].guest_path == NULL) continue;
313 if (memcmp(&des[de].guest_path[0], path, path_len) != 0) continue;
332 if (!isSamePath(&des[de].guest_path[0], path, path_len)) continue;
314333 if (des[de].guest_path[path_len] != '\0') continue;
315334 if (res_de != NULL) *res_de = de;
316335 return wasi_errno_success;
......@@ -323,9 +342,9 @@ static enum wasi_errno DirEntry_lookup(uint32_t dir_fd, uint32_t flags, const ch
323342 size_t dir_guest_path_len = strlen(dir_de->guest_path);
324343 for (uint32_t de = 0; de < de_len; de += 1) {
325344 if (des[de].guest_path == NULL) continue;
326 if (memcmp(&des[de].guest_path[0], dir_de->guest_path, dir_guest_path_len) != 0) continue;
327 if (des[de].guest_path[dir_guest_path_len] != '/') continue;
328 if (memcmp(&des[de].guest_path[dir_guest_path_len + 1], path, path_len) != 0) continue;
345 if (!isSamePath(&des[de].guest_path[0], dir_de->guest_path, dir_guest_path_len)) continue;
346 if (!isPathSep(des[de].guest_path[dir_guest_path_len])) continue;
347 if (!isSamePath(&des[de].guest_path[dir_guest_path_len + 1], path, path_len)) continue;
329348 if (des[de].guest_path[dir_guest_path_len + 1 + path_len] != '\0') continue;
330349 if (res_de != NULL) *res_de = de;
331350 return wasi_errno_success;