-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.rs
More file actions
1278 lines (1130 loc) · 35.4 KB
/
sync.rs
File metadata and controls
1278 lines (1130 loc) · 35.4 KB
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
use crate::Result;
use crate::config::types::{Config, SftpPermissions, SyncEngine};
use crate::ssh::client::Client;
use crate::ui::create_spinner;
use color_eyre::eyre::{Context as _, ContextCompat as _, bail};
use console::style;
use core::mem::take;
use gethostname::gethostname;
use globset::{Glob, GlobSet, GlobSetBuilder};
use ignore::WalkBuilder;
use indicatif::ProgressBar;
use russh_sftp::client::SftpSession;
use russh_sftp::protocol::{FileAttributes, OpenFlags};
use sha2::{Digest as _, Sha256};
use std::collections::{HashMap, HashSet};
use std::fs::{self, File};
use std::io;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt as _;
use std::path::{Path, PathBuf};
use tokio::fs::{File as AsyncFile, metadata};
use tokio::io::{BufReader as AsyncBufReader, copy as async_copy};
use tokio::task::spawn_blocking;
use tracing::{debug, info, warn};
/// Separator emitted by the remote sync-state script before file hash lines.
const REMOTE_FILE_MARKER: &str = "__BIWA_FILE_HASHES__";
/// Conservative upper bound for a batched remote `mkdir -p` command.
const MAX_REMOTE_MKDIR_COMMAND_LEN: usize = 4096;
/// Statistics for a synchronization operation.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Stats {
/// Number of files uploaded.
pub uploaded: usize,
/// Number of files deleted.
pub deleted: usize,
/// Number of files unchanged.
pub unchanged: usize,
}
/// A local file and its SHA-256 hash.
#[derive(Debug, Clone)]
pub(super) struct LocalFile {
/// The relative path to the file from the project root.
pub path: PathBuf,
/// The SHA-256 hash of the file content.
pub hash: String,
}
/// The local sync state collected from the project root.
#[derive(Debug, Default)]
struct LocalState {
/// The local files that should exist on the remote side.
files: Vec<LocalFile>,
/// The local directories that should exist on the remote side.
directories: HashSet<String>,
}
/// The remote sync state collected from the project directory.
#[derive(Debug, Default)]
struct RemoteState {
/// The remote files and their hashes.
file_hashes: HashMap<String, String>,
/// The remote directories that currently exist.
directories: HashSet<String>,
}
/// Options for a synchronization operation.
#[derive(Debug, Default, Clone)]
pub struct Options {
/// Force synchronization of all files, ignoring incremental hash checks.
pub force: bool,
/// Exclude files matching these paths or globs.
pub exclude: Vec<String>,
/// Only synchronize files matching these paths or globs.
pub include: Vec<String>,
}
/// Builds a `GlobSet` from a slice of pattern strings.
fn build_globset(patterns: &[String]) -> Result<Option<GlobSet>> {
if patterns.is_empty() {
return Ok(None);
}
let mut builder = GlobSetBuilder::new();
for pattern in patterns {
builder.add(Glob::new(pattern)?);
}
builder
.build()
.wrap_err("Failed to build glob set")
.map(Some)
}
/// Returns whether a path matches a globset.
///
/// For directories, also try the path with a trailing slash so patterns like
/// `foo/**` match the `foo` directory entry itself, not just its descendants.
fn path_matches_globset(globset: &GlobSet, path: &Path, is_dir: bool) -> bool {
let path = path.to_string_lossy();
if globset.is_match(path.as_ref()) {
return true;
}
is_dir && globset.is_match(format!("{path}/"))
}
/// Shell-quotes a remote path while preserving home directory expansion.
///
/// If the path starts with `~/`, the `~` is replaced with `$HOME` and placed
/// outside the quotes so the shell can expand it. Otherwise, the entire path
/// is quoted with `shell_words::quote`.
pub(super) fn shell_quote_path(path: &str) -> String {
if path == "~" || path == "$HOME" {
return "\"$HOME\"".to_owned();
}
if let Some(rest) = path
.strip_prefix("~/")
.or_else(|| path.strip_prefix("$HOME/"))
{
return format!("\"$HOME\"/{}", shell_words::quote(rest));
}
shell_words::quote(path).into_owned()
}
/// A wrapper around a hasher that implements `std::io::Write`.
struct HasherWriter<'a, H> {
/// The underlying hasher instance.
hasher: &'a mut H,
}
impl<H: sha2::Digest> io::Write for HasherWriter<'_, H> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.hasher.update(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
/// Computes the SHA-256 hash of a file.
fn hash_file(path: &Path) -> Result<String> {
let file = File::open(path)?;
let mut reader = io::BufReader::new(file);
let mut hasher = Sha256::new();
io::copy(
&mut reader,
&mut HasherWriter {
hasher: &mut hasher,
},
)
.wrap_err_with(|| format!("Failed to hash file: {}", path.display()))?;
Ok(hex::encode(hasher.finalize()))
}
/// Returns whether a local path should participate in synchronization.
fn should_sync_path(
root: &Path,
path: &Path,
is_dir: bool,
is_symlink: bool,
exclude_globs: Option<&GlobSet>,
include_globs: Option<&GlobSet>,
) -> Result<Option<PathBuf>> {
let relative = path.strip_prefix(root).wrap_err("Failed to strip prefix")?;
if relative.as_os_str().is_empty() {
return Ok(None);
}
if is_symlink {
return Ok(None);
}
if exclude_globs
.as_ref()
.is_some_and(|set| path_matches_globset(set, path, is_dir))
{
return Ok(None);
}
if include_globs
.as_ref()
.is_some_and(|set| !path_matches_globset(set, path, is_dir))
{
return Ok(None);
}
Ok(Some(relative.to_path_buf()))
}
/// Collects local files and directories from the project root, respecting ignore rules.
fn collect_local_state(
root: &Path,
config_exclude: &[String],
options: &Options,
) -> Result<LocalState> {
let mut builder = WalkBuilder::new(root);
builder.standard_filters(true); // .gitignore, .ignore, etc.
builder.add_custom_ignore_filename(".biwaignore");
builder.hidden(false); // Include hidden files (e.g. .env, .gitignore)
builder.require_git(false); // Respect .gitignore even outside of git repositories
let combined_exclude = config_exclude
.iter()
.chain(options.exclude.iter())
.map(ToString::to_string)
.collect::<Vec<_>>();
let exclude_globs = build_globset(&combined_exclude)?;
let include_globs = build_globset(&options.include)?;
let mut state = LocalState::default();
for entry in builder.build() {
let entry = entry?;
let path = entry.path();
let file_type = fs::symlink_metadata(path)
.wrap_err_with(|| format!("Failed to read metadata for {}", path.display()))?
.file_type();
let is_dir = file_type.is_dir();
let is_symlink = file_type.is_symlink();
let Some(relative) = should_sync_path(
root,
path,
is_dir,
is_symlink,
exclude_globs.as_ref(),
include_globs.as_ref(),
)?
else {
continue;
};
if !is_dir && !is_symlink && path.is_file() {
state.files.push(LocalFile {
path: relative,
hash: hash_file(path)?,
});
continue;
}
if is_dir {
state
.directories
.insert(relative.to_string_lossy().into_owned());
}
}
Ok(state)
}
/// Computes the absolute remote path for a given local file.
pub(super) fn compute_remote_path(
remote_root: &Path,
project_name: &str,
relative: &Path,
) -> String {
let mut path = remote_root.to_string_lossy().into_owned();
if !path.ends_with('/') {
path.push('/');
}
path.push_str(project_name);
let rel_str = relative.to_string_lossy();
if !rel_str.is_empty() {
if !path.ends_with('/') && !rel_str.starts_with('/') {
path.push('/');
}
path.push_str(&rel_str);
}
path
}
/// Computes a unique project name based on the hostname and project root's canonical path.
fn compute_unique_project_name(project_root: &Path) -> Result<String> {
let project_name = project_root
.file_name()
.wrap_err("Invalid project root directory")?
.to_string_lossy()
.into_owned();
// Create a unique hash based on the hostname and absolute path to prevent
// collisions between projects with the same name across machines.
let mut hasher = Sha256::new();
hasher.update(gethostname().to_string_lossy().as_bytes());
hasher.update([0]);
hasher.update(
project_root
.canonicalize()
.wrap_err("Failed to canonicalize project root")?
.to_string_lossy()
.as_bytes(),
);
let hash_hex = hex::encode(hasher.finalize());
#[expect(
clippy::string_slice,
reason = "Hex encoded strings are strictly ASCII, slicing is safe"
)]
Ok(format!("{}-{}", project_name, &hash_hex[..8]))
}
/// Computes the remote directory path for a given project.
///
/// This is the directory where synced files are stored on the remote server.
pub fn compute_project_remote_dir(config: &Config, project_root: &Path) -> Result<String> {
let unique_project_name = compute_unique_project_name(project_root)?;
Ok(compute_remote_path(
&config.sync.remote_root,
&unique_project_name,
Path::new(""),
))
}
/// Extends a directory set with parent directories implied by local files.
fn collect_parent_directories_into(files: &[LocalFile], directories: &mut HashSet<String>) {
for local_file in files {
for ancestor in local_file.path.ancestors() {
if ancestor.as_os_str().is_empty() || ancestor == local_file.path.as_path() {
continue;
}
directories.insert(ancestor.to_string_lossy().into_owned());
}
}
}
/// Fetches the current remote directory and file state.
async fn fetch_remote_state(
client: &Client,
config: &Config,
remote_dir: &str,
) -> Result<RemoteState> {
let quoted_remote_dir = shell_quote_path(remote_dir);
let dir_mode = format!("{:04o}", 0o777 & !config.ssh.umask.as_u32());
let quoted_marker = shell_words::quote(REMOTE_FILE_MARKER).into_owned();
// Create the remote dir, normalize directory permissions, then print directories and file hashes.
let script = format!(
"umask {} && mkdir -p -- {quoted_remote_dir} && \
if [ -L {quoted_remote_dir} ]; then echo 'Error: remote directory is a symlink' >&2; exit 1; fi && \
cd -- {quoted_remote_dir} && \
(find . -type d -exec chmod {dir_mode} {{}} + || true) && \
(find . -mindepth 1 -type d -print || true) && \
printf '%s\n' {quoted_marker} && \
(find . -type f -exec sha256sum {{}} + || true)",
&config.ssh.umask
);
let result = client
.execute(&script)
.await
.wrap_err("Failed to fetch remote state")?;
if result.exit_status != 0 {
let stderr = result.stderr.trim();
if stderr.contains("remote directory is a symlink") {
bail!("remote directory is a symlink");
}
bail!(
"Remote script failed with code {}: {}",
result.exit_status,
stderr
);
}
let output = result.stdout;
Ok(parse_remote_state(&output))
}
/// Actions to perform during synchronization.
struct SyncActions {
/// Files to upload to the remote server.
uploads: Vec<PathBuf>,
/// Files to delete from the remote server.
file_deletions: Vec<String>,
/// Directories to create on the remote server.
directory_creations: Vec<String>,
/// Directories to delete from the remote server.
directory_deletions: Vec<String>,
}
/// Compares local and remote sync state to determine which actions are required.
fn calculate_sync_actions(
local_state: &LocalState,
remote_state: &RemoteState,
options: &Options,
) -> SyncActions {
let mut desired_dirs = local_state.directories.clone();
collect_parent_directories_into(&local_state.files, &mut desired_dirs);
let mut to_upload = Vec::new();
let mut local_paths_str = HashSet::new();
for local_file in &local_state.files {
let rel_path_str = local_file.path.to_string_lossy().into_owned();
local_paths_str.insert(rel_path_str.clone());
if !options.force
&& !remote_state.directories.contains(&rel_path_str)
&& let Some(remote_hash) = remote_state.file_hashes.get(&rel_path_str)
&& remote_hash == &local_file.hash
{
continue;
}
to_upload.push(local_file.path.clone());
}
let mut to_delete_files = HashSet::new();
let mut remote_paths: Vec<_> = remote_state.file_hashes.keys().cloned().collect();
remote_paths.sort_unstable(); // Sort to avoid iter_over_hash_type issue and ensure determinism
for remote_path in remote_paths {
if !local_paths_str.contains(&remote_path) || desired_dirs.contains(&remote_path) {
to_delete_files.insert(remote_path);
}
}
let mut to_create_dirs = desired_dirs
.iter()
.filter(|path| !remote_state.directories.contains(*path))
.cloned()
.collect::<Vec<_>>();
to_create_dirs.sort_unstable();
let mut to_delete_dirs = remote_state
.directories
.iter()
.filter(|path| !desired_dirs.contains(*path) || local_paths_str.contains(*path))
.cloned()
.collect::<Vec<_>>();
to_delete_dirs.sort_unstable_by(|left, right| {
let left_depth = left.bytes().filter(|byte| *byte == b'/').count();
let right_depth = right.bytes().filter(|byte| *byte == b'/').count();
right_depth.cmp(&left_depth).then_with(|| left.cmp(right))
});
let mut to_delete_files = to_delete_files.into_iter().collect::<Vec<_>>();
to_delete_files.sort_unstable();
to_upload.sort_unstable();
SyncActions {
uploads: to_upload,
file_deletions: to_delete_files,
directory_creations: to_create_dirs,
directory_deletions: to_delete_dirs,
}
}
/// Aborts synchronization when too many local files are considered for sync.
fn ensure_sync_file_limit(file_count: usize, max_files_to_sync: usize) -> Result<()> {
if file_count > max_files_to_sync {
bail!(
"Aborting synchronization: {} files to sync exceeds the limit of {}.\nIf this is expected, increase `sync.sftp.max_files_to_sync` in your configuration.",
file_count,
max_files_to_sync
);
}
Ok(())
}
/// SFTP naturally resolves paths not starting with `/` relative to the user's home directory.
/// It does NOT expand `~/` or `$HOME/` like a shell would. Therefore, we strip them so SFTP
/// looks in the home directory instead of looking for literal `~` or `$HOME` folders.
fn resolve_sftp_path(remote_path: &str) -> &str {
remote_path
.strip_prefix("~/")
.or_else(|| remote_path.strip_prefix("$HOME/"))
.unwrap_or_else(|| {
if remote_path == "~" || remote_path == "$HOME" {
"."
} else {
remote_path
}
})
}
/// Returns whether `candidate` is nested beneath `ancestor`.
fn is_nested_directory(candidate: &str, ancestor: &str) -> bool {
candidate != ancestor && Path::new(candidate).starts_with(ancestor)
}
/// Reduces a directory set to its deepest leaf paths.
fn collect_leaf_directories(paths: &[String]) -> Vec<String> {
let mut sorted = paths.to_vec();
sorted.sort_unstable_by(|left, right| {
let left_depth = left.bytes().filter(|byte| *byte == b'/').count();
let right_depth = right.bytes().filter(|byte| *byte == b'/').count();
right_depth.cmp(&left_depth).then_with(|| left.cmp(right))
});
let mut leaves = Vec::new();
for path in sorted {
if leaves
.iter()
.any(|existing: &String| is_nested_directory(existing, &path))
{
continue;
}
leaves.push(path);
}
leaves.sort_unstable();
leaves
}
/// Deletes remote directories one-by-one over the existing SFTP session.
async fn delete_remote_directories(
sftp: &SftpSession,
remote_dir: &str,
relative_paths: &[String],
) -> usize {
let mut deleted = 0_usize;
for path in relative_paths {
let full_path = format!("{remote_dir}/{path}");
let sftp_path = resolve_sftp_path(&full_path);
match sftp.remove_dir(sftp_path).await {
Ok(()) => {
deleted = deleted.saturating_add(1);
}
Err(error) => {
warn!(error = %error, path = sftp_path, "Failed to delete remote directory");
}
}
}
deleted
}
/// Collects the set of directories that must exist before uploading files.
fn collect_directories_to_create(actions: &SyncActions) -> Vec<String> {
let mut directories = actions
.directory_creations
.iter()
.cloned()
.collect::<HashSet<_>>();
for rel_path in &actions.uploads {
if let Some(parent) = rel_path.parent() {
let parent = parent.to_string_lossy().into_owned();
if !parent.is_empty() {
directories.insert(parent);
}
}
}
let mut directories = directories.into_iter().collect::<Vec<_>>();
directories.sort_unstable();
collect_leaf_directories(&directories)
}
/// Splits directory creation into bounded `mkdir -p` batches.
fn build_mkdir_commands(umask: &str, remote_dir: &str, relative_paths: &[String]) -> Vec<String> {
if relative_paths.is_empty() {
return Vec::new();
}
let prefix = format!("umask {umask} && mkdir -p --");
let mut commands = Vec::new();
let mut current = prefix.clone();
for quoted_path in relative_paths
.iter()
.map(|path| format!("{remote_dir}/{path}"))
.map(|path| shell_quote_path(&path))
{
let projected_len = current
.len()
.saturating_add(1)
.saturating_add(quoted_path.len());
if current.len() > prefix.len() && projected_len > MAX_REMOTE_MKDIR_COMMAND_LEN {
commands.push(take(&mut current));
current.clone_from(&prefix);
}
current.push(' ');
current.push_str("ed_path);
}
if current.len() > prefix.len() {
commands.push(current);
}
commands
}
/// Creates remote directories with the configured umask.
async fn create_remote_directories(
client: &Client,
config: &Config,
remote_dir: &str,
relative_paths: &[String],
) -> Result<()> {
for mkdir_cmd in build_mkdir_commands(&config.ssh.umask.to_string(), remote_dir, relative_paths)
{
let result = client
.execute(&mkdir_cmd)
.await
.wrap_err("Failed to create remote directories")?;
if result.exit_status != 0 {
bail!(
"Failed to create remote directories: {}",
result.stderr.trim()
);
}
}
Ok(())
}
/// Uploads a file to a remote SFTP server using an existing session.
///
/// We provide our own upload method so we can set file attributes atomically on
/// creation (`open_with_flags_and_attributes`), avoiding races where sensitive
/// files might be briefly world-readable.
async fn upload_file(
sftp: &SftpSession,
local_path: &Path,
remote_path: &str,
secure_mode: u32,
permissions: &SftpPermissions,
) -> Result<()> {
let perm_attrs = FileAttributes {
permissions: Some(secure_mode | 0x8000), // S_IFREG | permission bits
..Default::default()
};
let mut local_file = AsyncFile::open(local_path)
.await
.wrap_err_with(|| format!("Failed to open local file: {}", local_path.display()))?;
let mut local_file_buffered = AsyncBufReader::new(&mut local_file);
let sftp_path = resolve_sftp_path(remote_path);
if let Ok(attrs) = sftp.symlink_metadata(sftp_path).await
&& attrs.is_symlink()
{
sftp.remove_file(sftp_path)
.await
.wrap_err_with(|| format!("Failed to remove remote symlink: {sftp_path}"))?;
}
if matches!(permissions, SftpPermissions::Recreate) {
let should_remove = sftp.metadata(sftp_path).await.map_or(true, |attrs| {
attrs
.permissions
.map_or_else(|| true, |p| (p & 0o777) != secure_mode)
}); // Default to true if metadata fails
if should_remove && let Err(e) = sftp.remove_file(sftp_path).await {
debug!(error = %e, path = sftp_path, "Failed to remove pre-existing file or file did not exist");
}
}
let open_flags = OpenFlags::CREATE | OpenFlags::TRUNCATE | OpenFlags::WRITE;
let mut remote_file = sftp
.open_with_flags_and_attributes(sftp_path, open_flags, perm_attrs.clone())
.await
.wrap_err_with(|| format!("Failed to open remote file: {sftp_path}"))?;
if matches!(permissions, SftpPermissions::Setstat)
&& let Err(e) = remote_file.set_metadata(perm_attrs).await
{
warn!(
error = %e,
path = sftp_path,
"Failed to enforce file permissions via fsetstat. \
Consider setting `sync.sftp.permissions = \"recreate\"` in your config."
);
}
async_copy(&mut local_file_buffered, &mut remote_file)
.await
.wrap_err("Failed to write to remote file")?;
Ok(())
}
/// Target and actions for a synchronization operation.
struct SyncTarget<'a> {
/// The local project root directory.
project_root: &'a Path,
/// The remote directory path.
remote_dir: &'a str,
/// The synchronization actions to execute.
actions: SyncActions,
}
/// Executes the synchronization actions by uploading and deleting files via SFTP.
async fn apply_sync_actions(
client: &Client,
config: &Config,
target: SyncTarget<'_>,
stats: &mut Stats,
spinner: Option<&ProgressBar>,
) -> Result<()> {
let SyncTarget {
project_root,
remote_dir,
actions,
} = target;
if actions.file_deletions.is_empty()
&& actions.directory_creations.is_empty()
&& actions.directory_deletions.is_empty()
&& actions.uploads.is_empty()
{
return Ok(());
}
let channel = client
.get_channel()
.await
.wrap_err("Failed to get SFTP channel")?;
channel
.request_subsystem(true, "sftp")
.await
.wrap_err("Failed to request SFTP subsystem")?;
let sftp = SftpSession::new(channel.into_stream())
.await
.wrap_err("Failed to initialize SFTP session")?;
// Remove deleted files via SFTP.
for path in &actions.file_deletions {
let full_path = format!("{remote_dir}/{path}");
let sftp_path = resolve_sftp_path(&full_path);
if let Err(e) = sftp.remove_file(sftp_path).await {
warn!(error = %e, path = sftp_path, "Failed to delete remote file");
} else {
stats.deleted = stats.deleted.saturating_add(1);
}
}
// Remove deleted directories deepest-first so parents become empty first.
stats.deleted = stats.deleted.saturating_add(
delete_remote_directories(&sftp, remote_dir, &actions.directory_deletions).await,
);
// Pre-create directories respecting umask.
let dirs_to_create = collect_directories_to_create(&actions);
create_remote_directories(client, config, remote_dir, &dirs_to_create).await?;
// Upload files and change permissions to match local user permissions (respecting umask)
let total_to_upload = actions.uploads.len();
for (i, rel_path) in actions.uploads.into_iter().enumerate() {
if let Some(s) = spinner {
s.set_message(format!(
"Synchronizing files... ({}/{total_to_upload})",
i.saturating_add(1)
));
}
let local_path = project_root.join(&rel_path);
let rel_str = rel_path.to_string_lossy().into_owned();
let remote_path = format!("{remote_dir}/{rel_str}");
// Read local permissions
let local_mode = metadata(&local_path)
.await
.wrap_err_with(|| format!("Failed to read metadata for {}", local_path.display()))?
.permissions()
.mode();
// Apply configured umask to local permissions
let secure_mode = local_mode & !config.ssh.umask.as_u32();
upload_file(
&sftp,
&local_path,
&remote_path,
secure_mode,
&config.sync.sftp.permissions,
)
.await?;
stats.uploaded = stats.uploaded.saturating_add(1);
}
Ok(())
}
/// Synchronizes a project to a remote server.
#[expect(clippy::module_name_repetitions, reason = "No better name exists")]
pub async fn sync_project(
client: &Client,
config: &Config,
project_root: &Path,
options: &Options,
remote_dir_override: Option<&str>,
quiet: bool,
) -> Result<Stats> {
if config.sync.engine != SyncEngine::Sftp {
bail!("Only SFTP sync engine is currently supported");
}
info!(
project_root = %project_root.display(),
force = options.force,
include_patterns = options.include.len(),
exclude_patterns = options.exclude.len(),
has_remote_override = remote_dir_override.is_some(),
"Starting project synchronization"
);
let unique_project_name = compute_unique_project_name(project_root)?;
let local_state = {
let project_root = project_root.to_path_buf();
let exclude = config.sync.exclude.clone();
let options = options.clone();
spawn_blocking(move || collect_local_state(&project_root, &exclude, &options))
.await
.wrap_err("Failed to join blocking task")??
};
info!(
local_directories = local_state.directories.len(),
local_files = local_state.files.len(),
"Collected local sync state"
);
ensure_sync_file_limit(local_state.files.len(), config.sync.sftp.max_files_to_sync)?;
let spinner = if quiet {
None
} else {
Some(create_spinner("Synchronizing files...".to_owned()))
};
// Compute remote directory base
let remote_dir = remote_dir_override.map_or_else(
|| {
compute_remote_path(
&config.sync.remote_root,
&unique_project_name,
Path::new(""),
)
},
String::from,
);
let remote_state = fetch_remote_state(client, config, &remote_dir).await?;
debug!(
remote_dir = %remote_dir,
remote_directories = remote_state.directories.len(),
remote_files = remote_state.file_hashes.len(),
"Fetched remote sync state"
);
let mut stats = Stats::default();
let actions = calculate_sync_actions(&local_state, &remote_state, options);
stats.unchanged = local_state
.files
.len()
.saturating_sub(actions.uploads.len());
info!(
to_create_dirs = actions.directory_creations.len(),
to_delete_dirs = actions.directory_deletions.len(),
to_upload = actions.uploads.len(),
to_delete = actions.file_deletions.len(),
unchanged = stats.unchanged,
"Calculated synchronization actions"
);
apply_sync_actions(
client,
config,
SyncTarget {
project_root,
remote_dir: &remote_dir,
actions,
},
&mut stats,
spinner.as_ref(),
)
.await?;
if let Some(s) = spinner {
s.finish_and_clear();
}
info!("Sync completed: {:?}", stats);
if !quiet {
eprintln!(
"{} Sync completed: {} uploaded, {} deleted, {} unchanged",
style("✓").green().bold(),
stats.uploaded,
stats.deleted,
stats.unchanged
);
}
Ok(stats)
}
/// Normalizes a remote relative path and rejects absolute or traversal paths.
fn parse_remote_path(raw_path: &str, entry_kind: &str) -> Option<String> {
let path = raw_path.strip_prefix("./").unwrap_or(raw_path);
if path.is_empty() || path == "." {
return None;
}
if path.starts_with('/')
|| path == "~"
|| path.starts_with("~/")
|| path == "$HOME"
|| path.starts_with("$HOME/")
{
warn!(
"Skipping remote {entry_kind} with invalid absolute path: {}",
path
);
return None;
}
if path.split('/').any(|comp| comp == "..") {
warn!(
"Skipping remote {entry_kind} with invalid path traversal components: {}",
path
);
return None;
}
Some(path.to_owned())
}
/// Parses the output of the remote sync-state script into a directory set and file hash map.
fn parse_remote_state(output: &str) -> RemoteState {
let mut remote_state = RemoteState::default();
let mut parsing_files = false;
for line in output.lines() {
if line == REMOTE_FILE_MARKER {
parsing_files = true;
continue;
}
if parsing_files {
if let Some((hash, raw_path)) = line.split_once(" ")
&& let Some(path) = parse_remote_path(raw_path, "file")
{
remote_state.file_hashes.insert(path, hash.to_owned());
}
continue;
}
if let Some(path) = parse_remote_path(line, "directory") {
remote_state.directories.insert(path);
}
}
remote_state
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use std::fs;
use tempfile::tempdir;
#[test]
fn collect_local_files_basic() {
let dir = tempdir().unwrap();
let file_path = dir.path().join("test.txt");
fs::write(&file_path, "hello").unwrap();
let files = collect_local_state(dir.path(), &[], &Options::default())
.unwrap()
.files;
assert_eq!(files.len(), 1);
assert_eq!(files.first().unwrap().path.to_string_lossy(), "test.txt");
let expected_hash = hex::encode(Sha256::digest(b"hello"));
assert_eq!(files.first().unwrap().hash, expected_hash);
}
#[test]
fn collect_local_files_respects_gitignore() {
let dir = tempdir().unwrap();
fs::write(dir.path().join(".gitignore"), "ignored.txt\n").unwrap();
fs::write(dir.path().join("ignored.txt"), "ignored").unwrap();
fs::write(dir.path().join("kept.txt"), "kept").unwrap();
let files = collect_local_state(dir.path(), &[], &Options::default())
.unwrap()
.files;
let names: Vec<_> = files
.iter()
.map(|f| f.path.to_string_lossy().to_string())
.collect();
assert!(names.contains(&".gitignore".to_owned()));
assert!(!names.contains(&"ignored.txt".to_owned()));
assert!(names.contains(&"kept.txt".to_owned()));
}
#[test]
fn collect_local_files_includes_hidden() {
let dir = tempdir().unwrap();
fs::write(dir.path().join(".hidden"), "hidden content").unwrap();
fs::write(dir.path().join("visible.txt"), "visible content").unwrap();
let files = collect_local_state(dir.path(), &[], &Options::default())
.unwrap()
.files;
let names: Vec<_> = files
.iter()
.map(|f| f.path.to_string_lossy().to_string())
.collect();