ffcv 1.1.1

Firefox Configuration Viewer - Parse and query Firefox preference files
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! Firefox profile management
//!
//! This module provides functionality for detecting and managing Firefox profiles
//! across different operating systems (Linux, macOS, Windows). It can parse
//! profiles.ini to find profile directories and determine the default profile.
//!
//! # Example
//!
//! ```rust,no_run
//! use ffcv::{find_profile_path, list_profiles};
//!
//! // List all available Firefox profiles
//! let profiles = list_profiles(None)?;
//! for profile in profiles {
//!     println!("{}: {}", profile.name, profile.path.display());
//! }
//!
//! // Find a specific profile
//! let profile_path = find_profile_path("default", None)?;
//! println!("Default profile: {}", profile_path.display());
//! # Ok::<(), ffcv::Error>(())
//! ```

use crate::error::{Error, Result};
use serde::Serialize;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Firefox profile information parsed from profiles.ini
#[derive(Debug, Clone)]
struct FirefoxProfile {
    name: String,
    path: PathBuf,
    is_relative: bool,
    is_default: bool,
}

/// Public profile information for listing
#[derive(Debug, Serialize)]
pub struct ProfileInfo {
    pub name: String,
    pub path: PathBuf,
    pub is_default: bool,
    pub is_relative: bool,
    pub locked_to_install: Option<String>,
}

/// Find the Firefox profile directory based on the profile name
///
/// This function locates a Firefox profile by name. It parses profiles.ini
/// and supports Firefox 67+ install sections.
///
/// # Arguments
///
/// * `profile_name` - Name of the profile to find (e.g., "default")
/// * `profiles_dir_opt` - Optional custom profiles directory path
///
/// # Example
///
/// ```rust,no_run
/// use ffcv::find_profile_path;
///
/// let profile_path = find_profile_path("default", None)?;
/// println!("Profile path: {}", profile_path.display());
/// # Ok::<(), ffcv::Error>(())
/// ```
pub fn find_profile_path(
    profile_name: &str,
    profiles_dir_opt: Option<&std::path::Path>,
) -> Result<PathBuf> {
    let profiles_dir = get_profiles_directory(profiles_dir_opt)?;
    let profiles_ini = profiles_dir.join("profiles.ini");

    // Try parsing profiles.ini first (primary method)
    if profiles_ini.exists() {
        if let Ok(profiles) = parse_profiles_ini(&profiles_ini) {
            // First try exact name match
            if let Some(profile) = profiles.iter().find(|p| p.name == profile_name) {
                let full_path = if profile.is_relative {
                    profiles_dir.join(&profile.path)
                } else {
                    profile.path.clone()
                };

                if full_path.exists() {
                    return Ok(full_path);
                }
            }

            // If no exact match and profile_name is "default", try to find the default
            // profile for the current Firefox installation (Firefox 67+)
            if profile_name == "default" {
                if let Ok(default_profile) = get_default_profile_for_install(
                    &profiles_ini,
                    profiles_dir.as_path(),
                    &profiles,
                ) {
                    return Ok(default_profile);
                }

                // Fallback: use the profile marked as default in profiles.ini
                if let Some(profile) = profiles.iter().find(|p| p.is_default) {
                    let full_path = if profile.is_relative {
                        profiles_dir.join(&profile.path)
                    } else {
                        profile.path.clone()
                    };

                    if full_path.exists() {
                        return Ok(full_path);
                    }
                }
            }
        }
    }

    // Fallback: Directory scanning with improved matching
    scan_profiles_directory(&profiles_dir, profile_name)
}

