podbox-cli 0.6.7

Declarative Podman-native container environment manager. Define an environment as a TOML file and let systemd own its lifecycle.
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
use std::ffi::OsString;
use std::os::unix::fs::PermissionsExt;
use std::path::PathBuf;

use anyhow::Result;

use crate::error::PodboxError;

/// Standard XDG application directories searched inside the container,
/// in priority order.  Many apps install to `~/.local/share/applications/`
/// (per-user), `/usr/local/share/applications/`, or `/opt/<app>/share/applications/`.
const DESKTOP_SEARCH_PATHS: &[&str] = &[
    "/usr/share/applications",
    "/usr/local/share/applications",
    "/usr/share/applications/kde",
    "/usr/share/applications/gnome",
    "/opt",
];

fn is_valid_app_name(app: &str) -> bool {
    !app.is_empty()
        && app
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.')
}

/// Export an application as a .desktop file on the host.
pub fn export_app(container_name: &str, app: &str) -> Result<()> {
    if !is_valid_app_name(app) {
        return Err(PodboxError::ExportFailed {
            details: format!("invalid app name: '{app}'"),
        }
        .into());
    }

    // 1. Locate .desktop file in container, searching XDG directories.
    let (container_path, desktop_content) = find_desktop_file(container_name, app)?;

    // 2. Rewrite Name= and Exec= lines
    let rewritten = rewrite_desktop_file(&desktop_content, container_name, app);

    // 3. Write host .desktop file
    let apps_dir = dirs::data_dir()
        .unwrap_or_else(|| {
            dirs::home_dir()
                .map(|h| h.join(".local/share"))
                .unwrap_or_else(|| PathBuf::from("/usr/local/share"))
        })
        .join("applications");
    std::fs::create_dir_all(&apps_dir)?;

    let host_path = apps_dir.join(format!("podbox-{}-{}.desktop", container_name, app));
    std::fs::write(&host_path, rewritten)?;

    // 4. Try to extract icon
    if let Some(icon_name) = extract_icon_name(&desktop_content) {
        if let Err(e) = copy_icon_from_container(container_name, &icon_name, container_name) {
            eprintln!("Warning: failed to copy icon '{}': {}", icon_name, e);
        }
    }

    // 5. Update desktop database
    if let Err(e) = std::process::Command::new("update-desktop-database")
        .arg(&apps_dir)
        .output()
        .map(|_| ())
    {
        eprintln!("Warning: update-desktop-database failed: {}", e);
    }

    println!(
        "Exported app '{}'.desktop (from {}) -> {}",
        app,
        container_path,
        host_path.display()
    );
    Ok(())
}

/// Find a `.desktop` file in the container by searching XDG dirs,
/// falling back to user-installed locations.
fn find_desktop_file(container_name: &str, app: &str) -> Result<(String, String)> {
    if !is_valid_app_name(app) {
        return Err(PodboxError::ExportFailed {
            details: format!("invalid app name: '{app}'"),
        }
        .into());
    }
    let filename = format!("{}.desktop", app);

    // First: search well-known system locations.
    for dir in DESKTOP_SEARCH_PATHS {
        if *dir == "/opt" {
            // /opt is a prefix — search one level deep for share/applications.
            continue;
        }
        let candidate = format!("{}/{}", dir, filename);
        if let Some(content) = try_cat(container_name, &candidate)? {
            return Ok((candidate, content));
        }
    }

    // Second: per-user installs.
    let user_dirs = ["/root/.local/share/applications", "/home"];
    for dir in user_dirs {
        if let Some(content) = try_cat(container_name, &format!("{}/{}", dir, filename))? {
            return Ok((format!("{}/{}", dir, filename), content));
        }
    }

    // Third: /opt — search for any /opt/*/share/applications/<app>.desktop.
    if let Some((path, content)) = find_desktop_in_opt(container_name, app)? {
        return Ok((path, content));
    }

    Err(PodboxError::ExportFailed {
        details: format!(
            "app {} not found in container (searched: {})",
            app,
            DESKTOP_SEARCH_PATHS.join(", ")
        ),
    }
    .into())
}

