# apt-ostree APT/DEB Package Management

## Overview

apt-ostree uses APT (Advanced Package Tool) and DEB packages for package management, providing a sophisticated integration between traditional Debian/Ubuntu package management and OSTree's atomic deployment model. This document explains how apt-ostree implements APT/DEB package management.

## Core APT Integration

### libapt-pkg Integration

apt-ostree uses libapt-pkg for advanced package management capabilities:

```rust
// libapt-pkg integration in src/apt.rs
use std::ffi::{CString, CStr};
use std::os::raw::c_char;

#[link(name = "apt-pkg")]
extern "C" {
    fn pkgInitConfig() -> *mut std::ffi::c_void;
    fn pkgInitSystem() -> *mut std::ffi::c_void;
    fn pkgCacheFile::Open() -> *mut pkgCacheFile;
    fn pkgCacheFile::GetPkgCache() -> *mut pkgCache;
    fn pkgCache::FindPkg(name: *const c_char) -> *mut pkgCache::PkgIterator;
}

pub struct AptManager {
    cache_file: *mut pkgCacheFile,
    cache: *mut pkgCache,
}

impl AptManager {
    // Initialize APT context for OSTree operations
    pub fn initialize_apt_context(
        &mut self,
        deployment_path: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Initialize APT configuration
        unsafe {
            pkgInitConfig();
            pkgInitSystem();
        }
        
        // Open cache file
        self.cache_file = unsafe { pkgCacheFile::Open() };
        if self.cache_file.is_null() {
            return Err("Failed to open APT cache file".into());
        }
        
        // Get package cache
        self.cache = unsafe { pkgCacheFile::GetPkgCache(self.cache_file) };
        if self.cache.is_null() {
            return Err("Failed to get APT package cache".into());
        }
        
        // Configure for OSTree deployment
        self.configure_for_ostree(deployment_path)?;
        
        Ok(())
    }
    
    // Resolve package dependencies
    pub fn resolve_package_dependencies(
        &self,
        package_name: &str,
    ) -> Result<Vec<String>, Box<dyn std::error::Error>> {
        let c_package_name = CString::new(package_name)?;
        
        unsafe {
            let pkg_iter = pkgCache::FindPkg(self.cache, c_package_name.as_ptr());
            if pkg_iter.is_null() {
                return Err(format!("Package {} not found", package_name).into());
            }
            
            // Resolve dependencies using APT's native resolver
            let mut resolved_packages = Vec::new();
            // ... dependency resolution logic ...
            
            Ok(resolved_packages)
        }
    }
}
```

### DEB Package Processing

apt-ostree processes DEB packages for OSTree integration:

```rust
// DEB processing in src/apt.rs
use std::process::Command;
use std::fs;

pub struct DebProcessor;

impl DebProcessor {
    // Extract DEB package to filesystem
    pub fn extract_deb_package(
        deb_path: &str,
        extract_path: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Create extraction directory
        fs::create_dir_all(extract_path)?;
        
        // Extract DEB package using dpkg-deb
        let output = Command::new("dpkg-deb")
            .args(&["-R", deb_path, extract_path])
            .output()?;
        
        if !output.status.success() {
            return Err(format!(
                "Failed to extract DEB package: {}",
                String::from_utf8_lossy(&output.stderr)
            ).into());
        }
        
        Ok(())
    }
    
    // Process DEB package scripts
    pub fn process_deb_scripts(
        deb_path: &str,
        deployment_path: &str,
        script_type: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Extract scripts from DEB
        let script_path = format!("{}/var/lib/dpkg/info", deployment_path);
        fs::create_dir_all(&script_path)?;
        
        // Extract control information
        let output = Command::new("dpkg-deb")
            .args(&["-I", deb_path, "control"])
            .output()?;
        
        if output.status.success() {
            // Parse control file and extract script information
            let control_content = String::from_utf8(output.stdout)?;
            Self::parse_control_file(&control_content, &script_path)?;
        }
        
        Ok(())
    }
    
    // Parse DEB control file
    fn parse_control_file(
        control_content: &str,
        script_path: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Parse control file for script information
        for line in control_content.lines() {
            if line.starts_with("Preinst-Script:") || 
               line.starts_with("Postinst-Script:") ||
               line.starts_with("Prerm-Script:") ||
               line.starts_with("Postrm-Script:") {
                // Extract and save script
                let script_content = line.splitn(2, ':').nth(1).unwrap_or("").trim();
                if !script_content.is_empty() {
                    let script_file = format!("{}/{}.{}", 
                        script_path, 
                        line.split(':').next().unwrap_or(""),
                        line.split(':').next().unwrap_or("").to_lowercase()
                    );
                    fs::write(script_file, script_content)?;
                }
            }
        }
        
        Ok(())
    }
}
```

