lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
//! Advanced LCPFS features
//!
//! This example demonstrates:
//! - Properties (get/set)
//! - Multiple files and directories
//! - Incremental replication
//!
//! Run with: cargo run --example advanced

use lcpfs::{FsResult, Pool, PropertyValue};

fn main() -> FsResult<()> {
    println!("=== LCPFS Advanced Features ===\n");

    let mut pool = Pool::create_pool(0, "mypool")?;

    // =========================================================================
    // PART 1: Properties
    // =========================================================================
    println!("PART 1: Properties");
    println!("==================\n");

    println!("1. Setting pool properties...");
    pool.set_property("compression", PropertyValue::String("lz4".into()))?;
    println!("   ✓ Set compression property to lz4\n");

    println!("2. Getting pool properties...");
    let compression = pool.get_property("compression")?;
    println!("   compression: {:?}", compression);

    println!("3. Listing all properties...");
    let props = pool.list_properties();
    println!("   Pool properties:");
    for (name, value, source) in &props {
        println!("     - {} = {:?} (source: {:?})", name, value, source);
    }
    println!();

    // =========================================================================
    // PART 2: File Operations
    // =========================================================================
    println!("PART 2: File Operations");
    println!("========================\n");

    println!("4. Creating directory structure...");
    pool.mkdir("/home", 0o755)?;
    pool.mkdir("/home/user1", 0o755)?;
    pool.mkdir("/var", 0o755)?;
    pool.mkdir("/var/log", 0o755)?;
    println!("   ✓ Created /home, /home/user1, /var, /var/log\n");

    println!("5. Creating files...");
    for i in 0..10 {
        let path = format!("/home/user1/file_{}.txt", i);
        let fd = pool.create(&path, 0o644)?;
        pool.write(fd, format!("Content of file {}", i).as_bytes())?;
        pool.close(fd)?;
    }
    println!("   ✓ Created 10 files in /home/user1\n");

    println!("6. Listing /home/user1...");
    let entries = pool.readdir("/home/user1")?;
    println!("   Files:");
    for entry in &entries {
        println!("     - {}", entry.name);
    }
    println!();

    // =========================================================================
    // PART 3: Snapshots and Replication
    // =========================================================================
    println!("PART 3: Snapshots and Replication");
    println!("===================================\n");

    println!("7. Creating baseline snapshot...");
    pool.snapshot("baseline")?;
    println!("   ✓ Snapshot 'baseline' created\n");

    println!("8. Making changes...");
    let fd = pool.create("/var/log/system.log", 0o644)?;
    pool.write(fd, b"System started\nServices initialized\n")?;
    pool.close(fd)?;
    pool.snapshot("after_logs")?;
    println!("   ✓ Added log file and created 'after_logs' snapshot\n");

    println!("9. Generating full replication stream...");
    let full_stream = pool.send()?;
    println!("   ✓ Full stream: {} bytes\n", full_stream.len());

    println!("10. Generating incremental stream (baseline → after_logs)...");
    let incr_stream = pool.send_snapshot_incremental("baseline", Some("after_logs"))?;
    println!("    ✓ Incremental stream: {} bytes", incr_stream.len());
    println!(
        "    Savings: {:.1}% smaller than full stream\n",
        (1.0 - (incr_stream.len() as f64 / full_stream.len() as f64)) * 100.0
    );

    // =========================================================================
    // PART 4: File Statistics
    // =========================================================================
    println!("PART 4: File Statistics");
    println!("========================\n");

    println!("11. Getting file stats...");
    let stat = pool.stat("/home/user1/file_0.txt")?;
    println!("    /home/user1/file_0.txt:");
    println!("      size:  {} bytes", stat.st_size);
    println!("      mode:  0o{:o}", stat.st_mode);
    println!("      uid:   {}", stat.st_uid);
    println!("      gid:   {}", stat.st_gid);
    println!();

    // =========================================================================
    // Summary
    // =========================================================================
    println!("=== Example completed successfully! ===");
    println!("\nKey takeaways:");
    println!("  • Properties: Per-pool configuration (compression, etc.)");
    println!("  • Directories: Hierarchical namespace organization");
    println!("  • Snapshots: Point-in-time copies");
    println!("  • Replication: Full and incremental send streams");
    println!("  • Stats: POSIX-compatible file metadata");

    Ok(())
}