/// `podman exec <container> cat <path>` — returns `Some(content)` if the
/// file exists, `None` if `cat` reports missing, error on other failures.
fn try_cat(container_name: &str, path: &str) -> Result<Option<String>> {
    let args: Vec<OsString> = vec![
        "exec".into(),
        container_name.into(),
        "cat".into(),
        path.into(),
    ];
    let output = crate::process::run_piped("podman", &args)?;
    if output.status.success() {
        Ok(Some(String::from_utf8_lossy(&output.stdout).into_owned()))
    } else {
        Ok(None)
    }
}

/// Search /opt/*/share/applications/ for a matching .desktop file.
fn find_desktop_in_opt(container_name: &str, app: &str) -> Result<Option<(String, String)>> {
    let args: Vec<OsString> = vec![
        "exec".into(),
        container_name.into(),
        "sh".into(),
        "-c".into(),
        format!(
            "for d in /opt/*/share/applications; do \
               [ -f \"$d/{app}.desktop\" ] && echo \"$d/{app}.desktop\"; \
             done"
        )
        .into(),
    ];
    let output = crate::process::run_piped("podman", &args)?;
    if !output.status.success() {
        return Ok(None);
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    for line in stdout.lines().take(500) {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        if let Some(content) = try_cat(container_name, line)? {
            return Ok(Some((line.to_string(), content)));
        }
    }
    Ok(None)
}

/// Export a binary shim to ~/.local/bin.
pub fn export_bin(container_name: &str, bin: &str) -> Result<()> {
    let bin_dir = dirs::home_dir()
        .map(|h| h.join(".local/bin"))
        .unwrap_or_else(|| PathBuf::from("/usr/local/bin"));
    std::fs::create_dir_all(&bin_dir)?;

    let exe = std::env::current_exe()
        .map(|p| p.to_string_lossy().to_string())
        .unwrap_or_else(|_| "podbox".to_string());
    let shim = format!(
        "#!/bin/sh\nexec {} --container \"{}\" exec \"{}\" \"$@\"\n",
        exe,
        container_name.replace('"', "\\\""),
        bin.replace('"', "\\\"")
    );

    let shim_path = bin_dir.join(bin);
    std::fs::write(&shim_path, shim)?;
    #[allow(clippy::print_literal)]
    {
        let _ = std::fs::set_permissions(&shim_path, std::fs::Permissions::from_mode(0o755));
    }

    println!("Exported bin shim '{}' -> {}", bin, shim_path.display());
    Ok(())
}

/// Remove all exports for a container.
pub fn unexport_all(container_name: &str) -> Result<()> {
    let apps_dir = dirs::data_dir()
        .unwrap_or_else(|| {
            dirs::home_dir()
                .map(|h| h.join(".local/share"))
                .unwrap_or_else(|| PathBuf::from("/usr/local/share"))
        })
        .join("applications");
    let prefix = format!("podbox-{}", container_name);

    if let Ok(entries) = std::fs::read_dir(&apps_dir) {
        for entry in entries.flatten() {
            let name = entry.file_name();
            if name.to_string_lossy().starts_with(&prefix) {
                let _ = std::fs::remove_file(entry.path());
            }
        }
    }

    let icons_dir = dirs::data_dir()
        .unwrap_or_else(|| {
            dirs::home_dir()
                .map(|h| h.join(".local/share"))
                .unwrap_or_else(|| PathBuf::from("/usr/local/share"))
        })
        .join(format!("icons/podbox/{}", container_name));
    // Also remove legacy icons path
    let old_icons_dir = dirs::data_dir()
        .unwrap_or_else(|| {
            dirs::home_dir()
                .map(|h| h.join(".local/share"))
                .unwrap_or_else(|| PathBuf::from("/usr/local/share"))
        })
        .join(format!("icons/podmgr/{}", container_name));
    let _ = std::fs::remove_dir_all(&icons_dir);
    if old_icons_dir.exists() {
        let _ = std::fs::remove_dir_all(&old_icons_dir);
    }

    let bin_dir = dirs::home_dir()
        .map(|h| h.join(".local/bin"))
        .unwrap_or_else(|| PathBuf::from("/usr/local/bin"));

    // Remove shims that reference this container
    let marker = format!("--container \"{}\"", container_name);
    if let Ok(entries) = std::fs::read_dir(&bin_dir) {
        for entry in entries.flatten() {
            if let Ok(mut file) = std::fs::File::open(entry.path()) {
                use std::io::Read;
                let mut chunk = vec![0u8; 4096];
                if let Ok(bytes_read) = file.read(&mut chunk) {
                    let content = String::from_utf8_lossy(&chunk[..bytes_read]);
                    if content.contains(&marker) {
                        let _ = std::fs::remove_file(entry.path());
                    }
                }
            }
        }
    }

    println!("Unexported all apps and bins for '{}'.", container_name);
    Ok(())
}

/// List the .desktop apps and bin shims exported to the host for a container.
pub fn list_exports(container_name: &str) -> Result<()> {
    let apps_dir = dirs::data_dir()
        .unwrap_or_else(|| {
            dirs::home_dir()
                .map(|h| h.join(".local/share"))
                .unwrap_or_else(|| PathBuf::from("/usr/local/share"))
        })
        .join("applications");
    let prefix = format!("podbox-{}-", container_name);
    let suffix = ".desktop";

    let mut apps: Vec<String> = Vec::new();
    if let Ok(entries) = std::fs::read_dir(&apps_dir) {
        for entry in entries.flatten() {
            let name = entry.file_name().to_string_lossy().into_owned();
            if name.starts_with(&prefix) && name.ends_with(suffix) {
                apps.push(name[prefix.len()..name.len() - suffix.len()].to_string());
            }
        }
    }
    apps.sort();

    let bin_dir = dirs::home_dir()
        .map(|h| h.join(".local/bin"))
        .unwrap_or_else(|| PathBuf::from("/usr/local/bin"));
    let marker = format!("--container \"{}\"", container_name);
    let mut bins: Vec<String> = Vec::new();
    if let Ok(entries) = std::fs::read_dir(&bin_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if let Ok(mut file) = std::fs::File::open(&path) {
                use std::io::Read;
                let mut chunk = vec![0u8; 4096];
                if let Ok(bytes_read) = file.read(&mut chunk) {
                    let content = String::from_utf8_lossy(&chunk[..bytes_read]);
                    if content.contains(&marker) {
                        bins.push(entry.file_name().to_string_lossy().into_owned());
                    }
                }
            }
        }
    }
    bins.sort();

    if apps.is_empty() && bins.is_empty() {
        println!("No exports for '{}'.", container_name);
        return Ok(());
    }

    if !apps.is_empty() {
        println!("Apps:");
        for app in &apps {
            println!("  {app}");
        }
    }
    if !bins.is_empty() {
        if !apps.is_empty() {
            println!();
        }
        println!("Bins:");
        for bin in &bins {
            println!("  {bin}");
        }
    }
    Ok(())
}

