Skip to content

Commit

Permalink
Merge pull request #33 from niklasf/cap-field-sizes
Browse files Browse the repository at this point in the history
also cap mtime and mode in header
  • Loading branch information
mdsteele authored Mar 28, 2024
2 parents 99b8505 + d265dd1 commit a078c72
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
29 changes: 17 additions & 12 deletions fuzz/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use arbitrary::{Arbitrary, Unstructured};
use std::cmp::min;

#[derive(Clone, Default, Debug, Eq, PartialEq, Arbitrary)]
pub struct Model {
Expand Down Expand Up @@ -76,29 +77,33 @@ impl Arbitrary<'_> for Identifier {
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Arbitrary)]
struct Mode(u32);

impl Arbitrary<'_> for Mode {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
u32::arbitrary(u).map(|v| Mode(v & 0o7777))
impl PartialEq for Mode {
fn eq(&self, other: &Mode) -> bool {
self.0 & 0o7777_7777 == other.0 & 0o7777_7777
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Arbitrary)]
struct Timestamp(u64);

impl Arbitrary<'_> for Timestamp {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
u.int_in_range(0..=999_999_999_999).map(Timestamp) // TODO: Deal with entire range
impl PartialEq for Timestamp {
fn eq(&self, other: &Timestamp) -> bool {
min(self.0, 999_999_999_999) == min(other.0, 999_999_999_999)
}
}

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[derive(Copy, Clone, Debug, Eq, Arbitrary)]
struct Uid(u32);

impl Arbitrary<'_> for Uid {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
u.int_in_range(0..=999_999).map(Uid) // TODO: Deal with entire range (#29)
impl PartialEq for Uid {
fn eq(&self, other: &Uid) -> bool {
let mut left = self.0.to_string();
left.truncate(6);
let mut right = other.0.to_string();
right.truncate(6);
left == right
}
}
21 changes: 15 additions & 6 deletions src/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::min;
use std::collections::HashMap;
use std::fs::Metadata;
use std::io::{self, Error, ErrorKind, Read, Result, Write};
Expand Down Expand Up @@ -244,10 +245,10 @@ impl Header {
writer,
"#1/{:<13}{:<12}{:<6.6}{:<6.6}{:<8o}{:<10}`",
padded_length,
self.mtime,
cap_mtime(self.mtime),
self.uid.to_string(),
self.gid.to_string(),
self.mode,
cap_mode(self.mode),
self.size + padded_length as u64
)?;
writer.write_all(&self.identifier)?;
Expand All @@ -258,10 +259,10 @@ impl Header {
writeln!(
writer,
"{:<12}{:<6.6}{:<6.6}{:<8o}{:<10}`",
self.mtime,
cap_mtime(self.mtime),
self.uid.to_string(),
self.gid.to_string(),
self.mode,
cap_mode(self.mode),
self.size
)?;
}
Expand All @@ -287,16 +288,24 @@ impl Header {
writeln!(
writer,
"{:<12}{:<6.6}{:<6.6}{:<8o}{:<10}`",
self.mtime,
cap_mtime(self.mtime),
self.uid.to_string(),
self.gid.to_string(),
self.mode,
cap_mode(self.mode),
self.size
)?;
Ok(())
}
}

fn cap_mtime(mtime: u64) -> u64 {
min(mtime, 999_999_999_999) // Closest representable timestamp
}

fn cap_mode(mode: u32) -> u32 {
mode & 0o7777_7777 // Preserve as many bits as possible
}

fn parse_number(field_name: &str, bytes: &[u8], radix: u32) -> Result<u64> {
if let Ok(string) = str::from_utf8(bytes) {
if let Ok(value) = u64::from_str_radix(string.trim_end(), radix) {
Expand Down

0 comments on commit a078c72

Please sign in to comment.