pacsea 0.8.2

A fast, friendly TUI for browsing and installing Arch and AUR packages with built-in news and security scanning
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
use std::sync::OnceLock;
use tokio::sync::mpsc;

#[cfg(target_os = "windows")]
use crate::app::runtime::workers::updates_helpers::UpdateCheckPayload;
use crate::app::runtime::workers::updates_helpers::check_aur_helper;
#[cfg(not(target_os = "windows"))]
use crate::app::runtime::workers::updates_helpers::{
    REASON_CHECKUPDATES_FAILED, REASON_CHECKUPDATES_UNAVAILABLE, REASON_FAKEROOT_UNAVAILABLE,
    REASON_STALE_DB_FALLBACK, REASON_TEMP_DB_SYNC_FAILED, UpdateCheckPayload,
    classify_pacman_stderr_for_update_check, has_checkupdates, has_fakeroot, setup_temp_db,
    sync_temp_db,
};
use crate::app::runtime::workers::updates_parsing::{
    get_installed_version, parse_checkupdates, parse_checkupdates_tool, parse_qua,
};

/// What: Merge official-repo command output into package maps when exit status is authoritative.
///
/// Inputs:
/// - `output`: `std::process::Output` result from pacman or checkupdates.
/// - `is_checkupdates_tool`: `true` for `checkupdates` line format; `false` for `pacman -Qu`.
/// - `packages_map` / `packages_set`: Accumulators.
///
/// Output:
/// - `true` if exit code was 0 or 1 (success or no updates).
/// - `false` on IO error or unexpected exit code.
///
/// Details:
/// - Exit code 1 means no upgrades for both pacman and checkupdates in this app’s convention.
fn ingest_official_repo_output(
    output: Result<std::process::Output, std::io::Error>,
    is_checkupdates_tool: bool,
    packages_map: &mut std::collections::HashMap<String, String>,
    packages_set: &mut std::collections::HashSet<String>,
) -> bool {
    match output {
        Ok(output) => {
            let exit_code = output.status.code();
            if output.status.success() {
                if is_checkupdates_tool {
                    let packages = parse_checkupdates_tool(&output.stdout);
                    let count = packages.len();
                    for (name, new_version) in packages {
                        let old_version =
                            get_installed_version(&name).unwrap_or_else(|| "unknown".to_string());
                        let formatted = format!("{name} - {old_version} -> {name} - {new_version}");
                        packages_map.insert(name.clone(), formatted);
                        packages_set.insert(name);
                    }
                    tracing::debug!(
                        "checkupdates completed successfully (exit code: {:?}): found {} packages from official repos",
                        exit_code,
                        count
                    );
                } else {
                    let packages = parse_checkupdates(&output.stdout);
                    let count = packages.len();
                    for (name, old_version, new_version) in packages {
                        let formatted = format!("{name} - {old_version} -> {name} - {new_version}");
                        packages_map.insert(name.clone(), formatted);
                        packages_set.insert(name);
                    }
                    tracing::debug!(
                        "pacman -Qu completed successfully (exit code: {:?}): found {} packages from official repos",
                        exit_code,
                        count
                    );
                }
                true
            } else if output.status.code() == Some(1) {
                if is_checkupdates_tool {
                    tracing::debug!(
                        "checkupdates returned exit code 1 (no updates available in official repos)"
                    );
                } else {
                    tracing::debug!(
                        "pacman -Qu returned exit code 1 (no updates available in official repos)"
                    );
                }
                true
            } else {
                let stderr = String::from_utf8_lossy(&output.stderr);
                if is_checkupdates_tool {
                    tracing::warn!(
                        "checkupdates command failed with exit code: {:?}, stderr: {}",
                        exit_code,
                        stderr.trim()
                    );
                } else {
                    tracing::warn!(
                        "pacman -Qu command failed with exit code: {:?}, stderr: {}",
                        exit_code,
                        stderr.trim()
                    );
                }
                false
            }
        }
        Err(e) => {
            if is_checkupdates_tool {
                tracing::warn!("Failed to execute checkupdates: {}", e);
            } else {
                tracing::warn!("Failed to execute pacman -Qu: {}", e);
            }
            false
        }
    }
}

