1use std::fs;
2use std::time::UNIX_EPOCH;
3
4pub fn get_timestamp(path: &str) -> std::io::Result<u64> {
5 let metadata = fs::metadata(path)?;
6 let modified_time = metadata.modified()?;
7 let duration_since_epoch = modified_time.duration_since(UNIX_EPOCH)
8 .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "Time went backwards"))?;
9 Ok(duration_since_epoch.as_secs())
10}
11
12use std::fs::File;
13use std::io::BufReader;
14use std::io::BufWriter;
15use std::path::Path;
16
17pub fn xz_decompress(lzma_file_path: &str) -> Result<(), Box<dyn std::error::Error>> {
18 let lzma_file = File::open(lzma_file_path)?;
20 let path = Path::new(lzma_file_path);
21 let lzma_file_dir = path.parent().ok_or("No parent directory")?;
23 let lzma_file_name = path.file_name().ok_or("No file name")?.to_str().ok_or("Cannot convert to str")?.replace(".xz", "");
25 let mut lzma_reader = BufReader::new(lzma_file);
26
27 let tar_file_name = lzma_file_name.clone().replace(".xz", "");
29
30 let tar_file = File::create(lzma_file_dir.join(lzma_file_name))?;
32 let mut tar_writer = BufWriter::new(tar_file);
33
34 lzma_rs::xz_decompress(&mut lzma_reader, &mut tar_writer)?;
36
37 let tar_file = File::open(lzma_file_dir.join(tar_file_name))?;
39 let mut archive = tar::Archive::new(BufReader::new(tar_file));
40 archive.unpack(lzma_file_dir)?;
41
42 Ok(())
43}
44
45
46pub fn xz_compress(tar_dir_path: &str) -> Result<(), Box<dyn std::error::Error>> {
47 let tar_dir = Path::new(tar_dir_path);
48 let tar_parent_dir = tar_dir.parent().ok_or("No parent directory")?;
49 let tar_dir = tar_dir.file_name().ok_or("No file name")?.to_str().ok_or("Cannot convert to str")?;
51
52 #[cfg(target_os = "windows")]
54 let data = std::process::Command::new("wsl")
55 .arg("tar")
56 .arg("-cJf")
57 .arg(format!("{}.tar.xz", tar_dir))
58 .arg(tar_dir)
59 .current_dir(tar_parent_dir)
60 .output().expect("failed to execute process");
61
62 #[cfg(target_os = "linux")]
63 let data = std::process::Command::new("tar")
64 .arg("-cJf")
65 .arg(format!("{}.tar.xz", tar_dir))
66 .arg(tar_dir)
67 .current_dir(tar_parent_dir)
68 .output().expect("failed to execute process");
69
70 if !data.status.success() {
71 let data = format!("{:?}", data);
72 return std::result::Result::Err(data.into());
73 }
74
75 Ok(())
76}