Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor archive type #748

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions crates/rattler_package_streaming/src/archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//! Functions that enable extracting or streaming a Conda package for objects that implement the

use std::path::{Path, PathBuf};

use rattler_conda_types::package::ArchiveType;

use crate::read::{folder_from_conda, folder_from_tar_bz2};
/// Test
pub struct Archive {
/// Test
pub archive_type: ArchiveType,
/// Test
pub location: PathBuf,
}

impl Archive {
/// Test
pub fn new(archive_type: ArchiveType, location: PathBuf) -> Self {
Archive {
archive_type,
location,
}
}

/// Test
pub fn extract_a_folder(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also rename this to match the function it calls?

Suggested change
pub fn extract_a_folder(
pub fn extract_directory(

&self,
folder_to_extract: &Path,
destination: &Path,
) -> Result<(), std::io::Error> {
match self.archive_type {
ArchiveType::TarBz2 => {
folder_from_tar_bz2(&self.location, folder_to_extract, destination)
}
ArchiveType::Conda => folder_from_conda(&self.location, folder_to_extract, destination),
}
}
}

impl TryFrom<PathBuf> for Archive {
type Error = std::io::Error;

fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
let archive_type = ArchiveType::try_from(path.as_path()).ok_or(std::io::Error::new(
std::io::ErrorKind::NotFound,
"package does not point to valid archive",
))?;
Ok(Archive {
archive_type,
location: path,
})
}
}
nichmor marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions crates/rattler_package_streaming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rattler_digest::{Md5Hash, Sha256Hash};
#[cfg(feature = "reqwest")]
use rattler_networking::Redact;

pub mod archive;
pub mod read;
pub mod seek;

Expand Down
59 changes: 59 additions & 0 deletions crates/rattler_package_streaming/src/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Functions that enable extracting or streaming a Conda package for objects that implement the
//! [`std::io::Read`] trait.

use crate::{read, seek};

use super::{ExtractError, ExtractResult};
use std::mem::ManuallyDrop;
use std::{ffi::OsStr, io::Read, path::Path};
Expand Down Expand Up @@ -86,3 +88,60 @@ pub fn extract_conda(reader: impl Read, destination: &Path) -> Result<ExtractRes

Ok(ExtractResult { sha256, md5 })
}

/// Extracts a folder from a tar.bz2 archive.
pub fn folder_from_tar_bz2(
nichmor marked this conversation as resolved.
Show resolved Hide resolved
archive_path: &Path,
find_path: &Path,
dest_folder: &Path,
) -> Result<(), std::io::Error> {
let reader = std::fs::File::open(archive_path)?;
let mut archive = read::stream_tar_bz2(reader);

for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?;
if let Ok(stripped_path) = path.strip_prefix(find_path) {
let dest_file = dest_folder.join(stripped_path);
if let Some(parent_folder) = dest_file.parent() {
if !parent_folder.exists() {
std::fs::create_dir_all(parent_folder)?;
}
}
let mut dest_file = std::fs::File::create(dest_file)?;
std::io::copy(&mut entry, &mut dest_file)?;
}
}
Ok(())
}

/// Extracts a folder from a conda archive.
pub fn folder_from_conda(
archive_path: &Path,
find_path: &Path,
dest_folder: &Path,
) -> Result<(), std::io::Error> {
let reader = std::fs::File::open(archive_path)?;
nichmor marked this conversation as resolved.
Show resolved Hide resolved

let mut archive = if find_path.starts_with("info") {
seek::stream_conda_info(reader).expect("Could not open conda file")
} else {
todo!("Not implemented yet");
};

for entry in archive.entries()? {
let mut entry = entry?;
let path = entry.path()?;
if let Ok(stripped_path) = path.strip_prefix(find_path) {
let dest_file = dest_folder.join(stripped_path);
if let Some(parent_folder) = dest_file.parent() {
if !parent_folder.exists() {
std::fs::create_dir_all(parent_folder)?;
}
}
let mut dest_file = std::fs::File::create(dest_file)?;
std::io::copy(&mut entry, &mut dest_file)?;
}
}
Ok(())
}