Given an fd, returns the string path that the fd refers to. TODO: full error handling TODO: maybe make this a public API, as it is useful for debugging
| 26 | // TODO: full error handling |
| 27 | // TODO: maybe make this a public API, as it is useful for debugging |
| 28 | std::string getPath(int fd) { |
| 29 | auto fileTable = wasmfs::wasmFS.getFileTable().locked(); |
| 30 | |
| 31 | auto openFile = fileTable.getEntry(fd); |
| 32 | if (!openFile) { |
| 33 | return "!"; |
| 34 | } |
| 35 | |
| 36 | auto curr = openFile->locked().getFile(); |
| 37 | std::string result = ""; |
| 38 | while (curr != wasmfs::wasmFS.getRootDirectory()) { |
| 39 | auto parent = curr->locked().getParent(); |
| 40 | // Check if the parent exists. The parent may not exist if curr was |
| 41 | // unlinked. |
| 42 | if (!parent) { |
| 43 | return "?/" + result; |
| 44 | } |
| 45 | |
| 46 | auto parentDir = parent->dynCast<wasmfs::Directory>(); |
| 47 | auto name = parentDir->locked().getName(curr); |
| 48 | result = '/' + name + result; |
| 49 | curr = parentDir; |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | } // namespace wasmfs |
| 55 |
no test coverage detected