/// What: Process -Qua output and add packages to collections.
///
/// Inputs:
/// - `result`: Command output result
/// - `helper`: Helper name for logging
/// - `packages_map`: Mutable `HashMap` to store formatted package strings
/// - `packages_set`: Mutable `HashSet` to track unique package names
fn process_qua_output(
    result: Option<Result<std::process::Output, std::io::Error>>,
    helper: &str,
    packages_map: &mut std::collections::HashMap<String, String>,
    packages_set: &mut std::collections::HashSet<String>,
) {
    if let Some(result) = result {
        match result {
            Ok(output) => {
                let exit_code = output.status.code();
                if output.status.success() {
                    let packages = parse_qua(&output.stdout);
                    let count = packages.len();
                    let before_count = packages_set.len();

                    for (name, old_version, new_version) in packages {
                        let formatted = format!("{name} - {old_version} -> {name} - {new_version}");
                        packages_map.insert(name.clone(), formatted);
                        packages_set.insert(name);
                    }

                    let after_count = packages_set.len();
                    tracing::debug!(
                        "{} -Qua completed successfully (exit code: {:?}): found {} packages from AUR, {} total ({} new)",
                        helper,
                        exit_code,
                        count,
                        after_count,
                        after_count - before_count
                    );
                } else if output.status.code() == Some(1) {
                    tracing::debug!(
                        "{} -Qua returned exit code 1 (no updates available in AUR)",
                        helper
                    );
                } else {
                    tracing::warn!(
                        "{} -Qua command failed with exit code: {:?}",
                        helper,
                        exit_code
                    );
                }
            }
            Err(e) => {
                tracing::warn!("Failed to execute {} -Qua: {}", helper, e);
            }
        }
    } else {
        tracing::debug!("No AUR helper available, skipping AUR updates check");
    }
}

/// Static mutex to prevent concurrent update checks.
///
/// What: Tracks whether an update check is currently in progress.
///
/// Details:
/// - Uses `OnceLock` for lazy initialization
/// - Uses `tokio::sync::Mutex` for async-safe synchronization
/// - Prevents overlapping file writes to `available_updates.txt`
static UPDATE_CHECK_IN_PROGRESS: OnceLock<tokio::sync::Mutex<bool>> = OnceLock::new();

/// What: Attempt authoritative official-repo listing via fakeroot temp-db sync + `pacman -Qu`.
///
/// Inputs:
/// - `temp_db_path`: Prepared temp DB dir from [`setup_temp_db`].
/// - `packages_map` / `packages_set`: Accumulators.
/// - `reason_codes`: Diagnostic codes appended on failure paths.
///
/// Output:
/// - `Some("temp_db_pacman_qu")` when the probe is authoritative; `None` otherwise.
///
/// Details:
/// - No-ops when `fakeroot` or temp DB path is unavailable without adding sync-failed reasons.
#[cfg(not(target_os = "windows"))]
fn unix_try_authoritative_fakeroot(
    temp_db_path: Option<&std::path::PathBuf>,
    packages_map: &mut std::collections::HashMap<String, String>,
    packages_set: &mut std::collections::HashSet<String>,
    reason_codes: &mut Vec<String>,
) -> Option<&'static str> {
    use std::process::{Command, Stdio};

    if !has_fakeroot() {
        reason_codes.push(REASON_FAKEROOT_UNAVAILABLE.to_string());
        tracing::debug!("fakeroot not available; skipping temp-database sync");
        return None;
    }
    let db = temp_db_path?;
    match sync_temp_db(db) {
        Ok(()) => {
            tracing::debug!(
                "Executing: pacman -Qu --dbpath {:?} (using synced temp database)",
                db
            );
            let output = Command::new("pacman")
                .args(["-Qu", "--dbpath"])
                .arg(db)
                .stdin(Stdio::null())
                .stdout(Stdio::piped())
                .stderr(Stdio::piped())
                .output();
            if ingest_official_repo_output(output, false, packages_map, packages_set) {
                Some("temp_db_pacman_qu")
            } else {
                None
            }
        }
        Err(stderr) => {
            tracing::warn!("Temp database sync failed");
            reason_codes.push(REASON_TEMP_DB_SYNC_FAILED.to_string());
            reason_codes.extend(classify_pacman_stderr_for_update_check(&stderr));
            None
        }
    }
}

