fastsync 0.10.1

A fast, safe one-way directory synchronization tool for local folders and network transfers.
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
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::{Instant, UNIX_EPOCH};

use quinn::{RecvStream, SendStream};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::task::JoinSet;
use tracing::info;

use crate::error::{FastSyncError, Result};
use crate::filter::PathFilter;
use crate::i18n::tr_current;
use crate::progress::{ProgressPhase, SyncProgress};

use super::{
    BUFFER_SIZE, HASH_REQUEST_BATCH_SIZE,
    protocol::{
        DirManifest, FileManifest, FileTransfer, Manifest, TransferOptions, WireMessage,
        WireMetadata,
    },
    protocol_io::{read_message, write_message},
    summary::ReceiveSummary,
    util::{
        hex_digest, io_path, other, other_message, safe_join, throughput_bps, unique_temp_path,
    },
};

#[cfg(test)]
pub(super) async fn send_tree(
    root: &Path,
    connection: &quinn::Connection,
    send: &mut SendStream,
    recv: &mut RecvStream,
    options: TransferOptions,
) -> Result<()> {
    send_tree_with_progress(
        root,
        connection,
        send,
        recv,
        options,
        &PathFilter::disabled(),
        &SyncProgress::new(false),
    )
    .await
}

/// 发送一棵目录树,并把网络侧长耗时阶段报告给交互式进度层。
///
/// 进度句柄只记录 manifest、哈希请求、文件请求和文件发送进展;协议消息、
/// 安全校验、并发调度和错误语义仍由传输函数本身控制。
pub(super) async fn send_tree_with_progress(
    root: &Path,
    connection: &quinn::Connection,
    send: &mut SendStream,
    recv: &mut RecvStream,
    options: TransferOptions,
    filter: &PathFilter,
    progress: &SyncProgress,
) -> Result<()> {
    let phase_started = Instant::now();
    let manifest_progress = progress.spinner("progress.network_send_manifest");
    let manifest = send_manifest_with_progress(root, send, filter, &manifest_progress).await?;
    manifest_progress.finish();
    info!(
        phase = "network_send_manifest",
        elapsed_ms = phase_started.elapsed().as_millis(),
        "{}",
        tr_current("log.phase_finished")
    );
    let phase_started = Instant::now();
    let hash_progress = progress.spinner("progress.network_serve_hashes");
    let hashes =
        serve_hash_requests_with_progress(root, &manifest, send, recv, &hash_progress).await?;
    hash_progress.finish();
    info!(
        phase = "network_serve_hash_requests",
        elapsed_ms = phase_started.elapsed().as_millis(),
        "{}",
        tr_current("log.phase_finished")
    );
    let request_progress = progress.spinner("progress.network_read_requests");
    let requested = read_requested_paths_with_progress(recv, &request_progress).await?;
    request_progress.finish();

    let phase_started = Instant::now();
    let send_progress = progress.bar("progress.network_send_files", requested.len());
    let (sent_files, total_bytes) = send_requested_file_streams_with_progress(
        root,
        connection,
        &manifest,
        requested,
        &hashes,
        options.file_concurrency,
        &send_progress,
    )
    .await?;
    send_progress.finish();
    info!(
        phase = "network_send_files",
        elapsed_ms = phase_started.elapsed().as_millis(),
        files = sent_files,
        bytes = total_bytes,
        file_concurrency = options.file_concurrency,
        throughput_bps = throughput_bps(total_bytes, phase_started.elapsed().as_millis()),
        "{}",
        tr_current("log.phase_finished")
    );
    write_message(send, &WireMessage::Done).await?;
    send.finish()
        .map_err(|error| other("finish QUIC send stream", error))?;
    info!(
        files = sent_files,
        bytes = total_bytes,
        "finished sending tree"
    );
    Ok(())
}

/// 扫描本地目录并发送 manifest,同时记录 manifest 规模。
async fn send_manifest_with_progress(
    root: &Path,
    send: &mut SendStream,
    filter: &PathFilter,
    progress: &ProgressPhase,
) -> Result<Manifest> {
    let snapshot = crate::scan::scan_directory_filtered(root, false, filter)?;
    let mut manifest = Manifest {
        dirs: Vec::new(),
        files: Vec::new(),
    };
    let mut bytes = 0_u64;

    write_message(send, &WireMessage::ManifestStart).await?;
    for entry in snapshot.entries.values() {
        match entry.kind {
            crate::scan::EntryKind::Directory => {
                let dir = DirManifest {
                    path: entry.relative_path.clone(),
                    metadata: WireMetadata::from_entry(entry),
                };
                write_message(send, &WireMessage::ManifestDir(dir.clone())).await?;
                manifest.dirs.push(dir);
            }
            crate::scan::EntryKind::File => {
                bytes = bytes.saturating_add(entry.len);
                let file = FileManifest {
                    path: entry.relative_path.clone(),
                    len: entry.len,
                    metadata: WireMetadata::from_entry(entry),
                };
                write_message(send, &WireMessage::ManifestFile(file.clone())).await?;
                manifest.files.push(file);
            }
            crate::scan::EntryKind::Symlink => {}
        }
        progress.set_manifest_status(manifest.dirs.len(), manifest.files.len(), bytes);
    }
    write_message(send, &WireMessage::ManifestEnd).await?;

    info!(
        root = %root.display(),
        dirs = manifest.dirs.len(),
        files = manifest.files.len(),
        bytes = manifest.files.iter().map(|file| file.len).sum::<u64>(),
        "sent manifest"
    );
    Ok(manifest)
}

