aff4-image 0.2.0

Experimental Rust reader, verifier, and writer for AFF4 evidence containers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
use std::collections::BTreeMap;
use std::fs::{self, File};
#[path = "collect.rs"]
mod collect;
#[path = "collection_budget.rs"]
mod collection_budget;
pub use collection_budget::CollectionLimits;
#[path = "logical_metadata.rs"]
mod logical_metadata;
pub use collect::{CollectionIssue, CollectionOptions, CollectionReport};
pub use logical_metadata::{CaseMetadata, LogicalMetadata, SubstreamKind};
use std::io::{Read, Write};
use std::ops::ControlFlow;
use std::path::{Path, PathBuf};

use md5::{Digest, Md5};
use rayon::prelude::*;
use sha1::Sha1;
use sha2::{Sha256, Sha512};
use tempfile::NamedTempFile;
use zip::{ZipWriter, write::SimpleFileOptions};

use crate::{Error, Result, malformed, reader::hex};

/// Container profile chosen before any evidence bytes are written.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub enum Profile {
    /// AFF4 Standard 1.0 physical images with ImageStream storage.
    Physical,
    /// Legacy AFF4-L 1.1 logical files with ZIP segment storage.
    Logical,
}

/// Physical chunk codec. Logical ZIP segments use Stored or Deflate.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub enum Compression {
    /// Uncompressed bytes.
    Stored,
    /// Zlib chunk compression / ZIP Deflate.
    Zlib,
    /// Raw Snappy chunks (physical profile only).
    Snappy,
    /// Raw LZ4 blocks (physical profile only).
    Lz4,
}

/// Bounded writer geometry. Output is always a new single ZIP volume.
#[derive(Debug, Clone, serde::Serialize)]
pub struct WriteOptions {
    /// Physical chunk bytes (1 through 16 MiB).
    pub chunk_bytes: u32,
    /// Physical chunks per bevy. A bevy cannot exceed 128 MiB uncompressed.
    pub chunks_per_bevy: u32,
    /// Physical codec or logical ZIP codec.
    pub compression: Compression,
}
impl Default for WriteOptions {
    fn default() -> Self {
        Self {
            chunk_bytes: 32768,
            chunks_per_bevy: 1024,
            compression: Compression::Zlib,
        }
    }
}

/// Digests of source bytes accepted for one image or file.
#[derive(Debug, Clone, serde::Serialize)]
pub struct AcquiredStream {
    /// Image or FileImage resource identifier.
    pub id: String,
    /// Exact byte count, excluding final chunk padding.
    pub size: u64,
    /// Hexadecimal MD5.
    pub md5: String,
    /// Hexadecimal SHA1.
    pub sha1: String,
    /// Hexadecimal SHA256.
    pub sha256: String,
}

/// Published acquisition result. Caller may reopen to verify independently.
#[derive(Debug, serde::Serialize)]
pub struct WriteResult {
    /// Final, exclusively created path.
    pub path: PathBuf,
    /// Per-resource source hashes.
    pub streams: Vec<AcquiredStream>,
    /// SHA256 of the exact information.turtle bytes; record externally.
    pub metadata_sha256: String,
}

/// Streaming writer with exclusive publication and no full-source spool.
///
/// A failed/cancelled input poisons the writer; finish then refuses publication.
/// Dropping the writer removes its temporary file. Crash leftovers may remain.
/// Inputs must be stable snapshots; device opening, retries, splitting, resume,
/// encryption, and metadata/ACL collection are outside this API.
pub struct Writer {
    zip: Option<ZipWriter<File>>,
    temporary: NamedTempFile,
    path: PathBuf,
    profile: Profile,
    volume: String,
    options: WriteOptions,
    metadata: String,
    archive_entries: u64,
    streams: Vec<AcquiredStream>,
    poisoned: bool,
    logical_zip_threshold: u64,
    folders: BTreeMap<String, (String, Vec<String>)>,
    roots: Vec<String>,
    path_separator: char,
}

