synwire-agent 0.1.0

Agent runtime implementations for synwire
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
//! Archive backend for tar/gzip/zip operations.

use std::io::{Read, Write};
use std::path::Path;

use serde::{Deserialize, Serialize};
use synwire_core::BoxFuture;
use synwire_core::vfs::error::VfsError;
use synwire_core::vfs::types::ArchiveInfo;

/// Conflict resolution policy for extraction.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ConflictPolicy {
    /// Skip existing files.
    Skip,
    /// Overwrite existing files.
    Overwrite,
    /// Fail with an error.
    Fail,
}

/// Archive format.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[non_exhaustive]
pub enum ArchiveFormat {
    /// Tar with gzip compression.
    TarGz,
    /// Tar with bzip2 compression.
    TarBz2,
    /// Tar without compression.
    Tar,
    /// Zip archive.
    Zip,
}

impl ArchiveFormat {
    /// Detect format from file extension.
    pub fn from_path(path: &str) -> Option<Self> {
        let p = std::path::Path::new(path);
        let ext = p
            .extension()
            .and_then(|e| e.to_str())
            .map(str::to_ascii_lowercase);
        let ext = ext.as_deref().unwrap_or("");
        if path.ends_with(".tar.gz") || ext == "tgz" {
            Some(Self::TarGz)
        } else if path.ends_with(".tar.bz2") || ext == "tbz2" {
            Some(Self::TarBz2)
        } else if ext == "tar" {
            Some(Self::Tar)
        } else if ext == "zip" {
            Some(Self::Zip)
        } else {
            None
        }
    }
}

/// Archive backend for creating, extracting, and listing archives.
#[derive(Debug, Default, Clone)]
pub struct ArchiveManager;

impl ArchiveManager {
    /// Create a new archive backend.
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    /// Create an archive from a directory.
    pub fn create_archive<'a>(
        &'a self,
        source_dir: &'a str,
        output_path: &'a str,
        format: ArchiveFormat,
    ) -> BoxFuture<'a, Result<ArchiveInfo, VfsError>> {
        Box::pin(async move {
            let source = Path::new(source_dir);
            if !source.exists() {
                return Err(VfsError::NotFound(source_dir.to_string()));
            }

            match format {
                ArchiveFormat::TarGz => create_tar_gz(source, output_path).await,
                ArchiveFormat::Tar => create_tar(source, output_path).await,
                ArchiveFormat::Zip => create_zip(source, output_path).await,
                ArchiveFormat::TarBz2 => create_tar_bz2(source, output_path).await,
            }
        })
    }

    /// Extract an archive to a directory.
    pub fn extract_archive<'a>(
        &'a self,
        archive_path: &'a str,
        dest_dir: &'a str,
        policy: ConflictPolicy,
    ) -> BoxFuture<'a, Result<(), VfsError>> {
        Box::pin(async move {
            let format = ArchiveFormat::from_path(archive_path).ok_or_else(|| {
                VfsError::Unsupported(format!("unknown archive format: {archive_path}"))
            })?;
            let dest = Path::new(dest_dir);
            tokio::fs::create_dir_all(dest)
                .await
                .map_err(VfsError::Io)?;

            match format {
                ArchiveFormat::TarGz | ArchiveFormat::Tar | ArchiveFormat::TarBz2 => {
                    extract_tar(archive_path, dest, format, policy).await
                }
                ArchiveFormat::Zip => extract_zip(archive_path, dest, policy).await,
            }
        })
    }

    /// List archive contents.
    pub fn list_contents<'a>(
        &'a self,
        archive_path: &'a str,
    ) -> BoxFuture<'a, Result<ArchiveInfo, VfsError>> {
        Box::pin(async move {
            let format = ArchiveFormat::from_path(archive_path).ok_or_else(|| {
                VfsError::Unsupported(format!("unknown archive format: {archive_path}"))
            })?;
            match format {
                ArchiveFormat::TarGz | ArchiveFormat::Tar | ArchiveFormat::TarBz2 => {
                    list_tar(archive_path, format).await
                }
                ArchiveFormat::Zip => list_zip(archive_path).await,
            }
        })
    }
}

// ── tar helpers ─────────────────────────────────────────────────────────────