/// 响应对端发起的 BLAKE3 请求,并暴露已完成请求数。
async fn serve_hash_requests_with_progress(
    root: &Path,
    manifest: &Manifest,
    send: &mut SendStream,
    recv: &mut RecvStream,
    progress: &ProgressPhase,
) -> Result<HashMap<PathBuf, String>> {
    let manifest_paths: HashSet<_> = manifest
        .files
        .iter()
        .map(|file| file.path.clone())
        .collect();
    let mut hashes = HashMap::new();

    loop {
        match read_message(recv).await? {
            WireMessage::HashRequest { path } => {
                if !manifest_paths.contains(&path) {
                    return Err(other_message(
                        "serve network hash request",
                        "peer requested hash outside the manifest",
                    ));
                }
                let digest = hash_manifest_path(root, &path)?;
                write_message(
                    send,
                    &WireMessage::Hash {
                        path: path.clone(),
                        blake3: digest.clone(),
                    },
                )
                .await?;
                hashes.insert(path, digest);
                progress.set_request_status(hashes.len(), 0);
            }
            WireMessage::HashRequestEnd => break,
            _ => {
                return Err(other_message(
                    "serve network hash requests",
                    "unexpected message",
                ));
            }
        }
    }

    Ok(hashes)
}

#[cfg(test)]
pub(super) async fn read_requested_paths(recv: &mut RecvStream) -> Result<HashSet<PathBuf>> {
    read_requested_paths_with_progress(recv, &ProgressPhase::disabled()).await
}

/// 读取接收端最终请求的文件列表,并显示请求数量。
async fn read_requested_paths_with_progress(
    recv: &mut RecvStream,
    progress: &ProgressPhase,
) -> Result<HashSet<PathBuf>> {
    let mut requested = HashSet::new();
    loop {
        match read_message(recv).await? {
            WireMessage::RequestFile { path } => {
                requested.insert(path);
                progress.set_request_status(0, requested.len());
            }
            WireMessage::RequestEnd => break,
            _ => {
                return Err(other_message(
                    "read requested network files",
                    "unexpected message",
                ));
            }
        }
    }
    Ok(requested)
}

#[cfg(test)]
pub(super) async fn receive_tree(
    root: &Path,
    connection: &quinn::Connection,
    recv: &mut RecvStream,
    send: &mut SendStream,
    options: TransferOptions,
) -> Result<ReceiveSummary> {
    receive_tree_with_progress(
        root,
        connection,
        recv,
        send,
        options,
        &PathFilter::disabled(),
        &SyncProgress::new(false),
    )
    .await
}

