From 6179b89bcc9e9d4603c557d8976ba0c96072913d Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Sat, 4 Jun 2016 14:50:52 +0100 Subject: [PATCH] Add conversion of UID to username for `ls` --- src/ls/ls.rs | 89 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/src/ls/ls.rs b/src/ls/ls.rs index 3c7c4e58f..775f61277 100644 --- a/src/ls/ls.rs +++ b/src/ls/ls.rs @@ -8,37 +8,40 @@ * For the full copyright and license information, please view the LICENSE file * that was distributed with this source code. */ - + extern crate getopts; extern crate pretty_bytes; use pretty_bytes::converter::convert; - + #[macro_use] extern crate uucore; - + use getopts::Options; use std::fs; use std::fs::{ReadDir, DirEntry, FileType, Metadata}; -use std::ffi::{OsString}; +use std::ffi::{OsString,CStr}; use std::path::Path; use std::io::Write; - +use std::ptr; +use uucore::c_types::getpwuid; + + #[derive(Copy, Clone, PartialEq)] enum Mode { Help, Version, List } - + static NAME: &'static str = "ls"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); - + pub fn uumain(args: Vec) -> i32 { let mut opts = Options::new(); - + opts.optflag("", "help", "display this help and exit"); opts.optflag("", "version", "output version information and exit"); - + opts.optflag("a", "all", "Do not ignore hidden files (files with names that start with '.')."); opts.optflag("A", "almost-all", "In a directory, do not ignore all file names that start with '.', only ignore '.' and '..'."); opts.optflag("B", "ignore-backups", "Ignore files that end with ~. Equivalent to using `--ignore='*~'` or `--ignore='.*~'."); @@ -53,7 +56,7 @@ pub fn uumain(args: Vec) -> i32 { panic!() }, }; - + let mode = if matches.opt_present("version") { Mode::Version } else if matches.opt_present("help") { @@ -61,20 +64,20 @@ pub fn uumain(args: Vec) -> i32 { } else { Mode::List }; - + match mode { Mode::Version => version(), Mode::Help => help(), Mode::List => list(matches) } - + 0 } - + fn version() { println!("{} {}", NAME, VERSION); } - + fn help() { let msg = format!("{0} {1}\n\n\ Usage: {0} [OPTION]... DIRECTORY \n \ @@ -93,15 +96,15 @@ fn list(options: getopts::Matches) { } else { options.free.iter().cloned().collect() }; - + for loc in locs { let p = Path::new(&loc); - + if !p.exists() { show_error!("Cannot find path '{}' because it does not exist.", loc); panic!(); } - + if p.is_dir() { match fs::read_dir(p) { Err(e) => { @@ -110,8 +113,8 @@ fn list(options: getopts::Matches) { }, Ok(entries) => enter_directory(entries, &options), }; - } - + } + if p.is_file() { display_item(Path::new(p), &options) } @@ -127,31 +130,52 @@ fn enter_directory(contents: ReadDir, options: &getopts::Matches) { }, Ok(en) => en }; - + // Currently have a DirEntry that we can believe in. display_dir_entry(entry, options); - } + } } fn display_dir_entry(entry: DirEntry, options: &getopts::Matches) { let md = match entry.metadata() { Err(e) => { - show_error!("Unable to retrieve metadata for {}. \n Error: {}", + show_error!("Unable to retrieve metadata for {}. \n Error: {}", display_file_name(entry.file_name()), e); panic!(); }, Ok(md) => md }; - - println!(" {}{} {} somebody somegroup {: >9} {}", + + println!(" {}{} {} {} {} {: >9} {}", display_file_type(entry.file_type()), display_permissions(&md), display_symlink_count(&md), + display_uname(&md), + display_group(&md), display_file_size(&md, options), display_file_name(entry.file_name()) ); } +fn display_uname(metadata: &Metadata) -> String { + use std::os::unix::fs::MetadataExt; + + let pw = unsafe { getpwuid(metadata.uid()) }; + if !pw.is_null() { + let pw_name = unsafe { + String::from_utf8_lossy(CStr::from_ptr(ptr::read(pw).pw_name).to_bytes()).to_string() + }; + pw_name + } else { + metadata.uid().to_string() + } +} + +fn display_group(metadata: &Metadata) -> String { + use std::os::unix::fs::MetadataExt; + metadata.gid().to_string() +} + fn display_file_size(metadata: &Metadata, options: &getopts::Matches) -> String { if options.opt_present("human-readable") { convert(metadata.len() as f64) @@ -166,9 +190,9 @@ fn display_file_type(file_type: Result) -> String { show_error!("{}", e); panic!() }, - Ok(ft) => ft + Ok(ft) => ft }; - + if file_type.is_dir() { "d".to_string() } else if file_type.is_symlink() { @@ -193,7 +217,6 @@ fn display_symlink_count(metadata: &Metadata) -> String { #[cfg(target_family = "unix")] fn display_symlink_count(metadata: &Metadata) -> String { use std::os::unix::fs::MetadataExt; - metadata.nlink().to_string() } @@ -204,7 +227,7 @@ fn display_permissions(metadata: &Metadata) -> String { } #[cfg(target_family = "unix")] - + fn display_permissions(_metadata: &Metadata) -> String { //use std::os::unix::fs::PermissionsExt; "xxxxxxxxx".to_string() @@ -213,15 +236,15 @@ fn display_permissions(_metadata: &Metadata) -> String { fn display_item(item: &Path, options: &getopts::Matches) { // let fileType = item.file // let mut fileMeta = String::new(); - + // fileMeta = fileMeta + if item.is_dir() { // "d" // } else if item.sy // } else { // "-" // }; - - - + + + // println!("{}{}", displayString, item.display()); -} \ No newline at end of file +}