- Core transaction API with add_package, resolve, commit, rollback - Version tracking for upgrades/downgrades - Simple package info with FFI conversion functions - Comprehensive error handling - Basic test suite - Clean, minimal implementation (~326 lines total)
195 lines
4.3 KiB
Markdown
195 lines
4.3 KiB
Markdown
# APT Wrapper
|
|
|
|
A simple DNF-like API wrapper around APT for porting rpm-ostree to apt-ostree.
|
|
|
|
## Purpose
|
|
|
|
This library provides a simple transaction interface that mimics DNF's imperative model, making it easier to adapt rpm-ostree code for Debian/Ubuntu systems.
|
|
|
|
## Features
|
|
|
|
- **Simple transaction interface**: `add_package()`, `resolve()`, `commit()`, `rollback()`
|
|
- **DNF-like API**: Easy to port from rpm-ostree
|
|
- **Version-based rollback**: Track versions and restore previous states
|
|
- **Minimal dependencies**: Only `anyhow` and `thiserror`
|
|
- **~250 lines total**: Focused and maintainable
|
|
|
|
## Quick Start
|
|
|
|
```rust
|
|
use apt_wrapper::{AptTransaction, init};
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Initialize
|
|
init()?;
|
|
|
|
// Create transaction
|
|
let mut tx = AptTransaction::new()?;
|
|
|
|
// Add packages
|
|
tx.add_package("vim")?;
|
|
tx.add_package("git")?;
|
|
|
|
// Resolve dependencies
|
|
tx.resolve()?;
|
|
|
|
// Commit transaction
|
|
tx.commit()?;
|
|
|
|
// If something goes wrong, rollback
|
|
// tx.rollback()?;
|
|
|
|
Ok(())
|
|
}
|
|
```
|
|
|
|
## API
|
|
|
|
### AptTransaction
|
|
|
|
```rust
|
|
pub struct AptTransaction {
|
|
packages: Vec<String>,
|
|
}
|
|
|
|
impl AptTransaction {
|
|
pub fn new() -> Result<Self>; // Create new transaction
|
|
pub fn add_package(&mut self, name: &str) -> Result<()>; // Add package
|
|
pub fn resolve(&self) -> Result<()>; // Resolve dependencies
|
|
pub fn commit(&mut self) -> Result<()>; // Commit transaction
|
|
pub fn rollback(&self) -> Result<()>; // Rollback transaction
|
|
pub fn packages(&self) -> &[String]; // Get package list
|
|
pub fn changed_packages(&self) -> Vec<String>; // Get changed packages
|
|
pub fn is_empty(&self) -> bool; // Check if empty
|
|
}
|
|
```
|
|
|
|
### Utility Functions
|
|
|
|
```rust
|
|
pub fn init() -> Result<()>; // Initialize
|
|
pub fn search_packages(query: &str) -> Result<Vec<String>>; // Search packages
|
|
pub fn is_package_installed(name: &str) -> Result<bool>; // Check installed
|
|
pub fn get_package_info(name: &str) -> Result<AptPackage>; // Get package info
|
|
```
|
|
|
|
## Installation
|
|
|
|
Add to your `Cargo.toml`:
|
|
|
|
```toml
|
|
[dependencies]
|
|
apt-wrapper = "0.1.0"
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Basic Transaction
|
|
|
|
```rust
|
|
use apt_wrapper::AptTransaction;
|
|
|
|
let mut tx = AptTransaction::new()?;
|
|
tx.add_package("vim")?;
|
|
tx.add_package("git")?;
|
|
tx.resolve()?;
|
|
tx.commit()?;
|
|
```
|
|
|
|
### Search Packages
|
|
|
|
```rust
|
|
use apt_wrapper::search_packages;
|
|
|
|
let packages = search_packages("editor")?;
|
|
for package in packages {
|
|
println!("Found: {}", package);
|
|
}
|
|
```
|
|
|
|
### Check Installation
|
|
|
|
```rust
|
|
use apt_wrapper::is_package_installed;
|
|
|
|
if is_package_installed("vim")? {
|
|
println!("vim is installed");
|
|
}
|
|
```
|
|
|
|
### Rollback Support
|
|
|
|
```rust
|
|
use apt_wrapper::AptTransaction;
|
|
|
|
let mut tx = AptTransaction::new()?;
|
|
tx.add_package("vim")?;
|
|
tx.add_package("git")?;
|
|
tx.resolve()?;
|
|
|
|
// Commit the transaction
|
|
tx.commit()?;
|
|
|
|
// If something goes wrong later, rollback
|
|
tx.rollback()?;
|
|
|
|
// Check what was changed
|
|
println!("Changed packages: {:?}", tx.changed_packages());
|
|
```
|
|
|
|
## Testing
|
|
|
|
```bash
|
|
cargo test
|
|
```
|
|
|
|
## Examples
|
|
|
|
```bash
|
|
cargo run --example simple_usage
|
|
```
|
|
|
|
## Design Philosophy
|
|
|
|
This wrapper is designed to be:
|
|
|
|
1. **Simple**: Minimal API surface, easy to understand
|
|
2. **Focused**: Only what's needed for apt-ostree porting
|
|
3. **DNF-like**: Familiar interface for rpm-ostree developers
|
|
4. **Minimal**: ~200 lines total, no complex abstractions
|
|
|
|
## Differences from DNF
|
|
|
|
- **APT is declarative**: Dependencies are resolved automatically
|
|
- **No complex repo management**: APT uses simple text files
|
|
- **Simpler error handling**: APT provides clear error messages
|
|
- **No transaction rollback**: APT doesn't have built-in rollback
|
|
|
|
## OSTree Integration
|
|
|
|
For atomic operations, use OSTree's native checkpoint/rollback:
|
|
|
|
```rust
|
|
// 1. Create OSTree checkpoint
|
|
let checkpoint = ostree_create_checkpoint()?;
|
|
|
|
// 2. Run APT transaction
|
|
let mut tx = AptTransaction::new()?;
|
|
tx.add_package("vim")?;
|
|
tx.commit()?;
|
|
|
|
// 3. Commit or rollback based on result
|
|
if success {
|
|
ostree_commit_changes()?;
|
|
} else {
|
|
ostree_rollback_to_checkpoint(checkpoint)?;
|
|
}
|
|
```
|
|
|
|
## License
|
|
|
|
MIT License - see [LICENSE](LICENSE) file for details.
|
|
|
|
## Contributing
|
|
|
|
This is a focused tool for apt-ostree. Contributions should maintain simplicity and focus on the core use case.
|