Add unistd::symlink function

This commit is contained in:
Nathan Fisher 2023-02-02 23:47:46 -05:00
parent 6963ba4a4b
commit bcb5725a5c
3 changed files with 33 additions and 5 deletions

View File

@ -1,7 +1,8 @@
use super::Cmd; use super::Cmd;
use crate::{ use crate::{
args, args,
mode::{get_umask, Parser}, stat, mode::{get_umask, Parser},
stat,
}; };
use clap::{value_parser, Arg, ArgMatches, Command}; use clap::{value_parser, Arg, ArgMatches, Command};
use std::{convert::Infallible, error::Error, io, str::FromStr}; use std::{convert::Infallible, error::Error, io, str::FromStr};

View File

@ -2,7 +2,7 @@ use crate::unistd;
use super::Cmd; use super::Cmd;
use clap::{Arg, ArgAction, Command}; use clap::{Arg, ArgAction, Command};
use std::{error::Error, io, fs::OpenOptions}; use std::{error::Error, fs::OpenOptions, io};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct Sync; pub struct Sync;

View File

@ -1,6 +1,6 @@
use libc::utsname; use libc::utsname;
use sc::syscall; use sc::syscall;
use std::{ffi::CString, io, u8, fs::File, os::fd::AsRawFd}; use std::{ffi::CString, fs::File, io, os::fd::AsRawFd, u8};
#[inline(always)] #[inline(always)]
fn new_utsname() -> utsname { fn new_utsname() -> utsname {
@ -70,9 +70,20 @@ pub fn unlink(name: &str) -> io::Result<()> {
#[inline(always)] #[inline(always)]
pub fn readlink(name: &str) -> io::Result<String> { pub fn readlink(name: &str) -> io::Result<String> {
let mut buf = Vec::<i8>::with_capacity(1024); let mut buf = Vec::<i8>::with_capacity(1024);
let ret = unsafe { syscall!(READLINK, CString::new(name)?.as_ptr(), buf.as_mut_ptr(), 1024) }; let ret = unsafe {
syscall!(
READLINK,
CString::new(name)?.as_ptr(),
buf.as_mut_ptr(),
1024
)
};
if ret == 0 { if ret == 0 {
let path = buf.iter().map(|x| *x as u8).take_while(|x| *x != 0).collect(); let path = buf
.iter()
.map(|x| *x as u8)
.take_while(|x| *x != 0)
.collect();
String::from_utf8(path).map_err(|e| io::Error::new(io::ErrorKind::Other, e)) String::from_utf8(path).map_err(|e| io::Error::new(io::ErrorKind::Other, e))
} else { } else {
Err(io::Error::last_os_error()) Err(io::Error::last_os_error())
@ -113,3 +124,19 @@ pub fn fsync(fd: &File) -> io::Result<()> {
pub fn sync() { pub fn sync() {
unsafe { syscall!(SYNC) }; unsafe { syscall!(SYNC) };
} }
#[inline(always)]
pub fn symlink(source: &str, dest: &str) -> io::Result<()> {
let ret = unsafe {
syscall!(
SYMLINK,
CString::new(source)?.as_ptr(),
CString::new(dest)?.as_ptr()
)
};
if ret == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}