fastsync 0.2.0

A fast, safe one-way directory synchronization tool for large local folders.
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
use std::fs::{self, File, OpenOptions};
use std::io::{BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::thread;

use crossbeam_channel::{Receiver, Sender, bounded, unbounded};
use tracing::{debug, info};

use crate::config::SyncConfig;
use crate::error::{FastSyncError, Result, io_context};
use crate::plan::{CopyReason, PlanOperation, SyncPlan};
use crate::summary::SyncSummary;
use crate::verify::verify_file;

static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);

#[derive(Debug, Clone)]
enum WorkerTask {
    CopyFile {
        relative_path: PathBuf,
        bytes: u64,
        reason: CopyReason,
    },
    SetMetadata {
        relative_path: PathBuf,
    },
}

#[derive(Debug, Default)]
struct WorkerReport {
    copied_files: usize,
    metadata_updates: usize,
    verified_files: usize,
    bytes_copied: u64,
}

/// 执行同步计划。
///
/// 目录创建和删除按顺序执行,文件复制与元数据修正通过有界队列交给固定数量
/// worker 处理,以避免扫描/调度阶段无限占用内存。
pub fn execute_plan(config: &SyncConfig, plan: &SyncPlan) -> Result<SyncSummary> {
    if config.dry_run {
        return Ok(dry_run_summary(config, plan));
    }

    io_context(
        format!("创建目标根目录: {}", config.target.display()),
        fs::create_dir_all(&config.target),
    )?;

    let mut summary = SyncSummary {
        dry_run: false,
        ..SyncSummary::default()
    };
    let mut worker_tasks = Vec::new();
    let mut delete_ops = Vec::new();

    for operation in &plan.operations {
        match operation {
            PlanOperation::CreateDirectory { relative_path } => {
                create_directory(&config.target, relative_path)?;
                summary.created_dirs += 1;
            }
            PlanOperation::CopyFile {
                relative_path,
                bytes,
                reason,
            } => worker_tasks.push(WorkerTask::CopyFile {
                relative_path: relative_path.clone(),
                bytes: *bytes,
                reason: *reason,
            }),
            PlanOperation::SetMetadata { relative_path } => {
                worker_tasks.push(WorkerTask::SetMetadata {
                    relative_path: relative_path.clone(),
                });
            }
            PlanOperation::DeleteFile { .. }
            | PlanOperation::DeleteDirectory { .. }
            | PlanOperation::DeleteSymlink { .. } => delete_ops.push(operation.clone()),
        }
    }

    let report = run_workers(config, worker_tasks)?;
    summary.copied_files += report.copied_files;
    summary.metadata_updates += report.metadata_updates;
    summary.verified_files += report.verified_files;
    summary.bytes_copied += report.bytes_copied;

    for operation in delete_ops {
        match operation {
            PlanOperation::DeleteFile { relative_path } => {
                delete_file(&config.target, &relative_path)?;
                summary.deleted_files += 1;
            }
            PlanOperation::DeleteDirectory { relative_path } => {
                delete_directory(&config.target, &relative_path)?;
                summary.deleted_dirs += 1;
            }
            PlanOperation::DeleteSymlink { relative_path } => {
                delete_symlink(&config.target, &relative_path)?;
                summary.deleted_symlinks += 1;
            }
            _ => {}
        }
    }

    Ok(summary)
}

fn dry_run_summary(config: &SyncConfig, plan: &SyncPlan) -> SyncSummary {
    let mut summary = SyncSummary {
        dry_run: true,
        ..SyncSummary::default()
    };

    for operation in &plan.operations {
        match operation {
            PlanOperation::CreateDirectory { .. } => summary.created_dirs += 1,
            PlanOperation::CopyFile { bytes, .. } => {
                summary.copied_files += 1;
                summary.bytes_copied = summary.bytes_copied.saturating_add(*bytes);
            }
            PlanOperation::SetMetadata { .. } => summary.metadata_updates += 1,
            PlanOperation::DeleteFile { .. } => summary.deleted_files += 1,
            PlanOperation::DeleteDirectory { .. } => summary.deleted_dirs += 1,
            PlanOperation::DeleteSymlink { .. } => summary.deleted_symlinks += 1,
        }
    }
    summary.errors = 0;
    summary.source = config.source.clone();
    summary.target = config.target.clone();
    summary
}

