fstool 0.1.0

Build disk images and filesystems (ext2/3/4, MBR, GPT) from a directory tree and TOML spec, in the spirit of genext2fs.
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
//! Unit tests for the NTFS read implementation.
//!
//! We hand-craft minimal NTFS-like images (no `mkntfs` available) and
//! verify each layer in isolation: boot decode, USA fixup, attribute
//! walk, $INDEX_ROOT decode, then a full `list_path` / `open_file_reader`
//! / `read_xattrs` round trip on a single-directory image.

use super::*;
use crate::block::MemoryBackend;

const BPS: u16 = 512;
const SPC: u8 = 8; // 4 KiB clusters
const REC_SIZE: u32 = 1024; // 1 KiB MFT record

fn fake_boot(bps: u16, spc: u8, mft_lcn: u64, mft_rec: i8) -> Vec<u8> {
    let mut v = vec![0u8; 512];
    v[0..3].copy_from_slice(&[0xEB, 0x52, 0x90]);
    v[3..11].copy_from_slice(boot::NTFS_OEM);
    v[11..13].copy_from_slice(&bps.to_le_bytes());
    v[13] = spc;
    v[0x28..0x30].copy_from_slice(&1024u64.to_le_bytes());
    v[0x30..0x38].copy_from_slice(&mft_lcn.to_le_bytes());
    v[0x38..0x40].copy_from_slice(&(mft_lcn + 1).to_le_bytes());
    v[0x40] = mft_rec as u8;
    v[0x44] = 1;
    v[0x48..0x50].copy_from_slice(&0x1234_5678_9abc_def0u64.to_le_bytes());
    v
}

#[test]
fn decode_recognises_oem_id() {
    let buf = fake_boot(512, 8, 4, -10);
    let bs = boot::BootSector::decode(&buf).unwrap();
    assert_eq!(bs.bytes_per_sector, 512);
    assert_eq!(bs.sectors_per_cluster, 8);
    assert_eq!(bs.mft_record_size(), 1024);
    assert_eq!(bs.cluster_size(), 4096);
}

#[test]
fn decode_handles_positive_clusters_per_mft_record() {
    let buf = fake_boot(512, 8, 4, 2);
    let bs = boot::BootSector::decode(&buf).unwrap();
    assert_eq!(bs.mft_record_size(), 8192);
}

#[test]
fn decode_rejects_wrong_oem() {
    let mut buf = fake_boot(512, 8, 4, -10);
    buf[3..11].copy_from_slice(b"EXFAT   ");
    assert!(boot::BootSector::decode(&buf).is_none());
}

#[test]
fn probe_detects_ntfs() {
    let mut dev = MemoryBackend::new(4096);
    dev.write_at(0, &fake_boot(512, 8, 4, -10)).unwrap();
    assert!(probe(&mut dev).unwrap());
}

#[test]
fn fixup_roundtrip() {
    // Build a 1024-byte record where bytes 510..512 and 1022..1024 are
    // distinctive. install_fixup then apply_fixup must restore them.
    let mut buf = vec![0u8; 1024];
    buf[0..4].copy_from_slice(b"FILE");
    buf[4..6].copy_from_slice(&42u16.to_le_bytes()); // usa_offset
    buf[6..8].copy_from_slice(&3u16.to_le_bytes()); // usa_size (USN + 2 sectors)
    buf[510] = 0xAA;
    buf[511] = 0xBB;
    buf[1022] = 0xCC;
    buf[1023] = 0xDD;
    mft::install_fixup(&mut buf, 512, 0x7777);
    // The tails are now 0x77 0x77; originals are stashed in the USA.
    assert_eq!(buf[510], 0x77);
    assert_eq!(buf[511], 0x77);
    mft::apply_fixup(&mut buf, 512).unwrap();
    assert_eq!(buf[510], 0xAA);
    assert_eq!(buf[511], 0xBB);
    assert_eq!(buf[1022], 0xCC);
    assert_eq!(buf[1023], 0xDD);
}

#[test]
fn fixup_detects_torn_write() {
    let mut buf = vec![0u8; 1024];
    buf[0..4].copy_from_slice(b"FILE");
    buf[4..6].copy_from_slice(&42u16.to_le_bytes());
    buf[6..8].copy_from_slice(&3u16.to_le_bytes());
    mft::install_fixup(&mut buf, 512, 0x7777);
    // Corrupt the first sector's tail to simulate a torn write.
    buf[511] = 0x00;
    let err = mft::apply_fixup(&mut buf, 512).unwrap_err();
    assert!(matches!(err, crate::Error::InvalidImage(_)));
}

// ---------------------------------------------------------------------
// Minimal whole-volume fixture.
//
// Layout (cluster size = 4 KiB):
//   LBA 0:    boot sector
//   cluster 4 (offset 0x4000):  MFT starts here
//     record 0:  $MFT itself, with one non-resident $DATA run covering
//                clusters 4..6 (8 KiB of MFT).
//     record 5:  root dir, with $INDEX_ROOT $I30 containing the entry
//                for "hello.txt" + a tail entry, no $INDEX_ALLOCATION.
//     record 6:  hello.txt with resident $DATA = b"hi\n" and a named
//                $DATA "stream1" with b"AAAA".
//
// We write each record raw (no fixup) then call install_fixup to make
// the USAs valid.
// ---------------------------------------------------------------------

fn build_attr_header(
    type_code: u32,
    total_length: u32,
    non_resident: bool,
    name_len_u16: u8,
    name_off: u16,
    flags: u16,
    attr_id: u16,
) -> Vec<u8> {
    let mut h = vec![0u8; 16];
    h[0..4].copy_from_slice(&type_code.to_le_bytes());
    h[4..8].copy_from_slice(&total_length.to_le_bytes());
    h[8] = non_resident as u8;
    h[9] = name_len_u16;
    h[10..12].copy_from_slice(&name_off.to_le_bytes());
    h[12..14].copy_from_slice(&flags.to_le_bytes());
    h[14..16].copy_from_slice(&attr_id.to_le_bytes());
    h
}

