|
| 1 | +import type { WASIFarmAnimal } from "@oligami/browser_wasi_shim-threads"; |
| 2 | +import { wasi } from "@bjorn3/browser_wasi_shim"; |
| 3 | + |
| 4 | +export const get_data = (path__: string, _animal: WASIFarmAnimal): Uint8Array => { |
| 5 | + // biome-ignore lint/suspicious/noExplicitAny: <explanation> |
| 6 | + const animal = _animal as any; |
| 7 | + |
| 8 | + // path is absolute |
| 9 | + let path = path__; |
| 10 | + if (!path.startsWith("/")) { |
| 11 | + path = `/${path}`; |
| 12 | + } |
| 13 | + |
| 14 | + // first: get opened fd dir name |
| 15 | + let root_fd: number; |
| 16 | + const dir_names: Map<number, string> = new Map(); |
| 17 | + for (let fd = 0; fd < animal.fd_map.length; fd++) { |
| 18 | + const [mapped_fd, wasi_farm_ref] = animal.get_fd_and_wasi_ref(fd); |
| 19 | + if (mapped_fd === undefined || wasi_farm_ref === undefined) { |
| 20 | + continue; |
| 21 | + } |
| 22 | + const [prestat, ret] = wasi_farm_ref.fd_prestat_get(mapped_fd); |
| 23 | + if (ret !== wasi.ERRNO_SUCCESS) { |
| 24 | + continue; |
| 25 | + } |
| 26 | + if (prestat) { |
| 27 | + const [tag, name_len] = prestat; |
| 28 | + if (tag === wasi.PREOPENTYPE_DIR) { |
| 29 | + const [path, ret] = wasi_farm_ref.fd_prestat_dir_name( |
| 30 | + mapped_fd, |
| 31 | + name_len, |
| 32 | + ); |
| 33 | + if (ret !== wasi.ERRNO_SUCCESS) { |
| 34 | + continue; |
| 35 | + } |
| 36 | + if (path) { |
| 37 | + const decoded_path = new TextDecoder().decode(path); |
| 38 | + dir_names.set(fd, decoded_path); |
| 39 | + if (decoded_path === "/") { |
| 40 | + root_fd = fd; |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // second: most match path |
| 48 | + let matched_fd = root_fd; |
| 49 | + let matched_dir_len = 1; |
| 50 | + const parts_path = path.split("/"); |
| 51 | + for (const [fd, dir_name] of dir_names) { |
| 52 | + const parts_dir_name = dir_name.split("/"); |
| 53 | + let dir_len = 0; |
| 54 | + for (let i = 0; i < parts_dir_name.length; i++) { |
| 55 | + if (parts_dir_name[i] === parts_path[i]) { |
| 56 | + dir_len++; |
| 57 | + } else { |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + if (dir_len > matched_dir_len) { |
| 62 | + matched_fd = fd; |
| 63 | + matched_dir_len = dir_len; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + if (matched_dir_len === 0) { |
| 68 | + throw new Error("no matched dir"); |
| 69 | + } |
| 70 | + |
| 71 | + console.log("matched_dir_name", dir_names.get(matched_fd)); |
| 72 | + |
| 73 | + // third: tale the rest of path |
| 74 | + const rest_path = parts_path.slice(matched_dir_len).join("/"); |
| 75 | + console.log("rest_path", rest_path); |
| 76 | + |
| 77 | + // fourth: open file |
| 78 | + const [mapped_fd, wasi_farm_ref_n] = animal.get_fd_and_wasi_ref_n(matched_fd); |
| 79 | + const [opened_fd, ret] = animal.wasi_farm_refs[wasi_farm_ref_n].path_open( |
| 80 | + mapped_fd, |
| 81 | + 0, |
| 82 | + new TextEncoder().encode(rest_path), |
| 83 | + 0, |
| 84 | + 0n, |
| 85 | + 0n, |
| 86 | + 0, |
| 87 | + ); |
| 88 | + |
| 89 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 90 | + const mapped_opened_fd = animal.map_new_fd_and_notify( |
| 91 | + opened_fd, |
| 92 | + wasi_farm_ref_n, |
| 93 | + ); |
| 94 | + |
| 95 | + if (ret !== wasi.ERRNO_SUCCESS) { |
| 96 | + throw new Error("failed to open file"); |
| 97 | + } |
| 98 | + |
| 99 | + // fifth: read file |
| 100 | + let file_data: Uint8Array = new Uint8Array(); |
| 101 | + let offset = 0n; |
| 102 | + |
| 103 | + // eslint-disable-next-line no-constant-condition |
| 104 | + while (true) { |
| 105 | + console.log("offset", offset); |
| 106 | + |
| 107 | + const iovs = new Uint32Array(2); |
| 108 | + // buf_ptr so any value |
| 109 | + iovs[0] = 0; |
| 110 | + // buf_len |
| 111 | + iovs[1] = 1024; |
| 112 | + const [nread_and_buf, ret] = animal.wasi_farm_refs[ |
| 113 | + wasi_farm_ref_n |
| 114 | + ].fd_pread(opened_fd, iovs, offset); |
| 115 | + if (ret !== wasi.ERRNO_SUCCESS) { |
| 116 | + throw new Error("failed to read file"); |
| 117 | + } |
| 118 | + if (!nread_and_buf) { |
| 119 | + throw new Error("failed to read file"); |
| 120 | + } |
| 121 | + const [nread, buf] = nread_and_buf; |
| 122 | + if (nread === 0) { |
| 123 | + break; |
| 124 | + } |
| 125 | + const new_data = new Uint8Array(file_data.length + nread); |
| 126 | + new_data.set(file_data); |
| 127 | + new_data.set(buf, file_data.length); |
| 128 | + file_data = new_data; |
| 129 | + offset += BigInt(nread); |
| 130 | + if (nread < 1024) { |
| 131 | + break; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + animal.wasi_farm_refs[wasi_farm_ref_n].fd_close(opened_fd); |
| 136 | + |
| 137 | + return file_data; |
| 138 | +} |
0 commit comments