/// Parse profiles.ini to extract profile information
fn parse_profiles_ini(ini_path: &PathBuf) -> Result<Vec<FirefoxProfile>> {
    use configparser::ini::Ini;

    let mut ini = Ini::new();
    let content = std::fs::read_to_string(ini_path).map_err(|e| {
        Error::ProfilesIniParse(format!(
            "Failed to read profiles.ini from {}: {}",
            ini_path.display(),
            e
        ))
    })?;

    // Parse the INI file (configparser handles UTF-8 BOM automatically)
    if let Err(e) = ini.read(content) {
        return Err(Error::ProfilesIniParse(format!(
            "Failed to parse profiles.ini: {}",
            e
        )));
    }

    let mut profiles = Vec::new();

    // Get all section names
    let sections = ini.sections();

    for sec_name in sections {
        // Only process profilen sections (e.g., profile0, profile1, etc.)
        // Note: configparser converts section names to lowercase
        if sec_name.to_lowercase().starts_with("profile") {
            let name = ini.get(&sec_name, "Name").unwrap_or_default();
            let path_str = ini.get(&sec_name, "Path").unwrap_or_default();
            let is_relative = ini
                .getuint(&sec_name, "IsRelative")
                .ok()
                .flatten()
                .unwrap_or(1)
                == 1;
            let is_default = ini
                .getuint(&sec_name, "Default")
                .ok()
                .flatten()
                .unwrap_or(0)
                == 1;

            if !name.is_empty() && !path_str.is_empty() {
                profiles.push(FirefoxProfile {
                    name,
                    path: PathBuf::from(path_str),
                    is_relative,
                    is_default,
                });
            }
        }
    }

    Ok(profiles)
}

/// Parse install sections from profiles.ini (Firefox 67+)
/// Returns HashMap<install_hash, default_profile_path>
fn parse_installs_ini(ini_path: &PathBuf) -> Result<HashMap<String, String>> {
    use configparser::ini::Ini;

    let mut ini = Ini::new();
    let content = std::fs::read_to_string(ini_path)?;

    if let Err(e) = ini.read(content) {
        return Err(Error::ProfilesIniParse(format!(
            "Failed to parse profiles.ini: {}",
            e
        )));
    }

    let mut installs = HashMap::new();

    // Get all section names
    let sections = ini.sections();

    for sec_name in sections {
        // Install sections are named with hash (e.g., [308046B0AF4A39CB])
        // They don't start with "profile" or "general"
        // Note: configparser converts section names to lowercase
        let sec_lower = sec_name.to_lowercase();
        if !sec_lower.starts_with("profile") && sec_lower != "general" {
            if let Some(default_profile) = ini.get(&sec_name, "Default") {
                installs.insert(sec_name, default_profile);
            }
        }
    }

    Ok(installs)
}

/// Get the default profile for the current Firefox installation (Firefox 67+)
fn get_default_profile_for_install(
    ini_path: &PathBuf,
    profiles_dir: &Path,
    profiles: &[FirefoxProfile],
) -> Result<PathBuf> {
    use configparser::ini::Ini;

    // Try to find the Firefox installation path
    let firefox_path = get_firefox_install_path()?;

    // Hash the installation path to get the install section name
    // Firefox uses a simple hash of the installation path
    let install_hash = hash_install_path(&firefox_path);

    // Parse profiles.ini to get the install mapping
    let mut ini = Ini::new();
    let content = std::fs::read_to_string(ini_path)?;

    if let Err(e) = ini.read(content) {
        return Err(Error::ProfilesIniParse(format!(
            "Failed to parse profiles.ini: {}",
            e
        )));
    }

    // Look for the install section with the matching hash
    let sections = ini.sections();
    for sec_name in sections {
        let sec_lower = sec_name.to_lowercase();
        // Check if this is an install section and if the hash matches
        if !sec_lower.starts_with("profile")
            && sec_lower != "general"
            && sec_lower == install_hash.to_lowercase()
        {
            // Found the install section! Get the default profile
            if let Some(default_profile_path) = ini.get(&sec_name, "Default") {
                // Find the profile with this path
                if let Some(profile) = profiles.iter().find(|p| {
                    p.path.to_string_lossy() == default_profile_path
                        || p.path.to_string_lossy() == format!("Profiles/{}", default_profile_path)
                }) {
                    let full_path = if profile.is_relative {
                        profiles_dir.join(&profile.path)
                    } else {
                        profile.path.clone()
                    };

                    if full_path.exists() {
                        return Ok(full_path);
                    }
                }

                // If profile not found in list, try to construct path directly
                let profile_path = PathBuf::from(&default_profile_path);
                let full_path = if profile_path.is_absolute() {
                    profile_path
                } else {
                    profiles_dir.join(&profile_path)
                };

                if full_path.exists() {
                    return Ok(full_path);
                }
            }
        }
    }

    // No install section found for this installation
    Err(Error::ProfileNotFound {
        name: "default".to_string(),
        directory: firefox_path,
    })
}