impl Writer {
    /// Creates private staging beside a new destination. Existing files,
    /// including symlinks, are never overwritten.
    pub fn create(path: impl AsRef<Path>, profile: Profile, options: WriteOptions) -> Result<Self> {
        if options.chunk_bytes == 0
            || options.chunk_bytes > 16 * 1024 * 1024
            || options.chunks_per_bevy == 0
            || u64::from(options.chunk_bytes) * u64::from(options.chunks_per_bevy)
                > 128 * 1024 * 1024
        {
            return Err(malformed("invalid writer chunk geometry"));
        }
        if profile == Profile::Logical
            && !matches!(options.compression, Compression::Stored | Compression::Zlib)
        {
            return Err(Error::Unsupported(
                "logical ZIP compression must be Stored or Zlib".into(),
            ));
        }
        let input_path = path.as_ref();
        let name = input_path
            .file_name()
            .ok_or_else(|| malformed("missing output filename"))?;
        #[cfg(windows)]
        if name.to_string_lossy().contains(':') {
            return Err(malformed("alternate-stream output is unsupported"));
        }
        let parent = fs::canonicalize(
            input_path
                .parent()
                .filter(|p| !p.as_os_str().is_empty())
                .unwrap_or_else(|| Path::new(".")),
        )?;
        let path = parent.join(name);
        match fs::symlink_metadata(&path) {
            Ok(_) => return Err(malformed("output already exists")),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
            Err(e) => return Err(e.into()),
        }
        let temporary = tempfile::Builder::new()
            .prefix(".aff4-acquire-")
            .tempfile_in(&parent)?;
        let mut zip = ZipWriter::new(temporary.as_file().try_clone()?);
        let volume = identifier();
        zip.set_comment(volume.as_str())?;
        write_member(&mut zip, "container.description", volume.as_bytes())?;
        let version = format!(
            "major=1\nminor={}\ntool=aff4-image {}\n",
            match profile {
                Profile::Physical => 0,
                Profile::Logical => 1,
            },
            env!("CARGO_PKG_VERSION")
        );
        write_member(&mut zip, "version.txt", version.as_bytes())?;
        let metadata = format!(
            "@prefix a: <http://aff4.org/Schema#> .\n@prefix l: <https://aff4.org/Schema/2022/#> .\n<{volume}> a a:ZipVolume .\n"
        );
        Ok(Self {
            zip: Some(zip),
            temporary,
            path,
            profile,
            volume,
            options,
            metadata,
            archive_entries: 2,
            streams: Vec::new(),
            poisoned: false,
            logical_zip_threshold: 1024 * 1024,
            folders: BTreeMap::new(),
            roots: Vec::new(),
            path_separator: '/',
        })
    }

    /// Acquires exactly `size` bytes as a physical ImageStream, preserving
    /// trailing input bytes for the caller. Progress is before/after chunks.
    pub fn add_image(
        &mut self,
        size: u64,
        input: &mut impl Read,
        progress: impl FnMut(u64, u64) -> ControlFlow<()>,
    ) -> Result<String> {
        self.healthy()?;
        if self.profile != Profile::Physical {
            return Err(Error::Unsupported(
                "physical image in a logical writer".into(),
            ));
        }
        self.add_chunked(size, input, progress, None, "")
    }

    /// Adds a physical image with its known logical sector size (`blockSize`).
    /// The size must be nonzero, sector aligned, and the sector size a power of two.
    pub fn add_image_with_sector_size(
        &mut self,
        size: u64,
        sector_size: u32,
        input: &mut impl Read,
        progress: impl FnMut(u64, u64) -> ControlFlow<()>,
    ) -> Result<String> {
        self.healthy()?;
        if !sector_size.is_power_of_two()
            || size == 0
            || !size.is_multiple_of(u64::from(sector_size))
        {
            return Err(malformed("invalid physical sector geometry"));
        }
        let id = self.add_image(size, input, progress)?;
        self.metadata
            .push_str(&format!("<{id}> a:blockSize {sector_size} .\n"));
        Ok(id)
    }