fn run_workers(config: &SyncConfig, tasks: Vec<WorkerTask>) -> Result<WorkerReport> {
    if tasks.is_empty() {
        return Ok(WorkerReport::default());
    }

    let (task_sender, task_receiver) = bounded(config.queue_size);
    let (report_sender, report_receiver) = unbounded();

    thread::scope(|scope| {
        for worker_id in 0..config.threads {
            let task_receiver = task_receiver.clone();
            let report_sender = report_sender.clone();
            scope.spawn(move || worker_loop(worker_id, config, task_receiver, report_sender));
        }

        drop(report_sender);
        send_tasks(&task_sender, tasks);
        drop(task_sender);

        collect_worker_reports(config, report_receiver)
    })
}

fn send_tasks(sender: &Sender<WorkerTask>, tasks: Vec<WorkerTask>) {
    for task in tasks {
        if sender.send(task).is_err() {
            break;
        }
    }
}

fn worker_loop(
    worker_id: usize,
    config: &SyncConfig,
    receiver: Receiver<WorkerTask>,
    sender: Sender<Result<WorkerReport>>,
) {
    let mut report = WorkerReport::default();

    for task in receiver {
        let result = match task {
            WorkerTask::CopyFile {
                relative_path,
                bytes,
                reason,
            } => copy_one_file(config, &relative_path, bytes, reason, &mut report),
            WorkerTask::SetMetadata { relative_path } => {
                apply_file_metadata(config, &relative_path).map(|_| {
                    report.metadata_updates += 1;
                })
            }
        };

        if let Err(error) = result {
            let _ = sender.send(Err(error));
            if config.stop_on_error {
                debug!(worker_id, "worker 因错误提前停止");
                return;
            }
        }
    }

    let _ = sender.send(Ok(report));
}

fn collect_worker_reports(
    config: &SyncConfig,
    receiver: Receiver<Result<WorkerReport>>,
) -> Result<WorkerReport> {
    let mut merged = WorkerReport::default();
    let mut errors = Vec::new();

    for message in receiver {
        match message {
            Ok(report) => {
                merged.copied_files += report.copied_files;
                merged.metadata_updates += report.metadata_updates;
                merged.verified_files += report.verified_files;
                merged.bytes_copied += report.bytes_copied;
            }
            Err(error) => {
                errors.push(error.to_string());
                if config.stop_on_error || errors.len() >= config.max_errors {
                    break;
                }
            }
        }
    }

    if let Some(first) = errors.first() {
        Err(FastSyncError::Many {
            count: errors.len(),
            first: first.clone(),
        })
    } else {
        Ok(merged)
    }
}

fn copy_one_file(
    config: &SyncConfig,
    relative_path: &Path,
    bytes: u64,
    reason: CopyReason,
    report: &mut WorkerReport,
) -> Result<()> {
    let source = config.source.join(relative_path);
    let target = config.target.join(relative_path);

    if reason == CopyReason::Missing {
        copy_file_direct(&source, &target)?;
    } else if config.atomic_write {
        copy_file_atomic(&source, &target)?;
    } else {
        copy_file_direct(&source, &target)?;
    }
    apply_file_metadata(config, relative_path)?;

    if reason != CopyReason::Missing && config.verify_mode.verify_changed_files() {
        verify_file(&source, &target, relative_path)?;
        report.verified_files += 1;
    }

    report.copied_files += 1;
    report.bytes_copied = report.bytes_copied.saturating_add(bytes);
    info!(path = %relative_path.display(), bytes, "文件复制完成");
    Ok(())
}

fn create_directory(target_root: &Path, relative_path: &Path) -> Result<()> {
    let path = target_root.join(relative_path);
    io_context(
        format!("创建目录: {}", path.display()),
        fs::create_dir_all(path),
    )
}

fn delete_file(target_root: &Path, relative_path: &Path) -> Result<()> {
    let path = target_root.join(relative_path);
    io_context(
        format!("删除文件: {}", path.display()),
        fs::remove_file(path),
    )?;
    info!(path = %relative_path.display(), "文件删除完成");
    Ok(())
}

