aerosync 0.1.0

Fast, agent-friendly file transfer with auto protocol negotiation (HTTP/QUIC), resumable chunked uploads, and CLI. Library + binary.
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
use crate::{AeroSyncError, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use tokio::io::AsyncReadExt;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileInfo {
    pub path: PathBuf,
    pub name: String,
    pub size: u64,
    pub is_directory: bool,
    pub modified_time: SystemTime,
    pub permissions: FilePermissions,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilePermissions {
    pub readable: bool,
    pub writable: bool,
    pub executable: bool,
}

pub struct FileManager;

impl Default for FileManager {
    fn default() -> Self {
        Self::new()
    }
}

impl FileManager {
    pub fn new() -> Self {
        Self
    }

    pub async fn get_file_info<P: AsRef<Path>>(path: P) -> Result<FileInfo> {
        let path = path.as_ref();
        let metadata = tokio::fs::metadata(path).await?;

        // Better handling for file names with non-ASCII characters (e.g., Chinese)
        let name = path
            .file_name()
            .and_then(|os_str| os_str.to_str())
            .map(|s| s.to_string())
            .unwrap_or_else(|| {
                // Fallback to lossy conversion if not valid UTF-8
                path.file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .to_string()
            });

        let permissions = FilePermissions {
            readable: is_readable(path),
            writable: !metadata.permissions().readonly(),
            executable: is_executable(path),
        };

        Ok(FileInfo {
            path: path.to_path_buf(),
            name,
            size: metadata.len(),
            is_directory: metadata.is_dir(),
            modified_time: metadata.modified()?,
            permissions,
        })
    }

    pub async fn list_directory<P: AsRef<Path>>(path: P) -> Result<Vec<FileInfo>> {
        let path = path.as_ref();
        let mut entries = tokio::fs::read_dir(path).await?;
        let mut files = Vec::new();

        while let Some(entry) = entries.next_entry().await? {
            match Self::get_file_info(entry.path()).await {
                Ok(file_info) => files.push(file_info),
                Err(e) => {
                    tracing::warn!("Failed to get info for file {:?}: {}", entry.path(), e);
                }
            }
        }

        files.sort_by(|a, b| match (a.is_directory, b.is_directory) {
            (true, false) => std::cmp::Ordering::Less,
            (false, true) => std::cmp::Ordering::Greater,
            _ => a.name.cmp(&b.name),
        });

        Ok(files)
    }

    pub async fn create_directory<P: AsRef<Path>>(path: P) -> Result<()> {
        tokio::fs::create_dir_all(path).await?;
        Ok(())
    }

    pub async fn file_exists<P: AsRef<Path>>(path: P) -> bool {
        tokio::fs::metadata(path).await.is_ok()
    }

    pub async fn get_available_space<P: AsRef<Path>>(path: P) -> Result<u64> {
        let path = path.as_ref();
        available_space(path)
    }

    pub fn validate_path<P: AsRef<Path>>(path: P) -> Result<()> {
        Self::validate_path_with_base(path, None)
    }

    /// 校验路径合法性。
    ///
    /// 检查项:
    /// 1. 路径不能为空字符串
    /// 2. 路径不能包含 null byte
    /// 3. 路径不能含有 `..` 组件(防止目录穿越)
    /// 4. 若提供 `base_dir`,则路径经 canonicalize 后必须在 `base_dir` 内部
    pub fn validate_path_with_base<P: AsRef<Path>>(path: P, base_dir: Option<&Path>) -> Result<()> {
        let path = path.as_ref();

        if path.to_string_lossy().is_empty() {
            return Err(AeroSyncError::InvalidConfig("Empty path".to_string()));
        }

        let path_str = path.to_string_lossy();
        if path_str.contains('\0') {
            return Err(AeroSyncError::InvalidConfig(
                "Path contains null character".to_string(),
            ));
        }

        // 禁止路径中出现 `..` 组件,防止目录穿越攻击
        use std::path::Component;
        if path.components().any(|c| c == Component::ParentDir) {
            return Err(AeroSyncError::InvalidConfig(
                "Path must not contain '..' components".to_string(),
            ));
        }

        // 若提供了 base_dir,确认路径在其内部(通过 canonicalize 解析符号链接)
        if let Some(base) = base_dir {
            let canonical_base = std::fs::canonicalize(base).map_err(|e| {
                AeroSyncError::InvalidConfig(format!("Cannot canonicalize base_dir: {}", e))
            })?;
            let canonical_path = std::fs::canonicalize(path).map_err(|e| {
                AeroSyncError::InvalidConfig(format!("Cannot canonicalize path: {}", e))
            })?;
            if !canonical_path.starts_with(&canonical_base) {
                return Err(AeroSyncError::InvalidConfig(format!(
                    "Path '{}' is outside the allowed base directory '{}'",
                    canonical_path.display(),
                    canonical_base.display()
                )));
            }
        }

        Ok(())
    }

    /// 计算文件的 SHA-256 哈希(十六进制字符串),流式分块读取,支持大文件
    pub async fn compute_sha256<P: AsRef<Path>>(path: P) -> Result<String> {
        const BUF_SIZE: usize = 1024 * 1024; // 1 MB chunks
        let mut file = tokio::fs::File::open(path).await?;
        let mut hasher = Sha256::new();
        let mut buf = vec![0u8; BUF_SIZE];
        loop {
            let n = file.read(&mut buf).await?;
            if n == 0 {
                break;
            }
            hasher.update(&buf[..n]);
        }
        Ok(hex::encode(hasher.finalize()))
    }

    /// 校验文件 SHA-256 与期望值是否匹配
    pub async fn verify_sha256<P: AsRef<Path>>(path: P, expected: &str) -> Result<bool> {
        let actual = Self::compute_sha256(path).await?;
        Ok(actual == expected)
    }
}

// ── 平台相关辅助函数 ─────────────────────────────────────────────────────────

/// 检查路径对当前进程是否可读
fn is_readable(path: &Path) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::metadata(path)
            .map(|m| m.permissions().mode() & 0o444 != 0)
            .unwrap_or(false)
    }
    #[cfg(windows)]
    {
        // Windows: 能获取到 metadata 即视为可读
        std::fs::metadata(path).is_ok()
    }
    #[cfg(not(any(unix, windows)))]
    {
        std::fs::metadata(path).is_ok()
    }
}