## Package Layering System

### Layer Management

apt-ostree implements sophisticated package layering:

```rust
// Layer management in src/package_manager.rs
pub struct PackageLayerManager;

impl PackageLayerManager {
    // Create package layer
    pub fn create_package_layer(
        base_commit: &str,
        new_commit: &str,
        packages: &[String],
    ) -> Result<String, Box<dyn std::error::Error>> {
        // 1. Extract base filesystem from OSTree commit
        let ostree_manager = OstreeManager::new()?;
        let base_tree = ostree_manager.read_commit(base_commit)?;
        
        // 2. Create temporary directory for layer
        let layer_path = tempfile::tempdir()?.path().to_path_buf();
        
        // 3. Apply DEB packages to layer
        for package in packages {
            Self::download_and_extract_package(package, &layer_path)?;
            Self::process_package_scripts(package, &layer_path)?;
        }
        
        // 4. Merge layer with base tree
        let layered_tree = Self::merge_layer_with_base(&base_tree, &layer_path)?;
        
        // 5. Create new OSTree commit
        let new_commit_checksum = ostree_manager.create_commit(
            &layered_tree,
            "Package layer update",
            None,
            None,
        )?;
        
        // 6. Update ref to point to new commit
        ostree_manager.set_ref(None, "ubuntu/24.04/x86_64/desktop", &new_commit_checksum)?;
        
        Ok(new_commit_checksum)
    }
    
    // Remove package layer
    pub fn remove_package_layer(
        base_commit: &str,
        new_commit: &str,
        packages: &[String],
    ) -> Result<String, Box<dyn std::error::Error>> {
        // Create new deployment without specified packages
        Self::create_deployment_without_packages(base_commit, new_commit, packages)
    }
    
    // Download and extract package
    fn download_and_extract_package(
        package: &str,
        layer_path: &std::path::Path,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Download package using apt
        let output = Command::new("apt")
            .args(&["download", package])
            .current_dir(layer_path)
            .output()?;
        
        if !output.status.success() {
            return Err(format!(
                "Failed to download package {}: {}",
                package,
                String::from_utf8_lossy(&output.stderr)
            ).into());
        }
        
        // Find downloaded DEB file
        let deb_files: Vec<_> = fs::read_dir(layer_path)?
            .filter_map(|entry| entry.ok())
            .filter(|entry| entry.path().extension().map_or(false, |ext| ext == "deb"))
            .collect();
        
        for deb_file in deb_files {
            DebProcessor::extract_deb_package(
                deb_file.path().to_str().unwrap(),
                layer_path.to_str().unwrap(),
            )?;
        }
        
        Ok(())
    }
    
    // Process package scripts
    fn process_package_scripts(
        package: &str,
        layer_path: &std::path::Path,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Process pre-installation scripts
        let preinst_path = layer_path.join("var/lib/dpkg/info").join(format!("{}.preinst", package));
        if preinst_path.exists() {
            BubblewrapManager::execute_package_script(
                preinst_path.to_str().unwrap(),
                layer_path.to_str().unwrap(),
                package,
            )?;
        }
        
        // Process post-installation scripts
        let postinst_path = layer_path.join("var/lib/dpkg/info").join(format!("{}.postinst", package));
        if postinst_path.exists() {
            BubblewrapManager::execute_package_script(
                postinst_path.to_str().unwrap(),
                layer_path.to_str().unwrap(),
                package,
            )?;
        }
        
        Ok(())
    }
    
    // Merge layer with base tree
    fn merge_layer_with_base(
        base_tree: &OstreeRepoFile,
        layer_path: &std::path::Path,
    ) -> Result<OstreeRepoFile, Box<dyn std::error::Error>> {
        // Create merged tree
        let merged_path = tempfile::tempdir()?.path().to_path_buf();
        
        // Copy base tree
        Self::copy_tree(base_tree, &merged_path)?;
        
        // Overlay layer on top
        Self::overlay_layer(&merged_path, layer_path)?;
        
        // Convert back to OSTree format
        let ostree_manager = OstreeManager::new()?;
        ostree_manager.create_tree_from_path(&merged_path)
    }
}
```

