unistd: add umask()

This commit is contained in:
Mark Poliakov 2024-01-16 16:12:13 +02:00
parent 4a4e4d8d13
commit bfad8bb567
4 changed files with 39 additions and 6 deletions

View File

@ -140,6 +140,9 @@ unsafe extern "C" fn fcntl(fd: c_int, cmd: c_int, args: ...) -> CIntCountResult
let file = RawFile::e_try_from(fd)?; let file = RawFile::e_try_from(fd)?;
match cmd { match cmd {
// TODO implement these somehow?
F_GETFD => CIntCountResult(0),
F_SETFD => CIntCountResult(0),
_ => todo!("fcntl({}, {}, ...)", fd, cmd), _ => todo!("fcntl({}, {}, ...)", fd, cmd),
} }
} }

View File

@ -4,7 +4,7 @@ use yggdrasil_rt::io::{FileAttr, FileMode, FileType, RawFd};
use crate::{ use crate::{
error::{CIntZeroResult, TryFromExt}, error::{CIntZeroResult, TryFromExt},
io, io, process,
util::{self, Nullable}, util::{self, Nullable},
}; };
@ -74,8 +74,8 @@ impl From<FileAttr> for stat {
} }
st.st_size = value.size.try_into().unwrap(); st.st_size = value.size.try_into().unwrap();
// TODO // TODO
st.st_uid = 0; st.st_uid = u32::from(value.uid).try_into().unwrap();
st.st_gid = 0; st.st_gid = u32::from(value.gid).try_into().unwrap();
// TODO // TODO
st.st_blksize = 512; st.st_blksize = 512;
st.st_blocks = (st.st_size + 511) / 512; st.st_blocks = (st.st_size + 511) / 512;
@ -108,7 +108,9 @@ unsafe extern "C" fn fchmodat(
#[no_mangle] #[no_mangle]
unsafe extern "C" fn umask(mode: mode_t) -> mode_t { unsafe extern "C" fn umask(mode: mode_t) -> mode_t {
todo!() let new = FileMode::new((mode as u32) & 0o777);
let old = process::update_umask(new);
old.bits() as mode_t
} }
// Create stuff // Create stuff

View File

@ -150,6 +150,7 @@ impl<W: Write> Write for BufWriter<W> {
fn write(&mut self, mut data: &[u8]) -> EResult<usize> { fn write(&mut self, mut data: &[u8]) -> EResult<usize> {
if data.len() + self.buffer.len() >= self.buffer.capacity() { if data.len() + self.buffer.len() >= self.buffer.capacity() {
self.flush()?; self.flush()?;
assert_eq!(self.buffer.len(), 0);
} }
let len = data.len(); let len = data.len();
@ -167,7 +168,7 @@ impl<W: Write> Write for BufWriter<W> {
} }
// Store the data in the buffer // Store the data in the buffer
assert!(data.len() < self.buffer.capacity()); assert!(data.len() <= self.buffer.capacity());
self.buffer.extend_from_slice(data); self.buffer.extend_from_slice(data);
EResult::Ok(len) EResult::Ok(len)
} }

View File

@ -2,7 +2,8 @@ use core::{ffi::c_char, ptr::null_mut, slice::memchr};
use alloc::{ffi::CString, vec::Vec}; use alloc::{ffi::CString, vec::Vec};
use yggdrasil_rt::{ use yggdrasil_rt::{
process::{ExitCode, ProgramArgumentInner, Signal}, io::FileMode,
process::{ExitCode, ProcessInfoElement, ProgramArgumentInner, Signal},
sys as syscall, sys as syscall,
}; };
@ -81,6 +82,32 @@ pub fn abort() -> ! {
} }
} }
pub fn get_process_info(info: &mut ProcessInfoElement) -> EResult<()> {
unsafe {
syscall::get_process_info(info)?;
}
EResult::Ok(())
}
pub fn set_process_info(info: &ProcessInfoElement) -> EResult<()> {
unsafe {
syscall::set_process_info(info)?;
}
EResult::Ok(())
}
pub fn update_umask(new: FileMode) -> FileMode {
let mut old = ProcessInfoElement::Umask(FileMode::empty());
let new = ProcessInfoElement::Umask(new);
get_process_info(&mut old).unwrap();
set_process_info(&new).unwrap();
let ProcessInfoElement::Umask(old) = old;
old
}
// Env // Env
static mut ENVS: Vec<CString> = Vec::new(); static mut ENVS: Vec<CString> = Vec::new();