]> git.proxmox.com Git - proxmox-apt.git/commitdiff
file: add pre-parsed content variant
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 5 Apr 2022 13:07:43 +0000 (15:07 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 21 Jul 2022 10:10:23 +0000 (12:10 +0200)
to allow usage with in-memory contents instead of actual files.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
src/repositories/file.rs

index 1b3ac857a9bafe64fc92b0019135d34b29fc2b90..83ee54ec51dede2ea4b5d5f86e0327974c8a7fdc 100644 (file)
@@ -59,6 +59,9 @@ pub struct APTRepositoryFile {
     /// List of repositories in the file.
     pub repositories: Vec<APTRepository>,
 
+    /// The file content, if already parsed.
+    pub content: Option<String>,
+
     /// Digest of the original contents.
     pub digest: Option<[u8; 32]>,
 }
@@ -187,20 +190,45 @@ impl APTRepositoryFile {
             file_type,
             repositories: vec![],
             digest: None,
+            content: None,
         }))
     }
 
+    pub fn with_content(content: String, content_type: APTRepositoryFileType) -> Self {
+        Self {
+            file_type: content_type,
+            content: Some(content),
+            path: "-".to_string(),
+            repositories: vec![],
+            digest: None,
+        }
+    }
+
     /// Check if the file exists.
     pub fn exists(&self) -> bool {
-        PathBuf::from(&self.path).exists()
+        if self.path != "-" {
+            PathBuf::from(&self.path).exists()
+        } else {
+            false
+        }
     }
 
     pub fn read_with_digest(&self) -> Result<(Vec<u8>, [u8; 32]), APTRepositoryFileError> {
-        let content = std::fs::read(&self.path).map_err(|err| self.err(format_err!("{}", err)))?;
-
-        let digest = openssl::sha::sha256(&content);
-
-        Ok((content, digest))
+        if self.path != "-" {
+            let content =
+                std::fs::read(&self.path).map_err(|err| self.err(format_err!("{}", err)))?;
+            let digest = openssl::sha::sha256(&content);
+
+            Ok((content, digest))
+        } else if let Some(ref content) = self.content {
+            let content = content.as_bytes();
+            let digest = openssl::sha::sha256(content);
+            Ok((content.to_vec(), digest))
+        } else {
+            Err(self.err(format_err!(
+                "Neither 'path' nor 'content' set, cannot read APT repository info."
+            )))
+        }
     }
 
     /// Create an `APTRepositoryFileError`.
@@ -244,6 +272,12 @@ impl APTRepositoryFile {
     /// If a digest is provided, checks that the current content of the file still
     /// produces the same one.
     pub fn write(&self) -> Result<(), APTRepositoryFileError> {
+        if self.path == "-" {
+            return Err(self.err(format_err!(
+                "Cannot write to APT repository file without path."
+            )));
+        }
+
         if let Some(digest) = self.digest {
             if !self.exists() {
                 return Err(self.err(format_err!("digest specified, but file does not exist")));