Skip to content

Commit

Permalink
uucore/fs: change FileInformation::from_file to return IOResult as well
Browse files Browse the repository at this point in the history
  • Loading branch information
niyaznigmatullin committed Jul 7, 2022
1 parent 1403a37 commit 6e68272
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/uu/cat/src/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ fn cat_path(

if let Some(out_info) = out_info {
if out_info.file_size() != 0
&& FileInformation::from_file(&file).as_ref() == Some(out_info)
&& FileInformation::from_file(&file).ok().as_ref() == Some(out_info)
{
return Err(CatError::OutputIsInput);
}
Expand All @@ -367,7 +367,7 @@ fn cat_path(
}

fn cat_files(files: &[String], options: &OutputOptions) -> UResult<()> {
let out_info = FileInformation::from_file(&std::io::stdout());
let out_info = FileInformation::from_file(&std::io::stdout()).ok();

let mut state = OutputState {
line_number: 1,
Expand Down
21 changes: 8 additions & 13 deletions src/uucore/src/lib/features/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,16 @@ pub struct FileInformation(
impl FileInformation {
/// Get information from a currently open file
#[cfg(unix)]
pub fn from_file(file: &impl AsRawFd) -> Option<Self> {
if let Ok(x) = nix::sys::stat::fstat(file.as_raw_fd()) {
Some(Self(x))
} else {
None
}
pub fn from_file(file: &impl AsRawFd) -> IOResult<Self> {
let stat = nix::sys::stat::fstat(file.as_raw_fd())?;
Ok(Self(stat))
}

/// Get information from a currently open file
#[cfg(target_os = "windows")]
pub fn from_file(file: &impl AsHandleRef) -> Option<Self> {
if let Ok(x) = winapi_util::file::information(file.as_handle_ref()) {
Some(Self(x))
} else {
None
}
pub fn from_file(file: &impl AsHandleRef) -> IOResult<Self> {
let info = winapi_util::file::information(file.as_handle_ref())?;
Ok(Self(info))
}

/// Get information for a given path.
Expand All @@ -88,7 +82,8 @@ impl FileInformation {
if !dereference {
open_options.custom_flags(winapi::um::winbase::FILE_FLAG_OPEN_REPARSE_POINT);
}
Self::from_file(open_options.read(true).open(path.as_ref())?)
let file = open_options.read(true).open(path.as_ref())?;
Self::from_file(&file)
}
}

Expand Down

0 comments on commit 6e68272

Please sign in to comment.