/// 接收一棵目录树,并把 manifest、请求规划、文件接收和清理阶段报告给进度层。
///
/// 该函数保持网络协议、校验和落盘语义不变;进度只作为终端观测信息存在。
pub(super) async fn receive_tree_with_progress(
    root: &Path,
    connection: &quinn::Connection,
    recv: &mut RecvStream,
    send: &mut SendStream,
    options: TransferOptions,
    filter: &PathFilter,
    progress: &SyncProgress,
) -> Result<ReceiveSummary> {
    let phase_started = Instant::now();
    let manifest_progress = progress.spinner("progress.network_receive_manifest");
    let manifest =
        receive_manifest_with_progress(root, recv, options, filter, &manifest_progress).await?;
    manifest_progress.finish();
    info!(
        phase = "network_receive_manifest",
        elapsed_ms = phase_started.elapsed().as_millis(),
        "{}",
        tr_current("log.phase_finished")
    );
    info!(
        root = %root.display(),
        dirs = manifest.dirs.len(),
        files = manifest.files.len(),
        bytes = manifest.files.iter().map(|file| file.len).sum::<u64>(),
        "receiving manifest"
    );
    let phase_started = Instant::now();
    let request_progress = progress.bar("progress.network_plan_requests", manifest.files.len());
    let requested_files = send_file_requests_with_progress(
        root,
        &manifest,
        options.strict,
        filter,
        send,
        recv,
        &request_progress,
    )
    .await?;
    request_progress.finish();
    let requested_paths: HashSet<_> = requested_files.iter().cloned().collect();
    info!(
        requested_files = requested_files.len(),
        skipped_files = manifest.files.len().saturating_sub(requested_files.len()),
        strict = options.strict,
        elapsed_ms = phase_started.elapsed().as_millis(),
        "planned network file requests"
    );

    let phase_started = Instant::now();
    let receive_progress = progress.bar("progress.network_receive_files", requested_files.len());
    let (files, bytes) = receive_requested_files_with_progress(
        root,
        connection,
        requested_files,
        options,
        &receive_progress,
    )
    .await?;
    receive_progress.finish();
    info!(
        phase = "network_receive_files",
        elapsed_ms = phase_started.elapsed().as_millis(),
        files,
        bytes,
        file_concurrency = options.file_concurrency,
        throughput_bps = throughput_bps(bytes, phase_started.elapsed().as_millis()),
        "{}",
        tr_current("log.phase_finished")
    );
    match read_message(recv).await? {
        WireMessage::Done => {}
        _ => return Err(other_message("receive tree", "unexpected message")),
    }
    let deleted = if options.delete {
        let phase_started = Instant::now();
        let delete_progress = progress.spinner("progress.network_delete_obsolete");
        let deleted =
            delete_obsolete_with_progress(root, &manifest, filter, &delete_progress).await?;
        delete_progress.finish();
        info!(
            phase = "network_delete_obsolete",
            elapsed_ms = phase_started.elapsed().as_millis(),
            deleted,
            "{}",
            tr_current("log.phase_finished")
        );
        deleted
    } else {
        0
    };
    let phase_started = Instant::now();
    let metadata_progress = progress.spinner("progress.network_apply_metadata");
    let skipped_files: Vec<_> = manifest
        .files
        .iter()
        .filter(|file| !requested_paths.contains(&file.path))
        .cloned()
        .collect();
    apply_file_metadata(root, &skipped_files, options)?;
    apply_directory_metadata(root, &manifest.dirs, options)?;
    metadata_progress.finish();
    info!(
        phase = "network_apply_metadata",
        elapsed_ms = phase_started.elapsed().as_millis(),
        "{}",
        tr_current("log.phase_finished")
    );
    info!(files, bytes, deleted, "finished receiving tree");
    Ok(ReceiveSummary {
        files,
        bytes,
        deleted,
    })
}

/// 接收 manifest 并在创建目录时显示已接收的目录/文件规模。
async fn receive_manifest_with_progress(
    root: &Path,
    recv: &mut RecvStream,
    options: TransferOptions,
    filter: &PathFilter,
    progress: &ProgressPhase,
) -> Result<Manifest> {
    match read_message(recv).await? {
        WireMessage::ManifestStart => {}
        _ => return Err(other_message("receive manifest", "unexpected message")),
    }

    tokio::fs::create_dir_all(root)
        .await
        .map_err(|error| io_path("create receive root", root, error))?;

    let mut manifest = Manifest {
        dirs: Vec::new(),
        files: Vec::new(),
    };
    let mut bytes = 0_u64;
    loop {
        match read_message(recv).await? {
            WireMessage::ManifestDir(dir) => {
                if !filter.allows_entry(&dir.path, true) {
                    continue;
                }
                let path = safe_join(root, &dir.path)?;
                ensure_directory_path(&path, options.delete).await?;
                tokio::fs::create_dir_all(&path)
                    .await
                    .map_err(|error| io_path("create received directory", &path, error))?;
                manifest.dirs.push(dir);
                progress.set_manifest_status(manifest.dirs.len(), manifest.files.len(), bytes);
            }
            WireMessage::ManifestFile(file) => {
                if !filter.allows_entry(&file.path, false) {
                    continue;
                }
                bytes = bytes.saturating_add(file.len);
                manifest.files.push(file);
                progress.set_manifest_status(manifest.dirs.len(), manifest.files.len(), bytes);
            }
            WireMessage::ManifestEnd => break,
            _ => return Err(other_message("receive manifest", "unexpected message")),
        }
    }

    Ok(manifest)
}

#[cfg(test)]
pub(super) async fn send_file_requests(
    root: &Path,
    manifest: &Manifest,
    strict: bool,
    send: &mut SendStream,
    recv: &mut RecvStream,
) -> Result<Vec<PathBuf>> {
    send_file_requests_with_progress(
        root,
        manifest,
        strict,
        &PathFilter::disabled(),
        send,
        recv,
        &ProgressPhase::disabled(),
    )
    .await
}