fn delete_directory(target_root: &Path, relative_path: &Path) -> Result<()> {
    let path = target_root.join(relative_path);
    io_context(
        format!("删除目录: {}", path.display()),
        fs::remove_dir(path),
    )?;
    info!(path = %relative_path.display(), "目录删除完成");
    Ok(())
}

fn delete_symlink(target_root: &Path, relative_path: &Path) -> Result<()> {
    let path = target_root.join(relative_path);
    io_context(
        format!("删除符号链接: {}", path.display()),
        fs::remove_file(path),
    )?;
    info!(path = %relative_path.display(), "符号链接删除完成");
    Ok(())
}

fn copy_file_atomic(source: &Path, target: &Path) -> Result<()> {
    let parent = ensure_parent(target)?;
    let temp_path = unique_temp_path(parent);

    let copy_result = (|| {
        let source_file = io_context(
            format!("打开源文件: {}", source.display()),
            File::open(source),
        )?;
        let temp_file = io_context(
            format!("创建临时文件: {}", temp_path.display()),
            OpenOptions::new()
                .write(true)
                .create_new(true)
                .open(&temp_path),
        )?;
        let mut reader = BufReader::with_capacity(1024 * 1024, source_file);
        let mut writer = BufWriter::with_capacity(1024 * 1024, temp_file);
        io_context(
            format!("复制到临时文件: {}", temp_path.display()),
            std::io::copy(&mut reader, &mut writer),
        )?;
        io_context(
            format!("刷新临时文件: {}", temp_path.display()),
            writer.flush(),
        )?;
        let file = writer.into_inner().map_err(|error| FastSyncError::Io {
            context: format!("完成临时文件写入: {}", temp_path.display()),
            source: error.into_error(),
        })?;
        io_context(
            format!("同步临时文件数据: {}", temp_path.display()),
            file.sync_data(),
        )?;
        Ok(())
    })();

    if let Err(error) = copy_result {
        let _ = fs::remove_file(&temp_path);
        return Err(error);
    }

    match fs::rename(&temp_path, target) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
            io_context(
                format!("替换目标文件前删除旧文件: {}", target.display()),
                fs::remove_file(target),
            )?;
            io_context(
                format!("重命名临时文件到目标: {}", target.display()),
                fs::rename(&temp_path, target),
            )
        }
        Err(error) => {
            let _ = fs::remove_file(&temp_path);
            Err(FastSyncError::Io {
                context: format!("重命名临时文件到目标: {}", target.display()),
                source: error,
            })
        }
    }
}

fn copy_file_direct(source: &Path, target: &Path) -> Result<()> {
    ensure_parent(target)?;
    io_context(
        format!("直接复制文件: {} -> {}", source.display(), target.display()),
        fs::copy(source, target),
    )?;
    Ok(())
}

fn ensure_parent(path: &Path) -> Result<&Path> {
    let Some(parent) = path.parent() else {
        return Err(FastSyncError::Io {
            context: format!("目标路径缺少父目录: {}", path.display()),
            source: std::io::Error::new(std::io::ErrorKind::InvalidInput, "missing parent"),
        });
    };
    io_context(
        format!("创建父目录: {}", parent.display()),
        fs::create_dir_all(parent),
    )?;
    Ok(parent)
}

fn unique_temp_path(parent: &Path) -> PathBuf {
    let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
    parent.join(format!(".fastsync.tmp.{}.{}", std::process::id(), counter))
}

fn apply_file_metadata(config: &SyncConfig, relative_path: &Path) -> Result<()> {
    let source = config.source.join(relative_path);
    let target = config.target.join(relative_path);
    let source_metadata = io_context(
        format!("读取源文件元数据: {}", source.display()),
        fs::metadata(&source),
    )?;

    if config.preserve_permissions.enabled() {
        let permissions = source_metadata.permissions();
        io_context(
            format!("设置目标文件权限: {}", target.display()),
            fs::set_permissions(&target, permissions),
        )?;
    }

    if config.preserve_times.enabled() {
        let atime = filetime::FileTime::from_last_access_time(&source_metadata);
        let mtime = filetime::FileTime::from_last_modification_time(&source_metadata);
        io_context(
            format!("设置目标文件时间戳: {}", target.display()),
            filetime::set_file_times(&target, atime, mtime),
        )?;
    }

    Ok(())
}