/// Build a resident attribute (header + resident-specific fields + value).
fn build_resident_attr(type_code: u32, name_utf16: &[u8], value: &[u8], attr_id: u16) -> Vec<u8> {
    // Layout: 16 byte common header + 8 byte resident header + name (UTF16) + padding to 8 + value
    let name_len_u16 = (name_utf16.len() / 2) as u8;
    let name_off: u16 = if name_utf16.is_empty() { 0 } else { 0x18 };
    let header_block_len = 0x18 + name_utf16.len();
    let header_block_aligned = (header_block_len + 7) & !7;
    let total = header_block_aligned + value.len();
    let total = (total + 7) & !7;
    let value_offset = header_block_aligned as u16;

    let mut buf = Vec::with_capacity(total);
    let mut hdr = build_attr_header(
        type_code,
        total as u32,
        false,
        name_len_u16,
        name_off,
        0,
        attr_id,
    );
    // Resident-specific:
    let mut resident = vec![0u8; 8];
    resident[0..4].copy_from_slice(&(value.len() as u32).to_le_bytes());
    resident[4..6].copy_from_slice(&value_offset.to_le_bytes());
    resident[6] = 0;
    hdr.extend_from_slice(&resident);
    buf.extend_from_slice(&hdr);
    if !name_utf16.is_empty() {
        buf.extend_from_slice(name_utf16);
    }
    while buf.len() < header_block_aligned {
        buf.push(0);
    }
    buf.extend_from_slice(value);
    while buf.len() < total {
        buf.push(0);
    }
    buf
}

/// Build a non-resident attribute with the given mapping-pairs blob.
#[allow(clippy::too_many_arguments)]
fn build_non_resident_attr(
    type_code: u32,
    name_utf16: &[u8],
    runs: &[u8],
    starting_vcn: u64,
    last_vcn: u64,
    allocated: u64,
    real: u64,
    initialized: u64,
    attr_id: u16,
) -> Vec<u8> {
    // Layout: 16 byte common header + 0x30 non-resident header bytes + name + runs
    let name_len_u16 = (name_utf16.len() / 2) as u8;
    let header_block_len = 0x40 + name_utf16.len();
    let header_block_aligned = (header_block_len + 7) & !7;
    let runs_off = header_block_aligned as u16;
    let total = ((header_block_aligned + runs.len()) + 7) & !7;

    let name_off: u16 = if name_utf16.is_empty() { 0 } else { 0x40 };
    let mut hdr = build_attr_header(
        type_code,
        total as u32,
        true,
        name_len_u16,
        name_off,
        0,
        attr_id,
    );
    let mut nonresident = vec![0u8; 0x30];
    nonresident[0x00..0x08].copy_from_slice(&starting_vcn.to_le_bytes());
    nonresident[0x08..0x10].copy_from_slice(&last_vcn.to_le_bytes());
    nonresident[0x10..0x12].copy_from_slice(&runs_off.to_le_bytes());
    nonresident[0x12..0x14].copy_from_slice(&0u16.to_le_bytes()); // compression unit
    nonresident[0x18..0x20].copy_from_slice(&allocated.to_le_bytes());
    nonresident[0x20..0x28].copy_from_slice(&real.to_le_bytes());
    nonresident[0x28..0x30].copy_from_slice(&initialized.to_le_bytes());
    hdr.extend_from_slice(&nonresident);
    let mut buf = hdr;
    if !name_utf16.is_empty() {
        buf.extend_from_slice(name_utf16);
    }
    while buf.len() < header_block_aligned {
        buf.push(0);
    }
    buf.extend_from_slice(runs);
    while buf.len() < total {
        buf.push(0);
    }
    buf
}

fn build_record(record_size: usize, flags: u16, attrs: Vec<Vec<u8>>) -> Vec<u8> {
    let mut rec = vec![0u8; record_size];
    rec[0..4].copy_from_slice(b"FILE");
    // usa_offset = 0x2A (42), usa_size depends on sectors covered.
    rec[4..6].copy_from_slice(&0x2Au16.to_le_bytes());
    let sectors = record_size / BPS as usize;
    let usa_size = (sectors + 1) as u16;
    rec[6..8].copy_from_slice(&usa_size.to_le_bytes());
    // first_attr_offset = aligned past USA. USA occupies 0x2A..(0x2A + usa_size*2).
    let usa_end = 0x2A + usa_size as usize * 2;
    let first_attr_off = ((usa_end + 7) & !7) as u16; // align to 8
    rec[0x14..0x16].copy_from_slice(&first_attr_off.to_le_bytes());
    rec[0x16..0x18].copy_from_slice(&flags.to_le_bytes());

    let mut cursor = first_attr_off as usize;
    for a in &attrs {
        rec[cursor..cursor + a.len()].copy_from_slice(a);
        cursor += a.len();
    }
    // Terminator
    let term = [0xFFu8, 0xFF, 0xFF, 0xFF];
    rec[cursor..cursor + 4].copy_from_slice(&term);
    cursor += 4;

    let bytes_in_use = cursor as u32;
    rec[0x18..0x1C].copy_from_slice(&bytes_in_use.to_le_bytes());
    rec[0x1C..0x20].copy_from_slice(&(record_size as u32).to_le_bytes());

    mft::install_fixup(&mut rec, BPS as usize, 0x0001);
    rec
}

fn utf16_le(s: &str) -> Vec<u8> {
    s.encode_utf16().flat_map(|u| u.to_le_bytes()).collect()
}

/// Build a $FILE_NAME attribute value (just the payload, no header).
fn build_file_name_value(
    parent_ref: u64,
    name: &str,
    flags: u32,
    real_size: u64,
    namespace: u8,
) -> Vec<u8> {
    let name_utf16 = utf16_le(name);
    let mut v = vec![0u8; 66 + name_utf16.len()];
    v[0..8].copy_from_slice(&parent_ref.to_le_bytes());
    // timestamps zero
    v[40..48].copy_from_slice(&real_size.to_le_bytes());
    v[48..56].copy_from_slice(&real_size.to_le_bytes());
    v[56..60].copy_from_slice(&flags.to_le_bytes());
    v[64] = (name_utf16.len() / 2) as u8;
    v[65] = namespace;
    v[66..].copy_from_slice(&name_utf16);
    v
}

