Skip to content

A fast file manager for Windows and macOS that never freezes: tabs, dual panes, archives as folders, Quick Look-style previews. A File Explorer and Finder alternative written in Rust.

9.8 KB
Raw Blame
//! The portable copy keeps its settings next to itself.
//!
//! WHY (issue 71: "The portable version should save the settings in the same folder as the executable and not inside the system's
//! AppData/Roaming folder"). Velopack's portable zip is a folder with `Update.exe`, a `.portable` file, and the program in `current\`;
//! an update replaces `current\` and nothing else. So the data goes into `data\` beside `current\`: it moves with the folder (a USB
//! stick, another PC) and survives updates.
//!
//! The first time the folder is made, the files the program had kept in `%APPDATA%\rustyfiles` until now (its settings, layouts,
//! sessions) are COPIED into it, so a portable copy that ran before keeps what it had. Folders (the file index, the jobs) are not
//! copied: they are made again. Nothing is moved or deleted in AppData.
//!
//! Scoop (issue 459: an update from 1.0.3 to 1.0.4 "reset" the settings): it unpacks every version into a folder of its own,
//! `scoop\apps\rustyfiles\<version>\`, so each version found no `data\` and started from AppData's old copy. When the folder has
//! no settings yet, the newest `data\` of a sibling folder (the previous version's) is taken first. That also covers Scoop's
//! `persist`, which hands a new version an empty `data\` the first time.

use std::path::{Path, PathBuf};
use std::sync::OnceLock;

/// The data folder of a portable copy started from `exe`: `<root>\data` when `exe` is in `<root>\current` and `<root>\.portable`
/// exists (`is_file` says whether a path is a file).
pub fn data_dir_for(exe: &Path, is_file: &dyn Fn(&Path) -> bool) -> Option<PathBuf> {
    let current = exe.parent()?;
    if !current.file_name()?.to_string_lossy().eq_ignore_ascii_case("current") {
        return None;
    }
    let root = current.parent()?;
    is_file(&root.join(".portable")).then(|| root.join("data"))
}

/// A file bigger than this is not copied from AppData (none of the settings files comes near it).
const COPY_AT_MOST: u64 = 20 << 20;

/// Copies the files (not the folders) of `from` into `to`, which is made. A file `to` already has is never replaced.
fn first_fill(from: &Path, to: &Path) {
    if std::fs::create_dir_all(to).is_err() {
        return;
    }
    let Ok(rd) = std::fs::read_dir(from) else { return };
    for e in rd.flatten() {
        let Ok(meta) = e.metadata() else { continue };
        if meta.is_file() && meta.len() <= COPY_AT_MOST && !to.join(e.file_name()).exists() {
            let _ = std::fs::copy(e.path(), to.join(e.file_name()));
        }
    }
}

/// The `data` folder with the newest `settings.toml` among the folders beside `root` (other versions of a Scoop install), not
/// `root`'s own and not one that is the same folder under another name (Scoop's `current` link).
fn newest_sibling_data(root: &Path) -> Option<PathBuf> {
    let parent = root.parent()?;
    let own = std::fs::canonicalize(root.join("data")).ok();
    let mut best: Option<(std::time::SystemTime, PathBuf)> = None;
    for e in std::fs::read_dir(parent).ok()?.flatten() {
        let data = e.path().join("data");
        if e.path() == root || !e.path().join(".portable").is_file() {
            continue;
        }
        if own.is_some() && std::fs::canonicalize(&data).ok() == own {
            continue;
        }
        let Ok(t) = std::fs::metadata(data.join("settings.toml")).and_then(|m| m.modified()) else { continue };
        if best.as_ref().is_none_or(|(b, _)| t > *b) {
            best = Some((t, data));
        }
    }
    best.map(|(_, d)| d)
}

/// Fills `dir` (the portable copy's data folder, which has no settings yet): from the newest sibling version's data, else from AppData.
fn fill_new(root: &Path, dir: &Path) {
    if let Some(prev) = newest_sibling_data(root) {
        log::info!("portable: settings taken from {}", prev.display());
        first_fill(&prev, dir);
    } else if let Some(old) = std::env::var_os("APPDATA").map(|a| PathBuf::from(a).join("rustyfiles")) {
        first_fill(&old, dir);
    }
}

/// This program's data folder when it runs as the portable copy (made, and filled the first time: see the module notes), else `None`.
pub fn data_dir() -> Option<PathBuf> {
    static DIR: OnceLock<Option<PathBuf>> = OnceLock::new();
    DIR.get_or_init(|| {
        let exe = std::env::current_exe().ok()?;
        let dir = data_dir_for(&exe, &|p| p.is_file())?;
        if !dir.join("settings.toml").exists() {
            if let Some(root) = dir.parent() {
                fill_new(root, &dir);
            }
            let _ = std::fs::create_dir_all(&dir);
        }
        Some(dir)
    })
    .clone()
}

/// The portable copy's own launcher, `<root>\rustyfiles.exe`: Velopack's stub beside `current\`, which an update leaves where it is.
/// `None` for an installed copy, or a portable folder without one.
pub fn launcher() -> Option<PathBuf> {
    data_dir()?;
    let exe = std::env::current_exe().ok()?;
    let stub = exe.parent()?.parent()?.join(exe.file_name()?);
    stub.is_file().then_some(stub)
}