/// 检查路径对当前进程是否可执行
fn is_executable(path: &Path) -> bool {
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::metadata(path)
            .map(|m| m.permissions().mode() & 0o111 != 0)
            .unwrap_or(false)
    }
    #[cfg(windows)]
    {
        // Windows: 以扩展名判断是否可执行
        path.extension()
            .and_then(|e| e.to_str())
            .map(|e| {
                matches!(
                    e.to_ascii_lowercase().as_str(),
                    "exe" | "bat" | "cmd" | "com"
                )
            })
            .unwrap_or(false)
    }
    #[cfg(not(any(unix, windows)))]
    {
        false
    }
}

/// 返回指定路径所在文件系统的可用空间(字节)
fn available_space(path: &Path) -> Result<u64> {
    #[cfg(unix)]
    {
        use std::ffi::CString;
        use std::os::unix::ffi::OsStrExt;

        // 路径需以 NUL 结尾传给 statvfs
        let cpath = CString::new(path.as_os_str().as_bytes())
            .map_err(|_| AeroSyncError::InvalidConfig("Path contains null byte".to_string()))?;

        let mut stat: libc::statvfs = unsafe { std::mem::zeroed() };
        let ret = unsafe { libc::statvfs(cpath.as_ptr(), &mut stat) };
        if ret != 0 {
            return Err(AeroSyncError::FileIo(std::io::Error::last_os_error()));
        }
        #[allow(clippy::unnecessary_cast)]
        Ok(stat.f_bavail as u64 * stat.f_frsize as u64)
    }
    #[cfg(not(unix))]
    {
        // Windows / wasm / other: best-effort fallback. Returning u64::MAX
        // means "assume plenty of space"—safer than a panic for the only
        // current caller (FileManager::get_available_space, used in
        // optional preflight). When a Windows user actually needs accurate
        // free-space reporting we can wire up `windows-sys` and call
        // `GetDiskFreeSpaceExW`; that is intentionally not a v0.1.0 dep.
        let _ = path;
        Ok(u64::MAX)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    // ── helpers ───────────────────────────────────────────────────────────────

    async fn write_temp_file(dir: &TempDir, name: &str, content: &[u8]) -> PathBuf {
        let path = dir.path().join(name);
        tokio::fs::write(&path, content).await.unwrap();
        path
    }

    // ── SHA-256 ───────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_compute_sha256_known_value() {
        let dir = TempDir::new().unwrap();
        // SHA-256 of empty string is known
        let path = write_temp_file(&dir, "empty.bin", b"").await;
        let hash = FileManager::compute_sha256(&path).await.unwrap();
        assert_eq!(
            hash,
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
        );
    }

    #[tokio::test]
    async fn test_compute_sha256_deterministic() {
        let dir = TempDir::new().unwrap();
        let content = b"hello aerosync";
        let path = write_temp_file(&dir, "data.bin", content).await;
        let hash1 = FileManager::compute_sha256(&path).await.unwrap();
        let hash2 = FileManager::compute_sha256(&path).await.unwrap();
        assert_eq!(hash1, hash2);
    }

    #[tokio::test]
    async fn test_compute_sha256_different_content() {
        let dir = TempDir::new().unwrap();
        let path1 = write_temp_file(&dir, "a.bin", b"content_a").await;
        let path2 = write_temp_file(&dir, "b.bin", b"content_b").await;
        let hash1 = FileManager::compute_sha256(&path1).await.unwrap();
        let hash2 = FileManager::compute_sha256(&path2).await.unwrap();
        assert_ne!(hash1, hash2);
    }

    #[tokio::test]
    async fn test_verify_sha256_match() {
        let dir = TempDir::new().unwrap();
        let path = write_temp_file(&dir, "c.bin", b"verify me").await;
        let hash = FileManager::compute_sha256(&path).await.unwrap();
        let ok = FileManager::verify_sha256(&path, &hash).await.unwrap();
        assert!(ok);
    }

    #[tokio::test]
    async fn test_verify_sha256_mismatch() {
        let dir = TempDir::new().unwrap();
        let path = write_temp_file(&dir, "d.bin", b"original").await;
        let ok = FileManager::verify_sha256(
            &path,
            "0000000000000000000000000000000000000000000000000000000000000000",
        )
        .await
        .unwrap();
        assert!(!ok);
    }

    #[tokio::test]
    async fn test_compute_sha256_nonexistent_file_errors() {
        let result = FileManager::compute_sha256("/nonexistent/path/file.bin").await;
        assert!(result.is_err());
    }

    // ── file_info ─────────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_get_file_info_regular_file() {
        let dir = TempDir::new().unwrap();
        let content = b"hello world";
        let path = write_temp_file(&dir, "info.txt", content).await;
        let info = FileManager::get_file_info(&path).await.unwrap();
        assert_eq!(info.name, "info.txt");
        assert_eq!(info.size, content.len() as u64);
        assert!(!info.is_directory);
    }

    #[tokio::test]
    async fn test_get_file_info_directory() {
        let dir = TempDir::new().unwrap();
        let sub = dir.path().join("subdir");
        tokio::fs::create_dir(&sub).await.unwrap();
        let info = FileManager::get_file_info(&sub).await.unwrap();
        assert!(info.is_directory);
    }

    #[tokio::test]
    async fn test_get_file_info_nonexistent_errors() {
        let result = FileManager::get_file_info("/no/such/file.txt").await;
        assert!(result.is_err());
    }

    // ── list_directory ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_list_directory_returns_files() {
        let dir = TempDir::new().unwrap();
        write_temp_file(&dir, "z.bin", b"z").await;
        write_temp_file(&dir, "a.bin", b"a").await;

        let entries = FileManager::list_directory(dir.path()).await.unwrap();
        assert_eq!(entries.len(), 2);
        // Should be sorted alphabetically
        assert_eq!(entries[0].name, "a.bin");
        assert_eq!(entries[1].name, "z.bin");
    }

    #[tokio::test]
    async fn test_list_directory_dirs_before_files() {
        let dir = TempDir::new().unwrap();
        write_temp_file(&dir, "file.bin", b"data").await;
        tokio::fs::create_dir(dir.path().join("subdir"))
            .await
            .unwrap();

        let entries = FileManager::list_directory(dir.path()).await.unwrap();
        assert_eq!(entries.len(), 2);
        assert!(entries[0].is_directory, "first entry should be a directory");
        assert!(!entries[1].is_directory, "second entry should be a file");
    }

    #[tokio::test]
    async fn test_list_empty_directory() {
        let dir = TempDir::new().unwrap();
        let entries = FileManager::list_directory(dir.path()).await.unwrap();
        assert!(entries.is_empty());
    }

    // ── create_directory ──────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_create_directory_nested() {
        let dir = TempDir::new().unwrap();
        let nested = dir.path().join("a").join("b").join("c");
        FileManager::create_directory(&nested).await.unwrap();
        assert!(nested.exists());
    }

    #[tokio::test]
    async fn test_create_directory_idempotent() {
        let dir = TempDir::new().unwrap();
        let sub = dir.path().join("existing");
        FileManager::create_directory(&sub).await.unwrap();
        // Second call should not fail
        FileManager::create_directory(&sub).await.unwrap();
        assert!(sub.exists());
    }

    // ── file_exists ───────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_file_exists_true() {
        let dir = TempDir::new().unwrap();
        let path = write_temp_file(&dir, "exists.bin", b"hi").await;
        assert!(FileManager::file_exists(&path).await);
    }

    #[tokio::test]
    async fn test_file_exists_false() {
        assert!(!FileManager::file_exists("/no/such/path.bin").await);
    }

    // ── validate_path ─────────────────────────────────────────────────────────

    #[test]
    fn test_validate_path_valid() {
        assert!(FileManager::validate_path("/some/valid/path.txt").is_ok());
    }

    #[test]
    fn test_validate_path_with_null_byte_errors() {
        let bad_path = "/some/path\0/file";
        assert!(FileManager::validate_path(bad_path).is_err());
    }

    // ── large file sha256 ─────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_sha256_large_file() {
        let dir = TempDir::new().unwrap();
        // 1MB of zeros
        let content = vec![0u8; 1024 * 1024];
        let path = write_temp_file(&dir, "large.bin", &content).await;
        let hash = FileManager::compute_sha256(&path).await.unwrap();
        assert_eq!(hash.len(), 64); // hex-encoded 32 bytes
                                    // Verify consistency
        let hash2 = FileManager::compute_sha256(&path).await.unwrap();
        assert_eq!(hash, hash2);
    }
}