fix struct stat to match Linux ABI

master
WangRunji 6 years ago
parent cab19688d5
commit 70a950bf40

@ -3,6 +3,7 @@
use super::*; use super::*;
use crate::fs::{ROOT_INODE, OpenOptions}; use crate::fs::{ROOT_INODE, OpenOptions};
use rcore_fs::vfs::Timespec;
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult { pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len); info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
@ -185,49 +186,126 @@ impl DirEntry {
#[repr(C)] #[repr(C)]
pub struct Stat { pub struct Stat {
/// protection mode and file type /// ID of device containing file
mode: StatMode, dev: u64,
/// inode number
ino: u64,
/// number of hard links /// number of hard links
nlinks: u32, nlink: u64,
/// number of blocks file is using
blocks: u32, /// file type and mode
/// file size (bytes) mode: StatMode,
size: u32, /// user ID of owner
uid: u32,
/// group ID of owner
gid: u32,
/// padding
_pad0: u32,
/// device ID (if special file)
rdev: u64,
/// total size, in bytes
size: u64,
/// blocksize for filesystem I/O
blksize: u64,
/// number of 512B blocks allocated
blocks: u64,
/// last access time
atime: Timespec,
/// last modification time
mtime: Timespec,
/// last status change time
ctime: Timespec,
} }
bitflags! { bitflags! {
pub struct StatMode: u32 { pub struct StatMode: u32 {
const NULL = 0; const NULL = 0;
/// ordinary regular file /// Type
const FILE = 0o10000; const TYPE_MASK = 0o170000;
/// directory /// FIFO
const DIR = 0o20000; const FIFO = 0o010000;
/// symbolic link
const LINK = 0o30000;
/// character device /// character device
const CHAR = 0o40000; const CHAR = 0o020000;
/// directory
const DIR = 0o040000;
/// block device /// block device
const BLOCK = 0o50000; const BLOCK = 0o060000;
/// ordinary regular file
const FILE = 0o100000;
/// symbolic link
const LINK = 0o120000;
/// socket
const SOCKET = 0o140000;
/// Set-user-ID on execution.
const SET_UID = 0o4000;
/// Set-group-ID on execution.
const SET_GID = 0o2000;
/// Read, write, execute/search by owner.
const OWNER_MASK = 0o700;
/// Read permission, owner.
const OWNER_READ = 0o400;
/// Write permission, owner.
const OWNER_WRITE = 0o200;
/// Execute/search permission, owner.
const OWNER_EXEC = 0o100;
/// Read, write, execute/search by group.
const GROUP_MASK = 0o70;
/// Read permission, group.
const GROUP_READ = 0o40;
/// Write permission, group.
const GROUP_WRITE = 0o20;
/// Execute/search permission, group.
const GROUP_EXEC = 0o10;
/// Read, write, execute/search by others.
const OTHER_MASK = 0o7;
/// Read permission, others.
const OTHER_READ = 0o4;
/// Write permission, others.
const OTHER_WRITE = 0o2;
/// Execute/search permission, others.
const OTHER_EXEC = 0o1;
}
}
impl StatMode {
fn from_type_mode(type_: FileType, mode: u16) -> Self {
let type_ = match type_ {
FileType::File => StatMode::FILE,
FileType::Dir => StatMode::DIR,
FileType::SymLink => StatMode::LINK,
FileType::CharDevice => StatMode::CHAR,
FileType::BlockDevice => StatMode::BLOCK,
FileType::Socket => StatMode::SOCKET,
FileType::NamedPipe => StatMode::FIFO,
_ => StatMode::NULL,
};
let mode = StatMode::from_bits_truncate(mode as u32);
type_ | mode
} }
} }
impl From<Metadata> for Stat { impl From<Metadata> for Stat {
fn from(info: Metadata) -> Self { fn from(info: Metadata) -> Self {
Stat { Stat {
mode: match info.type_ { dev: info.dev as u64,
FileType::File => StatMode::FILE, ino: info.inode as u64,
FileType::Dir => StatMode::DIR, mode: StatMode::from_type_mode(info.type_, info.mode as u16),
FileType::SymLink => StatMode::LINK, nlink: info.nlinks as u64,
FileType::CharDevice => StatMode::CHAR, uid: info.uid as u32,
FileType::BlockDevice => StatMode::BLOCK, gid: info.gid as u32,
_ => StatMode::NULL, rdev: 0,
//Note: we should mark FileType as #[non_exhaustive] size: info.size as u64,
// but it is currently not implemented for enum blksize: info.blk_size as u64,
// see rust-lang/rust#44109 blocks: info.blocks as u64,
}, atime: info.atime,
nlinks: info.nlinks as u32, mtime: info.mtime,
blocks: info.blocks as u32, ctime: info.ctime,
size: info.size as u32, _pad0: 0
} }
} }
} }

@ -1 +1 @@
Subproject commit d84f21465c97dbcdbc998be72f3431de3b077fb0 Subproject commit cb1bd5b835965f6a8502d336592aafe06496bdc1
Loading…
Cancel
Save