    /// Sets the largest logical file stored as one ZIP segment (at most 1 GiB).
    /// Larger files use chunked ImageStreams with Maps and block integrity hashes.
    pub fn set_logical_zip_threshold(&mut self, bytes: u64) -> Result<()> {
        self.healthy()?;
        if bytes > 1024 * 1024 * 1024 {
            return Err(malformed("ZIP threshold exceeds 1 GiB"));
        }
        self.logical_zip_threshold = bytes;
        Ok(())
    }

    fn add_chunked(
        &mut self,
        size: u64,
        input: &mut impl Read,
        mut progress: impl FnMut(u64, u64) -> ControlFlow<()>,
        logical_path: Option<&str>,
        extra_properties: &str,
    ) -> Result<String> {
        self.poisoned = true;
        let entries = size
            .div_ceil(u64::from(self.options.chunk_bytes))
            .div_ceil(u64::from(self.options.chunks_per_bevy))
            .checked_mul(4)
            .and_then(|n| n.checked_add(if logical_path.is_some() { 0 } else { 2 }))
            .ok_or_else(|| malformed("archive entry count overflow"))?;
        self.archive_entries = self
            .archive_entries
            .checked_add(entries)
            .ok_or_else(|| malformed("archive entry count overflow"))?;
        let id = if logical_path.is_some() {
            format!("{}/files/{}", self.volume, uuid::Uuid::new_v4())
        } else {
            identifier()
        };
        let stream = if logical_path.is_some() {
            id.clone()
        } else {
            identifier()
        };
        let map = identifier();
        let storage = if logical_path.is_some() {
            id.strip_prefix(&format!("{}/", self.volume))
                .unwrap()
                .to_owned()
        } else {
            format!("aff4%3A%2F%2F{}", stream.strip_prefix("aff4://").unwrap())
        };
        let mut hashes = Hashes::new();
        let mut done = 0;
        let mut chunk = 0u64;
        let mut index = Vec::new();
        let mut md5_blocks = Vec::new();
        let mut sha256_blocks = Vec::new();
        let mut md5_tree = Sha512::new();
        let mut sha256_tree = Sha512::new();
        let mut index_tree = Sha512::new();
        let zip = self.zip.as_mut().unwrap();
        while done < size {
            let bevy = chunk / u64::from(self.options.chunks_per_bevy);
            zip.start_file(format!("{storage}/{bevy:08}"), stored())?;
            let mut bevy_bytes = 0u64;
            index.clear();
            md5_blocks.clear();
            sha256_blocks.clear();

            let bevy_end = chunk + u64::from(self.options.chunks_per_bevy);
            while done < size && chunk < bevy_end {
                let batch_chunks = automatic_encode_batch_chunks(self.options.chunk_bytes as usize)
                    .min(usize::try_from(bevy_end - chunk).unwrap_or(usize::MAX));
                let mut buffers = Vec::with_capacity(batch_chunks);
                let mut lengths = Vec::with_capacity(batch_chunks);
                while done < size && chunk + (buffers.len() as u64) < bevy_end {
                    if buffers.len() == batch_chunks {
                        break;
                    }
                    if progress(done, size).is_break() {
                        return Err(Error::Aborted);
                    }
                    let length = (size - done).min(u64::from(self.options.chunk_bytes)) as usize;
                    let mut buffer = vec![0; self.options.chunk_bytes as usize];
                    input.read_exact(&mut buffer[..length])?;
                    hashes.update(&buffer[..length]);
                    done += length as u64;
                    buffers.push(buffer);
                    lengths.push(length);
                }

                let prepared = prepare_chunks(&buffers, &lengths, self.options.compression)?;
                for (buffer, prepared) in buffers.iter().zip(prepared) {
                    md5_blocks.extend_from_slice(&prepared.md5);
                    sha256_blocks.extend_from_slice(&prepared.sha256);
                    let encoded = if self.options.compression != Compression::Stored
                        && prepared.encoded.len() < buffer.len().saturating_sub(16)
                    {
                        &prepared.encoded
                    } else {
                        buffer
                    };
                    zip.write_all(encoded)?;
                    index.extend_from_slice(&bevy_bytes.to_le_bytes());
                    index.extend_from_slice(&(encoded.len() as u32).to_le_bytes());
                    bevy_bytes += encoded.len() as u64;
                    chunk += 1;
                }
            }
            write_member(zip, &format!("{storage}/{bevy:08}.index"), &index)?;
            write_member(
                zip,
                &format!("{storage}/{bevy:08}.blockHash.md5"),
                &md5_blocks,
            )?;
            write_member(
                zip,
                &format!("{storage}/{bevy:08}.blockHash.sha256"),
                &sha256_blocks,
            )?;
            md5_tree.update(&md5_blocks);
            sha256_tree.update(&sha256_blocks);
            index_tree.update(&index);
        }
        if progress(done, size).is_break() {
            return Err(Error::Aborted);
        }
        let result = hashes.finish(id.clone(), size);
        let map_storage = format!("aff4%3A%2F%2F{}", map.strip_prefix("aff4://").unwrap());
        let mut range = Vec::new();
        if size != 0 {
            range.extend_from_slice(&0u64.to_le_bytes());
            range.extend_from_slice(&size.to_le_bytes());
            range.extend_from_slice(&0u64.to_le_bytes());
            range.extend_from_slice(&0u32.to_le_bytes());
        }
        if logical_path.is_none() {
            write_member(zip, &format!("{map_storage}/map"), &range)?;
            write_member(
                zip,
                &format!("{map_storage}/idx"),
                format!("{stream}\n").as_bytes(),
            )?;
        }
        let index_hash = hex(&index_tree.finalize());
        let md5_tree = md5_tree.finalize();
        let sha256_tree = sha256_tree.finalize();
        let points_hash = Sha512::digest(&range);
        let target_index = format!("{stream}\n");
        let target_hash = Sha512::digest(target_index.as_bytes());
        let empty_hash = Sha512::digest([]);
        let mut composite = Sha512::new();
        if size != 0 {
            composite.update(md5_tree);
            composite.update(sha256_tree);
        }
        composite.update(points_hash);
        composite.update(target_hash);
        composite.update(empty_hash);
        let composite = hex(&composite.finalize());
        let mut map_hash = Sha512::new();
        map_hash.update(&range);
        map_hash.update(target_index.as_bytes());
        let map_hash = hex(&map_hash.finalize());
        let compression: String = match self.options.compression {
            Compression::Stored => {
                "a:compressionMethod <http://aff4.org/Schema#compression/stored>;".into()
            }
            Compression::Zlib => {
                "a:compressionMethod <https://www.ietf.org/rfc/rfc1950.txt>;".into()
            }
            Compression::Snappy => "a:compressionMethod <http://code.google.com/p/snappy/>;".into(),
            Compression::Lz4 => "a:compressionMethod <https://code.google.com/p/lz4/>;".into(),
        };
        let kind = if logical_path.is_some() {
            "FileImage"
        } else {
            "DiskImage"
        };
        let logical_properties = logical_path
            .map(|path| {
                let path_literal = oxrdf::Literal::new_simple_literal(path).to_string();
                let name = oxrdf::Literal::new_simple_literal(
                    path.rsplit(['/', '\\']).next().unwrap_or(path),
                )
                .to_string();
                format!("a:originalFileName {path_literal}; a:fileName {name};")
            })
            .unwrap_or_default();
        let volume = &self.volume;
        if logical_path.is_some() {
            self.metadata.push_str(&format!("<{id}> a a:FileImage, a:Image, a:ImageStream; {extra_properties} {logical_properties} a:size {size}; a:chunkSize {}; a:chunksInSegment {}; {compression} a:stored <{volume}>; a:imageStreamIndexHash \"{index_hash}\"^^a:SHA512; {} .\n", self.options.chunk_bytes, self.options.chunks_per_bevy, hash_triples(&result)));
        } else {
            self.metadata.push_str(&format!("<{stream}> a a:ImageStream; a:size {size}; a:chunkSize {}; a:chunksInSegment {}; {compression} a:target <{map}>; a:stored <{volume}>; a:imageStreamIndexHash \"{index_hash}\"^^a:SHA512 .\n<{id}> a a:Image, a:ContiguousImage, a:{kind}; {logical_properties} a:size {size}; a:dataStream <{map}>; a:stored <{volume}>; {}; a:hash \"{composite}\"^^a:blockMapHashSHA512 .\n<{map}> a a:Map; a:size {size}; a:dependentStream <{stream}>; a:target <{id}>; a:stored <{volume}>; a:blockMapHash \"{composite}\"^^a:SHA512; a:mapPointHash \"{}\"^^a:SHA512; a:mapIdxHash \"{}\"^^a:SHA512; a:mapPathHash \"{}\"^^a:SHA512; a:mapHash \"{map_hash}\"^^a:SHA512 .\n", self.options.chunk_bytes, self.options.chunks_per_bevy, hash_triples(&result), hex(&points_hash), hex(&target_hash), hex(&empty_hash)));
        }
        if size != 0 {
            self.metadata.push_str(&format!("<{stream}/blockhash.md5> a a:BlockHashes; a:hash \"{}\"^^a:SHA512 .\n<{stream}/blockhash.sha256> a a:BlockHashes; a:hash \"{}\"^^a:SHA512 .\n", hex(&md5_tree), hex(&sha256_tree)));
        }
        self.streams.push(result);
        self.poisoned = false;
        Ok(id)
    }