/// Build an $INDEX_ROOT value with `entries` as raw entry blobs.
fn build_index_root_value(entries: &[Vec<u8>]) -> Vec<u8> {
    // 16 bytes header (indexed type + collation + index block size + cpib + padding)
    // 16 bytes index header
    // entries
    // The first 16 bytes:
    let mut v: Vec<u8> = Vec::new();
    v.extend_from_slice(&TYPE_FILE_NAME.to_le_bytes()); // indexed attr type
    v.extend_from_slice(&1u32.to_le_bytes()); // collation = filename
    v.extend_from_slice(&0u32.to_le_bytes()); // index block size (no allocation)
    v.push(0); // cpib
    v.extend_from_slice(&[0u8; 3]);

    // Index header at offset 16.
    let entries_total: usize = entries.iter().map(|e| e.len()).sum();
    let first_entry_offset = 16u32; // entries start right after the 16-byte index header
    let bytes_in_use = 16u32 + entries_total as u32;
    let bytes_allocated = bytes_in_use;
    let flags: u8 = 0; // SMALL_INDEX
    v.extend_from_slice(&first_entry_offset.to_le_bytes());
    v.extend_from_slice(&bytes_in_use.to_le_bytes());
    v.extend_from_slice(&bytes_allocated.to_le_bytes());
    v.push(flags);
    v.extend_from_slice(&[0u8; 3]);
    for e in entries {
        v.extend_from_slice(e);
    }
    v
}

/// Build an index entry holding a $FILE_NAME key. `child_vcn` adds a
/// child pointer (and the HAS_CHILD flag).
fn build_index_entry(
    file_ref: u64,
    file_name_value: &[u8],
    flags: u32,
    child_vcn: Option<u64>,
) -> Vec<u8> {
    let key_len = file_name_value.len();
    let mut payload_len = 16 + key_len;
    payload_len = (payload_len + 7) & !7;
    let entry_len = if child_vcn.is_some() {
        payload_len + 8
    } else {
        payload_len
    };
    let mut e = vec![0u8; entry_len];
    e[0..8].copy_from_slice(&file_ref.to_le_bytes());
    e[8..10].copy_from_slice(&(entry_len as u16).to_le_bytes());
    e[10..12].copy_from_slice(&(key_len as u16).to_le_bytes());
    let final_flags = flags
        | if child_vcn.is_some() {
            index::ENTRY_FLAG_HAS_CHILD
        } else {
            0
        };
    e[12..16].copy_from_slice(&final_flags.to_le_bytes());
    e[16..16 + key_len].copy_from_slice(file_name_value);
    if let Some(vcn) = child_vcn {
        let off = entry_len - 8;
        e[off..off + 8].copy_from_slice(&vcn.to_le_bytes());
    }
    e
}

/// Build a terminator (last) entry. Empty key, with optional child.
fn build_terminator_entry(child_vcn: Option<u64>) -> Vec<u8> {
    let mut payload_len = 16;
    let entry_len = if child_vcn.is_some() {
        payload_len += 8;
        payload_len
    } else {
        payload_len
    };
    let mut e = vec![0u8; entry_len];
    e[0..8].copy_from_slice(&0u64.to_le_bytes()); // file_ref = 0
    e[8..10].copy_from_slice(&(entry_len as u16).to_le_bytes());
    e[10..12].copy_from_slice(&0u16.to_le_bytes()); // key_len = 0
    let flags = index::ENTRY_FLAG_LAST
        | if child_vcn.is_some() {
            index::ENTRY_FLAG_HAS_CHILD
        } else {
            0
        };
    e[12..16].copy_from_slice(&flags.to_le_bytes());
    if let Some(vcn) = child_vcn {
        let off = entry_len - 8;
        e[off..off + 8].copy_from_slice(&vcn.to_le_bytes());
    }
    e
}

/// Builds a tiny but complete-enough NTFS image for the integration
/// tests. Returns (backend, mft byte offset).
fn build_tiny_image() -> MemoryBackend {
    let cluster_size = (BPS as u32) * (SPC as u32);
    let mft_lcn = 4u64;
    let mft_byte_off = mft_lcn * cluster_size as u64;

    // Total: 32 clusters = 128 KiB. Layout:
    //   cluster 4..6  : MFT (8 KiB == 8 records)
    //   cluster 8..9  : root dir's $INDEX_ALLOCATION (none, we use small index)
    //   cluster 10    : hello.txt's data... actually our $DATA is resident.
    //   cluster 12    : "stream1" named $DATA for hello.txt — resident too.
    let total_size = 32 * cluster_size as u64;
    let mut dev = MemoryBackend::new(total_size);

    // Boot sector
    let mut boot = fake_boot(BPS, SPC, mft_lcn, -10);
    dev.write_at(0, &boot[..]).unwrap();
    // Boot sector must specify the index-block-size field too; -12 = 4 KiB
    boot[0x44] = (-12i8) as u8;
    dev.write_at(0x44, &[(-12i8) as u8]).unwrap();

    // --- Record 0: $MFT ---
    // $STANDARD_INFORMATION (resident)
    let si_value = {
        let mut v = vec![0u8; 48];
        v[32..36].copy_from_slice(&0u32.to_le_bytes()); // attrs
        v
    };
    let si_attr = build_resident_attr(TYPE_STANDARD_INFORMATION, &[], &si_value, 0);
    // $FILE_NAME for $MFT (mostly cosmetic for record 0)
    let mft_fname_value = build_file_name_value(
        5,
        "$MFT",
        FileName::FLAG_DIRECTORY,
        8 * REC_SIZE as u64,
        FileName::NAMESPACE_WIN32,
    );
    let mft_fname_attr = build_resident_attr(TYPE_FILE_NAME, &[], &mft_fname_value, 1);
    // $DATA non-resident: one run, length=2 clusters, lcn=mft_lcn (4).
    // Run list: 0x11 (1-byte length, 1-byte offset) + 0x02 + 0x04 + 0x00.
    let mft_runs = vec![0x11u8, 0x02, 0x04, 0x00];
    let mft_data = build_non_resident_attr(
        TYPE_DATA,
        &[],
        &mft_runs,
        0,
        1,
        2 * cluster_size as u64,
        8 * REC_SIZE as u64,
        8 * REC_SIZE as u64,
        2,
    );
    let rec0 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![si_attr.clone(), mft_fname_attr, mft_data],
    );
    dev.write_at(mft_byte_off, &rec0).unwrap();

    // --- Record 5: root directory ---
    let root_fname_value = build_file_name_value(
        5,
        ".",
        FileName::FLAG_DIRECTORY,
        0,
        FileName::NAMESPACE_WIN32,
    );
    let root_fname_attr = build_resident_attr(TYPE_FILE_NAME, &[], &root_fname_value, 1);

    // Build $INDEX_ROOT pointing at "hello.txt" (file ref = record 6
    // with sequence 1 in the high 16 bits).
    let hello_ref: u64 = 6 | (1u64 << 48);
    let hello_fn = build_file_name_value(5, "hello.txt", 0, 3, FileName::NAMESPACE_WIN32);
    let hello_entry = build_index_entry(hello_ref, &hello_fn, 0, None);
    let term_entry = build_terminator_entry(None);
    let idx_root_value = build_index_root_value(&[hello_entry, term_entry]);
    // Index root name = "$I30" (UTF-16LE).
    let i30_name = utf16_le("$I30");
    let idx_root_attr = build_resident_attr(TYPE_INDEX_ROOT, &i30_name, &idx_root_value, 2);
    let rec5 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE | mft::RecordHeader::FLAG_DIRECTORY,
        vec![si_attr.clone(), root_fname_attr, idx_root_attr],
    );
    dev.write_at(mft_byte_off + 5 * REC_SIZE as u64, &rec5)
        .unwrap();

    // --- Record 6: hello.txt ---
    let file_fname_value = build_file_name_value(5, "hello.txt", 0, 3, FileName::NAMESPACE_WIN32);
    let file_fname_attr = build_resident_attr(TYPE_FILE_NAME, &[], &file_fname_value, 1);
    let file_data_attr = build_resident_attr(TYPE_DATA, &[], b"hi\n", 2);
    let stream_name = utf16_le("stream1");
    let stream_data_attr = build_resident_attr(TYPE_DATA, &stream_name, b"AAAA", 3);
    let rec6 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![si_attr, file_fname_attr, file_data_attr, stream_data_attr],
    );
    dev.write_at(mft_byte_off + 6 * REC_SIZE as u64, &rec6)
        .unwrap();

    dev
}