/// 比较本地状态并向发送端提交需要传输的文件请求。
///
/// 该阶段可能触发本地哈希和远端哈希请求,进度会显示已检查文件数、
/// 哈希比较数和最终请求数。
async fn send_file_requests_with_progress(
    root: &Path,
    manifest: &Manifest,
    strict: bool,
    filter: &PathFilter,
    send: &mut SendStream,
    recv: &mut RecvStream,
    progress: &ProgressPhase,
) -> Result<Vec<PathBuf>> {
    let target_snapshot = match crate::scan::scan_optional_directory_filtered(root, false, filter) {
        Ok(snapshot) => snapshot,
        Err(FastSyncError::InvalidTarget(path)) if path == root => {
            write_message(send, &WireMessage::HashRequestEnd).await?;
            let requested: Vec<_> = manifest
                .files
                .iter()
                .filter(|file| filter.allows_entry(&file.path, false))
                .map(|file| file.path.clone())
                .collect();
            send_requested_file_paths(requested.iter().cloned(), send).await?;
            progress.inc(manifest.files.len() as u64);
            progress.set_request_status(0, requested.len());
            return Ok(requested);
        }
        Err(error) => return Err(error),
    };

    let mut requested = Vec::new();
    let mut direct_requests = 0_usize;
    let mut hash_comparisons = Vec::new();
    for file in &manifest.files {
        if !filter.allows_entry(&file.path, false) {
            progress.inc(1);
            progress.set_request_status(hash_comparisons.len(), requested.len());
            continue;
        }
        match file_request_decision(&target_snapshot, file, strict)? {
            FileRequestDecision::Request => {
                requested.push(file.path.clone());
                direct_requests += 1;
            }
            FileRequestDecision::Skip => {}
            FileRequestDecision::CompareHash { local_path } => {
                let local_digest = hex_digest(crate::hash::blake3_file(&local_path)?);
                hash_comparisons.push((file.path.clone(), local_digest));
            }
        }
        progress.inc(1);
        progress.set_request_status(hash_comparisons.len(), requested.len());
    }

    let hash_started = Instant::now();
    let remote_hashes = request_remote_hashes_with_progress(
        send,
        recv,
        &hash_comparisons,
        requested.len(),
        progress,
    )
    .await?;
    let mut hash_changed = 0_usize;
    for (path, local_digest) in hash_comparisons {
        if remote_hashes.get(&path) != Some(&local_digest) {
            requested.push(path);
            hash_changed += 1;
            progress.set_request_status(remote_hashes.len(), requested.len());
        }
    }
    info!(
        hash_requests = remote_hashes.len(),
        changed_after_hash = hash_changed,
        elapsed_ms = hash_started.elapsed().as_millis(),
        "completed network hash comparison"
    );

    send_requested_file_paths(requested.iter().cloned(), send).await?;
    info!(
        direct_requests,
        hash_changed,
        requested_files = requested.len(),
        "completed network file request planning"
    );
    Ok(requested)
}

/// 批量请求远端哈希,并在每个响应到达后更新比较进度。
async fn request_remote_hashes_with_progress(
    send: &mut SendStream,
    recv: &mut RecvStream,
    comparisons: &[(PathBuf, String)],
    existing_requests: usize,
    progress: &ProgressPhase,
) -> Result<HashMap<PathBuf, String>> {
    let mut hashes = HashMap::new();

    for batch in comparisons.chunks(HASH_REQUEST_BATCH_SIZE) {
        let mut pending = HashSet::new();
        for (path, _) in batch {
            if !pending.insert(path.clone()) {
                return Err(other_message(
                    "request network hashes",
                    format!("duplicate hash request path: {}", path.display()),
                ));
            }
            write_message(send, &WireMessage::HashRequest { path: path.clone() }).await?;
        }

        for _ in 0..batch.len() {
            match read_message(recv).await? {
                WireMessage::Hash { path, blake3 } => {
                    if !pending.contains(&path) {
                        return Err(other_message(
                            "read network hash response",
                            format!("unexpected hash response path: {}", path.display()),
                        ));
                    }
                    if hashes.insert(path.clone(), blake3).is_some() {
                        return Err(other_message(
                            "read network hash response",
                            format!("duplicate hash response path: {}", path.display()),
                        ));
                    }
                    progress.set_request_status(hashes.len(), existing_requests);
                }
                _ => {
                    return Err(other_message(
                        "read network hash response",
                        "unexpected message",
                    ));
                }
            }
        }
    }

    write_message(send, &WireMessage::HashRequestEnd).await?;
    Ok(hashes)
}

pub(super) async fn send_requested_file_paths(
    paths: impl IntoIterator<Item = PathBuf>,
    send: &mut SendStream,
) -> Result<usize> {
    let mut requested = 0_usize;
    for path in paths {
        write_message(send, &WireMessage::RequestFile { path }).await?;
        requested += 1;
    }
    write_message(send, &WireMessage::RequestEnd).await?;
    Ok(requested)
}

#[cfg(test)]
pub(super) fn request_files_for_local_state(
    root: &Path,
    manifest: &Manifest,
    strict: bool,
    remote_hashes: &HashMap<PathBuf, String>,
) -> Result<Vec<PathBuf>> {
    request_files_for_local_state_filtered(
        root,
        manifest,
        strict,
        remote_hashes,
        &PathFilter::disabled(),
    )
}

