forked from proxmox-mirrors/proxmox
AptRepositoryFile: make path optional
Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
parent
03f14b74dd
commit
3e515dbc7d
@ -50,8 +50,9 @@ trait APTRepositoryParser {
|
|||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
/// Represents an abstract APT repository file.
|
/// Represents an abstract APT repository file.
|
||||||
pub struct APTRepositoryFile {
|
pub struct APTRepositoryFile {
|
||||||
/// The path to the file.
|
/// The path to the file. If None, `contents` must be set directly.
|
||||||
pub path: String,
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub path: Option<String>,
|
||||||
|
|
||||||
/// The type of the file.
|
/// The type of the file.
|
||||||
pub file_type: APTRepositoryFileType,
|
pub file_type: APTRepositoryFileType,
|
||||||
@ -186,7 +187,7 @@ impl APTRepositoryFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(Some(Self {
|
Ok(Some(Self {
|
||||||
path: path_string,
|
path: Some(path_string),
|
||||||
file_type,
|
file_type,
|
||||||
repositories: vec![],
|
repositories: vec![],
|
||||||
digest: None,
|
digest: None,
|
||||||
@ -198,7 +199,7 @@ impl APTRepositoryFile {
|
|||||||
Self {
|
Self {
|
||||||
file_type: content_type,
|
file_type: content_type,
|
||||||
content: Some(content),
|
content: Some(content),
|
||||||
path: "-".to_string(),
|
path: None,
|
||||||
repositories: vec![],
|
repositories: vec![],
|
||||||
digest: None,
|
digest: None,
|
||||||
}
|
}
|
||||||
@ -206,17 +207,16 @@ impl APTRepositoryFile {
|
|||||||
|
|
||||||
/// Check if the file exists.
|
/// Check if the file exists.
|
||||||
pub fn exists(&self) -> bool {
|
pub fn exists(&self) -> bool {
|
||||||
if self.path != "-" {
|
if let Some(path) = &self.path {
|
||||||
PathBuf::from(&self.path).exists()
|
PathBuf::from(path).exists()
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_with_digest(&self) -> Result<(Vec<u8>, [u8; 32]), APTRepositoryFileError> {
|
pub fn read_with_digest(&self) -> Result<(Vec<u8>, [u8; 32]), APTRepositoryFileError> {
|
||||||
if self.path != "-" {
|
if let Some(path) = &self.path {
|
||||||
let content =
|
let content = std::fs::read(path).map_err(|err| self.err(format_err!("{}", err)))?;
|
||||||
std::fs::read(&self.path).map_err(|err| self.err(format_err!("{}", err)))?;
|
|
||||||
let digest = openssl::sha::sha256(&content);
|
let digest = openssl::sha::sha256(&content);
|
||||||
|
|
||||||
Ok((content, digest))
|
Ok((content, digest))
|
||||||
@ -234,7 +234,7 @@ impl APTRepositoryFile {
|
|||||||
/// Create an `APTRepositoryFileError`.
|
/// Create an `APTRepositoryFileError`.
|
||||||
pub fn err(&self, error: Error) -> APTRepositoryFileError {
|
pub fn err(&self, error: Error) -> APTRepositoryFileError {
|
||||||
APTRepositoryFileError {
|
APTRepositoryFileError {
|
||||||
path: self.path.clone(),
|
path: self.path.clone().unwrap_or_default(),
|
||||||
error: error.to_string(),
|
error: error.to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -272,11 +272,14 @@ impl APTRepositoryFile {
|
|||||||
/// If a digest is provided, checks that the current content of the file still
|
/// If a digest is provided, checks that the current content of the file still
|
||||||
/// produces the same one.
|
/// produces the same one.
|
||||||
pub fn write(&self) -> Result<(), APTRepositoryFileError> {
|
pub fn write(&self) -> Result<(), APTRepositoryFileError> {
|
||||||
if self.path == "-" {
|
let path = match &self.path {
|
||||||
return Err(self.err(format_err!(
|
Some(path) => path,
|
||||||
"Cannot write to APT repository file without path."
|
None => {
|
||||||
)));
|
return Err(self.err(format_err!(
|
||||||
}
|
"Cannot write to APT repository file without path."
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(digest) = self.digest {
|
if let Some(digest) = self.digest {
|
||||||
if !self.exists() {
|
if !self.exists() {
|
||||||
@ -290,7 +293,7 @@ impl APTRepositoryFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if self.repositories.is_empty() {
|
if self.repositories.is_empty() {
|
||||||
return std::fs::remove_file(&self.path)
|
return std::fs::remove_file(&path)
|
||||||
.map_err(|err| self.err(format_err!("unable to remove file - {}", err)));
|
.map_err(|err| self.err(format_err!("unable to remove file - {}", err)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -304,7 +307,7 @@ impl APTRepositoryFile {
|
|||||||
.map_err(|err| self.err(format_err!("writing repository {} - {}", n + 1, err)))?;
|
.map_err(|err| self.err(format_err!("writing repository {} - {}", n + 1, err)))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = PathBuf::from(&self.path);
|
let path = PathBuf::from(&path);
|
||||||
let dir = match path.parent() {
|
let dir = match path.parent() {
|
||||||
Some(dir) => dir,
|
Some(dir) => dir,
|
||||||
None => return Err(self.err(format_err!("invalid path"))),
|
None => return Err(self.err(format_err!("invalid path"))),
|
||||||
@ -336,6 +339,11 @@ impl APTRepositoryFile {
|
|||||||
pub fn check_suites(&self, current_codename: DebianCodename) -> Vec<APTRepositoryInfo> {
|
pub fn check_suites(&self, current_codename: DebianCodename) -> Vec<APTRepositoryInfo> {
|
||||||
let mut infos = vec![];
|
let mut infos = vec![];
|
||||||
|
|
||||||
|
let path = match &self.path {
|
||||||
|
Some(path) => path.clone(),
|
||||||
|
None => return vec![],
|
||||||
|
};
|
||||||
|
|
||||||
for (n, repo) in self.repositories.iter().enumerate() {
|
for (n, repo) in self.repositories.iter().enumerate() {
|
||||||
if !repo.types.contains(&APTRepositoryPackageType::Deb) {
|
if !repo.types.contains(&APTRepositoryPackageType::Deb) {
|
||||||
continue;
|
continue;
|
||||||
@ -359,7 +367,7 @@ impl APTRepositoryFile {
|
|||||||
|
|
||||||
let mut add_info = |kind: &str, message| {
|
let mut add_info = |kind: &str, message| {
|
||||||
infos.push(APTRepositoryInfo {
|
infos.push(APTRepositoryInfo {
|
||||||
path: self.path.clone(),
|
path: path.clone(),
|
||||||
index: n,
|
index: n,
|
||||||
property: Some("Suites".to_string()),
|
property: Some("Suites".to_string()),
|
||||||
kind: kind.to_string(),
|
kind: kind.to_string(),
|
||||||
@ -421,6 +429,11 @@ impl APTRepositoryFile {
|
|||||||
pub fn check_uris(&self) -> Vec<APTRepositoryInfo> {
|
pub fn check_uris(&self) -> Vec<APTRepositoryInfo> {
|
||||||
let mut infos = vec![];
|
let mut infos = vec![];
|
||||||
|
|
||||||
|
let path = match &self.path {
|
||||||
|
Some(path) => path,
|
||||||
|
None => return vec![],
|
||||||
|
};
|
||||||
|
|
||||||
for (n, repo) in self.repositories.iter().enumerate() {
|
for (n, repo) in self.repositories.iter().enumerate() {
|
||||||
let mut origin = match repo.get_cached_origin() {
|
let mut origin = match repo.get_cached_origin() {
|
||||||
Ok(option) => option,
|
Ok(option) => option,
|
||||||
@ -433,7 +446,7 @@ impl APTRepositoryFile {
|
|||||||
|
|
||||||
if let Some(origin) = origin {
|
if let Some(origin) = origin {
|
||||||
infos.push(APTRepositoryInfo {
|
infos.push(APTRepositoryInfo {
|
||||||
path: self.path.clone(),
|
path: path.clone(),
|
||||||
index: n,
|
index: n,
|
||||||
kind: "origin".to_string(),
|
kind: "origin".to_string(),
|
||||||
property: None,
|
property: None,
|
||||||
|
@ -49,9 +49,13 @@ fn test_parse_write_dir(read_dir: &str) -> Result<(), Error> {
|
|||||||
assert!(errors.is_empty());
|
assert!(errors.is_empty());
|
||||||
|
|
||||||
for file in files.iter_mut() {
|
for file in files.iter_mut() {
|
||||||
let path = PathBuf::from(&file.path);
|
let path = match &file.path {
|
||||||
|
Some(path) => path,
|
||||||
|
None => continue,
|
||||||
|
};
|
||||||
|
let path = PathBuf::from(path);
|
||||||
let new_path = write_dir.join(path.file_name().unwrap());
|
let new_path = write_dir.join(path.file_name().unwrap());
|
||||||
file.path = new_path.into_os_string().into_string().unwrap();
|
file.path = Some(new_path.into_os_string().into_string().unwrap());
|
||||||
file.digest = None;
|
file.digest = None;
|
||||||
file.write()?;
|
file.write()?;
|
||||||
}
|
}
|
||||||
@ -104,7 +108,7 @@ fn test_digest() -> Result<(), Error> {
|
|||||||
file.parse()?;
|
file.parse()?;
|
||||||
|
|
||||||
let new_path = write_dir.join(path.file_name().unwrap());
|
let new_path = write_dir.join(path.file_name().unwrap());
|
||||||
file.path = new_path.clone().into_os_string().into_string().unwrap();
|
file.path = Some(new_path.clone().into_os_string().into_string().unwrap());
|
||||||
|
|
||||||
let old_digest = file.digest.unwrap();
|
let old_digest = file.digest.unwrap();
|
||||||
|
|
||||||
@ -156,7 +160,7 @@ fn test_empty_write() -> Result<(), Error> {
|
|||||||
file.parse()?;
|
file.parse()?;
|
||||||
|
|
||||||
let new_path = write_dir.join(path.file_name().unwrap());
|
let new_path = write_dir.join(path.file_name().unwrap());
|
||||||
file.path = new_path.clone().into_os_string().into_string().unwrap();
|
file.path = Some(new_path.clone().into_os_string().into_string().unwrap());
|
||||||
|
|
||||||
file.digest = None;
|
file.digest = None;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user