Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use $XDG_STATE_HOME to store history.db #349

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
77 changes: 65 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ debug = true
chrono = "0.4"
csv = "1"
humantime = "2.1"
directories-next = "2.0"
etcetera = "0.8"
itertools = "0.10"
libc = "0.2"
rand = "0.8"
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ Note that only single-character-prompts are allowed. setting `MCFLY_PROMPT` to `

### Database Location

McFly stores its SQLite database in the standard location for the OS. On OS X, this is in `~/Library/Application Support/McFly` and on Linux it is in `$XDG_DATA_DIR/mcfly/history.db` (default would be `~/.local/share/mcfly/history.db`). For legacy support, if `~/.mcfly/` exists, it is used instead.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think instead of migrating, it should keep using the old location if it exists.

McFly stores its SQLite database & cache according to the [`XDG Base Directory Specification`](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
i.e. in `$XDG_STATE_DIR/mcfly/history.db` (default would be `~/.local/state/mcfly/history.db`).
For legacy support, if `~/.mcfly/history.db` or `$XDG_DATA_HOME/mcfly/history.db` or `~/Library/Application Support/McFly/history.db` (on macOS) exists,
then it will automatically be migrated to the above location.

### Slow startup

Expand Down
11 changes: 4 additions & 7 deletions src/history/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl History {
let history = if db_path.exists() {
History::from_db_path(db_path)
} else {
History::from_shell_history(history_format)
History::from_shell_history(db_path, history_format)
};
schema::migrate(&history.connection);
history
Expand Down Expand Up @@ -755,8 +755,8 @@ impl History {
}
}

fn from_shell_history(history_format: HistoryFormat) -> History {
print!(
fn from_shell_history(mcfly_db_path: PathBuf, history_format: HistoryFormat) -> History {
println!(
"McFly: Importing shell history for the first time. This may take a minute or two..."
);
io::stdout()
Expand All @@ -767,14 +767,11 @@ impl History {
let commands =
shell_history::full_history(&shell_history::history_file_path(), history_format);

// Use ~/.mcfly if it already exists, or create 'mcfly' folder in XDG_DATA_DIR
let mcfly_db_path = Settings::mcfly_db_path();
// Create 'mcfly' folder in XDG_STATE_DIR, if it doesn't exit
let mcfly_db_dir = mcfly_db_path.parent().unwrap();

fs::create_dir_all(mcfly_db_dir)
.unwrap_or_else(|_| panic!("Unable to create {:?}", mcfly_db_dir));

// Make ~/.mcfly/history.db
let mut connection = Connection::open(&mcfly_db_path)
.unwrap_or_else(|_| panic!("Unable to create history DB at {:?}", &mcfly_db_path));

Expand Down
70 changes: 46 additions & 24 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::cli::{Cli, SubCommand};
use crate::shell_history;
use clap::Parser;
use directories_next::{ProjectDirs, UserDirs};
use std::env;
use std::path::PathBuf;
use etcetera::base_strategy::Xdg;
use etcetera::BaseStrategy;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::time::SystemTime;
use std::time::UNIX_EPOCH;
use std::{env, fs};

#[derive(Debug)]
pub enum Mode {
Expand Down Expand Up @@ -372,35 +373,56 @@ impl Settings {
settings
}

// Use ~/.mcfly only if it already exists, otherwise create 'mcfly' folder in XDG_CACHE_DIR
// Use or create the 'mcfly' folder in `$XDG_CACHE_DIR`
pub fn mcfly_training_cache_path() -> PathBuf {
let cache_dir = Settings::mcfly_xdg_dir().cache_dir().to_path_buf();

Settings::mcfly_base_path(cache_dir).join(PathBuf::from("training-cache.v1.csv"))
Settings::xdg_base_strategy()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between mcfly_xdg_dir and xdg_base_strategy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could reuse the name if you want.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just didn't understand what the difference is between them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mcfly_xdg_dir returned a ProjectDirs from the directories crate. xdg_base_strategy base directory returns an Xdg struct from etcetera crate which is the counterpart to it.

.cache_dir()
.join(PathBuf::from("mcfly/training-cache.v1.csv"))
}

// Use ~/.mcfly only if it already exists, otherwise create 'mcfly' folder in XDG_DATA_DIR
// Use or migrate to the 'mcfly' folder in `$XDG_STATE_DIR`
pub fn mcfly_db_path() -> PathBuf {
let data_dir = Settings::mcfly_xdg_dir().data_dir().to_path_buf();

Settings::mcfly_base_path(data_dir).join(PathBuf::from("history.db"))
}

fn mcfly_xdg_dir() -> ProjectDirs {
ProjectDirs::from("", "", "McFly").unwrap()
let basedirs = Settings::xdg_base_strategy();
let path = basedirs
.state_dir()
.unwrap()
.join(PathBuf::from("mcfly/history.db"));
if !path.exists() {
println!("McFly: history.db not found: `{}`", path.display());
let old = basedirs.data_dir().join(PathBuf::from("mcfly/history.db"));
if !Settings::migrate_old_path(old, &path) {
let old = basedirs.home_dir().join(PathBuf::from(".mcfly/history.db"));
if !Settings::migrate_old_path(old, &path) {
#[cfg(target_os = "macos")]
{
let old = etcetera::base_strategy::Apple::new()
.unwrap()
.data_dir()
.join(PathBuf::from("McFly/history.db"));
Settings::migrate_old_path(old, &path);
}
}
}
}
path
}

fn mcfly_base_path(base_dir: PathBuf) -> PathBuf {
Settings::mcfly_dir_in_home().unwrap_or(base_dir)
fn xdg_base_strategy() -> impl BaseStrategy {
Xdg::new().unwrap()
}

fn mcfly_dir_in_home() -> Option<PathBuf> {
let user_dirs_file = UserDirs::new()
.unwrap()
.home_dir()
.join(PathBuf::from(".mcfly"));

user_dirs_file.exists().then_some(user_dirs_file)
fn migrate_old_path(old: PathBuf, new: &Path) -> bool {
println!("McFly: Checking old history.db: `{}`", old.display());
let ret = old.exists();
if ret {
fs::create_dir_all(new.parent().unwrap()).unwrap();
fs::copy(&old, new).unwrap();
println!(
"McFly: history.db migrated.\nYou can now delete the old directory: `rm -r '{}'`",
old.parent().unwrap().display()
);
}
ret
}
}

Expand Down