1 //! Contains Tar-specific building and unpacking functions
7 sync::mpsc::{self, Receiver},
12 use same_file::Handle;
13 use ubyte::ToByteUnit;
19 utils::{self, EscapedPathDisplay, FileVisibilityPolicy},
23 /// Unpacks the archive given by `archive` into the folder given by `into`.
24 /// Assumes that output_folder is empty
25 pub fn unpack_archive(reader: Box<dyn Read>, output_folder: &Path, quiet: bool) -> crate::Result<usize> {
26 assert!(output_folder.read_dir().expect("dir exists").count() == 0);
27 let mut archive = tar::Archive::new(reader);
29 let mut files_unpacked = 0;
30 for file in archive.entries()? {
33 file.unpack_in(output_folder)?;
35 // This is printed for every file in the archive and has little
36 // importance for most users, but would generate lots of
37 // spoken text for users using screen readers, braille displays
42 "{:?} extracted. ({})",
43 utils::strip_cur_dir(&output_folder.join(file.path()?)),
54 /// List contents of `archive`, returning a vector of archive entries
56 mut archive: tar::Archive<impl Read + Send + 'static>,
57 ) -> impl Iterator<Item = crate::Result<FileInArchive>> {
58 struct Files(Receiver<crate::Result<FileInArchive>>);
59 impl Iterator for Files {
60 type Item = crate::Result<FileInArchive>;
62 fn next(&mut self) -> Option<Self::Item> {
67 let (tx, rx) = mpsc::channel();
68 thread::spawn(move || {
69 for file in archive.entries().expect("entries is only used once") {
70 let file_in_archive = (|| {
72 let path = file.path()?.into_owned();
73 let is_dir = file.header().entry_type().is_dir();
74 Ok(FileInArchive { path, is_dir })
76 tx.send(file_in_archive).unwrap();
83 /// Compresses the archives given by `input_filenames` into the file given previously to `writer`.
84 pub fn build_archive_from_paths<W>(
85 input_filenames: &[PathBuf],
88 file_visibility_policy: FileVisibilityPolicy,
94 let mut builder = tar::Builder::new(writer);
95 let output_handle = Handle::from_path(output_path);
97 for filename in input_filenames {
98 let previous_location = utils::cd_into_same_dir_as(filename)?;
100 // Safe unwrap, input shall be treated before
101 let filename = filename.file_name().unwrap();
103 for entry in file_visibility_policy.build_walker(filename) {
105 let path = entry.path();
107 // If the output_path is the same as the input file, warn the user and skip the input (in order to avoid compression recursion)
108 if let Ok(ref handle) = output_handle {
109 if matches!(Handle::from_path(path), Ok(x) if &x == handle) {
111 "The output file and the input file are the same: `{}`, skipping...",
112 output_path.display()
118 // This is printed for every file in `input_filenames` and has
119 // little importance for most users, but would generate lots of
120 // spoken text for users using screen readers, braille displays
123 info!(inaccessible, "Compressing '{}'.", EscapedPathDisplay::new(path));
127 builder.append_dir(path, path)?;
129 let mut file = match fs::File::open(path) {
132 if e.kind() == std::io::ErrorKind::NotFound && utils::is_symlink(path) {
133 // This path is for a broken symlink
137 return Err(e.into());
140 builder.append_file(path, file.file_mut()).map_err(|err| {
141 FinalError::with_title("Could not create archive")
142 .detail("Unexpected error while trying to read file")
143 .detail(format!("Error: {err}."))
147 env::set_current_dir(previous_location)?;
150 Ok(builder.into_inner()?)