async fn create_tar_gz(source: &Path, output_path: &str) -> Result<ArchiveInfo, VfsError> {
    let output = output_path.to_string();
    let source = source.to_path_buf();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::create(&output).map_err(VfsError::Io)?;
        let encoder = flate2::write::GzEncoder::new(file, flate2::Compression::default());
        let mut tar = tar::Builder::new(encoder);
        tar.append_dir_all(".", &source).map_err(VfsError::Io)?;
        tar.finish().map_err(VfsError::Io)?;
        let compressed_size = std::fs::metadata(&output).map(|m| m.len()).unwrap_or(0);
        Ok(ArchiveInfo {
            entries: Vec::new(),
            format: "tar.gz".to_string(),
            compressed_size,
        })
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

async fn create_tar_bz2(source: &Path, output_path: &str) -> Result<ArchiveInfo, VfsError> {
    let output = output_path.to_string();
    let source = source.to_path_buf();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::create(&output).map_err(VfsError::Io)?;
        let encoder = bzip2::write::BzEncoder::new(file, bzip2::Compression::default());
        let mut tar = tar::Builder::new(encoder);
        tar.append_dir_all(".", &source).map_err(VfsError::Io)?;
        let encoder = tar.into_inner().map_err(VfsError::Io)?;
        let _ = encoder.finish().map_err(VfsError::Io)?;
        let compressed_size = std::fs::metadata(&output).map(|m| m.len()).unwrap_or(0);
        Ok(ArchiveInfo {
            entries: Vec::new(),
            format: "tar.bz2".to_string(),
            compressed_size,
        })
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

async fn create_tar(source: &Path, output_path: &str) -> Result<ArchiveInfo, VfsError> {
    let output = output_path.to_string();
    let source = source.to_path_buf();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::create(&output).map_err(VfsError::Io)?;
        let mut tar = tar::Builder::new(file);
        tar.append_dir_all(".", &source).map_err(VfsError::Io)?;
        tar.finish().map_err(VfsError::Io)?;
        let compressed_size = std::fs::metadata(&output).map(|m| m.len()).unwrap_or(0);
        Ok(ArchiveInfo {
            entries: Vec::new(),
            format: "tar".to_string(),
            compressed_size,
        })
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

async fn extract_tar(
    archive_path: &str,
    dest: &Path,
    format: ArchiveFormat,
    _policy: ConflictPolicy,
) -> Result<(), VfsError> {
    let archive = archive_path.to_string();
    let dest = dest.to_path_buf();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::open(&archive).map_err(VfsError::Io)?;
        match format {
            ArchiveFormat::TarGz => {
                let decoder = flate2::read::GzDecoder::new(file);
                let mut tar = tar::Archive::new(decoder);
                tar.unpack(&dest).map_err(VfsError::Io)?;
            }
            ArchiveFormat::TarBz2 => {
                let decoder = bzip2::read::BzDecoder::new(file);
                let mut tar = tar::Archive::new(decoder);
                tar.unpack(&dest).map_err(VfsError::Io)?;
            }
            ArchiveFormat::Tar => {
                let mut tar = tar::Archive::new(file);
                tar.unpack(&dest).map_err(VfsError::Io)?;
            }
            ArchiveFormat::Zip => {
                return Err(VfsError::Unsupported(
                    "zip extraction handled separately".into(),
                ));
            }
        }
        Ok(())
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

async fn list_tar(archive_path: &str, format: ArchiveFormat) -> Result<ArchiveInfo, VfsError> {
    let archive = archive_path.to_string();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::open(&archive).map_err(VfsError::Io)?;
        let mut entries_out = Vec::new();
        match format {
            ArchiveFormat::TarGz => {
                let decoder = flate2::read::GzDecoder::new(file);
                let mut tar = tar::Archive::new(decoder);
                for e in tar.entries().map_err(VfsError::Io)? {
                    let e = e.map_err(VfsError::Io)?;
                    entries_out.push(synwire_core::vfs::types::ArchiveEntry {
                        path: e.path().map_err(VfsError::Io)?.display().to_string(),
                        is_dir: e.header().entry_type().is_dir(),
                        size: e.header().size().unwrap_or(0),
                    });
                }
            }
            ArchiveFormat::TarBz2 => {
                let decoder = bzip2::read::BzDecoder::new(file);
                let mut tar = tar::Archive::new(decoder);
                for e in tar.entries().map_err(VfsError::Io)? {
                    let e = e.map_err(VfsError::Io)?;
                    entries_out.push(synwire_core::vfs::types::ArchiveEntry {
                        path: e.path().map_err(VfsError::Io)?.display().to_string(),
                        is_dir: e.header().entry_type().is_dir(),
                        size: e.header().size().unwrap_or(0),
                    });
                }
            }
            ArchiveFormat::Tar => {
                let mut tar = tar::Archive::new(file);
                for e in tar.entries().map_err(VfsError::Io)? {
                    let e = e.map_err(VfsError::Io)?;
                    entries_out.push(synwire_core::vfs::types::ArchiveEntry {
                        path: e.path().map_err(VfsError::Io)?.display().to_string(),
                        is_dir: e.header().entry_type().is_dir(),
                        size: e.header().size().unwrap_or(0),
                    });
                }
            }
            ArchiveFormat::Zip => {
                return Err(VfsError::Unsupported(
                    "zip listing handled separately".into(),
                ));
            }
        }
        let compressed_size = std::fs::metadata(&archive).map(|m| m.len()).unwrap_or(0);
        Ok(ArchiveInfo {
            entries: entries_out,
            format: "tar".to_string(),
            compressed_size,
        })
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

// ── zip helpers ──────────────────────────────────────────────────────────────

async fn create_zip(source: &Path, output_path: &str) -> Result<ArchiveInfo, VfsError> {
    let output = output_path.to_string();
    let source = source.to_path_buf();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::create(&output).map_err(VfsError::Io)?;
        let mut zip = zip::ZipWriter::new(file);
        let opts: zip::write::FileOptions<'_, ()> = zip::write::FileOptions::default();
        write_dir_to_zip(&mut zip, &source, &source, opts)?;
        let _ = zip
            .finish()
            .map_err(|e| VfsError::Unsupported(e.to_string()))?;
        let compressed_size = std::fs::metadata(&output).map(|m| m.len()).unwrap_or(0);
        Ok(ArchiveInfo {
            entries: Vec::new(),
            format: "zip".to_string(),
            compressed_size,
        })
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

fn write_dir_to_zip(
    zip: &mut zip::ZipWriter<std::fs::File>,
    base: &Path,
    dir: &Path,
    opts: zip::write::FileOptions<'_, ()>,
) -> Result<(), VfsError> {
    for entry in std::fs::read_dir(dir).map_err(VfsError::Io)? {
        let entry = entry.map_err(VfsError::Io)?;
        let path = entry.path();
        let name = path
            .strip_prefix(base)
            .map_err(|e| VfsError::Unsupported(e.to_string()))?
            .display()
            .to_string();
        if path.is_dir() {
            zip.add_directory(&name, opts)
                .map_err(|e| VfsError::Unsupported(e.to_string()))?;
            write_dir_to_zip(zip, base, &path, opts)?;
        } else {
            zip.start_file(&name, opts)
                .map_err(|e| VfsError::Unsupported(e.to_string()))?;
            let mut f = std::fs::File::open(&path).map_err(VfsError::Io)?;
            let mut buf = Vec::new();
            let _ = f.read_to_end(&mut buf).map_err(VfsError::Io)?;
            zip.write_all(&buf)
                .map_err(|e| VfsError::Unsupported(e.to_string()))?;
        }
    }
    Ok(())
}

async fn extract_zip(
    archive_path: &str,
    dest: &Path,
    policy: ConflictPolicy,
) -> Result<(), VfsError> {
    let archive = archive_path.to_string();
    let dest = dest.to_path_buf();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::open(&archive).map_err(VfsError::Io)?;
        let mut zip =
            zip::ZipArchive::new(file).map_err(|e| VfsError::Unsupported(e.to_string()))?;
        for i in 0..zip.len() {
            let mut entry = zip
                .by_index(i)
                .map_err(|e| VfsError::Unsupported(e.to_string()))?;
            let out_path = dest.join(
                entry
                    .enclosed_name()
                    .ok_or_else(|| VfsError::Unsupported("circular symlink".into()))?,
            );
            if entry.is_dir() {
                std::fs::create_dir_all(&out_path).map_err(VfsError::Io)?;
            } else {
                if out_path.exists() {
                    match policy {
                        ConflictPolicy::Skip => continue,
                        ConflictPolicy::Fail => {
                            return Err(VfsError::Unsupported(format!(
                                "conflict: {} already exists",
                                out_path.display()
                            )));
                        }
                        ConflictPolicy::Overwrite => {}
                    }
                }
                if let Some(parent) = out_path.parent() {
                    std::fs::create_dir_all(parent).map_err(VfsError::Io)?;
                }
                let mut out_file = std::fs::File::create(&out_path).map_err(VfsError::Io)?;
                let _ = std::io::copy(&mut entry, &mut out_file).map_err(VfsError::Io)?;
            }
        }
        Ok(())
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

async fn list_zip(archive_path: &str) -> Result<ArchiveInfo, VfsError> {
    let archive = archive_path.to_string();
    tokio::task::spawn_blocking(move || {
        let file = std::fs::File::open(&archive).map_err(VfsError::Io)?;
        let mut zip =
            zip::ZipArchive::new(file).map_err(|e| VfsError::Unsupported(e.to_string()))?;
        let mut entries = Vec::new();
        for i in 0..zip.len() {
            let entry = zip
                .by_index(i)
                .map_err(|e| VfsError::Unsupported(e.to_string()))?;
            entries.push(synwire_core::vfs::types::ArchiveEntry {
                path: entry.name().to_string(),
                is_dir: entry.is_dir(),
                size: entry.size(),
            });
        }
        let compressed_size = std::fs::metadata(&archive).map(|m| m.len()).unwrap_or(0);
        Ok(ArchiveInfo {
            entries,
            format: "zip".to_string(),
            compressed_size,
        })
    })
    .await
    .map_err(|e| VfsError::Unsupported(e.to_string()))?
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_tar_gz_round_trip() {
        let tmp = tempdir();
        let src = tmp.join("src");
        std::fs::create_dir_all(&src).expect("mkdir");
        std::fs::write(src.join("hello.txt"), b"hello").expect("write");

        let archive_path = tmp.join("out.tar.gz").display().to_string();
        let backend = ArchiveManager::new();
        let _ = backend
            .create_archive(
                &src.display().to_string(),
                &archive_path,
                ArchiveFormat::TarGz,
            )
            .await
            .expect("create");

        let dst = tmp.join("dst");
        std::fs::create_dir_all(&dst).expect("mkdir dst");
        backend
            .extract_archive(
                &archive_path,
                &dst.display().to_string(),
                ConflictPolicy::Overwrite,
            )
            .await
            .expect("extract");

        assert!(dst.join("hello.txt").exists());
    }

    #[tokio::test]
    async fn test_tar_bz2_round_trip() {
        let tmp = tempdir();
        let src = tmp.join("src");
        std::fs::create_dir_all(&src).expect("mkdir");
        std::fs::write(src.join("hello.txt"), b"hello bz2").expect("write");

        let archive_path = tmp.join("out.tar.bz2").display().to_string();
        let backend = ArchiveManager::new();
        let info = backend
            .create_archive(
                &src.display().to_string(),
                &archive_path,
                ArchiveFormat::TarBz2,
            )
            .await
            .expect("create");
        assert_eq!(info.format, "tar.bz2");
        assert!(info.compressed_size > 0);

        let listing = backend.list_contents(&archive_path).await.expect("list");
        assert!(!listing.entries.is_empty());

        let dst = tmp.join("dst");
        std::fs::create_dir_all(&dst).expect("mkdir dst");
        backend
            .extract_archive(
                &archive_path,
                &dst.display().to_string(),
                ConflictPolicy::Overwrite,
            )
            .await
            .expect("extract");

        assert!(dst.join("hello.txt").exists());
        assert_eq!(
            std::fs::read_to_string(dst.join("hello.txt")).expect("read"),
            "hello bz2"
        );
    }

    #[tokio::test]
    async fn test_zip_round_trip() {
        let tmp = tempdir();
        let src = tmp.join("src");
        std::fs::create_dir_all(&src).expect("mkdir");
        std::fs::write(src.join("data.txt"), b"data").expect("write");

        let archive_path = tmp.join("out.zip").display().to_string();
        let backend = ArchiveManager::new();
        let _ = backend
            .create_archive(
                &src.display().to_string(),
                &archive_path,
                ArchiveFormat::Zip,
            )
            .await
            .expect("create");

        let dst = tmp.join("dst");
        std::fs::create_dir_all(&dst).expect("mkdir dst");
        backend
            .extract_archive(
                &archive_path,
                &dst.display().to_string(),
                ConflictPolicy::Overwrite,
            )
            .await
            .expect("extract");

        assert!(dst.join("data.txt").exists());
    }

    #[tokio::test]
    async fn test_conflict_policy_fail() {
        let tmp = tempdir();
        let src = tmp.join("src");
        std::fs::create_dir_all(&src).expect("mkdir");
        std::fs::write(src.join("f.txt"), b"original").expect("write");

        let archive_path = tmp.join("out.zip").display().to_string();
        let backend = ArchiveManager::new();
        let _ = backend
            .create_archive(
                &src.display().to_string(),
                &archive_path,
                ArchiveFormat::Zip,
            )
            .await
            .expect("create");

        let dst = tmp.join("dst");
        std::fs::create_dir_all(&dst).expect("mkdir");
        std::fs::write(dst.join("f.txt"), b"existing").expect("prewrite");

        let err = backend
            .extract_archive(
                &archive_path,
                &dst.display().to_string(),
                ConflictPolicy::Fail,
            )
            .await;
        assert!(err.is_err());
    }

    fn tempdir() -> std::path::PathBuf {
        let path = std::env::temp_dir().join(format!("synwire-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&path).expect("tempdir");
        path
    }
}