1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
use crate::cfg::config::Config;
use std::fs::{File, create_dir_all};
use std::path::Path;
use flate2::read::GzDecoder;
use tar::Archive;
pub enum archs {
x86_64,
aarch64,
x86,
}
pub struct Package {
name: String,
version: String,
arch: archs,
descr: String,
}
impl Package {
/// Checks if the archive contains INSTALL, SETTS and BUILD files.
///
/// Checks if INSTALL file exists and is not empty. If it does not exist or is empty, returns an error.
///
/// Checks if SETTS and BUILD files exist and are not empty. If they do not exist or are empty, logs a warning.
///
/// * Returns an error if INSTALL file does not exist or is empty.
/// * Returns an error if INSTALL file is empty.
///
// TODO: Add meta-files validation here.
pub fn check(path_to_archive: String) -> Result<bool, std::io::Error> {
let config: Config = Config::parse().unwrap();
create_dir_all(&config.paths.cache_dir)?;
let file = File::open(&path_to_archive)?;
let gz = GzDecoder::new(file);
let mut archive = Archive::new(gz);
archive.unpack(&config.paths.cache_dir)?;
let install_path = Path::new(&config.paths.cache_dir).join("INSTALL");
let setts_path = Path::new(&config.paths.cache_dir).join("SETTS");
let build_path = Path::new(&config.paths.cache_dir).join("BUILD");
if !install_path.exists() {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"INSTALL file not found in archive",
));
}
let install_content = std::fs::read_to_string(&install_path)?;
if install_content.trim().is_empty() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"INSTALL file is empty",
));
}
if !setts_path.exists() {
log::warn!("SETTS file not found in archive. Make sure you dont need this.");
} else {
let setts_content = std::fs::read_to_string(&setts_path)?;
if setts_content.trim().is_empty() {
log::warn!("SETTS file is empty. Make sure you dont need this.");
}
}
if !build_path.exists() {
log::warn!("BUILD file not found in archive. Make sure you dont need this.");
} else {
let build_content = std::fs::read_to_string(&build_path)?;
if build_content.trim().is_empty() {
log::warn!("BUILD file is empty. Make sure you dont need this.");
}
}
Ok(true)
}
pub fn build() -> Result<bool, std::io::Error> {
todo!();
}
pub fn install() -> Result<bool, std::io::Error> {
todo!();
}
}
|