/// What: Attempt authoritative official-repo listing via `checkupdates` with `CHECKUPDATES_DB`.
///
/// Inputs:
/// - `temp_db_path`: Existing temp DB dir if any.
/// - `packages_map` / `packages_set`: Accumulators.
/// - `reason_codes`: Diagnostic codes (e.g. `checkupdates_failed`).
///
/// Output:
/// - `Some("checkupdates_db")` on success; `None` when skipped or command failed.
///
/// Details:
/// - Requires `pacman-contrib`; records unavailability when the binary is missing.
#[cfg(not(target_os = "windows"))]
fn unix_try_authoritative_checkupdates(
    temp_db_path: Option<&std::path::PathBuf>,
    packages_map: &mut std::collections::HashMap<String, String>,
    packages_set: &mut std::collections::HashSet<String>,
    reason_codes: &mut Vec<String>,
) -> Option<&'static str> {
    use std::process::{Command, Stdio};

    if !has_checkupdates() {
        reason_codes.push(REASON_CHECKUPDATES_UNAVAILABLE.to_string());
        tracing::warn!("checkupdates not available; cannot refresh official db without root");
        return None;
    }
    let db_for_check = temp_db_path.cloned().or_else(setup_temp_db)?;
    tracing::info!(
        "Executing: checkupdates with CHECKUPDATES_DB={:?} (isolated sync db)",
        db_for_check
    );
    let output = Command::new("checkupdates")
        .env("CHECKUPDATES_DB", db_for_check.as_os_str())
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output();
    if ingest_official_repo_output(output, true, packages_map, packages_set) {
        Some("checkupdates_db")
    } else {
        reason_codes.push(REASON_CHECKUPDATES_FAILED.to_string());
        None
    }
}

/// What: Apply non-authoritative system `pacman -Qu` as last-resort official listing.
///
/// Inputs:
/// - `packages_map` / `packages_set`: Accumulators.
/// - `reason_codes`: Appends `REASON_STALE_DB_FALLBACK`.
///
/// Output:
/// - Always `"stale_pacman_qu"` for strategy labeling.
///
/// Details:
/// - May reflect a stale sync database; never marks the overall probe as authoritative.
#[cfg(not(target_os = "windows"))]
fn unix_apply_stale_official_pacman_qu(
    packages_map: &mut std::collections::HashMap<String, String>,
    packages_set: &mut std::collections::HashSet<String>,
    reason_codes: &mut Vec<String>,
) -> &'static str {
    use std::process::{Command, Stdio};

    reason_codes.push(REASON_STALE_DB_FALLBACK.to_string());
    tracing::warn!("Falling back to pacman -Qu (system database may be stale)");
    let output = Command::new("pacman")
        .args(["-Qu"])
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output();
    let _ = ingest_official_repo_output(output, false, packages_map, packages_set);
    "stale_pacman_qu"
}

