Skip to content

Commit

Permalink
Merge pull request #160 from ids1024/stat
Browse files Browse the repository at this point in the history
Use libstd instead of syscall for stat
  • Loading branch information
jackpot51 committed Jul 7, 2017
2 parents 546e6c2 + 99db1b1 commit c956b6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
1 change: 0 additions & 1 deletion src/bin/readlink.rs
Expand Up @@ -2,7 +2,6 @@

extern crate coreutils;
extern crate extra;
extern crate syscall;

use std::env;
use std::fs;
Expand Down
38 changes: 19 additions & 19 deletions src/bin/stat.rs
Expand Up @@ -2,12 +2,12 @@

extern crate coreutils;
extern crate extra;
extern crate syscall;

use std::{env, fmt, fs};
use std::io::{stdout, stderr, Write};
use coreutils::ArgParser;
use extra::option::OptionalExt;
use std::os::unix::fs::MetadataExt;

const MAN_PAGE: &'static str = /* @MANSTART{stat} */ r#"
NAME
Expand All @@ -25,13 +25,13 @@ OPTIONS
"#; /* @MANEND */


struct Perms(u16);
struct Perms(u32);

impl fmt::Display for Perms {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(0{:o}/", self.0 & 0o777)?;
let perm = |i, c| {
if self.0 & ((1 << i) as u16) != 0 {
if self.0 & ((1 << i) as u32) != 0 {
c
} else {
"-"
Expand Down Expand Up @@ -60,26 +60,26 @@ fn main() {
}

for path in &parser.args[0..] {
let mut st = syscall::Stat::default();
let fd = syscall::open(path, syscall::O_CLOEXEC | syscall::O_STAT | syscall::O_NOFOLLOW).unwrap();
syscall::fstat(fd, &mut st).unwrap();
syscall::close(fd).unwrap();
let file_type = match st.st_mode & syscall::MODE_TYPE {
syscall::MODE_FILE => "regular file",
syscall::MODE_DIR => "directory",
syscall::MODE_SYMLINK => "symbolic link",
_ => ""
let meta = fs::symlink_metadata(path).unwrap();
let file_type = if meta.file_type().is_symlink() {
"symbolic link"
} else if meta.is_file() {
"regular file"
} else if meta.is_dir() {
"directory"
} else {
""
};
if st.st_mode & syscall::MODE_SYMLINK == syscall::MODE_SYMLINK {
if meta.file_type().is_symlink() {
println!("File: {} -> {}", path, fs::read_link(path).unwrap().display());
} else {
println!("File: {}", path);
}
println!("Size: {} Blocks: {} IO Block: {} {}", st.st_size, st.st_blocks, st.st_blksize, file_type);
println!("Device: {} Inode: {} Links: {}", st.st_dev, st.st_ino, st.st_nlink);
println!("Access: {} Uid: {} Gid: {}", Perms(st.st_mode), st.st_uid, st.st_gid);
println!("Access: {}.{:09}", st.st_atime, st.st_atime_nsec);
println!("Modify: {}.{:09}", st.st_mtime, st.st_mtime_nsec);
println!("Change: {}.{:09}", st.st_ctime, st.st_ctime_nsec);
println!("Size: {} Blocks: {} IO Block: {} {}", meta.size(), meta.blocks(), meta.blksize(), file_type);
println!("Device: {} Inode: {} Links: {}", meta.dev(), meta.ino(), meta.nlink());
println!("Access: {} Uid: {} Gid: {}", Perms(meta.mode()), meta.uid(), meta.gid());
println!("Access: {}.{:09}", meta.atime(), meta.atime_nsec());
println!("Modify: {}.{:09}", meta.mtime(), meta.mtime_nsec());
println!("Change: {}.{:09}", meta.ctime(), meta.ctime_nsec());
}
}

0 comments on commit c956b6b

Please sign in to comment.