fn rewrite_desktop_file(content: &str, container_name: &str, _app: &str) -> String {
    let exe = std::env::current_exe()
        .map(|p| p.to_string_lossy().to_string())
        .unwrap_or_else(|_| "podbox".to_string());
    let suffix = format!("({})", container_name);
    content
        .lines()
        .map(|line| {
            if let Some(original) = line.strip_prefix("Exec=") {
                format!(
                    "                    Exec={} --container \"{}\" exec -- {}",
                    exe,
                    container_name.replace('"', "\\\""),
                    original
                )
            } else if let Some((key, val)) = line.split_once('=') {
                if (key == "Name" || key.starts_with("Name[")) && !val.contains(&suffix) {
                    format!("{}={} ({})", key, val, container_name)
                } else {
                    line.to_string()
                }
            } else {
                line.to_string()
            }
        })
        .collect::<Vec<_>>()
        .join("\n")
}

fn extract_icon_name(content: &str) -> Option<String> {
    content
        .lines()
        .find_map(|line| line.strip_prefix("Icon=").map(|s| s.to_string()))
}

fn copy_icon_from_container(container_name: &str, icon_name: &str, _profile: &str) -> Result<()> {
    // Sanitize icon name: refuse path separators to prevent traversal
    if icon_name.contains('/') || icon_name.contains("..") {
        return Err(anyhow::anyhow!(
            "icon name contains path separators, refusing: {}",
            icon_name
        ));
    }

    let icons_dir = dirs::data_dir()
        .unwrap_or_else(|| {
            dirs::home_dir()
                .map(|h| h.join(".local/share"))
                .unwrap_or_else(|| PathBuf::from("/usr/local/share"))
        })
        .join(format!("icons/podbox/{}", container_name));
    std::fs::create_dir_all(&icons_dir)?;

    let icon_paths: Vec<String> = vec![
        format!("/usr/share/icons/hicolor/48x48/apps/{}.png", icon_name),
        format!("/usr/share/icons/hicolor/scalable/apps/{}.svg", icon_name),
        format!("/usr/share/icons/hicolor/64x64/apps/{}.png", icon_name),
        format!("/usr/share/icons/hicolor/128x128/apps/{}.png", icon_name),
        format!("/usr/share/icons/hicolor/256x256/apps/{}.png", icon_name),
        format!("/usr/share/icons/hicolor/48x48/apps/{}.svg", icon_name),
    ];

    for path in &icon_paths {
        let ext = std::path::Path::new(path)
            .extension()
            .map(|e| e.to_string_lossy())
            .unwrap_or_default();
        let args: Vec<OsString> = vec![
            "exec".into(),
            container_name.into(),
            "cat".into(),
            path.into(),
        ];
        let output = crate::process::run_piped("podman", &args)?;
        if output.status.success() {
            let dest = icons_dir.join(format!("{}.{}", icon_name, ext));
            std::fs::write(dest, &output.stdout)?;
            break;
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn valid_app_names() {
        for name in &["firefox", "Firefox", "code-oss", "code_oss", "v1.2.3", "a"] {
            assert!(is_valid_app_name(name), "expected '{name}' to be valid");
        }
    }

    #[test]
    fn reject_empty_name() {
        assert!(!is_valid_app_name(""));
    }

    #[test]
    fn reject_shell_metacharacters() {
        for bad in &[
            "foo;rm", "foo\"bar", "foo`bar", "foo$bar", "foo|bar", "foo>bar", "foo<bar", "foo&bar",
            "foo\nbar", "../foo", "foo/bar", "foo bar", "foo\\bar", "foo'bar",
        ] {
            assert!(!is_valid_app_name(bad), "expected '{bad}' to be rejected");
        }
    }

    #[test]
    fn export_app_rejects_invalid_name() {
        let result = export_app("test-container", "foo;rm");
        assert!(result.is_err());
        let err = format!("{}", result.unwrap_err());
        assert!(
            err.contains("foo;rm") || err.contains("invalid"),
            "error should mention the name: {err}"
        );
    }

    #[test]
    fn find_desktop_file_rejects_invalid_name() {
        let result = find_desktop_file("test-container", "foo`whoami`");
        assert!(result.is_err());
        let err = format!("{}", result.unwrap_err());
        assert!(
            err.contains("foo`whoami`") || err.contains("invalid"),
            "error should mention the name: {err}"
        );
    }

    #[test]
    fn list_exports_lists_apps_and_bins() {
        let apps_dir = dirs::data_dir().expect("data dir").join("applications");
        std::fs::create_dir_all(&apps_dir).expect("create apps dir");
        let app_path = apps_dir.join("podbox-box-firefox.desktop");
        std::fs::write(&app_path, "[Desktop Entry]\nName=Firefox (box)\n").unwrap();

        let bin_dir = dirs::home_dir().expect("home dir").join(".local/bin");
        std::fs::create_dir_all(&bin_dir).expect("create bin dir");
        let shim_path = bin_dir.join("firefox");
        std::fs::write(
            &shim_path,
            "#!/bin/sh\nexec /usr/bin/podbox --container \"box\" exec \"firefox\" \"$@\"\n",
        )
        .unwrap();

        let apps_dir = dirs::data_dir().expect("data dir").join("applications");
        let prefix = format!("podbox-{}-", "box");
        let suffix = ".desktop";
        let mut apps: Vec<String> = std::fs::read_dir(&apps_dir)
            .unwrap()
            .flatten()
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .filter(|n| n.starts_with(&prefix) && n.ends_with(suffix))
            .map(|n| n[prefix.len()..n.len() - suffix.len()].to_string())
            .collect();
        apps.sort();

        let marker = format!("--container \"{}\"", "box");
        let mut bins: Vec<String> = std::fs::read_dir(&bin_dir)
            .unwrap()
            .flatten()
            .filter(|e| {
                std::fs::read_to_string(e.path())
                    .map(|c| c.contains(&marker))
                    .unwrap_or(false)
            })
            .map(|e| e.file_name().to_string_lossy().into_owned())
            .collect();
        bins.sort();

        assert_eq!(apps, vec!["firefox".to_string()]);
        assert_eq!(bins, vec!["firefox".to_string()]);

        let _ = std::fs::remove_file(&app_path);
        let _ = std::fs::remove_file(&shim_path);
    }
}