Skip to content

Commit

Permalink
Merge pull request #37 from sftse/outlook-compat
Browse files Browse the repository at this point in the history
Outlook compat
  • Loading branch information
mdsteele authored Jun 11, 2023
2 parents 5e1d44a + c1b9336 commit 959e029
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/internal/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,12 @@ impl<F: Write + Seek> Directory<F> {
);
// Create a new directory entry.
let stream_id = self.allocate_dir_entry()?;
let now = Timestamp::now();
*self.dir_entry_mut(stream_id) = DirEntry::new(name, obj_type, now);
// 2.6.1 streams must have creation and modified time of 0
let mut ts = Timestamp::zero();
if obj_type == ObjType::Storage {
ts = Timestamp::now();
}
*self.dir_entry_mut(stream_id) = DirEntry::new(name, obj_type, ts);

// Insert the new entry into the tree.
let mut sibling_id = self.dir_entry(parent_id).child;
Expand Down
2 changes: 1 addition & 1 deletion src/internal/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ mod tests {
fn make_valid_header() -> Header {
let mut header = Header {
version: Version::V3,
num_dir_sectors: 2,
num_dir_sectors: 0,
num_fat_sectors: 1,
first_dir_sector: 1,
first_minifat_sector: 2,
Expand Down
10 changes: 5 additions & 5 deletions src/internal/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,37 @@ mod tests {
#[test]
fn absolute_path_is_valid() {
assert_eq!(
name_chain_from_path(&Path::new("/foo/bar/baz/")).unwrap(),
name_chain_from_path(Path::new("/foo/bar/baz/")).unwrap(),
vec!["foo", "bar", "baz"]
);
}

#[test]
fn relative_path_is_valid() {
assert_eq!(
name_chain_from_path(&Path::new("foo/bar/baz")).unwrap(),
name_chain_from_path(Path::new("foo/bar/baz")).unwrap(),
vec!["foo", "bar", "baz"]
);
}

#[test]
fn path_with_parents_is_valid() {
assert_eq!(
name_chain_from_path(&Path::new("foo/bar/../baz")).unwrap(),
name_chain_from_path(Path::new("foo/bar/../baz")).unwrap(),
vec!["foo", "baz"]
);
}

#[test]
#[should_panic(expected = "Invalid path (must be within root)")]
fn parent_of_root_is_invalid() {
name_chain_from_path(&Path::new("foo/../../baz")).unwrap();
name_chain_from_path(Path::new("foo/../../baz")).unwrap();
}

#[test]
fn canonical_path_is_absolute() {
let path = Path::new("foo/bar/../baz");
let names = name_chain_from_path(&path).unwrap();
let names = name_chain_from_path(path).unwrap();
assert_eq!(path_from_name_chain(&names), PathBuf::from("/foo/baz"));
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/internal/stream.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::internal::{consts, MiniAllocator, ObjType, SectorInit, Timestamp};
use crate::internal::{consts, MiniAllocator, ObjType, SectorInit};
use std::cell::RefCell;
use std::io::{self, BufRead, Read, Seek, SeekFrom, Write};
use std::rc::{Rc, Weak};
Expand Down Expand Up @@ -385,7 +385,6 @@ fn write_data_to_stream<F: Read + Write + Seek>(
minialloc.with_dir_entry_mut(stream_id, |dir_entry| {
dir_entry.start_sector = new_start_sector;
dir_entry.stream_len = new_stream_len;
dir_entry.modified_time = Timestamp::now();
})
}

Expand Down Expand Up @@ -481,7 +480,6 @@ fn resize_stream<F: Read + Write + Seek>(
minialloc.with_dir_entry_mut(stream_id, |dir_entry| {
dir_entry.start_sector = new_start_sector;
dir_entry.stream_len = new_stream_len;
dir_entry.modified_time = Timestamp::now();
})
}

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,8 @@ impl<F: Read + Write + Seek> CompoundFile<F> {
) -> io::Result<CompoundFile<F>> {
let mut header = Header {
version,
num_dir_sectors: 1,
// 2.2 requires this to be zero in V3
num_dir_sectors: if version == Version::V3 { 0 } else { 1 },
num_fat_sectors: 1,
first_dir_sector: 1,
first_minifat_sector: consts::END_OF_CHAIN,
Expand Down Expand Up @@ -972,7 +973,7 @@ mod tests {
let mut data = Vec::<u8>::new();
let mut header = Header {
version,
num_dir_sectors: 1,
num_dir_sectors: 0,
num_fat_sectors: 1,
first_dir_sector: 1,
first_minifat_sector: consts::END_OF_CHAIN,
Expand Down
8 changes: 4 additions & 4 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ fn truncate_stream() {
assert_eq!(stream.seek(SeekFrom::Start(6000)).unwrap(), 6000);
stream.set_len(7000).unwrap();
assert_eq!(stream.len(), 7000);
assert_eq!(stream.seek(SeekFrom::Current(0)).unwrap(), 6000);
assert_eq!(stream.stream_position().unwrap(), 6000);
stream.set_len(5000).unwrap();
assert_eq!(stream.len(), 5000);
stream.write_all(&vec![b'x'; 1000]).unwrap();
Expand Down Expand Up @@ -672,13 +672,13 @@ fn extend_stream() {
assert_eq!(stream.seek(SeekFrom::Start(1000)).unwrap(), 1000);
stream.write_all(&vec![b'y'; 500]).unwrap();
assert_eq!(stream.len(), 2000);
assert_eq!(stream.seek(SeekFrom::Current(0)).unwrap(), 1500);
assert_eq!(stream.stream_position().unwrap(), 1500);
stream.set_len(5000).unwrap();
assert_eq!(stream.len(), 5000);
assert_eq!(stream.seek(SeekFrom::Current(0)).unwrap(), 1500);
assert_eq!(stream.stream_position().unwrap(), 1500);
stream.write_all(&vec![b'z'; 500]).unwrap();
assert_eq!(stream.len(), 5000);
assert_eq!(stream.seek(SeekFrom::Current(0)).unwrap(), 2000);
assert_eq!(stream.stream_position().unwrap(), 2000);
}

let cursor = comp.into_inner();
Expand Down
2 changes: 1 addition & 1 deletion tests/malformed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where

// Checks to see if a file can be walked over and read properly, or fail if it can not be read
fn can_read(path: &Path) {
let data = std::fs::read(&path).unwrap();
let data = std::fs::read(path).unwrap();

let cursor = Cursor::new(data);
let mut cfb = match CompoundFile::open(cursor) {
Expand Down

0 comments on commit 959e029

Please sign in to comment.