/// Get the path to the Firefox executable
fn get_firefox_install_path() -> Result<PathBuf> {
    use std::env;

    // Try to find Firefox in PATH
    if let Ok(path) = env::var("FIREFOX_BIN") {
        return Ok(PathBuf::from(path));
    }

    // Try common locations
    #[cfg(target_os = "linux")]
    {
        let common_paths = [
            "/usr/bin/firefox",
            "/usr/lib/firefox/firefox",
            "/snap/bin/firefox",
            "/opt/firefox/firefox",
        ];

        for path in &common_paths {
            if PathBuf::from(path).exists() {
                return Ok(PathBuf::from(path));
            }
        }
    }

    #[cfg(target_os = "macos")]
    {
        let common_paths = [
            "/Applications/Firefox.app/Contents/MacOS/firefox",
            "/Applications/Firefox Developer Edition.app/Contents/MacOS/firefox",
        ];

        for path in &common_paths {
            if PathBuf::from(path).exists() {
                return Ok(PathBuf::from(path));
            }
        }
    }

    #[cfg(target_os = "windows")]
    {
        if let Ok(program_files) = env::var("PROGRAMFILES") {
            let common_paths = [
                format!("{}/Mozilla Firefox/firefox.exe", program_files),
                format!("{}\\Mozilla Firefox\\firefox.exe", program_files),
            ];

            for path in &common_paths {
                if PathBuf::from(path).exists() {
                    return Ok(PathBuf::from(path));
                }
            }
        }
    }

    // Fallback: try to use 'which' or 'where' command
    #[cfg(target_os = "linux")]
    {
        if let Ok(output) = std::process::Command::new("which").arg("firefox").output() {
            if output.status.success() {
                let path_str = String::from_utf8_lossy(&output.stdout);
                let path = path_str.trim();
                if !path.is_empty() {
                    return Ok(PathBuf::from(path));
                }
            }
        }
    }

    #[cfg(target_os = "windows")]
    {
        if let Ok(output) = std::process::Command::new("where")
            .arg("firefox.exe")
            .output()
        {
            if output.status.success() {
                let path_str = String::from_utf8_lossy(&output.stdout);
                let path = path_str.lines().next().unwrap_or("").trim();
                if !path.is_empty() {
                    return Ok(PathBuf::from(path));
                }
            }
        }
    }

    Err(Error::ProfileNotFound {
        name: "Firefox".to_string(),
        directory: PathBuf::from("unknown"),
    })
}

/// Hash the installation path to get the install section name
/// Firefox uses CityHash64 to hash the installation directory path
fn hash_install_path(path: &Path) -> String {
    use cityhasher::hash;

    // Resolve symlinks first (critical for NixOS and similar systems)
    let resolved = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());

    // Get parent directory (installation directory)
    // Firefox hashes the installation directory, not the binary path
    let install_dir = resolved.parent().unwrap_or(&resolved);

    // Normalize path string
    let path_str = install_dir.to_string_lossy();

    // Hash with CityHash64 and format as 16 uppercase hex chars
    let hash: u64 = hash(path_str.as_bytes());
    format!("{:016X}", hash)
}

/// Improved fallback: Scan profiles directory with better matching strategies
fn scan_profiles_directory(profiles_dir: &PathBuf, profile_name: &str) -> Result<PathBuf> {
    let entries = std::fs::read_dir(profiles_dir).map_err(Error::Io)?;

    let mut matches: Vec<PathBuf> = Vec::new();

    for entry in entries {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            let dir_name = path.file_name().and_then(|s| s.to_str()).unwrap_or("");

            // Strategy 1: Exact match (uncommon but possible)
            if dir_name == profile_name {
                return Ok(path);
            }

            // Strategy 2: Firefox standard naming pattern (xxxxxxxx.name)
            if dir_name.ends_with(&format!(".{}", profile_name)) {
                matches.push(path);
            }
        }
    }

    // If exactly one match, use it
    if matches.len() == 1 {
        return Ok(matches.into_iter().next().unwrap());
    }

    // If multiple matches, return error listing them
    if matches.len() > 1 {
        let match_names: Vec<&str> = matches
            .iter()
            .filter_map(|p| p.file_name().and_then(|s| s.to_str()))
            .collect();

        return Err(Error::ProfilesIniParse(format!(
            "Multiple profiles match '{}': {}. \
             Please specify the exact profile name from 'ffcv --list' \
             or use the full directory name.",
            profile_name,
            match_names.join(", ")
        )));
    }

    // No matches
    Err(Error::ProfileNotFound {
        name: profile_name.to_string(),
        directory: profiles_dir.clone(),
    })
}

