mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
tee: correctly handle writing to read-only files
This commit is contained in:
parent
5c36ed92b8
commit
8ab825c49f
2 changed files with 38 additions and 11 deletions
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use clap::{builder::PossibleValue, crate_version, Arg, ArgAction, Command};
|
use clap::{builder::PossibleValue, crate_version, Arg, ArgAction, Command};
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::{copy, sink, stdin, stdout, Error, ErrorKind, Read, Result, Write};
|
use std::io::{copy, stdin, stdout, Error, ErrorKind, Read, Result, Write};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::UResult;
|
use uucore::error::UResult;
|
||||||
|
@ -150,8 +150,9 @@ fn tee(options: &Options) -> Result<()> {
|
||||||
.files
|
.files
|
||||||
.clone()
|
.clone()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|file| open(file, options.append, options.output_error.as_ref()))
|
.filter_map(|file| open(file, options.append, options.output_error.as_ref()))
|
||||||
.collect::<Result<Vec<NamedWriter>>>()?;
|
.collect::<Result<Vec<NamedWriter>>>()?;
|
||||||
|
let had_open_errors = writers.len() != options.files.len();
|
||||||
|
|
||||||
writers.insert(
|
writers.insert(
|
||||||
0,
|
0,
|
||||||
|
@ -176,14 +177,21 @@ fn tee(options: &Options) -> Result<()> {
|
||||||
_ => Ok(()),
|
_ => Ok(()),
|
||||||
};
|
};
|
||||||
|
|
||||||
if res.is_err() || output.flush().is_err() || output.error_occurred() {
|
if had_open_errors || res.is_err() || output.flush().is_err() || output.error_occurred() {
|
||||||
Err(Error::from(ErrorKind::Other))
|
Err(Error::from(ErrorKind::Other))
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open(name: String, append: bool, output_error: Option<&OutputErrorMode>) -> Result<NamedWriter> {
|
/// Tries to open the indicated file and return it. Reports an error if that's not possible.
|
||||||
|
/// If that error should lead to program termination, this function returns Some(Err()),
|
||||||
|
/// otherwise it returns None.
|
||||||
|
fn open(
|
||||||
|
name: String,
|
||||||
|
append: bool,
|
||||||
|
output_error: Option<&OutputErrorMode>,
|
||||||
|
) -> Option<Result<NamedWriter>> {
|
||||||
let path = PathBuf::from(name.clone());
|
let path = PathBuf::from(name.clone());
|
||||||
let mut options = OpenOptions::new();
|
let mut options = OpenOptions::new();
|
||||||
let mode = if append {
|
let mode = if append {
|
||||||
|
@ -192,18 +200,15 @@ fn open(name: String, append: bool, output_error: Option<&OutputErrorMode>) -> R
|
||||||
options.truncate(true)
|
options.truncate(true)
|
||||||
};
|
};
|
||||||
match mode.write(true).create(true).open(path.as_path()) {
|
match mode.write(true).create(true).open(path.as_path()) {
|
||||||
Ok(file) => Ok(NamedWriter {
|
Ok(file) => Some(Ok(NamedWriter {
|
||||||
inner: Box::new(file),
|
inner: Box::new(file),
|
||||||
name,
|
name,
|
||||||
}),
|
})),
|
||||||
Err(f) => {
|
Err(f) => {
|
||||||
show_error!("{}: {}", name.maybe_quote(), f);
|
show_error!("{}: {}", name.maybe_quote(), f);
|
||||||
match output_error {
|
match output_error {
|
||||||
Some(OutputErrorMode::Exit | OutputErrorMode::ExitNoPipe) => Err(f),
|
Some(OutputErrorMode::Exit | OutputErrorMode::ExitNoPipe) => Some(Err(f)),
|
||||||
_ => Ok(NamedWriter {
|
_ => None,
|
||||||
inner: Box::new(sink()),
|
|
||||||
name,
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// For the full copyright and license information, please view the LICENSE
|
// For the full copyright and license information, please view the LICENSE
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
use crate::common::util::TestScenario;
|
use crate::common::util::TestScenario;
|
||||||
|
use regex::Regex;
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
|
@ -92,6 +93,27 @@ fn test_tee_no_more_writeable_1() {
|
||||||
assert_eq!(at.read(file_out), content);
|
assert_eq!(at.read(file_out), content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_readonly() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let content_tee = "hello";
|
||||||
|
let content_file = "world";
|
||||||
|
let file_out = "tee_file_out";
|
||||||
|
let writable_file = "tee_file_out2";
|
||||||
|
at.write(file_out, content_file);
|
||||||
|
at.set_readonly(file_out);
|
||||||
|
ucmd.arg(file_out)
|
||||||
|
.arg(writable_file)
|
||||||
|
.pipe_in(content_tee)
|
||||||
|
.ignore_stdin_write_error()
|
||||||
|
.fails()
|
||||||
|
.stdout_is(content_tee)
|
||||||
|
// Windows says "Access is denied" for some reason.
|
||||||
|
.stderr_matches(&Regex::new("(Permission|Access is) denied").unwrap());
|
||||||
|
assert_eq!(at.read(file_out), content_file);
|
||||||
|
assert_eq!(at.read(writable_file), content_tee);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(target_os = "linux")]
|
#[cfg(target_os = "linux")]
|
||||||
fn test_tee_no_more_writeable_2() {
|
fn test_tee_no_more_writeable_2() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue