vdsl-sync 0.6.0

File synchronization engine — N-location, pluggable store & backend
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
//! LocationScanner — Locationごとのファイルスキャン抽象。
//!
//! 「このLocationに何があるか」を問い合わせるinfraトレイト。
//! Local/SSH/Cloudの3種を多態で扱う。
//!
//! TransferRouteは「AからBへどう運ぶか」を知る構造体であり、
//! 「Aに何があるか」を知る責務ではない。この分離がLocationScannerの存在理由。
//!
//! # フロー
//!
//! ```text
//! LocationScanner.scan() → ScannedFile[]
//!//! Domain純粋関数 compute_deltas(TopologyFile[], ScannedFile[]) → TopologyDelta[]
//! ```

use std::path::{Path, PathBuf};
use std::sync::Arc;

use async_trait::async_trait;

use crate::domain::file_type::FileType;
use crate::domain::fingerprint::FileFingerprint;
use crate::domain::location::LocationId;
use crate::infra::error::InfraError;
use crate::infra::hasher::{ContentHasher, HashResult};
use crate::infra::shell::RemoteShell;

use super::backend::StorageBackend;

// =============================================================================
// ScannedFile — scan出力型
// =============================================================================

/// スキャンで検出された1ファイル。
///
/// LocationScanner.scan() の出力単位。
/// Domain層のTopologyFile/LocationFileとは独立した、infra由来の生データ。
#[derive(Debug, Clone)]
pub struct ScannedFile {
    pub relative_path: String,
    pub file_type: FileType,
    pub fingerprint: FileFingerprint,
    pub origin: LocationId,
    pub embedded_id: Option<String>,
}

/// スキャン中の非致命的エラー(個別ファイル単位)。
#[derive(Debug, Clone)]
pub struct ScanError {
    pub path: String,
    pub error: String,
}

/// スキャン結果。
pub struct LocationScanResult {
    pub files: Vec<ScannedFile>,
    pub errors: Vec<ScanError>,
}

// =============================================================================
// LocationScanner trait
// =============================================================================

/// Locationのファイル一覧を取得するトレイト。
///
/// 各Locationは自分のスキャン方法を知っている:
/// - Local: walkdir + ContentHasher
/// - SSH: RemoteShell + batch_inspect
/// - Cloud: StorageBackend.list() (metadata only)
#[async_trait]
pub trait LocationScanner: Send + Sync {
    /// このスキャナーが担当するLocationId。
    fn location_id(&self) -> &LocationId;

    /// ファイルスキャンを実行する。
    ///
    /// excludes: スキャン除外globパターン。
    async fn scan(&self, excludes: &[glob::Pattern]) -> Result<LocationScanResult, InfraError>;
}

// =============================================================================
// LocalScanner
// =============================================================================

/// ローカルファイルシステムのスキャナー。
///
/// walkdir + ContentHasherでファイルリスト + ハッシュを取得する。
/// incremental scan: (size, mtime) 不変ならDB上のhashを再利用可能
/// (呼び出し側がLocationFileStoreとの突合で判断)。
pub struct LocalScanner {
    location_id: LocationId,
    root: PathBuf,
    hasher: Arc<dyn ContentHasher>,
}

impl LocalScanner {
    pub fn new(location_id: LocationId, root: PathBuf, hasher: Arc<dyn ContentHasher>) -> Self {
        Self {
            location_id,
            root,
            hasher,
        }
    }
}

#[async_trait]
impl LocationScanner for LocalScanner {
    fn location_id(&self) -> &LocationId {
        &self.location_id
    }

    async fn scan(&self, excludes: &[glob::Pattern]) -> Result<LocationScanResult, InfraError> {
        if !self.root.is_dir() {
            tracing::warn!(
                location = %self.location_id,
                root = %self.root.display(),
                "local_scan: root is not a directory"
            );
            return Ok(LocationScanResult {
                files: Vec::new(),
                errors: Vec::new(),
            });
        }

        tracing::debug!(
            location = %self.location_id,
            root = %self.root.display(),
            excludes = excludes.len(),
            "local_scan: listing files"
        );
        let files = list_local_files(&self.root).await?;
        tracing::debug!(
            location = %self.location_id,
            raw_files = files.len(),
            "local_scan: walkdir done"
        );

        let mut scanned = Vec::new();
        let mut errors = Vec::new();
        let mut excluded = 0usize;

        for (relative_path, size, modified_at) in files {
            if excludes.iter().any(|p| p.matches(&relative_path)) {
                excluded += 1;
                tracing::trace!(
                    path = %relative_path,
                    location = %self.location_id,
                    "local_scan: excluded by pattern"
                );
                continue;
            }

            let file_type = infer_file_type(&relative_path);
            let abs_path = self.root.join(&relative_path);

            match hash_local_file(&self.hasher, &abs_path).await {
                Ok((hash_result, file_size)) => {
                    scanned.push(ScannedFile {
                        relative_path,
                        file_type,
                        fingerprint: FileFingerprint {
                            byte_digest: Some(crate::domain::digest::ByteDigest::Djb2(
                                hash_result.file_hash,
                            )),
                            content_digest: hash_result
                                .content_hash
                                .map(crate::domain::digest::ContentDigest),
                            meta_digest: None,
                            size: file_size.unwrap_or(size),
                            modified_at,
                        },
                        origin: self.location_id.clone(),
                        embedded_id: None,
                    });
                }
                Err(e) => {
                    tracing::warn!(
                        path = %abs_path.display(),
                        location = %self.location_id,
                        error = %e,
                        "local_scan: hash failed"
                    );
                    errors.push(ScanError {
                        path: abs_path.display().to_string(),
                        error: format!("hash failed: {e}"),
                    });
                }
            }
        }

        tracing::info!(
            location = %self.location_id,
            scanned = scanned.len(),
            excluded = excluded,
            errors = errors.len(),
            "local_scan: complete"
        );

        Ok(LocationScanResult {
            files: scanned,
            errors,
        })
    }
}

// =============================================================================
// SshScanner
// =============================================================================

/// SSH経由リモートホストのスキャナー。
///
/// RemoteShell.batch_inspect() でファイルリスト + SHA256ハッシュを一括取得する。
pub struct SshScanner {
    location_id: LocationId,
    root: PathBuf,
    shell: Arc<dyn RemoteShell>,
}

impl SshScanner {
    pub fn new(location_id: LocationId, root: PathBuf, shell: Arc<dyn RemoteShell>) -> Self {
        Self {
            location_id,
            root,
            shell,
        }
    }
}

#[async_trait]
impl LocationScanner for SshScanner {
    fn location_id(&self) -> &LocationId {
        &self.location_id
    }

    async fn scan(&self, excludes: &[glob::Pattern]) -> Result<LocationScanResult, InfraError> {
        let root_str = self.root.to_str().ok_or_else(|| InfraError::Transfer {
            reason: format!(
                "ssh scan root is not valid UTF-8: {}",
                self.root.to_string_lossy()
            ),
        })?;

        // Phase 1: find でファイルリスト取得
        // -L: symlinkをfollow(pod上の output/ が symlink の場合に対応)
        let find_cmd = format!(
            "find -L '{}' -type f -not -name '.*'",
            root_str.replace('\'', "'\\''")
        );
        let output = self
            .shell
            .exec(&["bash", "-c", &find_cmd], Some(60))
            .await
            .map_err(|e| InfraError::Transfer {
                reason: format!("ssh exec failed: {e}"),
            })?;
        if !output.success {
            return Err(InfraError::Transfer {
                reason: format!(
                    "ssh find failed (exit {:?}): {}",
                    output.exit_code, output.stderr
                ),
            });
        }

        let root_prefix = if root_str.ends_with('/') {
            root_str.to_string()
        } else {
            format!("{}/", root_str)
        };

        let relative_paths: Vec<String> = output
            .stdout
            .lines()
            .filter_map(|line| {
                let line = line.trim();
                if line.is_empty() {
                    return None;
                }
                let rel = line.strip_prefix(&root_prefix).unwrap_or(line);
                if excludes.iter().any(|p| p.matches(rel)) {
                    return None;
                }
                Some(rel.to_string())
            })
            .collect();

        if relative_paths.is_empty() {
            return Ok(LocationScanResult {
                files: Vec::new(),
                errors: Vec::new(),
            });
        }

        // Phase 2: batch_inspect でハッシュ + サイズ一括取得
        let inspections = self
            .shell
            .batch_inspect(root_str, &relative_paths)
            .await
            .map_err(|e| InfraError::Transfer {
                reason: format!("ssh batch_inspect failed: {e}"),
            })?;

        let files: Vec<ScannedFile> = inspections
            .into_iter()
            .map(|fi| ScannedFile {
                file_type: infer_file_type(&fi.relative_path),
                fingerprint: FileFingerprint {
                    byte_digest: Some(crate::domain::digest::ByteDigest::Sha256(fi.sha256)),
                    content_digest: None,
                    meta_digest: None,
                    size: fi.size,
                    modified_at: None,
                },
                relative_path: fi.relative_path,
                origin: self.location_id.clone(),
                embedded_id: None,
            })
            .collect();

        Ok(LocationScanResult {
            files,
            errors: Vec::new(),
        })
    }
}

// =============================================================================
// CloudScanner
// =============================================================================