#[test]
fn read_mft_record_zero() {
    let mut dev = build_tiny_image();
    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let mut buf = vec![0u8; REC_SIZE as usize];
    ntfs.read_mft_record(&mut dev, 0, &mut buf).unwrap();
    assert_eq!(&buf[0..4], b"FILE");
    let hdr = mft::RecordHeader::parse(&buf).unwrap();
    assert!(hdr.is_in_use());
}

#[test]
fn list_root_directory() {
    let mut dev = build_tiny_image();
    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let entries = ntfs.list_path(&mut dev, "/").unwrap();
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0].name, "hello.txt");
    assert_eq!(entries[0].kind, crate::fs::EntryKind::Regular);
}

#[test]
fn read_hello_txt() {
    let mut dev = build_tiny_image();
    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let mut reader = ntfs.open_file_reader(&mut dev, "/hello.txt").unwrap();
    let mut buf = Vec::new();
    reader.read_to_end(&mut buf).unwrap();
    assert_eq!(buf, b"hi\n");
}

#[test]
fn read_xattrs_includes_dos_attrs_and_ads() {
    let mut dev = build_tiny_image();
    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let attrs = ntfs.read_xattrs(&mut dev, "/hello.txt").unwrap();
    assert!(attrs.contains_key(xattr_keys::DOS_ATTRS));
    assert!(attrs.contains_key(xattr_keys::TIMES_RAW));
    let ads_key = format!("{}stream1", xattr_keys::ADS_PREFIX);
    assert_eq!(
        attrs.get(&ads_key).map(|v| v.as_slice()),
        Some(b"AAAA" as &[u8])
    );
}

#[test]
fn lookup_path_missing_component() {
    let mut dev = build_tiny_image();
    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let err = ntfs.lookup_path(&mut dev, "/no_such_file").unwrap_err();
    assert!(matches!(err, crate::Error::InvalidImage(_)));
}

// --- Case-insensitive lookup via $UpCase ----------------------------------
//
// The tiny image has no $UpCase metadata file, so the driver should fall
// back to the identity table — lookups remain case-sensitive in that
// degraded mode. We exercise the case-folding code path by installing an
// ASCII-uppercasing UpCase directly into the cached field after open.

#[test]
fn case_insensitive_lookup_with_ascii_upcase() {
    let mut dev = build_tiny_image();
    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    // Read record 0 first to bootstrap the MFT runs.
    let mut scratch = vec![0u8; REC_SIZE as usize];
    ntfs.read_mft_record(&mut dev, 0, &mut scratch).unwrap();

    // Install an ASCII-uppercasing UpCase table directly.
    let mut bytes = Vec::with_capacity(0x10000 * 2);
    for i in 0..0x10000u32 {
        let v = if (0x61..=0x7A).contains(&i) {
            i as u16 - 0x20
        } else {
            i as u16
        };
        bytes.extend_from_slice(&v.to_le_bytes());
    }
    ntfs.upcase = Some(super::secure::UpcaseTable::from_bytes(&bytes));

    // Now "/HELLO.TXT" should resolve to the same record as "/hello.txt".
    let lower = ntfs.lookup_path(&mut dev, "/hello.txt").unwrap();
    let upper = ntfs.lookup_path(&mut dev, "/HELLO.TXT").unwrap();
    assert_eq!(lower, upper);
}

// --- $ATTRIBUTE_LIST spill ------------------------------------------------
//
// We fabricate a base record whose $DATA lives entirely in an extension
// record, referenced through an $ATTRIBUTE_LIST. The full attribute view
// is then assembled by `load_record_set` and `open_stream_by_record` must
// find $DATA across records.