#[cfg(test)]
pub(super) fn request_files_for_local_state_filtered(
    root: &Path,
    manifest: &Manifest,
    strict: bool,
    remote_hashes: &HashMap<PathBuf, String>,
    filter: &PathFilter,
) -> Result<Vec<PathBuf>> {
    let target_snapshot = match crate::scan::scan_optional_directory_filtered(root, false, filter) {
        Ok(snapshot) => snapshot,
        Err(FastSyncError::InvalidTarget(path)) if path == root => {
            return Ok(manifest
                .files
                .iter()
                .filter(|file| filter.allows_entry(&file.path, false))
                .map(|file| file.path.clone())
                .collect());
        }
        Err(error) => return Err(error),
    };

    let mut requested = Vec::new();
    for file in &manifest.files {
        if !filter.allows_entry(&file.path, false) {
            continue;
        }
        match file_request_decision(&target_snapshot, file, strict)? {
            FileRequestDecision::Request => requested.push(file.path.clone()),
            FileRequestDecision::Skip => {}
            FileRequestDecision::CompareHash { local_path } => {
                let local_digest = hex_digest(crate::hash::blake3_file(&local_path)?);
                if remote_hashes.get(&file.path) != Some(&local_digest) {
                    requested.push(file.path.clone());
                }
            }
        }
    }

    Ok(requested)
}

pub(super) enum FileRequestDecision {
    Request,
    Skip,
    CompareHash { local_path: PathBuf },
}

pub(super) fn file_request_decision(
    target_snapshot: &crate::scan::Snapshot,
    file: &FileManifest,
    strict: bool,
) -> Result<FileRequestDecision> {
    let Some(target_entry) = target_snapshot.get(&file.path) else {
        return Ok(FileRequestDecision::Request);
    };

    if !target_entry.is_file() || target_entry.len != file.len {
        return Ok(FileRequestDecision::Request);
    }

    if !strict && content_metadata_matches(target_entry, &file.metadata) {
        Ok(FileRequestDecision::Skip)
    } else {
        Ok(FileRequestDecision::CompareHash {
            local_path: target_entry.absolute_path.clone(),
        })
    }
}

pub(super) fn content_metadata_matches(
    entry: &crate::scan::FileEntry,
    metadata: &WireMetadata,
) -> bool {
    metadata_time_matches(entry, metadata) && metadata_permissions_match(entry, metadata)
}

pub(super) fn metadata_time_matches(
    entry: &crate::scan::FileEntry,
    metadata: &WireMetadata,
) -> bool {
    let Some(source_secs) = metadata.modified_secs else {
        return entry.modified.is_none();
    };
    let Some(source_nanos) = metadata.modified_nanos else {
        return entry.modified.is_none();
    };
    let Some(target_modified) = entry.modified else {
        return false;
    };
    let Ok(target_duration) = target_modified.duration_since(UNIX_EPOCH) else {
        return false;
    };

    target_duration.as_secs() as i64 == source_secs
        && target_duration.subsec_nanos() == source_nanos
}

pub(super) fn metadata_permissions_match(
    entry: &crate::scan::FileEntry,
    metadata: &WireMetadata,
) -> bool {
    if entry.readonly != metadata.readonly {
        return false;
    }

    #[cfg(unix)]
    {
        metadata.unix_mode.is_none_or(|mode| entry.mode == mode)
    }
    #[cfg(not(unix))]
    {
        true
    }
}

#[cfg(test)]
pub(super) fn build_manifest(root: &Path) -> Result<Manifest> {
    build_manifest_filtered(root, &PathFilter::disabled())
}

#[cfg(test)]
pub(super) fn build_manifest_filtered(root: &Path, filter: &PathFilter) -> Result<Manifest> {
    let snapshot = crate::scan::scan_directory_filtered(root, false, filter)?;
    let mut dirs = Vec::new();
    let mut files = Vec::new();

    for entry in snapshot.entries.values() {
        match entry.kind {
            crate::scan::EntryKind::Directory => dirs.push(DirManifest {
                path: entry.relative_path.clone(),
                metadata: WireMetadata::from_entry(entry),
            }),
            crate::scan::EntryKind::File => {
                files.push(FileManifest {
                    path: entry.relative_path.clone(),
                    len: entry.len,
                    metadata: WireMetadata::from_entry(entry),
                });
            }
            crate::scan::EntryKind::Symlink => {}
        }
    }

    Ok(Manifest { dirs, files })
}

#[cfg(test)]
pub(super) fn manifest_hashes(
    root: &Path,
    manifest: &Manifest,
) -> Result<HashMap<PathBuf, String>> {
    let mut hashes = HashMap::new();
    for file in &manifest.files {
        hashes.insert(file.path.clone(), hash_manifest_path(root, &file.path)?);
    }
    Ok(hashes)
}

pub(super) fn file_transfer(
    root: &Path,
    file: &FileManifest,
    cached_hash: Option<String>,
) -> Result<FileTransfer> {
    let blake3 = match cached_hash {
        Some(hash) => hash,
        None => hash_manifest_path(root, &file.path)?,
    };
    Ok(FileTransfer {
        path: file.path.clone(),
        len: file.len,
        blake3,
        metadata: file.metadata.clone(),
    })
}

