Skip to content

Commit d578e10

Browse files
committed
wip
1 parent 50e9051 commit d578e10

File tree

8 files changed

+26
-8
lines changed

8 files changed

+26
-8
lines changed

README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,13 @@ Future breaking changes will result in a major version bump.
7676
...
7777
[trailer]
7878
[magic, 4 bytes]
79-
; TODO: put version here, change magic(s)
79+
[version, 1 byte, 0x1]
8080
[checksum type, 1 byte, always 0x0]
8181
[toc checksum, 16 bytes]
82-
[toc_pointer, 8 bytes]
82+
[toc pos, 8 bytes]
83+
[toc len, 8 bytes]
8384
```
8485

85-
<!-- TODO: store ToC len (in bytes) in trailer -->
86-
8786
All integers are little-endian encoded.
8887

8988
## License

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ pub enum Error {
1010
/// IO error
1111
Io(std::io::Error),
1212

13+
/// Invalid header
14+
InvalidHeader,
15+
1316
/// Unsupported file format version
1417
InvalidVersion,
1518

src/toc/writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{checksum::Checksum, toc::entry::TocEntry};
66
use byteorder::WriteBytesExt;
77
use std::io::Write;
88

9-
pub const TOC_MAGIC: &[u8] = b"TOC1";
9+
pub const TOC_MAGIC: &[u8] = b"TOC!";
1010

1111
struct ChecksummedWriter<W: std::io::Write> {
1212
inner: W,

src/trailer/reader.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use byteorder::ReadBytesExt;
88
use std::io::{Read, Seek, SeekFrom};
99

1010
#[allow(clippy::cast_possible_wrap)]
11-
const TRAILER_SIZE: i64 = TRAILER_MAGIC.len() as i64 + 1 + 16 + 8;
11+
const TRAILER_SIZE: i64 = TRAILER_MAGIC.len() as i64 + 1 + 1 + 16 + 8 + 8;
1212

1313
#[derive(Debug, Eq, PartialEq)]
1414
pub struct ParsedTrailer {
@@ -31,6 +31,14 @@ impl TrailerReader {
3131
reader.read_exact(&mut buf)?;
3232

3333
if buf != TRAILER_MAGIC {
34+
log::error!("Invalid trailer header");
35+
return Err(crate::Error::InvalidHeader);
36+
}
37+
}
38+
39+
{
40+
let version = reader.read_u8()?;
41+
if version != 0x1 {
3442
log::error!("Invalid version");
3543
return Err(crate::Error::InvalidVersion);
3644
}
@@ -46,6 +54,7 @@ impl TrailerReader {
4654

4755
let toc_checksum = Checksum::from_raw(reader.read_u128::<LE>()?);
4856
let toc_pos = reader.read_u64::<LE>()?;
57+
// let _toc_len = reader.read_u64::<LE>()?;
4958

5059
Ok(ParsedTrailer {
5160
toc_checksum,

src/trailer/writer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::checksum::Checksum;
66
use byteorder::WriteBytesExt;
77

8-
pub const TRAILER_MAGIC: &[u8] = b"TRA1";
8+
pub const TRAILER_MAGIC: &[u8] = b"SFA!";
99

1010
pub struct TrailerWriter;
1111

@@ -14,15 +14,18 @@ impl TrailerWriter {
1414
mut writer: W,
1515
toc_checksum: Checksum,
1616
toc_pos: u64,
17+
toc_len: u64,
1718
) -> crate::Result<()> {
1819
use byteorder::LE;
1920

2021
log::trace!("Writing trailer");
2122

2223
writer.write_all(TRAILER_MAGIC)?;
24+
writer.write_u8(0x1)?; // Version
2325
writer.write_u8(0x0)?; // Checksum type, xxh3 = 0x0
2426
writer.write_u128::<LE>(toc_checksum.into_u128())?;
2527
writer.write_u64::<LE>(toc_pos)?;
28+
writer.write_u64::<LE>(toc_len)?;
2629

2730
Ok(())
2831
}

src/writer.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ impl Writer {
102102
let toc_pos = self.writer.stream_position()?;
103103
let toc_checksum = TocWriter::write_into(&mut self.writer, &self.toc)?;
104104

105+
let after_toc_pos = self.writer.stream_position()?;
106+
let toc_len = after_toc_pos - toc_pos;
107+
105108
// Write trailer
106-
TrailerWriter::write_into(&mut self.writer, toc_checksum, toc_pos)?;
109+
TrailerWriter::write_into(&mut self.writer, toc_checksum, toc_pos, toc_len)?;
107110

108111
// Flush & sync
109112
log::trace!("Syncing file");

test_fixture/cherry_pie_broken

9 Bytes
Binary file not shown.

tests/simple.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::io::{Read, Write};
55
pub fn cherry_pie() -> Result<(), sfa::Error> {
66
let dir = tempfile::tempdir()?;
77
let path = dir.path().join("cherry_pie");
8+
// let path = std::path::Path::new("test_fixture/cherry_pie_broken");
89

910
let mut writer = Writer::new_at_path(&path)?;
1011
writer.start("Verse 1")?;

0 commit comments

Comments
 (0)