## Dependency Resolution

### Advanced Dependency Resolution

apt-ostree uses APT's advanced dependency resolver:

```rust
// Dependency resolution in src/apt.rs
impl AptManager {
    // Resolve complex dependencies
    pub fn resolve_complex_dependencies(
        &self,
        requested_packages: &[String],
    ) -> Result<(Vec<String>, Vec<String>), Box<dyn std::error::Error>> {
        let mut resolved_packages = Vec::new();
        let mut conflicts = Vec::new();
        
        // Use APT's dependency resolver
        for package in requested_packages {
            match self.resolve_package_dependencies(package) {
                Ok(deps) => {
                    resolved_packages.extend(deps);
                }
                Err(e) => {
                    // Check if it's a conflict
                    if e.to_string().contains("conflict") {
                        conflicts.push(package.clone());
                    } else {
                        return Err(e);
                    }
                }
            }
        }
        
        // Remove duplicates
        resolved_packages.sort();
        resolved_packages.dedup();
        
        Ok((resolved_packages, conflicts))
    }
    
    // Check for dependency conflicts
    pub fn check_dependency_conflicts(
        &self,
        packages: &[String],
    ) -> Result<Vec<String>, Box<dyn std::error::Error>> {
        let mut conflicts = Vec::new();
        
        // Use APT's conflict detection
        for package in packages {
            if let Err(e) = self.resolve_package_dependencies(package) {
                if e.to_string().contains("conflict") {
                    conflicts.push(package.clone());
                }
            }
        }
        
        Ok(conflicts)
    }
    
    // Resolve file conflicts
    pub fn resolve_file_conflicts(
        &self,
        packages: &[String],
    ) -> Result<Vec<String>, Box<dyn std::error::Error>> {
        let mut conflict_files = Vec::new();
        
        // Check for file conflicts between packages
        let mut file_owners = std::collections::HashMap::new();
        
        for package in packages {
            // Get package files using dpkg
            let output = Command::new("dpkg")
                .args(&["-L", package])
                .output()?;
            
            if output.status.success() {
                let files = String::from_utf8(output.stdout)?;
                for file in files.lines() {
                    if let Some(existing_owner) = file_owners.get(file) {
                        if existing_owner != package {
                            // File conflict detected
                            conflict_files.push(file.to_string());
                        }
                    } else {
                        file_owners.insert(file.to_string(), package.clone());
                    }
                }
            }
        }
        
        Ok(conflict_files)
    }
}
```

## Package Override System

### Override Management

apt-ostree implements package overrides for customization:

```rust
// Override management in src/package_manager.rs
pub struct OverrideManager;

impl OverrideManager {
    // Add package override
    pub fn add_package_override(
        package_name: &str,
        override_type: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Create override configuration
        let override_config = format!(
            "[override]\n\
             package={}\n\
             type={}\n",
            package_name, override_type
        );
        
        // Write override configuration
        let override_path = format!("/etc/apt-ostree/overrides/{}", package_name);
        fs::create_dir_all(std::path::Path::new(&override_path).parent().unwrap())?;
        fs::write(&override_path, override_config)?;
        
        Ok(())
    }
    
    // Remove package override
    pub fn remove_package_override(package_name: &str) -> Result<(), Box<dyn std::error::Error>> {
        // Remove override configuration
        let override_path = format!("/etc/apt-ostree/overrides/{}", package_name);
        
        if std::path::Path::new(&override_path).exists() {
            fs::remove_file(&override_path)?;
        }
        
        Ok(())
    }
    
    // List package overrides
    pub fn list_package_overrides() -> Result<Vec<String>, Box<dyn std::error::Error>> {
        let mut overrides = Vec::new();
        let override_dir = "/etc/apt-ostree/overrides";
        
        if !std::path::Path::new(override_dir).exists() {
            return Ok(overrides);
        }
        
        for entry in fs::read_dir(override_dir)? {
            let entry = entry?;
            let path = entry.path();
            
            if path.is_file() {
                // Read override configuration
                if let Ok(content) = fs::read_to_string(&path) {
                    if let Some(package) = Self::parse_override_config(&content) {
                        overrides.push(package);
                    }
                }
            }
        }
        
        Ok(overrides)
    }
    
    // Parse override configuration
    fn parse_override_config(content: &str) -> Option<String> {
        for line in content.lines() {
            if line.starts_with("package=") {
                return Some(line.split('=').nth(1)?.trim().to_string());
            }
        }
        None
    }
}
```

