-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out database checks into a separate module.
- Loading branch information
Showing
2 changed files
with
43 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::{ | ||
os::unix::prelude::CommandExt, | ||
process::Command, | ||
time::{Duration, SystemTime}, | ||
}; | ||
|
||
/// Update the local nix-index database. | ||
pub fn update_database() { | ||
println!("Updating nix-index database, takes around 5 minutes."); | ||
Command::new("nix-index").exec(); | ||
} | ||
|
||
/// Prints warnings if the nix-index database is non-existent or out of date. | ||
pub fn check_database() { | ||
let base = xdg::BaseDirectories::with_prefix("nix-index").unwrap(); | ||
let cache_dir = base.get_cache_home(); | ||
let database_file = cache_dir.join("files"); | ||
if !database_file.exists() { | ||
println!("Warning: Nix-index database does not exist, try updating with `--update`."); | ||
} else if is_database_old(database_file) { | ||
println!( | ||
"Warning: Nix-index database is older than 30 days, try updating with `--update`." | ||
); | ||
} | ||
} | ||
|
||
/// Test whether the database is more than 30 days old | ||
fn is_database_old(database_file: std::path::PathBuf) -> bool { | ||
let modified = match database_file.metadata() { | ||
Ok(metadata) => metadata.modified().unwrap_or_else(|_| SystemTime::now()), | ||
Err(_) => return false, | ||
}; | ||
let time_since_modified = SystemTime::now() | ||
.duration_since(modified) | ||
.unwrap_or(Duration::new(0, 0)); | ||
if time_since_modified > Duration::from_secs(30 * 24 * 60 * 60) { | ||
return true; | ||
} | ||
false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters