mirror of
https://github.com/Llewellynvdm/zoxide.git
synced 2024-11-17 02:25:15 +00:00
Rework database fallback for v0.3 (#47)
Support migration from old database
This commit is contained in:
parent
d4c8297f9b
commit
9af0251bd6
@ -116,9 +116,12 @@ NOTE: There is no PWD hook provided for POSIX shells.
|
|||||||
|
|
||||||
### Environment variables
|
### Environment variables
|
||||||
|
|
||||||
- `$_ZO_DATA`: sets the location of the database (default: `~/.zo`)
|
- `$_ZO_DATA_DIR`: directory where `zoxide` will store its data files (default:
|
||||||
|
platform-specific; see the [`dirs` documentation] for more information)
|
||||||
- `$_ZO_ECHO`: `z` will print the matched directory before navigating to it
|
- `$_ZO_ECHO`: `z` will print the matched directory before navigating to it
|
||||||
- `$_ZO_EXCLUDE_DIRS`: list of directories separated by platform-specific
|
- `$_ZO_EXCLUDE_DIRS`: list of directories separated by platform-specific
|
||||||
characters (`:` on Linux and macOS, and `;` on Windows) to be excluded from
|
characters (`:` on Linux and macOS, and `;` on Windows) to be excluded from
|
||||||
the database
|
the database
|
||||||
- `$_ZO_MAXAGE`: sets the maximum total rank after which entries start getting deleted
|
- `$_ZO_MAXAGE`: sets the maximum total rank after which entries start getting deleted
|
||||||
|
|
||||||
|
[`dirs` documentation]: https://docs.rs/dirs/latest/dirs/fn.data_local_dir.html
|
||||||
|
@ -8,27 +8,26 @@ use std::fs;
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub const DB_MAX_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB
|
pub const DB_MAX_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB
|
||||||
|
|
||||||
pub const DB_VERSION: DBVersion = 3;
|
pub const DB_VERSION: DBVersion = 3;
|
||||||
|
|
||||||
pub fn zo_data() -> Result<PathBuf> {
|
pub fn zo_data_dir() -> Result<PathBuf> {
|
||||||
let path = match env::var_os("_ZO_DATA") {
|
let data_dir = match env::var_os("_ZO_DATA_DIR") {
|
||||||
Some(data_osstr) => PathBuf::from(data_osstr),
|
Some(data_osstr) => PathBuf::from(data_osstr),
|
||||||
None => {
|
None => {
|
||||||
if let Some(mut cache_dir) = dirs::cache_dir() {
|
if let Some(mut data_dir) = dirs::data_local_dir() {
|
||||||
cache_dir.push("zoxide");
|
data_dir.push("zoxide");
|
||||||
cache_dir
|
data_dir
|
||||||
} else if let Some(mut home_dir) = dirs::home_dir() {
|
|
||||||
home_dir.push(".zoxide");
|
|
||||||
home_dir
|
|
||||||
} else {
|
} else {
|
||||||
bail!("could not generate default directory, please set _ZO_DATA manually");
|
bail!("could not find database directory, please set _ZO_DATA_DIR manually");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fs::create_dir_all(&path).context("could not create _ZO_DATA directory")?;
|
// This will fail when `data_dir` points to a file or a broken symlink, but
|
||||||
Ok(path)
|
// will no-op on a valid symlink (to a directory), or an actual directory.
|
||||||
|
fs::create_dir_all(&data_dir).context("could not create data directory")?;
|
||||||
|
|
||||||
|
Ok(data_dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn zo_exclude_dirs() -> Vec<PathBuf> {
|
pub fn zo_exclude_dirs() -> Vec<PathBuf> {
|
||||||
@ -45,6 +44,7 @@ pub fn zo_maxage() -> Result<Rank> {
|
|||||||
let maxage = maxage_str
|
let maxage = maxage_str
|
||||||
.parse::<i64>()
|
.parse::<i64>()
|
||||||
.context("unable to parse _ZO_MAXAGE as integer")?;
|
.context("unable to parse _ZO_MAXAGE as integer")?;
|
||||||
|
|
||||||
Ok(maxage as Rank)
|
Ok(maxage as Rank)
|
||||||
}
|
}
|
||||||
None => bail!("invalid Unicode in _ZO_MAXAGE"),
|
None => bail!("invalid Unicode in _ZO_MAXAGE"),
|
||||||
|
@ -48,7 +48,7 @@ impl DB {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_old<P1, P2>(path_old: P1, path: P2) -> Result<DB>
|
pub fn open_and_migrate<P1, P2>(path_old: P1, path: P2) -> Result<DB>
|
||||||
where
|
where
|
||||||
P1: AsRef<Path>,
|
P1: AsRef<Path>,
|
||||||
P2: AsRef<Path>,
|
P2: AsRef<Path>,
|
||||||
|
18
src/util.rs
18
src/util.rs
@ -22,9 +22,21 @@ pub fn path_to_bytes<P: AsRef<Path>>(path: &P) -> Option<&[u8]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_db() -> Result<DB> {
|
pub fn get_db() -> Result<DB> {
|
||||||
let mut path = config::zo_data()?;
|
let mut db_path = config::zo_data_dir()?;
|
||||||
path.push("db.zo");
|
db_path.push("db.zo");
|
||||||
DB::open(path)
|
|
||||||
|
// FIXME: fallback to old database location; remove in next breaking version
|
||||||
|
if !db_path.is_file() {
|
||||||
|
if let Some(mut old_db_path) = dirs::home_dir() {
|
||||||
|
old_db_path.push(".zo");
|
||||||
|
|
||||||
|
if old_db_path.is_file() {
|
||||||
|
return DB::open_and_migrate(old_db_path, db_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::open(db_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_current_time() -> Result<Epoch> {
|
pub fn get_current_time() -> Result<Epoch> {
|
||||||
|
Loading…
Reference in New Issue
Block a user