pub(super) fn hash_manifest_path(root: &Path, path: &Path) -> Result<String> {
    let path = safe_join(root, path)?;
    crate::hash::blake3_file(&path).map(hex_digest)
}

/// 并发发送接收端请求的文件,并按已完成文件数更新进度。
async fn send_requested_file_streams_with_progress(
    root: &Path,
    connection: &quinn::Connection,
    manifest: &Manifest,
    requested: HashSet<PathBuf>,
    hashes: &HashMap<PathBuf, String>,
    file_concurrency: usize,
    progress: &ProgressPhase,
) -> Result<(usize, u64)> {
    let manifest_paths: HashSet<_> = manifest
        .files
        .iter()
        .map(|file| file.path.clone())
        .collect();
    if !requested.is_subset(&manifest_paths) {
        return Err(other_message(
            "send requested network files",
            "peer requested paths outside the manifest",
        ));
    }

    let mut tasks = JoinSet::new();
    let mut files = 0_usize;
    let mut bytes = 0_u64;

    for file in &manifest.files {
        if !requested.contains(&file.path) {
            continue;
        }

        info!(
            path = %file.path.display(),
            bytes = file.len,
            "sending file"
        );
        let transfer = file_transfer(root, file, hashes.get(&file.path).cloned())?;
        while tasks.len() >= file_concurrency {
            let (sent_files, sent_bytes) = join_file_task(&mut tasks).await?;
            files += sent_files;
            bytes = bytes.saturating_add(sent_bytes);
            progress.inc(sent_files as u64);
            progress.record_completed_transfer_files(sent_files);
        }

        let root = root.to_path_buf();
        let connection = connection.clone();
        let progress = progress.clone();
        tasks.spawn(async move {
            let bytes = transfer.len;
            let mut stream = connection
                .open_uni()
                .await
                .map_err(|error| other("open file transfer stream", error))?;
            write_message(&mut stream, &WireMessage::File(transfer.clone())).await?;
            send_file_with_progress(&root, &transfer, &mut stream, &progress).await?;
            finish_file_stream(&mut stream).await?;
            Ok((1_usize, bytes))
        });
    }

    while !tasks.is_empty() {
        let (sent_files, sent_bytes) = join_file_task(&mut tasks).await?;
        files += sent_files;
        bytes = bytes.saturating_add(sent_bytes);
        progress.inc(sent_files as u64);
        progress.record_completed_transfer_files(sent_files);
    }

    Ok((files, bytes))
}

pub(super) async fn finish_file_stream(send: &mut SendStream) -> Result<()> {
    send.finish()
        .map_err(|error| other("finish QUIC file stream", error))
}

pub(super) async fn join_file_task(
    tasks: &mut JoinSet<Result<(usize, u64)>>,
) -> Result<(usize, u64)> {
    match tasks.join_next().await {
        Some(Ok(result)) => result,
        Some(Err(error)) => Err(other("join network file transfer task", error)),
        None => Ok((0, 0)),
    }
}

async fn send_file_with_progress(
    root: &Path,
    file: &FileTransfer,
    send: &mut SendStream,
    progress: &ProgressPhase,
) -> Result<()> {
    let path = safe_join(root, &file.path)?;
    let mut input = tokio::fs::File::open(&path)
        .await
        .map_err(|error| io_path("open file for network send", &path, error))?;
    let mut remaining = file.len;
    let mut buffer = vec![0_u8; BUFFER_SIZE];

    while remaining > 0 {
        let read = input
            .read(&mut buffer)
            .await
            .map_err(|error| io_path("read file for network send", &path, error))?;
        if read == 0 {
            return Err(other_message(
                "send file",
                format!("file ended early: {}", file.path.display()),
            ));
        }
        send.write_all(&buffer[..read])
            .await
            .map_err(|error| other("write file chunk to QUIC stream", error))?;
        remaining = remaining.saturating_sub(read as u64);
        progress.add_transfer_bytes(read as u64);
    }

    Ok(())
}