    /// Acquires one logical file as a ZIP segment with recorded original path.
    /// Names stay in metadata; ZIP storage uses a generated identifier.
    /// Files above the configured threshold use ImageStreams with block hashes.
    pub fn add_file(
        &mut self,
        original_path: &str,
        size: u64,
        input: &mut impl Read,
        progress: impl FnMut(u64, u64) -> ControlFlow<()>,
    ) -> Result<String> {
        self.add_file_impl(original_path, size, input, progress, "")
    }

    fn add_file_impl(
        &mut self,
        original_path: &str,
        size: u64,
        input: &mut impl Read,
        mut progress: impl FnMut(u64, u64) -> ControlFlow<()>,
        extra_properties: &str,
    ) -> Result<String> {
        self.healthy()?;
        if self.profile != Profile::Logical {
            return Err(Error::Unsupported(
                "logical file in a physical writer".into(),
            ));
        }
        if original_path.is_empty() || original_path.chars().any(char::is_control) {
            return Err(malformed(
                "logical path must be nonempty UTF-8 without control characters",
            ));
        }
        if size > self.logical_zip_threshold {
            return self.add_chunked(size, input, progress, Some(original_path), extra_properties);
        }
        self.poisoned = true;
        let storage = format!("files/{}", uuid::Uuid::new_v4());
        let id = format!("{}/{storage}", self.volume);
        let zip = self.zip.as_mut().unwrap();
        let method = if self.options.compression == Compression::Stored {
            zip::CompressionMethod::Stored
        } else {
            zip::CompressionMethod::Deflated
        };
        self.archive_entries = self
            .archive_entries
            .checked_add(1)
            .ok_or_else(|| malformed("archive entry count overflow"))?;
        zip.start_file(&storage, stored().compression_method(method))?;
        let mut hashes = Hashes::new();
        let mut buffer = vec![0; 1024 * 1024];
        let mut done = 0;
        loop {
            if progress(done, size).is_break() {
                return Err(Error::Aborted);
            }
            if done == size {
                break;
            }
            let length = (size - done).min(buffer.len() as u64) as usize;
            input.read_exact(&mut buffer[..length])?;
            zip.write_all(&buffer[..length])?;
            hashes.update(&buffer[..length]);
            done += length as u64;
        }
        let result = hashes.finish(id.clone(), size);
        let name = original_path
            .rsplit(['/', '\\'])
            .next()
            .unwrap_or(original_path);
        let name = oxrdf::Literal::new_simple_literal(name).to_string();
        let path = oxrdf::Literal::new_simple_literal(original_path).to_string();
        self.metadata.push_str(&format!("<{id}> a a:FileImage, a:Image, a:zip_segment; {extra_properties} a:size {size}; a:fileName {name}; a:originalFileName {path}; a:stored <{}>; {} .\n", self.volume, hash_triples(&result)));
        self.streams.push(result);
        self.poisoned = false;
        Ok(id)
    }

