Skip to content

Commit

Permalink
Merge pull request rust-lang#135 from est31/master
Browse files Browse the repository at this point in the history
Two little improvements
  • Loading branch information
alexcrichton authored Nov 28, 2017
2 parents 945b134 + 0623308 commit 264419f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/gz/bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {
let mtime = ((header[4] as u32) << 0) | ((header[5] as u32) << 8) | ((header[6] as u32) << 16) |
((header[7] as u32) << 24);
let _xfl = header[8];
let _os = header[9];
let os = header[9];

let extra = if flg & FEXTRA != 0 {
let xlen = try!(read_le_u16(&mut crc_reader));
Expand Down Expand Up @@ -104,6 +104,7 @@ fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {
extra: extra,
filename: filename,
comment: comment,
operating_system: os,
mtime: mtime,
})
}
Expand Down
26 changes: 22 additions & 4 deletions src/gz/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ pub mod write;
///
/// The header can contain metadata about the file that was compressed, if
/// present.
#[derive(PartialEq, Debug)]
#[derive(PartialEq, Clone, Debug)]
pub struct GzHeader {
extra: Option<Vec<u8>>,
filename: Option<Vec<u8>>,
comment: Option<Vec<u8>>,
operating_system: u8,
mtime: u32,
}

Expand All @@ -43,6 +44,14 @@ impl GzHeader {
self.comment.as_ref().map(|s| &s[..])
}

/// Returns the `operating_system` field of this gzip stream's header.
///
/// There are predefined values for various operating systems.
/// 255 means that the value is unknown.
pub fn operating_system(&self) -> u8 {
self.operating_system
}

/// This gives the most recent modification time of the original file being compressed.
///
/// The time is in Unix format, i.e., seconds since 00:00:00 GMT, Jan. 1, 1970.
Expand Down Expand Up @@ -105,6 +114,7 @@ pub struct GzBuilder {
extra: Option<Vec<u8>>,
filename: Option<CString>,
comment: Option<CString>,
operating_system: Option<u8>,
mtime: u32,
}

Expand All @@ -115,6 +125,7 @@ impl GzBuilder {
extra: None,
filename: None,
comment: None,
operating_system: None,
mtime: 0,
}
}
Expand All @@ -125,6 +136,12 @@ impl GzBuilder {
self
}

/// Configure the `operating_system` field in the gzip header.
pub fn operating_system(mut self, os: u8) -> GzBuilder {
self.operating_system = Some(os);
self
}

/// Configure the `extra` field in the gzip header.
pub fn extra<T: Into<Vec<u8>>>(mut self, extra: T) -> GzBuilder {
self.extra = Some(extra.into());
Expand Down Expand Up @@ -183,6 +200,7 @@ impl GzBuilder {
extra,
filename,
comment,
operating_system,
mtime,
} = self;
let mut flg = 0;
Expand Down Expand Up @@ -222,9 +240,9 @@ impl GzBuilder {

// Typically this byte indicates what OS the gz stream was created on,
// but in an effort to have cross-platform reproducible streams just
// always set this to 255. I'm not sure that if we "correctly" set this
// it'd do anything anyway...
header[9] = 255;
// default this value to 255. I'm not sure that if we "correctly" set
// this it'd do anything anyway...
header[9] = operating_system.unwrap_or(255);
return header;
}
}
Expand Down

0 comments on commit 264419f

Please sign in to comment.