Skip to content

Commit 8228ea3

Browse files
committed
Improved extracting to an empty dir
1 parent c6a4d01 commit 8228ea3

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

crates/tmc-langs-framework/src/archive.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ impl<T: Read + Seek> Archive<T> {
124124
}
125125
}
126126

127+
pub fn into_inner(self) -> T {
128+
match self {
129+
Self(ArchiveInner::Tar(archive)) => archive.into_inner(),
130+
Self(ArchiveInner::TarZstd(archive)) => archive.into_inner().finish().into_inner(),
131+
Self(ArchiveInner::Zip(archive)) => archive.into_inner(),
132+
Self(ArchiveInner::Empty) => unreachable!("This is a bug."),
133+
}
134+
}
135+
127136
/// tar's entries functions require the archive's position to be at 0,
128137
/// but resetting the position is awkward, hence this helper function
129138
fn reset(&mut self) -> Result<(), TmcError> {

crates/tmc-langs/src/lib.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -824,11 +824,17 @@ pub fn extract_project(
824824
let mut archive = Archive::new(compressed_project, compression)?;
825825
plugin.extract_project(&mut archive, target_location, clean)?;
826826
} else {
827-
log::debug!(
828-
"no matching language plugin found for {}, overwriting",
829-
target_location.display()
830-
);
831-
extract_project_overwrite(compressed_project, target_location, compression)?;
827+
let mut archive = Archive::new(compressed_project, compression)?;
828+
if let Ok(plugin) = PluginType::from_archive(&mut archive) {
829+
plugin.extract_project(&mut archive, target_location, clean)?;
830+
} else {
831+
log::debug!(
832+
"no matching language plugin found for {}, extracting naively",
833+
target_location.display()
834+
);
835+
let compressed_project = archive.into_inner();
836+
extract_project_overwrite(compressed_project, target_location, compression)?;
837+
}
832838
}
833839
Ok(())
834840
}
@@ -919,11 +925,17 @@ pub fn extract_student_files(
919925
if let Ok(plugin) = PluginType::from_exercise(target_location) {
920926
plugin.extract_student_files(compressed_project, compression, target_location)?;
921927
} else {
922-
log::debug!(
923-
"no matching language plugin found for {}, overwriting",
924-
target_location.display()
925-
);
926-
extract_project_overwrite(compressed_project, target_location, Compression::Zip)?;
928+
let mut archive = Archive::new(compressed_project, compression)?;
929+
if let Ok(plugin) = PluginType::from_archive(&mut archive) {
930+
let compressed_project = archive.into_inner();
931+
plugin.extract_student_files(compressed_project, compression, target_location)?;
932+
} else {
933+
log::debug!(
934+
"no matching language plugin found for {}, extracting naively",
935+
target_location.display()
936+
);
937+
archive.extract(target_location)?;
938+
}
927939
}
928940
Ok(())
929941
}

0 commit comments

Comments
 (0)