Skip to content

Commit 923a285

Browse files
author
Paolo Tranquilli
committed
Ruby, Rust: add zstd compression option
1 parent 4a9e31e commit 923a285

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

ruby/codeql-extractor.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ options:
2727
title: Controls compression for the TRAP files written by the extractor.
2828
description: >
2929
This option is only intended for use in debugging the extractor. Accepted
30-
values are 'gzip' (the default, to write gzip-compressed TRAP) and 'none'
31-
(to write uncompressed TRAP).
30+
values are 'gzip' (the default, to write gzip-compressed TRAP) 'zstd' (to
31+
write Zstandard-compressed TRAP) and 'none' (to write uncompressed TRAP).
3232
type: string
33-
pattern: "^(none|gzip)$"
33+
pattern: "^(none|gzip|zstd)$"

rust/codeql-extractor.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ options:
2323
title: Controls compression for the TRAP files written by the extractor.
2424
description: >
2525
This option is only intended for use in debugging the extractor. Accepted
26-
values are 'gzip' (to write gzip-compressed TRAP) and 'none'
27-
(currently the default, to write uncompressed TRAP).
26+
values are 'gzip' (the default, to write gzip-compressed TRAP) 'zstd' (to
27+
write Zstandard-compressed TRAP) and 'none' (to write uncompressed TRAP).
2828
type: string
29-
pattern: "^(none|gzip)$"
29+
pattern: "^(none|gzip|zstd)$"
3030
cargo_target_dir:
3131
title: Directory to use for cargo output files.
3232
description: >

rust/extractor/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ pub enum Compression {
2929
#[default] // TODO make gzip default
3030
None,
3131
Gzip,
32+
Zstd,
3233
}
3334

3435
impl From<Compression> for trap::Compression {
3536
fn from(val: Compression) -> Self {
3637
match val {
3738
Compression::None => Self::None,
3839
Compression::Gzip => Self::Gzip,
40+
Compression::Zstd => Self::Zstd,
3941
}
4042
}
4143
}

rust/ql/integration-tests/hello-project/test_project.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def test_do_not_print_env(codeql, rust, rust_edition, cargo, check_env_not_dumpe
2020
@pytest.mark.ql_test("steps.ql", expected=".cargo.expected")
2121
@pytest.mark.parametrize(("rust_edition", "compression", "suffix"), [
2222
pytest.param(2024, "gzip", ".gz", id="gzip"),
23+
pytest.param(2024, "zstd", ".zst", id="zstd"),
2324
])
2425
def test_compression(codeql, rust, rust_edition, compression, suffix, cargo, rust_check_diagnostics, cwd):
2526
codeql.database.create(cleanup=False, _env={

shared/tree-sitter-extractor/src/trap.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,25 @@ impl Writer {
9696
self.write_trap_entries(&mut trap_file)
9797
}
9898
Compression::Gzip => {
99-
let trap_file = GzEncoder::new(trap_file, flate2::Compression::fast());
99+
let trap_file = GzEncoder::new(trap_file, Compression::GZIP_LEVEL);
100100
let mut trap_file = BufWriter::new(trap_file);
101101
self.write_trap_entries(&mut trap_file)
102102
}
103+
Compression::Zstd => {
104+
let trap_file = zstd::stream::Encoder::new(trap_file, Compression::ZSTD_LEVEL)?;
105+
let mut trap_file = BufWriter::new(trap_file);
106+
self.write_trap_entries(&mut trap_file)?;
107+
trap_file.into_inner()?.finish()?;
108+
Ok(())
109+
}
103110
}
104111
}
105112

106113
fn write_trap_entries<W: Write>(&self, file: &mut W) -> std::io::Result<()> {
107114
for trap_entry in &self.trap_output {
108115
writeln!(file, "{}", trap_entry)?;
109116
}
110-
std::io::Result::Ok(())
117+
Ok(())
111118
}
112119
}
113120

@@ -280,9 +287,13 @@ fn limit_string(string: &str, max_size: usize) -> &str {
280287
pub enum Compression {
281288
None,
282289
Gzip,
290+
Zstd,
283291
}
284292

285293
impl Compression {
294+
pub const ZSTD_LEVEL: i32 = 2;
295+
pub const GZIP_LEVEL: flate2::Compression = flate2::Compression::fast();
296+
286297
pub fn from_env(var_name: &str) -> Result<Compression, String> {
287298
match std::env::var(var_name) {
288299
Ok(method) => match Compression::from_string(&method) {
@@ -298,6 +309,7 @@ impl Compression {
298309
match s.to_lowercase().as_ref() {
299310
"none" => Some(Compression::None),
300311
"gzip" => Some(Compression::Gzip),
312+
"zstd" => Some(Compression::Zstd),
301313
_ => None,
302314
}
303315
}
@@ -306,6 +318,7 @@ impl Compression {
306318
match self {
307319
Compression::None => "trap",
308320
Compression::Gzip => "trap.gz",
321+
Compression::Zstd => "trap.zst",
309322
}
310323
}
311324
}

0 commit comments

Comments
 (0)