#[test]
fn data_attribute_from_extension_record() {
    let cluster_size = (BPS as u32) * (SPC as u32);
    let mft_lcn = 4u64;
    let mft_byte_off = mft_lcn * cluster_size as u64;
    let total_size = 32 * cluster_size as u64;
    let mut dev = MemoryBackend::new(total_size);
    // Boot
    let boot = fake_boot(BPS, SPC, mft_lcn, -10);
    dev.write_at(0, &boot[..]).unwrap();
    dev.write_at(0x44, &[(-12i8) as u8]).unwrap();

    let si_value = vec![0u8; 48];
    let si_attr = build_resident_attr(TYPE_STANDARD_INFORMATION, &[], &si_value, 0);

    // Record 0: $MFT
    let mft_fname_value = build_file_name_value(
        5,
        "$MFT",
        FileName::FLAG_DIRECTORY,
        8 * REC_SIZE as u64,
        FileName::NAMESPACE_WIN32,
    );
    let mft_fname_attr = build_resident_attr(TYPE_FILE_NAME, &[], &mft_fname_value, 1);
    let mft_runs = vec![0x11u8, 0x02, 0x04, 0x00];
    let mft_data = build_non_resident_attr(
        TYPE_DATA,
        &[],
        &mft_runs,
        0,
        1,
        2 * cluster_size as u64,
        8 * REC_SIZE as u64,
        8 * REC_SIZE as u64,
        2,
    );
    let rec0 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![si_attr.clone(), mft_fname_attr, mft_data],
    );
    dev.write_at(mft_byte_off, &rec0).unwrap();

    // Record 5: root directory pointing at "split.dat" (record 6).
    let root_fname_value = build_file_name_value(
        5,
        ".",
        FileName::FLAG_DIRECTORY,
        0,
        FileName::NAMESPACE_WIN32,
    );
    let root_fname_attr = build_resident_attr(TYPE_FILE_NAME, &[], &root_fname_value, 1);
    let split_ref: u64 = 6 | (1u64 << 48);
    let split_fn = build_file_name_value(5, "split.dat", 0, 6, FileName::NAMESPACE_WIN32);
    let split_entry = build_index_entry(split_ref, &split_fn, 0, None);
    let term_entry = build_terminator_entry(None);
    let idx_root_value = build_index_root_value(&[split_entry, term_entry]);
    let i30_name = utf16_le("$I30");
    let idx_root_attr = build_resident_attr(TYPE_INDEX_ROOT, &i30_name, &idx_root_value, 2);
    let rec5 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE | mft::RecordHeader::FLAG_DIRECTORY,
        vec![si_attr.clone(), root_fname_attr, idx_root_attr],
    );
    dev.write_at(mft_byte_off + 5 * REC_SIZE as u64, &rec5)
        .unwrap();

    // Record 6: base record. Holds $SI + $FILE_NAME + $ATTRIBUTE_LIST.
    // The $ATTRIBUTE_LIST points at record 7 for $DATA.
    let file_fname_value = build_file_name_value(5, "split.dat", 0, 6, FileName::NAMESPACE_WIN32);
    let file_fname_attr = build_resident_attr(TYPE_FILE_NAME, &[], &file_fname_value, 1);

    // Build $ATTRIBUTE_LIST entry: one row pointing at record 7 for $DATA.
    let mut alist_value: Vec<u8> = Vec::new();
    let entry_len: u16 = 0x20;
    alist_value.extend_from_slice(&TYPE_DATA.to_le_bytes()); // type
    alist_value.extend_from_slice(&entry_len.to_le_bytes()); // entry_len
    alist_value.push(0); // name_len
    alist_value.push(0x1A); // name_off
    alist_value.extend_from_slice(&0u64.to_le_bytes()); // starting_vcn
    let rec7_ref: u64 = 7 | (1u64 << 48);
    alist_value.extend_from_slice(&rec7_ref.to_le_bytes()); // mft ref to rec 7
    alist_value.extend_from_slice(&3u16.to_le_bytes()); // attr id
    while alist_value.len() < entry_len as usize {
        alist_value.push(0);
    }
    let alist_attr = build_resident_attr(TYPE_ATTRIBUTE_LIST, &[], &alist_value, 2);

    let rec6 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![si_attr.clone(), file_fname_attr, alist_attr],
    );
    dev.write_at(mft_byte_off + 6 * REC_SIZE as u64, &rec6)
        .unwrap();

    // Record 7: extension record carrying the actual $DATA = b"hello!".
    // base_record_ref points back at record 6 (low 48 bits) seq 1.
    let data_attr = build_resident_attr(TYPE_DATA, &[], b"hello!", 3);
    let mut rec7 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![data_attr],
    );
    // Patch base_record_ref at 0x20..0x28 to point at record 6.
    let base_ref: u64 = 6 | (1u64 << 48);
    rec7[0x20..0x28].copy_from_slice(&base_ref.to_le_bytes());
    // Re-install fixup since we touched bytes after build_record installed it.
    mft::install_fixup(&mut rec7, BPS as usize, 0x0001);
    dev.write_at(mft_byte_off + 7 * REC_SIZE as u64, &rec7)
        .unwrap();

    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let mut reader = ntfs.open_file_reader(&mut dev, "/split.dat").unwrap();
    let mut buf = Vec::new();
    reader.read_to_end(&mut buf).unwrap();
    assert_eq!(buf, b"hello!");
}

// --- LZNT1 compressed $DATA ----------------------------------------------
//
// Build a synthetic CU of 4 clusters (16 KiB at 4 KiB clusters). The CU's
// run list has 1 real cluster of LZNT1 data and 3 sparse clusters; the
// decoder should produce 6 bytes of output ("ABCABC"), then zero-fill to
// `real_size`.