    /// Finalizes ZIP metadata, synchronizes bytes, and publishes exclusively.
    /// Unix additionally synchronizes the parent directory. This API does not
    /// certify power-loss durability on every filesystem or device.
    /// `Error::PublishedButUnsynced` retains the result if that last sync fails.
    pub fn finish(self) -> Result<WriteResult> {
        self.finish_checked(|_, _| Ok(()))
            .map(|(result, ())| result)
    }

    /// Verifies the finalized temporary container under explicit reader budgets
    /// before exclusive publication. Limit, integrity, or cancellation failures
    /// remove the temporary file and leave the destination absent.
    /// `VerificationFailed` retains the complete available verification report.
    /// `PublishedButUnsynced` means verification passed but directory sync failed.
    pub fn finish_verified(
        self,
        limits: crate::Limits,
        mut progress: impl FnMut(&str, u64, u64) -> ControlFlow<()>,
    ) -> Result<(WriteResult, crate::ContainerVerification)> {
        self.finish_checked(|path, digest| {
            if progress("staged verification", 0, 0).is_break() {
                return Err(Error::Aborted);
            }
            let verification = crate::Container::open_with_limits(path, limits)?
                .verify_all(Some(digest), &mut progress)?;
            if !verification.all_match() {
                return Err(Error::VerificationFailed {
                    report: Box::new(verification),
                });
            }
            if progress("before publication", 0, 0).is_break() {
                return Err(Error::Aborted);
            }
            Ok(verification)
        })
    }