/// Cloud storage (rclone remote) のスキャナー。
///
/// StorageBackend.list() でメタデータのみ取得する。
/// コンテンツハッシュはダウンロードが必要なため取得しない。
pub struct CloudScanner {
    location_id: LocationId,
    root: PathBuf,
    backend: Arc<dyn StorageBackend>,
}

impl CloudScanner {
    pub fn new(location_id: LocationId, root: PathBuf, backend: Arc<dyn StorageBackend>) -> Self {
        Self {
            location_id,
            root,
            backend,
        }
    }
}

#[async_trait]
impl LocationScanner for CloudScanner {
    fn location_id(&self) -> &LocationId {
        &self.location_id
    }

    async fn scan(&self, excludes: &[glob::Pattern]) -> Result<LocationScanResult, InfraError> {
        let root_str = self.root.to_str().ok_or_else(|| InfraError::Transfer {
            reason: format!(
                "cloud scan root is not valid UTF-8: {}",
                self.root.to_string_lossy()
            ),
        })?;

        let remote_files = self
            .backend
            .list(root_str)
            .await
            .map_err(|e| InfraError::Transfer {
                reason: format!("cloud list failed: {e}"),
            })?;

        let files: Vec<ScannedFile> = remote_files
            .into_iter()
            .filter(|f| {
                // basenameが `.` 開始のhidden fileを除外(Local/SSH scannerと挙動統一)。
                // macOS AppleDouble (`._*`)・`.DS_Store` 等が全階層で除外される。
                let hidden = Path::new(&f.path)
                    .file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| n.starts_with('.'));
                if hidden {
                    return false;
                }
                !excludes.iter().any(|p| p.matches(&f.path))
            })
            .map(|f| ScannedFile {
                file_type: infer_file_type(&f.path),
                fingerprint: FileFingerprint {
                    byte_digest: None,
                    content_digest: None,
                    meta_digest: None,
                    size: f.size.unwrap_or(0),
                    modified_at: f.modified_at,
                },
                relative_path: f.path,
                origin: self.location_id.clone(),
                embedded_id: None,
            })
            .collect();

        Ok(LocationScanResult {
            files,
            errors: Vec::new(),
        })
    }
}

// =============================================================================
// Helpers
// =============================================================================

/// 拡張子からFileTypeを推定する。
fn infer_file_type(relative_path: &str) -> FileType {
    Path::new(relative_path)
        .extension()
        .and_then(|e| e.to_str())
        .map(FileType::from_extension)
        .unwrap_or(FileType::Asset)
}

/// ローカルファイルのハッシュ計算(blocking task)。
async fn hash_local_file(
    hasher: &Arc<dyn ContentHasher>,
    path: &Path,
) -> Result<(HashResult, Option<u64>), InfraError> {
    let hasher = Arc::clone(hasher);
    let hash_path = path.to_path_buf();
    let hash_result = tokio::task::spawn_blocking(move || hasher.hash_file(&hash_path))
        .await
        .map_err(|e| InfraError::Hash {
            op: "hasher",
            reason: format!("spawn_blocking join failed: {e}"),
        })?
        .map_err(|e| InfraError::Hash {
            op: "hasher",
            reason: format!("hash_file failed: {e}"),
        })?;
    let file_size = tokio::fs::metadata(path)
        .await
        .map(|m| Some(m.len()))
        .unwrap_or(None);
    Ok((hash_result, file_size))
}

/// ローカルディレクトリの再帰ファイルリスト取得。
///
/// 返り値: (relative_path, size, modified_at) のタプル。
async fn list_local_files(
    root: &Path,
) -> Result<Vec<(String, u64, Option<chrono::DateTime<chrono::Utc>>)>, InfraError> {
    use chrono::{DateTime, Utc};

    let root = root.to_path_buf();
    tokio::task::spawn_blocking(move || {
        let mut result = Vec::new();
        let walker = walkdir::WalkDir::new(&root)
            .follow_links(false)
            .into_iter()
            .filter_map(|e| e.ok());

        for entry in walker {
            if !entry.file_type().is_file() {
                continue;
            }
            // 隠しファイル除外
            if entry
                .file_name()
                .to_str()
                .is_some_and(|n| n.starts_with('.'))
            {
                continue;
            }

            let abs_path = entry.path();
            let relative = abs_path
                .strip_prefix(&root)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_default();

            if relative.is_empty() {
                continue;
            }

            let metadata = entry.metadata().ok();
            let size = metadata.as_ref().map_or(0, |m| m.len());
            let modified_at: Option<DateTime<Utc>> = metadata
                .and_then(|m| m.modified().ok())
                .map(DateTime::<Utc>::from);

            result.push((relative, size, modified_at));
        }
        Ok(result)
    })
    .await
    .map_err(|e| InfraError::Hash {
        op: "list_local_files",
        reason: format!("spawn_blocking join failed: {e}"),
    })?
}