/// 并发接收已请求的文件流,并按已落盘文件数更新进度。
async fn receive_requested_files_with_progress(
    root: &Path,
    connection: &quinn::Connection,
    requested_files: Vec<PathBuf>,
    options: TransferOptions,
    progress: &ProgressPhase,
) -> Result<(usize, u64)> {
    let requested: HashSet<_> = requested_files.into_iter().collect();
    let mut started = HashSet::new();
    let mut tasks = JoinSet::new();
    let mut files = 0_usize;
    let mut bytes = 0_u64;

    for _ in 0..requested.len() {
        while tasks.len() >= options.file_concurrency {
            let (received_files, received_bytes) = join_file_task(&mut tasks).await?;
            files += received_files;
            bytes = bytes.saturating_add(received_bytes);
            progress.inc(received_files as u64);
            progress.record_completed_transfer_files(received_files);
        }

        let mut stream = connection
            .accept_uni()
            .await
            .map_err(|error| other("accept file transfer stream", error))?;
        let file = match read_message(&mut stream).await? {
            WireMessage::File(file) => file,
            _ => return Err(other_message("receive file stream", "unexpected message")),
        };
        if !requested.contains(&file.path) {
            return Err(other_message(
                "receive file stream",
                format!("unrequested file stream: {}", file.path.display()),
            ));
        }
        if !started.insert(file.path.clone()) {
            return Err(other_message(
                "receive file stream",
                format!("duplicate file stream: {}", file.path.display()),
            ));
        }

        info!(
            path = %file.path.display(),
            bytes = file.len,
            "receiving file"
        );
        let root = root.to_path_buf();
        let progress = progress.clone();
        tasks.spawn(async move {
            let bytes = file.len;
            receive_file_with_progress(&root, &file, &mut stream, options, &progress).await?;
            Ok((1_usize, bytes))
        });
    }

    while !tasks.is_empty() {
        let (received_files, received_bytes) = join_file_task(&mut tasks).await?;
        files += received_files;
        bytes = bytes.saturating_add(received_bytes);
        progress.inc(received_files as u64);
        progress.record_completed_transfer_files(received_files);
    }

    Ok((files, bytes))
}

async fn receive_file_with_progress(
    root: &Path,
    file: &FileTransfer,
    recv: &mut RecvStream,
    options: TransferOptions,
    progress: &ProgressPhase,
) -> Result<()> {
    let target = safe_join(root, &file.path)?;
    let Some(parent) = target.parent() else {
        return Err(other_message("receive file", "target path has no parent"));
    };
    tokio::fs::create_dir_all(parent)
        .await
        .map_err(|error| io_path("create received file parent", parent, error))?;
    ensure_file_path(&target, options.delete).await?;
    let temp_path = unique_temp_path(parent);
    let mut output = tokio::fs::File::create(&temp_path)
        .await
        .map_err(|error| io_path("create network temp file", &temp_path, error))?;
    let mut hasher = blake3::Hasher::new();
    let mut remaining = file.len;
    let mut buffer = vec![0_u8; BUFFER_SIZE];

    while remaining > 0 {
        let read_len = next_chunk_len(remaining, buffer.len());
        let Some(read) = recv
            .read(&mut buffer[..read_len])
            .await
            .map_err(|error| other("read file chunk from QUIC stream", error))?
        else {
            let _ = tokio::fs::remove_file(&temp_path).await;
            return Err(other_message(
                "receive file",
                format!(
                    "stream ended before file completed: {}",
                    file.path.display()
                ),
            ));
        };
        output
            .write_all(&buffer[..read])
            .await
            .map_err(|error| io_path("write network temp file", &temp_path, error))?;
        hasher.update(&buffer[..read]);
        remaining = remaining.saturating_sub(read as u64);
        progress.add_transfer_bytes(read as u64);
    }
    output
        .flush()
        .await
        .map_err(|error| io_path("flush network temp file", &temp_path, error))?;
    output
        .sync_data()
        .await
        .map_err(|error| io_path("sync network temp file", &temp_path, error))?;
    drop(output);

    let actual = hex_digest(*hasher.finalize().as_bytes());
    if actual != file.blake3 {
        let _ = tokio::fs::remove_file(&temp_path).await;
        return Err(other_message(
            "verify received file",
            format!("BLAKE3 mismatch: {}", file.path.display()),
        ));
    }

    match tokio::fs::rename(&temp_path, &target).await {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
            tokio::fs::remove_file(&target).await.map_err(|error| {
                io_path("remove old target before network replace", &target, error)
            })?;
            tokio::fs::rename(&temp_path, &target)
                .await
                .map_err(|error| io_path("rename network temp file", &target, error))
        }
        Err(error) => {
            let _ = tokio::fs::remove_file(&temp_path).await;
            Err(io_path("rename network temp file", &target, error))
        }
    }?;
    apply_path_metadata(&target, &file.metadata, options)
}

pub(super) fn next_chunk_len(remaining: u64, buffer_len: usize) -> usize {
    if remaining > buffer_len as u64 {
        buffer_len
    } else {
        remaining as usize
    }
}

pub(super) async fn ensure_directory_path(path: &Path, delete_enabled: bool) -> Result<()> {
    match tokio::fs::symlink_metadata(path).await {
        Ok(metadata) if metadata.is_dir() => Ok(()),
        Ok(metadata) if delete_enabled => {
            if metadata.is_dir() {
                tokio::fs::remove_dir_all(path).await.map_err(|error| {
                    io_path("remove directory before network replace", path, error)
                })
            } else {
                tokio::fs::remove_file(path)
                    .await
                    .map_err(|error| io_path("remove file before network directory", path, error))
            }
        }
        Ok(_) => Err(other_message(
            "create received directory",
            format!(
                "target path exists and is not a directory: {}",
                path.display()
            ),
        )),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(io_path("read target metadata", path, error)),
    }
}