/// What: Run the blocking portion of a package update check (Unix).
///
/// Output:
/// - [`UpdateCheckPayload`] with deduplicated package names, file body saved under config.
#[cfg(not(target_os = "windows"))]
fn run_update_check_blocking_unix() -> UpdateCheckPayload {
    use std::collections::{HashMap, HashSet};
    use std::process::{Command, Stdio};

    let mut reason_codes: Vec<String> = Vec::new();
    let mut authoritative_official = false;
    let mut official_strategy: &'static str = "none";

    let (has_paru, has_yay, helper) = check_aur_helper();

    let mut packages_map: HashMap<String, String> = HashMap::new();
    let mut packages_set: HashSet<String> = HashSet::new();

    let temp_db_path = setup_temp_db();

    if let Some(s) = unix_try_authoritative_fakeroot(
        temp_db_path.as_ref(),
        &mut packages_map,
        &mut packages_set,
        &mut reason_codes,
    ) {
        authoritative_official = true;
        official_strategy = s;
    }

    if !authoritative_official
        && let Some(s) = unix_try_authoritative_checkupdates(
            temp_db_path.as_ref(),
            &mut packages_map,
            &mut packages_set,
            &mut reason_codes,
        )
    {
        authoritative_official = true;
        official_strategy = s;
    }

    if !authoritative_official {
        official_strategy = unix_apply_stale_official_pacman_qu(
            &mut packages_map,
            &mut packages_set,
            &mut reason_codes,
        );
    }

    let output_qua = if has_paru {
        tracing::debug!("Executing: paru -Qua (AUR updates)");
        Some(
            Command::new("paru")
                .args(["-Qua"])
                .stdin(Stdio::null())
                .stdout(Stdio::piped())
                .stderr(Stdio::null())
                .output(),
        )
    } else if has_yay {
        tracing::debug!("Executing: yay -Qua (AUR updates)");
        Some(
            Command::new("yay")
                .args(["-Qua"])
                .stdin(Stdio::null())
                .stdout(Stdio::piped())
                .stderr(Stdio::null())
                .output(),
        )
    } else {
        tracing::debug!("No AUR helper available (paru/yay), skipping AUR updates check");
        None
    };

    process_qua_output(output_qua, helper, &mut packages_map, &mut packages_set);

    let mut package_names: Vec<String> = packages_set.into_iter().collect();
    package_names.sort_unstable();

    let packages: Vec<String> = package_names
        .iter()
        .filter_map(|name| packages_map.get(name).cloned())
        .collect();

    let count = packages.len();

    tracing::info!(
        target: "pacsea::update_check",
        authoritative = authoritative_official,
        official_strategy = official_strategy,
        reasons = %reason_codes.join(","),
        package_count = count,
        "update_check_summary"
    );

    tracing::info!(
        "Update check completed: found {} total available updates (after deduplication)",
        count
    );

    let lists_dir = crate::theme::lists_dir();
    let updates_file = lists_dir.join("available_updates.txt");
    if let Err(e) = std::fs::write(&updates_file, packages.join("\n")) {
        tracing::warn!("Failed to save updates list to file: {}", e);
    } else {
        tracing::debug!("Saved updates list to {:?}", updates_file);
    }

    UpdateCheckPayload {
        count,
        package_names,
        authoritative: authoritative_official,
        reason_codes,
        official_strategy,
    }
}