#[test]
fn compressed_data_decodes_via_cu() {
    let cluster_size = (BPS as u32) * (SPC as u32);
    let mft_lcn = 4u64;
    let mft_byte_off = mft_lcn * cluster_size as u64;
    let total_size = 64 * cluster_size as u64;
    let mut dev = MemoryBackend::new(total_size);
    let boot = fake_boot(BPS, SPC, mft_lcn, -10);
    dev.write_at(0, &boot[..]).unwrap();
    dev.write_at(0x44, &[(-12i8) as u8]).unwrap();

    // Build a compressed CU payload: one chunk → "ABCABC".
    // (See compression::tests::decompresses_back_reference for the encoding.)
    let chunk_payload = vec![0x08u8, b'A', b'B', b'C', 0x00, 0x20];
    let chunk_len_minus_1 = chunk_payload.len() as u16 - 1;
    let header = 0xB000u16 | chunk_len_minus_1;
    let mut compressed = header.to_le_bytes().to_vec();
    compressed.extend_from_slice(&chunk_payload);
    // The driver expects one cluster of compressed data; pad to cluster.
    while compressed.len() < cluster_size as usize {
        compressed.push(0);
    }
    // Drop the data into cluster 20.
    let data_lcn = 20u64;
    dev.write_at(data_lcn * cluster_size as u64, &compressed)
        .unwrap();

    let si_value = vec![0u8; 48];
    let si_attr = build_resident_attr(TYPE_STANDARD_INFORMATION, &[], &si_value, 0);

    // Record 0: $MFT
    let mft_fname_attr = build_resident_attr(
        TYPE_FILE_NAME,
        &[],
        &build_file_name_value(
            5,
            "$MFT",
            FileName::FLAG_DIRECTORY,
            8 * REC_SIZE as u64,
            FileName::NAMESPACE_WIN32,
        ),
        1,
    );
    let mft_runs = vec![0x11u8, 0x02, 0x04, 0x00];
    let mft_data = build_non_resident_attr(
        TYPE_DATA,
        &[],
        &mft_runs,
        0,
        1,
        2 * cluster_size as u64,
        8 * REC_SIZE as u64,
        8 * REC_SIZE as u64,
        2,
    );
    let rec0 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![si_attr.clone(), mft_fname_attr, mft_data],
    );
    dev.write_at(mft_byte_off, &rec0).unwrap();

    // Record 5: root dir indexing "lz.dat" → record 6.
    let lz_ref: u64 = 6 | (1u64 << 48);
    let lz_fn = build_file_name_value(5, "lz.dat", 0, 6, FileName::NAMESPACE_WIN32);
    let lz_entry = build_index_entry(lz_ref, &lz_fn, 0, None);
    let term = build_terminator_entry(None);
    let idx_root_value = build_index_root_value(&[lz_entry, term]);
    let i30_name = utf16_le("$I30");
    let idx_root_attr = build_resident_attr(TYPE_INDEX_ROOT, &i30_name, &idx_root_value, 2);
    let rec5 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE | mft::RecordHeader::FLAG_DIRECTORY,
        vec![
            si_attr.clone(),
            build_resident_attr(
                TYPE_FILE_NAME,
                &[],
                &build_file_name_value(
                    5,
                    ".",
                    FileName::FLAG_DIRECTORY,
                    0,
                    FileName::NAMESPACE_WIN32,
                ),
                1,
            ),
            idx_root_attr,
        ],
    );
    dev.write_at(mft_byte_off + 5 * REC_SIZE as u64, &rec5)
        .unwrap();

    // Record 6: lz.dat. Compressed $DATA, compression_unit=2 (1 << 2 = 4
    // clusters per CU). Run list: 1 real cluster at LCN 20, then 3 sparse
    // clusters. real_size = 6 (we only emit "ABCABC").
    // Run encoding: 0x11 0x01 0x14 (len=1, offset=+20), 0x01 0x03 (len=3 sparse), 0x00.
    let runs = vec![0x11u8, 0x01, 0x14, 0x01, 0x03, 0x00];
    let mut data_attr = build_non_resident_attr(
        TYPE_DATA,
        &[],
        &runs,
        0,
        3,
        4 * cluster_size as u64,
        6,
        6,
        3,
    );
    // Set compression flag + compression_unit=2 inside the header.
    // Flags are at offset 12 of the attribute header.
    data_attr[12..14].copy_from_slice(&ATTR_FLAG_COMPRESSED.to_le_bytes());
    // compression_unit lives at attr_start + 0x22 (u16 low byte).
    data_attr[0x22] = 2;
    data_attr[0x23] = 0;
    let rec6 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![
            si_attr.clone(),
            build_resident_attr(
                TYPE_FILE_NAME,
                &[],
                &build_file_name_value(5, "lz.dat", 0, 6, FileName::NAMESPACE_WIN32),
                1,
            ),
            data_attr,
        ],
    );
    dev.write_at(mft_byte_off + 6 * REC_SIZE as u64, &rec6)
        .unwrap();

    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let mut reader = ntfs.open_file_reader(&mut dev, "/lz.dat").unwrap();
    let mut buf = Vec::new();
    reader.read_to_end(&mut buf).unwrap();
    assert_eq!(buf, b"ABCABC");
}

// --- Encrypted $DATA is rejected ----------------------------------------

