fix struct stat to match Linux ABI

master
WangRunji 6 years ago
parent cab19688d5
commit 70a950bf40

@ -3,6 +3,7 @@
use super::*;
use crate::fs::{ROOT_INODE, OpenOptions};
use rcore_fs::vfs::Timespec;
pub fn sys_read(fd: usize, base: *mut u8, len: usize) -> SysResult {
info!("read: fd: {}, base: {:?}, len: {:#x}", fd, base, len);
@ -185,49 +186,126 @@ impl DirEntry {
#[repr(C)]
pub struct Stat {
/// protection mode and file type
mode: StatMode,
/// ID of device containing file
dev: u64,
/// inode number
ino: u64,
/// number of hard links
nlinks: u32,
/// number of blocks file is using
blocks: u32,
/// file size (bytes)
size: u32,
nlink: u64,
/// file type and mode
mode: StatMode,
/// 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! {
pub struct StatMode: u32 {
const NULL = 0;
/// ordinary regular file
const FILE = 0o10000;
/// directory
const DIR = 0o20000;
/// symbolic link
const LINK = 0o30000;
/// Type
const TYPE_MASK = 0o170000;
/// FIFO
const FIFO = 0o010000;
/// character device
const CHAR = 0o40000;
const CHAR = 0o020000;
/// directory
const DIR = 0o040000;
/// 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 {
fn from(info: Metadata) -> Self {
Stat {
mode: match info.type_ {
FileType::File => StatMode::FILE,
FileType::Dir => StatMode::DIR,
FileType::SymLink => StatMode::LINK,
FileType::CharDevice => StatMode::CHAR,
FileType::BlockDevice => StatMode::BLOCK,
_ => StatMode::NULL,
//Note: we should mark FileType as #[non_exhaustive]
// but it is currently not implemented for enum
// see rust-lang/rust#44109
},
nlinks: info.nlinks as u32,
blocks: info.blocks as u32,
size: info.size as u32,
dev: info.dev as u64,
ino: info.inode as u64,
mode: StatMode::from_type_mode(info.type_, info.mode as u16),
nlink: info.nlinks as u64,
uid: info.uid as u32,
gid: info.gid as u32,
rdev: 0,
size: info.size as u64,
blksize: info.blk_size as u64,
blocks: info.blocks as u64,
atime: info.atime,
mtime: info.mtime,
ctime: info.ctime,
_pad0: 0
}
}
}

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