From bcb5725a5c93682daca730723fa96b6d5a4e53be Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Thu, 2 Feb 2023 23:47:46 -0500 Subject: [PATCH] Add unistd::symlink function --- src/cmd/mknod/mod.rs | 3 ++- src/cmd/sync/mod.rs | 2 +- src/unistd/mod.rs | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/cmd/mknod/mod.rs b/src/cmd/mknod/mod.rs index e06efa5..c556380 100644 --- a/src/cmd/mknod/mod.rs +++ b/src/cmd/mknod/mod.rs @@ -1,7 +1,8 @@ use super::Cmd; use crate::{ args, - mode::{get_umask, Parser}, stat, + mode::{get_umask, Parser}, + stat, }; use clap::{value_parser, Arg, ArgMatches, Command}; use std::{convert::Infallible, error::Error, io, str::FromStr}; diff --git a/src/cmd/sync/mod.rs b/src/cmd/sync/mod.rs index 27d3d59..fd238c2 100644 --- a/src/cmd/sync/mod.rs +++ b/src/cmd/sync/mod.rs @@ -2,7 +2,7 @@ use crate::unistd; use super::Cmd; use clap::{Arg, ArgAction, Command}; -use std::{error::Error, io, fs::OpenOptions}; +use std::{error::Error, fs::OpenOptions, io}; #[derive(Debug, Default)] pub struct Sync; diff --git a/src/unistd/mod.rs b/src/unistd/mod.rs index ddecb19..565c36c 100644 --- a/src/unistd/mod.rs +++ b/src/unistd/mod.rs @@ -1,6 +1,6 @@ use libc::utsname; 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)] fn new_utsname() -> utsname { @@ -70,9 +70,20 @@ pub fn unlink(name: &str) -> io::Result<()> { #[inline(always)] pub fn readlink(name: &str) -> io::Result { let mut buf = Vec::::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 { - 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)) } else { Err(io::Error::last_os_error()) @@ -113,3 +124,19 @@ pub fn fsync(fd: &File) -> io::Result<()> { pub fn 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()) + } +}