/// What: Run the blocking portion of a package update check (Windows / non-Arch).
#[cfg(target_os = "windows")]
fn run_update_check_blocking_windows() -> UpdateCheckPayload {
    use std::collections::{HashMap, HashSet};
    use std::process::{Command, Stdio};

    let (has_paru, has_yay, helper) = check_aur_helper();
    let mut packages_map: HashMap<String, String> = HashMap::new();
    let mut packages_set: HashSet<String> = HashSet::new();

    tracing::debug!("Executing: pacman -Qu (Windows fallback)");
    let output = Command::new("pacman")
        .args(["-Qu"])
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .output();

    let authoritative =
        ingest_official_repo_output(output, false, &mut packages_map, &mut packages_set);

    let output_qua = if has_paru {
        Some(
            Command::new("paru")
                .args(["-Qua"])
                .stdin(Stdio::null())
                .stdout(Stdio::piped())
                .stderr(Stdio::null())
                .output(),
        )
    } else if has_yay {
        Some(
            Command::new("yay")
                .args(["-Qua"])
                .stdin(Stdio::null())
                .stdout(Stdio::piped())
                .stderr(Stdio::null())
                .output(),
        )
    } else {
        None
    };
    process_qua_output(output_qua, helper, &mut packages_map, &mut packages_set);

    let mut package_names: Vec<String> = packages_set.into_iter().collect();
    package_names.sort_unstable();
    let packages: Vec<String> = package_names
        .iter()
        .filter_map(|name| packages_map.get(name).cloned())
        .collect();
    let count = packages.len();

    tracing::info!(
        target: "pacsea::update_check",
        authoritative = authoritative,
        official_strategy = "windows_pacman_qu",
        reasons = "",
        package_count = count,
        "update_check_summary"
    );

    let lists_dir = crate::theme::lists_dir();
    let updates_file = lists_dir.join("available_updates.txt");
    if let Err(e) = std::fs::write(&updates_file, packages.join("\n")) {
        tracing::warn!("Failed to save updates list to file: {}", e);
    }

    UpdateCheckPayload {
        count,
        package_names,
        authoritative,
        reason_codes: Vec::new(),
        official_strategy: "windows_pacman_qu",
    }
}

/// What: Spawn background worker to check for available package updates.
///
/// Inputs:
/// - `updates_tx`: Channel sender for [`UpdateCheckPayload`]
///
/// Output:
/// - None (spawns async task)
///
/// Details:
/// - Linux: temp db + `pacman -Qu --dbpath`, then `checkupdates` with `CHECKUPDATES_DB`, then stale `pacman -Qu`
/// - Executes `paru -Qua` / `yay -Qua` for AUR
/// - Saves list to `~/.config/pacsea/lists/available_updates.txt`
#[allow(clippy::too_many_lines)]
pub fn spawn_updates_worker(updates_tx: mpsc::UnboundedSender<UpdateCheckPayload>) {
    let updates_tx_once = updates_tx;

    tokio::spawn(async move {
        let mutex = UPDATE_CHECK_IN_PROGRESS.get_or_init(|| tokio::sync::Mutex::new(false));

        let mut in_progress = mutex.lock().await;
        if *in_progress {
            tracing::debug!("Update check already in progress, skipping concurrent call");
            return;
        }

        *in_progress = true;
        drop(in_progress);

        let result = tokio::task::spawn_blocking(move || {
            tracing::debug!("Starting update check");
            #[cfg(not(target_os = "windows"))]
            {
                run_update_check_blocking_unix()
            }
            #[cfg(target_os = "windows")]
            {
                run_update_check_blocking_windows()
            }
        })
        .await;

        let mutex = UPDATE_CHECK_IN_PROGRESS.get_or_init(|| tokio::sync::Mutex::new(false));
        let mut in_progress = mutex.lock().await;
        *in_progress = false;
        drop(in_progress);

        match result {
            Ok(payload) => {
                let _ = updates_tx_once.send(payload);
            }
            Err(e) => {
                tracing::error!("Updates worker task panicked: {:?}", e);
                let _ = updates_tx_once.send(UpdateCheckPayload::worker_panic());
            }
        }
    });
}

/// What: Spawns periodic updates worker that checks for package updates at intervals.
///
/// Inputs:
/// - `updates_tx`: Channel sender for package updates
/// - `updates_refresh_interval`: Refresh interval in seconds
pub fn spawn_periodic_updates_worker(
    updates_tx: &mpsc::UnboundedSender<UpdateCheckPayload>,
    updates_refresh_interval: u64,
) {
    spawn_updates_worker(updates_tx.clone());

    let updates_tx_periodic = updates_tx.clone();
    tokio::spawn(async move {
        let mut interval =
            tokio::time::interval(tokio::time::Duration::from_secs(updates_refresh_interval));
        interval.tick().await;
        loop {
            interval.tick().await;
            spawn_updates_worker(updates_tx_periodic.clone());
        }
    });
}