Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions src/uu/tar/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,63 @@ pub enum TarError {
Io(#[from] io::Error),

/// Cannot read entries from archive
#[error("tar: Cannot read archive entries: {0}")]
#[error("Cannot read archive entries: {0}")]
CannotReadEntries(io::Error),

/// Cannot read an individual archive entry
#[error("tar: Cannot read archive entry: {0}")]
#[error("Cannot read archive entry: {0}")]
CannotReadEntry(io::Error),

/// Cannot read the path of an archive entry
#[error("tar: Cannot read entry path: {0}")]
#[error("Cannot read entry path: {0}")]
CannotReadEntryPath(io::Error),

/// Invalid archive format or unsupported compression stream
#[error("tar: {0}")]
#[error("{0}")]
InvalidArchive(String),

/// File or directory not found
#[error("tar: {path}: Cannot open: No such file or directory")]
#[error("{path}: Cannot open: No such file or directory")]
FileNotFound { path: PathBuf },

/// Permission denied when accessing file
#[error("tar: {path}: Cannot open: Permission denied")]
#[error("{path}: Cannot open: Permission denied")]
PermissionDenied { path: PathBuf },

/// Cannot create archive file
#[error("tar: Cannot create archive '{path}': {source}")]
#[error("Cannot create archive '{path}': {source}")]
CannotCreateArchive { path: PathBuf, source: io::Error },

/// Cannot add a directory to the archive
#[error("tar: Cannot add directory '{path}': {source}")]
#[error("Cannot add directory '{path}': {source}")]
CannotAddDirectory { path: PathBuf, source: io::Error },

/// Cannot add a file to the archive
#[error("tar: Cannot add file '{path}': {source}")]
#[error("Cannot add file '{path}': {source}")]
CannotAddFile { path: PathBuf, source: io::Error },

/// Cannot extract an archive entry
#[error("tar: Cannot extract '{path}': {source}")]
#[error("Cannot extract '{path}': {source}")]
CannotExtract { path: PathBuf, source: io::Error },

/// General tar operation error
#[error("tar: {0}")]
#[error("{0}")]
TarOperationError(String),

/// Cannot finalize the archive
#[error("tar: Cannot finalize archive: {0}")]
#[error("Cannot finalize archive: {0}")]
CannotFinalizeArchive(io::Error),

/// Refusing to write archive contents to terminal
#[error("tar: Refusing to write archive contents to terminal (missing -f option?)")]
#[error("Refusing to write archive contents to terminal (missing -f option?)")]
RefuseWriteArchiveToTerminal,

/// A member name with a `..` component, which is not extracted
#[error("tar: {path}: Member name contains '..'")]
#[error("{path}: Member name contains '..'")]
MemberNameContainsDotDot { path: PathBuf },

/// Errors that were reported earlier and recovered from
#[error("tar: Exiting with failure status due to previous errors")]
#[error("Exiting with failure status due to previous errors")]
PreviousErrors,
}

Expand All @@ -95,15 +95,15 @@ impl TarError {
/// Reports an error that tar recovers from: the operation carries on, and has to
/// end with [`finish_after_previous_errors`].
pub fn report_recoverable(err: &TarError) -> UResult<()> {
writeln!(io::stderr(), "{err}").map_err(TarError::Io)?;
writeln!(io::stderr(), "{}: {err}", uucore::util_name()).map_err(TarError::Io)?;
Ok(())
}

/// Ends an operation that reported recoverable errors with exit status 2, like
/// GNU tar. The status is set right before returning `Ok`, which is how `tarapp`
/// tells these errors apart from fatal ones.
pub fn finish_after_previous_errors() -> UResult<()> {
writeln!(io::stderr(), "{}", TarError::PreviousErrors).map_err(TarError::Io)?;
report_recoverable(&TarError::PreviousErrors)?;
set_exit_code(2);
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions src/uu/tar/tests/test_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ fn test_tar_error_display() {
};
assert_eq!(
err.to_string(),
"tar: test.txt: Cannot open: No such file or directory"
"test.txt: Cannot open: No such file or directory"
);

let err = TarError::PermissionDenied {
path: PathBuf::from("/root/file"),
};
assert_eq!(
err.to_string(),
"tar: /root/file: Cannot open: Permission denied"
"/root/file: Cannot open: Permission denied"
);
}

Expand Down Expand Up @@ -103,7 +103,7 @@ fn test_from_io_error_not_found() {
assert!(matches!(tar_err, TarError::FileNotFound { .. }));
assert_eq!(
tar_err.to_string(),
"tar: myfile.txt: Cannot open: No such file or directory"
"myfile.txt: Cannot open: No such file or directory"
);
}

Expand All @@ -115,7 +115,7 @@ fn test_from_io_error_permission_denied() {
assert!(matches!(tar_err, TarError::PermissionDenied { .. }));
assert_eq!(
tar_err.to_string(),
"tar: /root/secret: Cannot open: Permission denied"
"/root/secret: Cannot open: Permission denied"
);
}

Expand Down
3 changes: 2 additions & 1 deletion tests/by-util/test_tar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,8 @@ fn test_extract_nonexistent_archive() {
.args(&["-xf", "nonexistent.tar"])
.fails()
.code_is(2)
.stderr_contains("nonexistent.tar");
.stderr_contains(": nonexistent.tar: Cannot open")
.stderr_does_not_contain(": tar: ");
}

#[test]
Expand Down
Loading