    fn finish_checked<T>(
        mut self,
        check: impl FnOnce(&Path, &str) -> Result<T>,
    ) -> Result<(WriteResult, T)> {
        self.healthy()?;
        self.finish_logical_metadata();
        let mut zip = self.zip.take().unwrap();
        write_member(&mut zip, "information.turtle", self.metadata.as_bytes())?;
        let metadata_sha256 = hex(&Sha256::digest(self.metadata.as_bytes()));
        let hashes = format!(
            "@prefix a: <http://aff4.org/Schema#> .\n<{}/information.turtle> a:hash \"{}\"^^a:SHA256 .\n",
            self.volume, metadata_sha256
        );
        write_member(&mut zip, "information.turtle.hashes", hashes.as_bytes())?;
        #[cfg(test)]
        failure_tests::boundary("metadata");
        let file = zip.finish()?;
        #[cfg(test)]
        failure_tests::boundary("zip_closed");
        file.sync_all()?;
        drop(file);
        // The finalized ZIP now owns these bytes. Release the write-side graph
        // before staged verification allocates its bounded read-side graph.
        self.metadata = String::new();
        self.folders.clear();
        self.roots = Vec::new();
        #[cfg(test)]
        failure_tests::boundary("file_synced");
        let checked = check(self.temporary.path(), &metadata_sha256)?;
        self.temporary
            .persist_noclobber(&self.path)
            .map_err(|error| Error::Io(error.error))?;
        let result = WriteResult {
            path: self.path,
            streams: self.streams,
            metadata_sha256,
        };
        #[cfg(test)]
        failure_tests::boundary("published");
        sync_published_directory(&result.path).map_err(|source| Error::PublishedButUnsynced {
            result: Box::new(WriteResult {
                path: result.path.clone(),
                streams: result.streams.clone(),
                metadata_sha256: result.metadata_sha256.clone(),
            }),
            source,
        })?;
        #[cfg(test)]
        failure_tests::boundary("directory_synced");
        Ok((result, checked))
    }