/// List all available Firefox profiles
pub fn list_profiles(profiles_dir_opt: Option<&std::path::Path>) -> Result<Vec<ProfileInfo>> {
    let profiles_dir = get_profiles_directory(profiles_dir_opt)?;
    let profiles_ini = profiles_dir.join("profiles.ini");

    if !profiles_ini.exists() {
        return Err(Error::ProfilesIniParse(format!(
            "profiles.ini not found at {}. \
             Firefox may not be installed or this is not a standard Firefox setup.",
            profiles_ini.display()
        )));
    }

    let profiles = parse_profiles_ini(&profiles_ini)?;
    let installs = parse_installs_ini(&profiles_ini)?;

    // Merge profile info with install locks
    let profile_infos: Vec<ProfileInfo> = profiles
        .into_iter()
        .map(|p| {
            // Resolve relative paths to absolute paths
            let full_path = if p.is_relative {
                profiles_dir.join(&p.path)
            } else {
                p.path.clone()
            };

            let path_string = p.path.to_string_lossy().to_string();
            let locked_to = installs
                .iter()
                .find(|(_, default_path)| *default_path == &path_string)
                .map(|(hash, _)| hash.clone());

            ProfileInfo {
                name: p.name,
                path: full_path,
                is_default: p.is_default,
                is_relative: p.is_relative,
                locked_to_install: locked_to,
            }
        })
        .collect();

    Ok(profile_infos)
}

/// Get the profiles directory path from CLI, env var, or auto-detection
///
/// Priority:
/// 1. Manual path provided via CLI or parameter
/// 2. MOZ_PROFILES_DIR environment variable
/// 3. Auto-detection based on OS
pub fn get_profiles_directory(manual_path: Option<&std::path::Path>) -> Result<PathBuf> {
    // Priority 1: Use manually specified path (from CLI or direct parameter)
    if let Some(path) = manual_path {
        return validate_and_use_profiles_dir(path);
    }

    // Priority 2: Check MOZ_PROFILES_DIR environment variable
    if let Ok(env_path) = std::env::var("MOZ_PROFILES_DIR") {
        let path = PathBuf::from(env_path);
        return validate_and_use_profiles_dir(&path);
    }

    // Priority 3: Auto-detect based on OS
    auto_detect_profiles_directory()
}

/// Validate and return the profiles directory path
fn validate_and_use_profiles_dir(path: &std::path::Path) -> Result<PathBuf> {
    // Check if path exists
    if !path.exists() {
        return Err(Error::ProfilesIniParse(format!(
            "Profiles directory does not exist: {}\n\
             Please verify the path and try again.",
            path.display()
        )));
    }

    // Check if path is a directory
    if !path.is_dir() {
        return Err(Error::ProfilesIniParse(format!(
            "Profiles directory path is not a directory: {}\n\
             Please provide a directory path, not a file.",
            path.display()
        )));
    }

    Ok(path.to_path_buf())
}

/// Auto-detect profiles directory based on operating system
fn auto_detect_profiles_directory() -> Result<PathBuf> {
    #[cfg(target_os = "linux")]
    {
        let home = std::env::var("HOME").map_err(|_| {
            Error::ProfilesIniParse("HOME environment variable not set".to_string())
        })?;
        Ok(PathBuf::from(home).join(".mozilla/firefox"))
    }

    #[cfg(target_os = "macos")]
    {
        let home = std::env::var("HOME").map_err(|_| {
            Error::ProfilesIniParse("HOME environment variable not set".to_string())
        })?;
        Ok(PathBuf::from(home).join("Library/Application Support/Firefox"))
    }

    #[cfg(target_os = "windows")]
    {
        let appdata = std::env::var("APPDATA").map_err(|_| {
            Error::ProfilesIniParse("APPDATA environment variable not set".to_string())
        })?;
        Ok(PathBuf::from(appdata).join("Mozilla/Firefox"))
    }

    #[cfg(not(any(target_os = "linux", target_os = "macos", target_os = "windows")))]
    {
        Err(Error::ProfilesIniParse(
            "Unsupported operating system".to_string(),
        ))
    }
}

