|
| 1 | +use core::{any::Any, ffi::c_int}; |
| 2 | + |
| 3 | +use alloc::{string::String, sync::Arc}; |
| 4 | +use axerrno::{LinuxError, LinuxResult}; |
| 5 | +use axio::PollState; |
| 6 | +use axsync::{Mutex, MutexGuard}; |
| 7 | +use linux_raw_sys::general::S_IFDIR; |
| 8 | + |
| 9 | +use super::{FileLike, Kstat, get_file_like}; |
| 10 | + |
| 11 | +/// File wrapper for `axfs::fops::File`. |
| 12 | +pub struct File { |
| 13 | + inner: Mutex<axfs::fops::File>, |
| 14 | + path: String, |
| 15 | +} |
| 16 | + |
| 17 | +impl File { |
| 18 | + pub fn new(inner: axfs::fops::File, path: String) -> Self { |
| 19 | + Self { |
| 20 | + inner: Mutex::new(inner), |
| 21 | + path, |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + /// Get the path of the file. |
| 26 | + pub fn path(&self) -> &str { |
| 27 | + &self.path |
| 28 | + } |
| 29 | + |
| 30 | + /// Get the inner node of the file. |
| 31 | + pub fn inner(&self) -> MutexGuard<axfs::fops::File> { |
| 32 | + self.inner.lock() |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl FileLike for File { |
| 37 | + fn read(&self, buf: &mut [u8]) -> LinuxResult<usize> { |
| 38 | + Ok(self.inner().read(buf)?) |
| 39 | + } |
| 40 | + |
| 41 | + fn write(&self, buf: &[u8]) -> LinuxResult<usize> { |
| 42 | + Ok(self.inner().write(buf)?) |
| 43 | + } |
| 44 | + |
| 45 | + fn stat(&self) -> LinuxResult<Kstat> { |
| 46 | + let metadata = self.inner().get_attr()?; |
| 47 | + let ty = metadata.file_type() as u8; |
| 48 | + let perm = metadata.perm().bits() as u32; |
| 49 | + |
| 50 | + Ok(Kstat { |
| 51 | + mode: ((ty as u32) << 12) | perm, |
| 52 | + size: metadata.size(), |
| 53 | + blocks: metadata.blocks(), |
| 54 | + blksize: 512, |
| 55 | + ..Default::default() |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> { |
| 60 | + self |
| 61 | + } |
| 62 | + |
| 63 | + fn poll(&self) -> LinuxResult<PollState> { |
| 64 | + Ok(PollState { |
| 65 | + readable: true, |
| 66 | + writable: true, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + fn set_nonblocking(&self, _nonblocking: bool) -> LinuxResult { |
| 71 | + Ok(()) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Directory wrapper for `axfs::fops::Directory`. |
| 76 | +pub struct Directory { |
| 77 | + inner: Mutex<axfs::fops::Directory>, |
| 78 | + path: String, |
| 79 | +} |
| 80 | + |
| 81 | +impl Directory { |
| 82 | + pub fn new(inner: axfs::fops::Directory, path: String) -> Self { |
| 83 | + Self { |
| 84 | + inner: Mutex::new(inner), |
| 85 | + path, |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /// Get the path of the directory. |
| 90 | + pub fn path(&self) -> &str { |
| 91 | + &self.path |
| 92 | + } |
| 93 | + |
| 94 | + /// Get the inner node of the directory. |
| 95 | + pub fn inner(&self) -> MutexGuard<axfs::fops::Directory> { |
| 96 | + self.inner.lock() |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl FileLike for Directory { |
| 101 | + fn read(&self, _buf: &mut [u8]) -> LinuxResult<usize> { |
| 102 | + Err(LinuxError::EBADF) |
| 103 | + } |
| 104 | + |
| 105 | + fn write(&self, _buf: &[u8]) -> LinuxResult<usize> { |
| 106 | + Err(LinuxError::EBADF) |
| 107 | + } |
| 108 | + |
| 109 | + fn stat(&self) -> LinuxResult<Kstat> { |
| 110 | + Ok(Kstat { |
| 111 | + mode: S_IFDIR | 0o755u32, // rwxr-xr-x |
| 112 | + ..Default::default() |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + fn into_any(self: Arc<Self>) -> Arc<dyn Any + Send + Sync> { |
| 117 | + self |
| 118 | + } |
| 119 | + |
| 120 | + fn poll(&self) -> LinuxResult<PollState> { |
| 121 | + Ok(PollState { |
| 122 | + readable: true, |
| 123 | + writable: false, |
| 124 | + }) |
| 125 | + } |
| 126 | + |
| 127 | + fn set_nonblocking(&self, _nonblocking: bool) -> LinuxResult { |
| 128 | + Ok(()) |
| 129 | + } |
| 130 | + |
| 131 | + fn from_fd(fd: c_int) -> LinuxResult<Arc<Self>> { |
| 132 | + get_file_like(fd)? |
| 133 | + .into_any() |
| 134 | + .downcast::<Self>() |
| 135 | + .map_err(|_| LinuxError::ENOTDIR) |
| 136 | + } |
| 137 | +} |
0 commit comments