## Performance Optimizations

### Package Caching

apt-ostree implements sophisticated package caching:

```rust
// Package caching in src/apt.rs
pub struct PackageCache {
    cache_dir: std::path::PathBuf,
    package_cache: std::collections::HashMap<String, String>,
}

impl PackageCache {
    // Initialize package cache
    pub fn new(cache_directory: &str) -> Result<Self, Box<dyn std::error::Error>> {
        let cache_dir = std::path::PathBuf::from(cache_directory);
        fs::create_dir_all(&cache_dir)?;
        
        let mut cache = PackageCache {
            cache_dir,
            package_cache: std::collections::HashMap::new(),
        };
        
        cache.load_existing_cache()?;
        Ok(cache)
    }
    
    // Cache package
    pub fn cache_package(
        &mut self,
        package_name: &str,
        package_path: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        // Check if package is already cached
        if self.package_cache.contains_key(package_name) {
            return Ok(()); // Already cached
        }
        
        // Copy package to cache
        let cached_path = self.cache_dir.join(package_name);
        fs::copy(package_path, &cached_path)?;
        
        // Add to cache table
        self.package_cache.insert(
            package_name.to_string(),
            cached_path.to_string_lossy().to_string(),
        );
        
        Ok(())
    }
    
    // Get cached package
    pub fn get_cached_package(&self, package_name: &str) -> Option<&String> {
        self.package_cache.get(package_name)
    }
    
    // Clean old cache entries
    pub fn clean_cache(&mut self, max_age_days: u64) -> Result<(), Box<dyn std::error::Error>> {
        let now = std::time::SystemTime::now();
        
        // Remove old cache entries
        self.package_cache.retain(|package_name, package_path| {
            if let Ok(metadata) = fs::metadata(package_path) {
                if let Ok(modified) = metadata.modified() {
                    if let Ok(age) = now.duration_since(modified) {
                        if age.as_secs() > max_age_days * 24 * 60 * 60 {
                            // Remove old cache entry
                            let _ = fs::remove_file(package_path);
                            return false;
                        }
                    }
                }
            }
            true
        });
        
        Ok(())
    }
    
    // Load existing cache
    fn load_existing_cache(&mut self) -> Result<(), Box<dyn std::error::Error>> {
        for entry in fs::read_dir(&self.cache_dir)? {
            let entry = entry?;
            let path = entry.path();
            
            if path.is_file() {
                if let Some(name) = path.file_name() {
                    self.package_cache.insert(
                        name.to_string_lossy().to_string(),
                        path.to_string_lossy().to_string(),
                    );
                }
            }
        }
        
        Ok(())
    }
}
```

## Future Enhancements

### Planned Features

1. **Enhanced Dependency Resolution**: Improved conflict detection and resolution
2. **Package Signing**: GPG signature verification for packages
3. **Delta Updates**: Efficient package updates using deltas
4. **Repository Management**: Advanced repository configuration and management

### Integration Roadmap

- **Phase 1**: Core APT/DEB integration (✅ Complete)
- **Phase 2**: Advanced dependency resolution (✅ Complete)
- **Phase 3**: Package caching and optimization (✅ Complete)
- **Phase 4**: Enhanced security features (🔄 In Progress)
- **Phase 5**: Delta update support (📋 Planned) 