-
Notifications
You must be signed in to change notification settings - Fork 0
/
canonicalize.patch
47 lines (44 loc) · 1.54 KB
/
canonicalize.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
diff --git a/library/std/src/sys/pal/wasi/fs.rs b/library/std/src/sys/pal/wasi/fs.rs
index e8238665452..5f77b656894 100644
--- a/library/std/src/sys/pal/wasi/fs.rs
+++ b/library/std/src/sys/pal/wasi/fs.rs
@@ -1,6 +1,7 @@
#![deny(unsafe_op_in_unsafe_fn)]
use super::fd::WasiFd;
+use crate::env;
use crate::ffi::{CStr, OsStr, OsString};
use crate::fmt;
use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom};
@@ -652,10 +653,30 @@ fn metadata_at(fd: &WasiFd, flags: wasi::Lookupflags, path: &Path) -> io::Result
Ok(FileAttr { meta })
}
-pub fn canonicalize(_p: &Path) -> io::Result<PathBuf> {
- // This seems to not be in wasi's API yet, and we may need to end up
- // emulating it ourselves. For now just return an error.
- unsupported()
+pub fn canonicalize(p: &Path) -> io::Result<PathBuf> {
+ fn __canonicalize(path: &Path, buf: &mut PathBuf) {
+ if path.is_absolute() {
+ buf.clear();
+ }
+ for part in path {
+ if part == ".." {
+ buf.pop();
+ } else if part != "." {
+ buf.push(part);
+ if let Ok(linkpath) = buf.read_link() {
+ buf.pop();
+ __canonicalize(&linkpath, buf);
+ }
+ }
+ }
+ }
+ let mut path = if p.is_absolute() {
+ PathBuf::new()
+ } else {
+ PathBuf::from(env::current_dir()?)
+ };
+ __canonicalize(p, &mut path);
+ Ok(path)
}
fn open_at(fd: &WasiFd, path: &Path, opts: &OpenOptions) -> io::Result<File> {