mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
cat: use FileInformation from uucore
This commit is contained in:
parent
bb35b0c37b
commit
8696193b66
1 changed files with 12 additions and 24 deletions
|
@ -20,6 +20,7 @@ use std::io::{self, Read, Write};
|
|||
use thiserror::Error;
|
||||
use uucore::display::Quotable;
|
||||
use uucore::error::UResult;
|
||||
use uucore::fs::FileInformation;
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::io::AsRawFd;
|
||||
|
@ -317,8 +318,7 @@ fn cat_path(
|
|||
path: &str,
|
||||
options: &OutputOptions,
|
||||
state: &mut OutputState,
|
||||
#[cfg(unix)] out_info: &nix::sys::stat::FileStat,
|
||||
#[cfg(windows)] out_info: &winapi_util::file::Information,
|
||||
out_info: Option<&FileInformation>,
|
||||
) -> CatResult<()> {
|
||||
if path == "-" {
|
||||
let stdin = io::stdin();
|
||||
|
@ -342,10 +342,15 @@ fn cat_path(
|
|||
}
|
||||
_ => {
|
||||
let file = File::open(path)?;
|
||||
#[cfg(any(windows, unix))]
|
||||
if same_file(out_info, &file) {
|
||||
|
||||
if let Some(out_info) = out_info {
|
||||
if out_info.file_size() != 0
|
||||
&& FileInformation::from_file(&file).as_ref() == Some(out_info)
|
||||
{
|
||||
return Err(CatError::OutputIsInput);
|
||||
}
|
||||
}
|
||||
|
||||
let mut handle = InputHandle {
|
||||
reader: file,
|
||||
is_interactive: false,
|
||||
|
@ -355,25 +360,8 @@ fn cat_path(
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn same_file(a_info: &nix::sys::stat::FileStat, b: &File) -> bool {
|
||||
let b_info = nix::sys::stat::fstat(b.as_raw_fd()).unwrap();
|
||||
b_info.st_size != 0 && b_info.st_dev == a_info.st_dev && b_info.st_ino == a_info.st_ino
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn same_file(a_info: &winapi_util::file::Information, b: &File) -> bool {
|
||||
let b_info = winapi_util::file::information(b).unwrap();
|
||||
b_info.file_size() != 0
|
||||
&& b_info.volume_serial_number() == a_info.volume_serial_number()
|
||||
&& b_info.file_index() == a_info.file_index()
|
||||
}
|
||||
|
||||
fn cat_files(files: Vec<String>, options: &OutputOptions) -> UResult<()> {
|
||||
#[cfg(windows)]
|
||||
let out_info = winapi_util::file::information(&std::io::stdout()).unwrap();
|
||||
#[cfg(unix)]
|
||||
let out_info = nix::sys::stat::fstat(std::io::stdout().as_raw_fd()).unwrap();
|
||||
let out_info = FileInformation::from_file(&std::io::stdout());
|
||||
|
||||
let mut state = OutputState {
|
||||
line_number: 1,
|
||||
|
@ -384,7 +372,7 @@ fn cat_files(files: Vec<String>, options: &OutputOptions) -> UResult<()> {
|
|||
let mut error_messages: Vec<String> = Vec::new();
|
||||
|
||||
for path in &files {
|
||||
if let Err(err) = cat_path(path, options, &mut state, &out_info) {
|
||||
if let Err(err) = cat_path(path, options, &mut state, out_info.as_ref()) {
|
||||
error_messages.push(format!("{}: {}", path.maybe_quote(), err));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue