lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
//! Basic LCPFS pool operations
//!
//! This example demonstrates:
//! - Creating a pool
//! - Creating and writing files
//! - Reading files
//! - Directory operations
//! - Proper cleanup
//!
//! Run with: cargo run --example basic_pool

use lcpfs::{FsResult, Pool};

fn main() -> FsResult<()> {
    println!("=== LCPFS Basic Pool Example ===\n");

    // Step 1: Create a pool
    println!("1. Creating pool 'mypool' on device 0...");
    let mut pool = Pool::create_pool(0, "mypool")?;
    println!("   ✓ Pool created successfully\n");

    // Step 2: Create a file
    println!("2. Creating file /hello.txt...");
    let fd = pool.create("/hello.txt", 0o644)?;
    println!("   ✓ File created with fd={}\n", fd);

    // Step 3: Write data
    println!("3. Writing data to file...");
    let data = b"Hello, LCPFS! This is a test file.";
    let written = pool.write(fd, data)?;
    println!("   ✓ Wrote {} bytes\n", written);

    // Step 4: Close the file
    println!("4. Closing file...");
    pool.close(fd)?;
    println!("   ✓ File closed\n");

    // Step 5: Reopen and read
    println!("5. Reopening file for reading...");
    let fd = pool.open("/hello.txt", 0)?;
    println!("   ✓ File opened with fd={}\n", fd);

    println!("6. Reading data...");
    let mut buffer = vec![0u8; 1024];
    let read = pool.read(fd, &mut buffer)?;
    println!("   ✓ Read {} bytes", read);
    println!(
        "   Content: {:?}\n",
        String::from_utf8_lossy(&buffer[..read])
    );

    pool.close(fd)?;

    // Step 7: Create a directory
    println!("7. Creating directory /documents...");
    pool.mkdir("/documents", 0o755)?;
    println!("   ✓ Directory created\n");

    // Step 8: Create file in directory
    println!("8. Creating /documents/notes.txt...");
    let fd = pool.create("/documents/notes.txt", 0o644)?;
    pool.write(fd, b"Important notes go here")?;
    pool.close(fd)?;
    println!("   ✓ File created in directory\n");

    // Step 9: List directory contents
    println!("9. Listing root directory...");
    let entries = pool.readdir("/")?;
    println!("   Files in /:");
    for entry in entries {
        println!("     - {}", entry.name);
    }

    println!("\n=== Example completed successfully! ===");
    Ok(())
}