/// Gives a window of the portable copy what the taskbar pins: the launcher ([`launcher`]), not the running program.
///
/// WHY (issue 184: "with each update the pinning goes away", on the portable copy). A pinned window is pinned as its program,
/// `current\rustyfiles.exe`; an update replaces `current\`, and Windows drops a pin whose file went away. The installed copy has
/// Velopack's Start menu shortcut, which the taskbar pins instead (same AppUserModelID); the portable copy has none. With the
/// window's relaunch command set to the launcher, the pin is a shortcut to the launcher, which every update keeps.
pub fn mark_window(hwnd: isize, app_id: &str) {
    let Some(stub) = launcher() else { return };
    let Ok(exe) = std::env::current_exe() else { return };
    use windows::Win32::Foundation::HWND;
    use windows::Win32::Storage::EnhancedStorage::{
        PKEY_AppUserModel_ID, PKEY_AppUserModel_RelaunchCommand, PKEY_AppUserModel_RelaunchDisplayNameResource,
        PKEY_AppUserModel_RelaunchIconResource,
    };
    use windows::Win32::System::Com::StructuredStorage::PROPVARIANT;
    use windows::Win32::UI::Shell::PropertiesSystem::{IPropertyStore, SHGetPropertyStoreForWindow};
    let command = format!("\"{}\"", stub.display());
    let icon = format!("{},0", exe.display());
    let set = || -> windows::core::Result<()> {
        let store: IPropertyStore = unsafe { SHGetPropertyStoreForWindow(HWND(hwnd as *mut _))? };
        unsafe {
            store.SetValue(&PKEY_AppUserModel_ID, &PROPVARIANT::from(app_id))?;
            store.SetValue(&PKEY_AppUserModel_RelaunchCommand, &PROPVARIANT::from(command.as_str()))?;
            store.SetValue(&PKEY_AppUserModel_RelaunchDisplayNameResource, &PROPVARIANT::from("rustyfiles"))?;
            store.SetValue(&PKEY_AppUserModel_RelaunchIconResource, &PROPVARIANT::from(icon.as_str()))?;
            store.Commit()
        }
    };
    if let Err(e) = set() {
        log::warn!("portable: the window's relaunch command could not be set: {e}");
    }
}

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

    #[test]
    fn only_a_copy_in_a_portable_folder_has_a_data_folder_of_its_own() {
        let marker = |root: &'static str| move |p: &Path| p == Path::new(root).join(".portable");
        let exe = Path::new(r"E:\Tools\rustyfiles\current\rustyfiles.exe");
        assert_eq!(data_dir_for(exe, &marker(r"E:\Tools\rustyfiles")), Some(PathBuf::from(r"E:\Tools\rustyfiles\data")));
        assert_eq!(data_dir_for(exe, &|_| false), None, "an installed copy has no .portable beside current");
        assert_eq!(data_dir_for(Path::new(r"E:\build\rustyfiles.exe"), &|_| true), None, "a build is not in current");
    }

    #[test]
    fn the_first_fill_copies_the_files_and_leaves_the_folders() {
        let base = std::env::temp_dir().join(format!("rf-portable-{}", std::process::id()));
        let (from, to) = (base.join("appdata"), base.join("data"));
        std::fs::create_dir_all(from.join("index")).unwrap();
        std::fs::write(from.join("settings.toml"), b"theme = \"dark\"").unwrap();
        std::fs::write(from.join("index").join("c.idx"), b"big").unwrap();
        first_fill(&from, &to);
        assert_eq!(std::fs::read(to.join("settings.toml")).unwrap(), b"theme = \"dark\"");
        assert!(!to.join("index").exists());
        assert!(from.join("settings.toml").exists(), "nothing leaves AppData");
        let _ = std::fs::remove_dir_all(base);
    }

    /// Scoop's layout (issue 459): a new version folder takes the settings of the newest other version, not AppData's.
    #[test]
    fn a_new_scoop_version_takes_the_previous_versions_settings() {
        let apps = std::env::temp_dir().join(format!("rf-scoop-{}", std::process::id()));
        let version = |v: &str, settings: Option<&str>| {
            let root = apps.join(v);
            std::fs::create_dir_all(root.join("current")).unwrap();
            std::fs::write(root.join(".portable"), b"").unwrap();
            if let Some(t) = settings {
                std::fs::create_dir_all(root.join("data")).unwrap();
                std::fs::write(root.join("data").join("settings.toml"), t).unwrap();
            }
            root
        };
        version("1.0.2", Some("theme = \"light\""));
        std::thread::sleep(std::time::Duration::from_millis(30));
        version("1.0.3", Some("theme = \"dark\""));
        std::fs::create_dir_all(apps.join("1.0.3").join("data").join("index")).unwrap();
        let new = version("1.0.4", None);
        // (a folder that is not a portable copy is no source)
        std::fs::create_dir_all(apps.join("other").join("data")).unwrap();
        assert_eq!(newest_sibling_data(&new), Some(apps.join("1.0.3").join("data")));
        fill_new(&new, &new.join("data"));
        assert_eq!(std::fs::read(new.join("data").join("settings.toml")).unwrap(), b"theme = \"dark\"");
        assert!(!new.join("data").join("index").exists(), "files only");
        let _ = std::fs::remove_dir_all(apps);
    }
}