#[test]
fn encrypted_data_is_unsupported() {
    let cluster_size = (BPS as u32) * (SPC as u32);
    let mft_lcn = 4u64;
    let mft_byte_off = mft_lcn * cluster_size as u64;
    let total_size = 32 * cluster_size as u64;
    let mut dev = MemoryBackend::new(total_size);
    let boot = fake_boot(BPS, SPC, mft_lcn, -10);
    dev.write_at(0, &boot[..]).unwrap();
    dev.write_at(0x44, &[(-12i8) as u8]).unwrap();

    let si_value = vec![0u8; 48];
    let si_attr = build_resident_attr(TYPE_STANDARD_INFORMATION, &[], &si_value, 0);

    let mft_fname_attr = build_resident_attr(
        TYPE_FILE_NAME,
        &[],
        &build_file_name_value(
            5,
            "$MFT",
            FileName::FLAG_DIRECTORY,
            8 * REC_SIZE as u64,
            FileName::NAMESPACE_WIN32,
        ),
        1,
    );
    let mft_runs = vec![0x11u8, 0x02, 0x04, 0x00];
    let mft_data = build_non_resident_attr(
        TYPE_DATA,
        &[],
        &mft_runs,
        0,
        1,
        2 * cluster_size as u64,
        8 * REC_SIZE as u64,
        8 * REC_SIZE as u64,
        2,
    );
    let rec0 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![si_attr.clone(), mft_fname_attr, mft_data],
    );
    dev.write_at(mft_byte_off, &rec0).unwrap();

    let efs_ref: u64 = 6 | (1u64 << 48);
    let efs_fn = build_file_name_value(5, "efs.dat", 0, 0, FileName::NAMESPACE_WIN32);
    let efs_entry = build_index_entry(efs_ref, &efs_fn, 0, None);
    let term = build_terminator_entry(None);
    let idx_root_value = build_index_root_value(&[efs_entry, term]);
    let i30_name = utf16_le("$I30");
    let idx_root_attr = build_resident_attr(TYPE_INDEX_ROOT, &i30_name, &idx_root_value, 2);
    let rec5 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE | mft::RecordHeader::FLAG_DIRECTORY,
        vec![
            si_attr.clone(),
            build_resident_attr(
                TYPE_FILE_NAME,
                &[],
                &build_file_name_value(
                    5,
                    ".",
                    FileName::FLAG_DIRECTORY,
                    0,
                    FileName::NAMESPACE_WIN32,
                ),
                1,
            ),
            idx_root_attr,
        ],
    );
    dev.write_at(mft_byte_off + 5 * REC_SIZE as u64, &rec5)
        .unwrap();

    // Record 6: efs.dat with ATTR_FLAG_ENCRYPTED set on $DATA.
    let mut data_attr = build_resident_attr(TYPE_DATA, &[], b"AAAA", 3);
    data_attr[12..14].copy_from_slice(&ATTR_FLAG_ENCRYPTED.to_le_bytes());
    let rec6 = build_record(
        REC_SIZE as usize,
        mft::RecordHeader::FLAG_IN_USE,
        vec![
            si_attr.clone(),
            build_resident_attr(
                TYPE_FILE_NAME,
                &[],
                &build_file_name_value(5, "efs.dat", 0, 0, FileName::NAMESPACE_WIN32),
                1,
            ),
            data_attr,
        ],
    );
    dev.write_at(mft_byte_off + 6 * REC_SIZE as u64, &rec6)
        .unwrap();

    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let result = ntfs.open_file_reader(&mut dev, "/efs.dat");
    match result {
        Ok(_) => panic!("expected Unsupported error for EFS-encrypted file"),
        Err(crate::Error::Unsupported(msg)) => assert!(msg.contains("EFS")),
        Err(other) => panic!("expected Unsupported, got {other:?}"),
    }
}

// ---------------------------------------------------------------------
// Writer tests.
//
// These format a fresh NTFS volume on an in-memory backend, exercise
// create_*, then re-open the volume and verify the read path can walk
// what was written.
// ---------------------------------------------------------------------

use super::format::FormatOpts;
use crate::fs::{FileMeta, FileSource};

fn fresh_volume(size: u64) -> (MemoryBackend, Ntfs) {
    let mut dev = MemoryBackend::new(size);
    let opts = FormatOpts {
        volume_label: "fstool-test".to_string(),
        ..Default::default()
    };
    let ntfs = Ntfs::format(&mut dev, &opts).unwrap();
    (dev, ntfs)
}

#[test]
fn writer_format_then_open_reads_boot_sector() {
    let (mut dev, _ntfs) = fresh_volume(8 * 1024 * 1024);
    // Re-open from device — verifies boot sector probe-able / decodable.
    assert!(probe(&mut dev).unwrap());
    let ntfs2 = Ntfs::open(&mut dev).unwrap();
    assert_eq!(ntfs2.cluster_size(), 4096);
    assert_eq!(ntfs2.mft_record_size(), 1024);
}

#[test]
fn writer_format_volume_has_root_directory() {
    let (mut dev, mut ntfs) = fresh_volume(8 * 1024 * 1024);
    let entries = ntfs.list_path(&mut dev, "/").unwrap();
    // Freshly formatted root should be empty.
    assert!(entries.is_empty());
}

#[test]
fn writer_creates_small_file_resident_data() {
    let (mut dev, mut ntfs) = fresh_volume(8 * 1024 * 1024);
    ntfs.create_file(
        &mut dev,
        "/hello.txt",
        FileSource::Reader {
            reader: Box::new(std::io::Cursor::new(b"hi\n".to_vec())),
            len: 3,
        },
        FileMeta::default(),
    )
    .unwrap();
    ntfs.flush(&mut dev).unwrap();

    // Re-open and read.
    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let entries = ntfs2.list_path(&mut dev, "/").unwrap();
    assert!(entries.iter().any(|e| e.name == "hello.txt"));
    let mut r = ntfs2.open_file_reader(&mut dev, "/hello.txt").unwrap();
    let mut buf = Vec::new();
    r.read_to_end(&mut buf).unwrap();
    assert_eq!(buf, b"hi\n");
}

#[test]
fn writer_creates_large_file_non_resident_data() {
    let (mut dev, mut ntfs) = fresh_volume(16 * 1024 * 1024);
    // 8000 bytes is larger than the resident budget and forces a
    // non-resident $DATA with a single cluster run.
    let payload: Vec<u8> = (0..8000).map(|i| (i & 0xFF) as u8).collect();
    ntfs.create_file(
        &mut dev,
        "/big.bin",
        FileSource::Reader {
            reader: Box::new(std::io::Cursor::new(payload.clone())),
            len: payload.len() as u64,
        },
        FileMeta::default(),
    )
    .unwrap();
    ntfs.flush(&mut dev).unwrap();

    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let mut r = ntfs2.open_file_reader(&mut dev, "/big.bin").unwrap();
    let mut buf = Vec::new();
    r.read_to_end(&mut buf).unwrap();
    assert_eq!(buf, payload);
}

#[test]
fn writer_creates_directory() {
    let (mut dev, mut ntfs) = fresh_volume(8 * 1024 * 1024);
    ntfs.create_dir(&mut dev, "/sub", FileMeta::default())
        .unwrap();
    ntfs.create_file(
        &mut dev,
        "/sub/note.txt",
        FileSource::Reader {
            reader: Box::new(std::io::Cursor::new(b"x".to_vec())),
            len: 1,
        },
        FileMeta::default(),
    )
    .unwrap();
    ntfs.flush(&mut dev).unwrap();

    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let root_entries = ntfs2.list_path(&mut dev, "/").unwrap();
    assert!(root_entries.iter().any(|e| e.name == "sub"));
    let sub_entries = ntfs2.list_path(&mut dev, "/sub").unwrap();
    assert!(sub_entries.iter().any(|e| e.name == "note.txt"));
}