/// Get the path to prefs.js for a given profile
pub fn get_prefs_path(profile_path: &std::path::Path) -> PathBuf {
    profile_path.join("prefs.js")
}

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

    #[test]
    fn test_get_prefs_path() {
        let profile_path = PathBuf::from("/home/user/.mozilla/firefox/test.default");
        let prefs_path = get_prefs_path(&profile_path);
        assert_eq!(
            prefs_path,
            PathBuf::from("/home/user/.mozilla/firefox/test.default/prefs.js")
        );
    }

    #[test]
    fn test_parse_valid_profiles_ini() {
        let ini_content = r#"
[General]
StartWithLastProfile=1
Version=2

[Profile0]
Name=default
IsRelative=1
Path=Profiles/abcdefgh.default
Default=1

[Profile1]
Name=work
IsRelative=1
Path=Profiles/work.profile
Default=0
"#;

        let mut ini = configparser::ini::Ini::new();
        ini.read(ini_content.to_string()).unwrap();

        // Verify General section
        assert_eq!(
            ini.get("General", "StartWithLastProfile"),
            Some("1".to_string())
        );

        // Verify Profile0 section
        assert_eq!(ini.get("Profile0", "Name"), Some("default".to_string()));
        assert_eq!(
            ini.get("Profile0", "Path"),
            Some("Profiles/abcdefgh.default".to_string())
        );
    }

    #[test]
    fn test_nixos_profile_name_mismatch() {
        // Test the scenario where profile name doesn't match directory name
        // This happens on NixOS with home-manager
        let ini_content = r#"
[General]
StartWithLastProfile=1

[Profile0]
Name=darkcodi
IsRelative=1
Path=default
Default=1
"#;

        let mut ini = configparser::ini::Ini::new();
        ini.read(ini_content.to_string()).unwrap();

        assert_eq!(ini.get("Profile0", "Name"), Some("darkcodi".to_string()));
        assert_eq!(ini.get("Profile0", "Path"), Some("default".to_string()));

        // Verify IsRelative is parsed correctly
        let is_relative = ini
            .get("Profile0", "IsRelative")
            .and_then(|v| v.parse::<u32>().ok())
            .unwrap_or(1);
        assert_eq!(is_relative, 1);
    }

    #[test]
    fn test_parse_installs_section() {
        // Test Firefox 67+ install section parsing
        let ini_content = r#"
[General]
StartWithLastProfile=1

[Profile0]
Name=default
IsRelative=1
Path=Profiles/abcdefgh.default
Default=1

[308046B0AF4A39CB]
Default=Profiles/abcdefgh.default
Locked=1
"#;

        let mut ini = configparser::ini::Ini::new();
        ini.read(ini_content.to_string()).unwrap();

        // Verify install section (hash based section name)
        assert_eq!(
            ini.get("308046B0AF4A39CB", "Default"),
            Some("Profiles/abcdefgh.default".to_string())
        );
        assert_eq!(ini.get("308046B0AF4A39CB", "Locked"), Some("1".to_string()));
    }

    #[test]
    fn test_profiles_dir_validation_nonexistent() {
        // Test validation with non-existent path
        let result = get_profiles_directory(Some(std::path::Path::new("/nonexistent/path")));
        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("does not exist"));
        assert!(error_msg.contains("/nonexistent/path"));
    }

    #[test]
    fn test_profiles_dir_validation_file_not_directory() {
        // Test validation when path is a file, not a directory
        let temp_file = tempfile::NamedTempFile::new().unwrap();
        let result = get_profiles_directory(Some(temp_file.path()));
        assert!(result.is_err());
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("not a directory"));
    }

    #[test]
    fn test_profiles_dir_validation_valid_directory() {
        // Test validation with a valid temporary directory
        let temp_dir = tempfile::TempDir::new().unwrap();
        let result = get_profiles_directory(Some(temp_dir.path()));
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), temp_dir.path());
    }
}