1 //! Filesystem utility functions.
11 use super::{to_utf, user_wants_to_overwrite};
12 use crate::{extension::Extension, info, QuestionPolicy};
14 /// Remove `path` asking the user to overwrite if necessary.
16 /// * `Ok(true)` means the path is clear,
17 /// * `Ok(false)` means the user doesn't want to overwrite
18 /// * `Err(_)` is an error
19 pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result<bool> {
20 if path.exists() && !user_wants_to_overwrite(path, question_policy)? {
24 remove_file_or_dir(path)?;
29 pub fn remove_file_or_dir(path: &Path) -> crate::Result<()> {
31 fs::remove_dir_all(path)?;
32 } else if path.is_file() {
33 fs::remove_file(path)?;
38 /// Creates a directory at the path, if there is nothing there.
39 pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> {
41 fs::create_dir_all(path)?;
42 // creating a directory is an important change to the file system we
43 // should always inform the user about
44 info!(accessible, "directory {} created.", to_utf(path));
49 /// Returns current directory, but before change the process' directory to the
50 /// one that contains the file pointed to by `filename`.
51 pub fn cd_into_same_dir_as(filename: &Path) -> crate::Result<PathBuf> {
52 let previous_location = env::current_dir()?;
54 let parent = filename.parent().ok_or(crate::Error::CompressingRootFolder)?;
55 env::set_current_dir(parent)?;
60 /// Try to detect the file extension by looking for known magic strings
61 /// Source: <https://en.wikipedia.org/wiki/List_of_file_signatures>
62 pub fn try_infer_extension(path: &Path) -> Option<Extension> {
63 fn is_zip(buf: &[u8]) -> bool {
65 && buf[..=1] == [0x50, 0x4B]
66 && (buf[2..=3] == [0x3, 0x4] || buf[2..=3] == [0x5, 0x6] || buf[2..=3] == [0x7, 0x8])
68 fn is_tar(buf: &[u8]) -> bool {
69 buf.len() > 261 && buf[257..=261] == [0x75, 0x73, 0x74, 0x61, 0x72]
71 fn is_gz(buf: &[u8]) -> bool {
72 buf.starts_with(&[0x1F, 0x8B, 0x8])
74 fn is_bz2(buf: &[u8]) -> bool {
75 buf.starts_with(&[0x42, 0x5A, 0x68])
77 fn is_xz(buf: &[u8]) -> bool {
78 buf.starts_with(&[0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])
80 fn is_lz4(buf: &[u8]) -> bool {
81 buf.starts_with(&[0x04, 0x22, 0x4D, 0x18])
83 fn is_sz(buf: &[u8]) -> bool {
84 buf.starts_with(&[0xFF, 0x06, 0x00, 0x00, 0x73, 0x4E, 0x61, 0x50, 0x70, 0x59])
86 fn is_zst(buf: &[u8]) -> bool {
87 buf.starts_with(&[0x28, 0xB5, 0x2F, 0xFD])
91 let mut buf = [0; 270];
93 // Error cause will be ignored, so use std::fs instead of fs_err
94 let result = std::fs::File::open(path).map(|mut file| file.read(&mut buf));
96 // In case of file open or read failure, could not infer a extension
103 use crate::extension::CompressionFormat::*;
105 Some(Extension::new(&[Zip], "zip"))
106 } else if is_tar(&buf) {
107 Some(Extension::new(&[Tar], "tar"))
108 } else if is_gz(&buf) {
109 Some(Extension::new(&[Gzip], "gz"))
110 } else if is_bz2(&buf) {
111 Some(Extension::new(&[Bzip], "bz2"))
112 } else if is_xz(&buf) {
113 Some(Extension::new(&[Lzma], "xz"))
114 } else if is_lz4(&buf) {
115 Some(Extension::new(&[Lz4], "lz4"))
116 } else if is_sz(&buf) {
117 Some(Extension::new(&[Snappy], "sz"))
118 } else if is_zst(&buf) {
119 Some(Extension::new(&[Zstd], "zst"))
125 /// Returns true if a path is a symlink.
126 /// This is the same as the nightly <https://doc.rust-lang.org/std/path/struct.Path.html#method.is_symlink>
127 /// Useful to detect broken symlinks when compressing. (So we can safely ignore them)
128 pub fn is_symlink(path: &Path) -> bool {
129 fs::symlink_metadata(path)
130 .map(|m| m.file_type().is_symlink())