    fn healthy(&self) -> Result<()> {
        if self.poisoned {
            return Err(Error::Unsupported(
                "acquisition failed; discard this writer".into(),
            ));
        }
        Ok(())
    }
}

fn sync_published_directory(path: &Path) -> std::io::Result<()> {
    #[cfg(test)]
    if failure_tests::FAIL_DIRECTORY_SYNC.get() {
        return Err(std::io::Error::other("injected directory sync failure"));
    }
    #[cfg(unix)]
    File::open(path.parent().unwrap())?.sync_all()?;
    #[cfg(not(unix))]
    let _ = path;
    Ok(())
}

#[cfg(test)]
#[path = "writer_failure_tests.rs"]
mod failure_tests;

fn identifier() -> String {
    format!("aff4://{}", uuid::Uuid::new_v4())
}
fn stored() -> SimpleFileOptions {
    SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Stored)
        .large_file(true)
}
fn write_member(zip: &mut ZipWriter<File>, name: &str, data: &[u8]) -> Result<()> {
    zip.start_file(name, stored())?;
    zip.write_all(data)?;
    Ok(())
}
fn hash_triples(result: &AcquiredStream) -> String {
    format!(
        "a:hash \"{}\"^^a:MD5, \"{}\"^^a:SHA1, \"{}\"^^a:SHA256",
        result.md5, result.sha1, result.sha256
    )
}

struct PreparedChunk {
    encoded: Vec<u8>,
    md5: [u8; 16],
    sha256: [u8; 32],
}

fn prepare_chunks(
    buffers: &[Vec<u8>],
    lengths: &[usize],
    codec: Compression,
) -> Result<Vec<PreparedChunk>> {
    let prepare = |(buffer, length): (&Vec<u8>, &usize)| {
        Ok(PreparedChunk {
            encoded: if codec == Compression::Stored {
                Vec::new()
            } else {
                encode(buffer, codec)?
            },
            md5: Md5::digest(&buffer[..*length]).into(),
            sha256: Sha256::digest(&buffer[..*length]).into(),
        })
    };
    if buffers.len() > 1 {
        buffers.par_iter().zip(lengths).map(prepare).collect()
    } else {
        buffers.iter().zip(lengths).map(prepare).collect()
    }
}

fn automatic_encode_batch_chunks(chunk_size: usize) -> usize {
    const MAXIMUM_BATCH_BYTES: usize = 8 * 1024 * 1024;
    let memory_limit = MAXIMUM_BATCH_BYTES
        .checked_div(chunk_size)
        .unwrap_or(0)
        .max(1);
    rayon::current_num_threads()
        .saturating_mul(2)
        .min(memory_limit)
        .max(1)
}

fn encode(data: &[u8], codec: Compression) -> Result<Vec<u8>> {
    match codec {
        Compression::Stored => Ok(data.to_vec()),
        Compression::Zlib => {
            let mut encoder =
                flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default());
            encoder.write_all(data)?;
            Ok(encoder.finish()?)
        }
        Compression::Snappy => snap::raw::Encoder::new()
            .compress_vec(data)
            .map_err(|e| malformed(e.to_string())),
        Compression::Lz4 => Ok(lz4_flex::block::compress(data)),
    }
}
struct Hashes {
    md5: Md5,
    sha1: Sha1,
    sha256: Sha256,
}
impl Hashes {
    fn new() -> Self {
        Self {
            md5: Md5::new(),
            sha1: Sha1::new(),
            sha256: Sha256::new(),
        }
    }
    fn update(&mut self, data: &[u8]) {
        self.md5.update(data);
        self.sha1.update(data);
        self.sha256.update(data);
    }
    fn finish(self, id: String, size: u64) -> AcquiredStream {
        AcquiredStream {
            id,
            size,
            md5: hex(&self.md5.finalize()),
            sha1: hex(&self.sha1.finalize()),
            sha256: hex(&self.sha256.finalize()),
        }
    }
}