Skip to content

Commit fdc57ce

Browse files
committed
ln: use wasi-libc symlink instead of unstable symlink_path
1 parent 35b442f commit fdc57ce

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/uu/ln/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ path = "src/ln.rs"
2020

2121
[dependencies]
2222
clap = { workspace = true }
23+
libc = { workspace = true }
2324
uucore = { workspace = true, features = ["backup-control", "fs"] }
2425
thiserror = { workspace = true }
2526
fluent = { workspace = true }

src/uu/ln/src/ln.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
7-
86
// spell-checker:ignore (ToDO) srcpath targetpath EEXIST
97

108
use clap::{Arg, ArgAction, Command};
@@ -21,10 +19,12 @@ use std::ffi::OsString;
2119
use std::fs;
2220
use thiserror::Error;
2321

22+
#[cfg(target_os = "wasi")]
23+
use std::ffi::CString;
24+
#[cfg(target_os = "wasi")]
25+
use std::io;
2426
#[cfg(any(unix, target_os = "redox"))]
2527
use std::os::unix::fs::symlink;
26-
#[cfg(target_os = "wasi")]
27-
use std::os::wasi::fs::symlink_path as symlink;
2828
#[cfg(windows)]
2929
use std::os::windows::fs::{symlink_dir, symlink_file};
3030
use std::path::{Path, PathBuf};
@@ -492,3 +492,19 @@ pub fn symlink<P1: AsRef<Path>, P2: AsRef<Path>>(src: P1, dst: P2) -> std::io::R
492492
symlink_file(src, dst)
493493
}
494494
}
495+
496+
#[cfg(target_os = "wasi")]
497+
pub fn symlink<P1: AsRef<Path>, P2: AsRef<Path>>(src: P1, dst: P2) -> io::Result<()> {
498+
use std::os::wasi::ffi::OsStrExt;
499+
500+
let src_c = CString::new(src.as_ref().as_os_str().as_bytes())
501+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
502+
let dst_c = CString::new(dst.as_ref().as_os_str().as_bytes())
503+
.map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;
504+
505+
if unsafe { libc::symlink(src_c.as_ptr(), dst_c.as_ptr()) } == 0 {
506+
Ok(())
507+
} else {
508+
Err(io::Error::last_os_error())
509+
}
510+
}

0 commit comments

Comments
 (0)