sys_unlink.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //! System call handler for unlinking files (unlink).
  2. use crate::arch::interrupt::TrapFrame;
  3. use crate::arch::syscall::nr::SYS_UNLINK;
  4. use crate::filesystem::vfs::syscall::AtFlags;
  5. use crate::filesystem::vfs::vcore::do_unlink_at;
  6. use crate::filesystem::vfs::MAX_PATHLEN;
  7. use crate::syscall::table::{FormattedSyscallParam, Syscall};
  8. use crate::syscall::user_access::check_and_clone_cstr;
  9. use alloc::vec::Vec;
  10. use system_error::SystemError;
  11. pub struct SysUnlinkHandle;
  12. impl Syscall for SysUnlinkHandle {
  13. /// Returns the number of arguments this syscall takes.
  14. fn num_args(&self) -> usize {
  15. 1
  16. }
  17. /// Handles the unlink syscall.
  18. fn handle(&self, args: &[usize], _frame: &mut TrapFrame) -> Result<usize, SystemError> {
  19. let path = Self::path(args);
  20. let path = check_and_clone_cstr(path, Some(MAX_PATHLEN))?
  21. .into_string()
  22. .map_err(|_| SystemError::EINVAL)?;
  23. return do_unlink_at(AtFlags::AT_FDCWD.bits(), &path).map(|v| v as usize);
  24. }
  25. /// Formats the syscall arguments for display/debugging purposes.
  26. fn entry_format(&self, args: &[usize]) -> Vec<FormattedSyscallParam> {
  27. vec![FormattedSyscallParam::new(
  28. "path",
  29. format!("{:#x}", Self::path(args) as usize),
  30. )]
  31. }
  32. }
  33. impl SysUnlinkHandle {
  34. /// Extracts the path argument from syscall parameters.
  35. fn path(args: &[usize]) -> *const u8 {
  36. args[0] as *const u8
  37. }
  38. }
  39. syscall_table_macros::declare_syscall!(SYS_UNLINK, SysUnlinkHandle);