Skip to content

Commit b41919d

Browse files
committed
removed unnecessary temp file from zstd compression
1 parent e086f45 commit b41919d

File tree

2 files changed

+8
-11
lines changed

2 files changed

+8
-11
lines changed

tmc-langs/src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ pub enum LangsError {
1414
TempDir(#[source] std::io::Error),
1515
#[error("Invalid parameter key/value: {0}")]
1616
InvalidParam(String, #[source] ParamError),
17-
#[error("Error compressing file at {0} with zstd")]
18-
Zstd(PathBuf, #[source] std::io::Error),
17+
#[error("Error compressing data with zstd")]
18+
Zstd(#[source] std::io::Error),
1919
#[error("Error retrieving file handle from tar builder")]
2020
TarIntoInner(#[source] std::io::Error),
2121
#[error("Error finishing tar")]

tmc-langs/src/submission_packaging.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::TmcProjectYml;
44
use crate::data::{OutputFormat, TmcParams};
55
use crate::error::LangsError;
66
use std::ffi::OsStr;
7-
use std::io::Write;
7+
use std::io::{Cursor, Write};
88
use std::path::Path;
99
use std::path::PathBuf;
1010
use tmc_langs_plugins::Plugin;
@@ -287,9 +287,8 @@ pub fn prepare_submission(
287287
archive.finish()?;
288288
}
289289
OutputFormat::TarZstd => {
290-
// create temporary tar file
291-
let temp = tempfile::NamedTempFile::new().map_err(LangsError::TempFile)?;
292-
let mut archive = tar::Builder::new(temp);
290+
let buf = Cursor::new(vec![]);
291+
let mut archive = tar::Builder::new(buf);
293292
log::debug!(
294293
"appending \"{}\" at \"{}\"",
295294
dest.display(),
@@ -299,11 +298,9 @@ pub fn prepare_submission(
299298
.append_dir_all(prefix, &dest)
300299
.map_err(|e| LangsError::TarAppend(dest, e))?;
301300
archive.finish().map_err(LangsError::TarFinish)?;
302-
let tar = archive.into_inner().map_err(LangsError::TarIntoInner)?;
303-
// the existing file handle has been read to the end and is now empty, so we reopen it
304-
let reopened = file_util::open_file(tar.path())?;
305-
zstd::stream::copy_encode(reopened, archive_file, 0)
306-
.map_err(|e| LangsError::Zstd(tar.path().to_path_buf(), e))?;
301+
let mut tar = archive.into_inner().map_err(LangsError::TarIntoInner)?;
302+
tar.set_position(0); // reset the cursor
303+
zstd::stream::copy_encode(tar, archive_file, 0).map_err(LangsError::Zstd)?;
307304
}
308305
}
309306
Ok(())

0 commit comments

Comments
 (0)