Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
135 changes: 37 additions & 98 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gitlab-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde = { version = "1.0.228", features = [ "derive" ] }
serde_json = "1.0.145"
thiserror = "2.0.18"
bytes = "1.11.1"
zip = "8.4.0"
zip = "9.0.0"
pin-project = "1.1.11"
futures = "0.3.32"
async-trait = "0.1.89"
Expand Down
9 changes: 8 additions & 1 deletion gitlab-runner/examples/demo-runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,19 @@ impl Run {
for i in 0..artifact.len() {
let (name, data) = {
let mut f = artifact.by_index(i).expect("Failed to open file");
let name = match f.name() {
Ok(name) => name.into_owned(),
Err(e) => {
outputln!("Failed to get file name: {}\n", e);
continue;
}
};
let mut s = String::new();
if f.read_to_string(&mut s).is_err() {
s.clear();
s.push_str("Failed to read file to text");
}
(f.name().to_string(), s)
(name, s)
};
outputln!("=== File: {} ===", name);
outputln!("{}", data);
Expand Down
39 changes: 35 additions & 4 deletions gitlab-runner/src/artifact.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
//! Helpers for artifacts downloaded from gitlab
use std::{
borrow::Cow,
io::{Read, Seek},
path::PathBuf,
};

use zip::{read::ZipFile, result::ZipResult};

fn zip_error_to_io(error: zip::result::ZipError) -> std::io::Error {
match error {
zip::result::ZipError::Io(error) => error,
error => std::io::Error::other(error),
}
}

/// A file in a gitlab artifact
///
/// Most importantly this implements [`Read`] to read out the content of the file
pub struct ArtifactFile<'a>(ZipFile<'a, Box<dyn ReadSeek + Send>>);

impl ArtifactFile<'_> {
/// Get the name of the file
pub fn name(&self) -> &str {
self.0.name()
pub fn name(&self) -> std::io::Result<Cow<'_, str>> {
self.0.name().map_err(zip_error_to_io)
}

/// Get the safe path of the file
Expand Down Expand Up @@ -52,8 +60,10 @@ impl Artifact {
///
/// The returned file name isn't sanatized in any way and should *not* be used as a path on the
/// filesystem. To get a safe path of a given file use [`ArtifactFile::path`]
pub fn file_names(&self) -> impl Iterator<Item = &str> {
self.zip.file_names()
pub fn file_names(&self) -> impl Iterator<Item = std::io::Result<Cow<'_, str>>> {
self.zip
.file_names()
.map(|name| name.map_err(zip_error_to_io))
}

/// Get a file in the artifact by name
Expand All @@ -76,3 +86,24 @@ impl Artifact {
self.zip.len()
}
}

#[cfg(test)]
mod tests {
use super::zip_error_to_io;
use std::io::{Error, ErrorKind};
use zip::result::ZipError;

#[test]
fn preserves_io_errors() {
let error = zip_error_to_io(ZipError::Io(Error::from(ErrorKind::NotFound)));

assert_eq!(error.kind(), ErrorKind::NotFound);
}

#[test]
fn maps_other_errors_to_other() {
let error = zip_error_to_io(ZipError::FileNotFound);

assert_eq!(error.kind(), ErrorKind::Other);
}
}
Loading
Loading