#[test]
fn writer_creates_symlink() {
    let (mut dev, mut ntfs) = fresh_volume(8 * 1024 * 1024);
    ntfs.create_symlink(&mut dev, "/link", "target.txt", FileMeta::default())
        .unwrap();
    ntfs.flush(&mut dev).unwrap();

    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let entries = ntfs2.list_path(&mut dev, "/").unwrap();
    assert!(entries.iter().any(|e| e.name == "link"));
    // Reparse data should be present on the entry.
    let xattrs = ntfs2.read_xattrs(&mut dev, "/link").unwrap();
    assert!(xattrs.contains_key(xattr_keys::REPARSE));
}

#[test]
fn writer_refuses_device_creation() {
    let (mut dev, mut ntfs) = fresh_volume(8 * 1024 * 1024);
    let err = ntfs
        .create_device(
            &mut dev,
            "/dev/null",
            crate::fs::DeviceKind::Char,
            1,
            3,
            FileMeta::default(),
        )
        .unwrap_err();
    assert!(matches!(err, crate::Error::Unsupported(_)));
}

#[test]
fn writer_create_without_format_errors() {
    // Open an existing read-only image (the tiny one) and try to create.
    let mut dev = build_tiny_image();
    let mut ntfs = Ntfs::open(&mut dev).unwrap();
    let err = ntfs
        .create_file(
            &mut dev,
            "/new.txt",
            FileSource::Reader {
                reader: Box::new(std::io::Cursor::new(b"x".to_vec())),
                len: 1,
            },
            FileMeta::default(),
        )
        .unwrap_err();
    assert!(matches!(err, crate::Error::Unsupported(_)));
}

#[test]
fn writer_dir_promotes_to_index_allocation() {
    // Add enough entries to a directory that the $INDEX_ROOT overflows
    // its 512-byte budget and gets promoted to $INDEX_ALLOCATION.
    let (mut dev, mut ntfs) = fresh_volume(16 * 1024 * 1024);
    for i in 0..16 {
        let path = format!("/file_{i:02}.txt");
        ntfs.create_file(
            &mut dev,
            &path,
            FileSource::Reader {
                reader: Box::new(std::io::Cursor::new(b"x".to_vec())),
                len: 1,
            },
            FileMeta::default(),
        )
        .unwrap();
    }
    ntfs.flush(&mut dev).unwrap();
    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let entries = ntfs2.list_path(&mut dev, "/").unwrap();
    let names: std::collections::HashSet<String> = entries.iter().map(|e| e.name.clone()).collect();
    for i in 0..16 {
        assert!(
            names.contains(&format!("file_{i:02}.txt")),
            "missing file_{i:02}.txt"
        );
    }
}

#[test]
fn writer_streams_large_file_through_scratch_buffer() {
    // 200 KiB file forces multiple scratch buffers worth of streaming.
    let (mut dev, mut ntfs) = fresh_volume(64 * 1024 * 1024);
    let size = 200 * 1024;
    let payload: Vec<u8> = (0..size).map(|i| ((i * 7) & 0xFF) as u8).collect();
    ntfs.create_file(
        &mut dev,
        "/stream.bin",
        FileSource::Reader {
            reader: Box::new(std::io::Cursor::new(payload.clone())),
            len: payload.len() as u64,
        },
        FileMeta::default(),
    )
    .unwrap();
    ntfs.flush(&mut dev).unwrap();
    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let mut r = ntfs2.open_file_reader(&mut dev, "/stream.bin").unwrap();
    let mut buf = Vec::new();
    r.read_to_end(&mut buf).unwrap();
    assert_eq!(buf.len(), payload.len());
    assert_eq!(buf, payload);
}

#[test]
fn writer_zero_length_file() {
    let (mut dev, mut ntfs) = fresh_volume(8 * 1024 * 1024);
    ntfs.create_file(
        &mut dev,
        "/empty.txt",
        FileSource::Zero(0),
        FileMeta::default(),
    )
    .unwrap();
    ntfs.flush(&mut dev).unwrap();
    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let entries = ntfs2.list_path(&mut dev, "/").unwrap();
    assert!(entries.iter().any(|e| e.name == "empty.txt"));
    let mut r = ntfs2.open_file_reader(&mut dev, "/empty.txt").unwrap();
    let mut buf = Vec::new();
    r.read_to_end(&mut buf).unwrap();
    assert_eq!(buf, Vec::<u8>::new());
}

#[test]
fn writer_nested_directories() {
    let (mut dev, mut ntfs) = fresh_volume(16 * 1024 * 1024);
    ntfs.create_dir(&mut dev, "/a", FileMeta::default())
        .unwrap();
    ntfs.create_dir(&mut dev, "/a/b", FileMeta::default())
        .unwrap();
    ntfs.create_dir(&mut dev, "/a/b/c", FileMeta::default())
        .unwrap();
    ntfs.create_file(
        &mut dev,
        "/a/b/c/deep.txt",
        FileSource::Reader {
            reader: Box::new(std::io::Cursor::new(b"deep!".to_vec())),
            len: 5,
        },
        FileMeta::default(),
    )
    .unwrap();
    ntfs.flush(&mut dev).unwrap();
    let mut ntfs2 = Ntfs::open(&mut dev).unwrap();
    let entries = ntfs2.list_path(&mut dev, "/a/b/c").unwrap();
    assert!(entries.iter().any(|e| e.name == "deep.txt"));
    let mut r = ntfs2.open_file_reader(&mut dev, "/a/b/c/deep.txt").unwrap();
    let mut buf = Vec::new();
    r.read_to_end(&mut buf).unwrap();
    assert_eq!(buf, b"deep!");
}

#[test]
fn writer_format_emits_upcase_table() {
    let (mut dev, mut ntfs) = fresh_volume(8 * 1024 * 1024);
    // After format, $UpCase record 10 should exist and have a non-trivial
    // $DATA stream that round-trips through the reader.
    let mut buf = vec![0u8; ntfs.mft_record_size() as usize];
    ntfs.read_mft_record(&mut dev, MFT_RECORD_UPCASE, &mut buf)
        .unwrap();
    let hdr = mft::RecordHeader::parse(&buf).unwrap();
    assert!(hdr.is_in_use());
}