pub(super) async fn ensure_file_path(path: &Path, delete_enabled: bool) -> Result<()> {
    match tokio::fs::symlink_metadata(path).await {
        Ok(metadata) if metadata.is_dir() && delete_enabled => tokio::fs::remove_dir_all(path)
            .await
            .map_err(|error| io_path("remove directory before network file", path, error)),
        Ok(metadata) if metadata.is_dir() => Err(other_message(
            "receive file",
            format!("target path exists and is a directory: {}", path.display()),
        )),
        Ok(_) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(error) => Err(io_path("read target metadata", path, error)),
    }
}

#[cfg(test)]
pub(super) async fn delete_obsolete(root: &Path, manifest: &Manifest) -> Result<usize> {
    delete_obsolete_with_progress(
        root,
        manifest,
        &PathFilter::disabled(),
        &ProgressPhase::disabled(),
    )
    .await
}

#[cfg(test)]
pub(super) async fn delete_obsolete_filtered(
    root: &Path,
    manifest: &Manifest,
    filter: &PathFilter,
) -> Result<usize> {
    delete_obsolete_with_progress(root, manifest, filter, &ProgressPhase::disabled()).await
}

/// 删除目标端中 manifest 不再包含的陈旧项,并展示已删除数量。
async fn delete_obsolete_with_progress(
    root: &Path,
    manifest: &Manifest,
    filter: &PathFilter,
    progress: &ProgressPhase,
) -> Result<usize> {
    let target_snapshot = match crate::scan::scan_optional_directory_filtered(root, false, filter) {
        Ok(snapshot) => snapshot,
        Err(FastSyncError::InvalidTarget(path)) if path == root => return Ok(0),
        Err(error) => return Err(error),
    };
    let desired_dirs: HashSet<_> = manifest.dirs.iter().map(|dir| dir.path.clone()).collect();
    let desired_files: HashSet<_> = manifest
        .files
        .iter()
        .map(|file| file.path.clone())
        .collect();
    let mut obsolete: Vec<_> = target_snapshot
        .entries
        .values()
        .filter(|entry| {
            !desired_dirs.contains(&entry.relative_path)
                && !desired_files.contains(&entry.relative_path)
        })
        .cloned()
        .collect();
    obsolete.sort_by_key(|entry| std::cmp::Reverse(entry.relative_path.components().count()));

    let mut deleted = 0_usize;
    for entry in obsolete {
        let path = safe_join(root, &entry.relative_path)?;
        match entry.kind {
            crate::scan::EntryKind::Directory => {
                tokio::fs::remove_dir(&path)
                    .await
                    .map_err(|error| io_path("delete obsolete network directory", &path, error))?;
            }
            crate::scan::EntryKind::File | crate::scan::EntryKind::Symlink => {
                tokio::fs::remove_file(&path)
                    .await
                    .map_err(|error| io_path("delete obsolete network file", &path, error))?;
            }
        }
        deleted += 1;
        progress.set_delete_status(deleted);
        info!(path = %entry.relative_path.display(), "deleted obsolete network entry");
    }

    Ok(deleted)
}

pub(super) fn apply_directory_metadata(
    root: &Path,
    dirs: &[DirManifest],
    options: TransferOptions,
) -> Result<()> {
    let mut dirs = dirs.to_vec();
    dirs.sort_by_key(|dir| std::cmp::Reverse(dir.path.components().count()));
    for dir in dirs {
        let path = safe_join(root, &dir.path)?;
        apply_path_metadata(&path, &dir.metadata, options)?;
    }
    Ok(())
}

pub(super) fn apply_file_metadata(
    root: &Path,
    files: &[FileManifest],
    options: TransferOptions,
) -> Result<()> {
    for file in files {
        let path = safe_join(root, &file.path)?;
        apply_path_metadata(&path, &file.metadata, options)?;
    }
    Ok(())
}

pub(super) fn apply_path_metadata(
    path: &Path,
    metadata: &WireMetadata,
    options: TransferOptions,
) -> Result<()> {
    if options.preserve_permissions {
        set_permissions(path, metadata)?;
    }
    if options.preserve_times {
        if let Some(mtime) = metadata.modified_filetime() {
            filetime::set_file_mtime(path, mtime)
                .map_err(|error| io_path("set received path modified time", path, error))?;
        }
    }
    Ok(())
}

pub(super) fn set_permissions(path: &Path, metadata: &WireMetadata) -> Result<()> {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Some(mode) = metadata.unix_mode {
            let permissions = std::fs::Permissions::from_mode(mode);
            std::fs::set_permissions(path, permissions)
                .map_err(|error| io_path("set received path permissions", path, error))?;
            return Ok(());
        }
    }

    let mut permissions = std::fs::metadata(path)
        .map_err(|error| io_path("read received path permissions", path, error))?
        .permissions();
    permissions.set_readonly(metadata.readonly);
    std::fs::set_permissions(path, permissions)
        .map_err(|error| io_path("set received path permissions", path, error))
}