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
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
/// Used to construct global FetchData instance.
///
/// This is a re-export from crate [`ctor`](https://crates.io/crates/ctor).
pub use ctor::ctor;
use directories::ProjectDirs;
use sha2::{Digest, Sha256};
use std::{
collections::HashMap,
fs::{self, read_dir, File},
path::{Path, PathBuf},
sync::Mutex,
};
use thiserror::Error;
/// Used to fetch data files from a URL, if needed. It verifies file contents via a hash.
///
/// # Thread Safety
///
/// `FetchData` works well with multithreaded testing, It is thread safe (via a Mutex).
///
pub struct FetchData {
mutex: Mutex<Result<Internals, FetchDataError>>,
}
impl FetchData {
/// Create a new FetchData object.
///
/// # Errors
///
/// To make `FetchData` work well as a static global, `new` never fails. Instead, `FetchData` stores any error
/// and returns it when the first call to `fetch_file`, etc., is made.
///
/// # Arguments
/// *all inputs are string-like*
///
/// * `registry_contents` - Whitespace delimited list of files and hashes.
/// Use Rust's [`std::include_str`](https://doc.rust-lang.org/std/macro.include_str.html)
/// macro to include the contents of a file.
/// * `url_root` - Base URL for remote files.
/// * `env_key` - Environment variable that may contain the path to the data directory.
/// If not set, the data directory will be create via
/// [`ProjectDirs`](https://docs.rs/directories/latest/directories/struct.ProjectDirs.html#method.from_path)
/// and the next three arguments.
/// * `qualifier` - The reverse domain name notation of the application, excluding the organization or application name itself.
/// * `organization` - The name of the organization that develops this application.
/// * `application` - The name of the application itself.
///
/// # Example
/// ```
/// use fetch_data::{FetchData};
///
/// // Create a new FetchData instance.
/// let fetch_data = FetchData::new(
/// "small.fam 36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2
/// small.bim 56b6657a3766e2e52273f89d28be6135f9424ca1d204d29f3fa1c5a90eca794e",
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/",
/// "BAR_APP_DATA_DIR",
/// "com",
/// "Foo Corp",
/// "Bar App",
/// );
///
/// // If the local file exists and has the right hash, just return its path.
/// // Otherwise, download the file, confirm its hash, and return its path.
/// let local_path = fetch_data.fetch_file("small.bim")?;
/// assert!(local_path.exists());
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn new<S0, S1, S3, S4, S5, S6>(
registry_contents: S0,
url_root: S1,
env_key: S3,
qualifier: S4,
organization: S5,
application: S6,
) -> FetchData
where
// any string-like input
S0: AsRef<str>,
S1: AsRef<str>,
S3: AsRef<str>,
S4: AsRef<str>,
S5: AsRef<str>,
S6: AsRef<str>,
{
FetchData {
mutex: Mutex::new(Internals::new(
registry_contents.as_ref(),
url_root.as_ref(),
env_key.as_ref(),
qualifier.as_ref(),
organization.as_ref(),
application.as_ref(),
)),
}
}
fn lock(&self) -> std::sync::MutexGuard<Result<Internals, FetchDataError>> {
let lock = match self.mutex.lock() {
Ok(lock) => lock,
Err(err) => err.into_inner(),
};
lock
}
/// Fetch data files from a URL, but only if needed. Verify contents via a hash.
///
/// # Example
/// ```
/// use fetch_data::{FetchData};
///
/// // Create a new FetchData object.
/// let fetch_data = FetchData::new(
/// "small.fam 36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2
/// small.bim 56b6657a3766e2e52273f89d28be6135f9424ca1d204d29f3fa1c5a90eca794e",
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/",
/// "BAR_APP_DATA_DIR",
/// "com",
/// "Foo Corp",
/// "Bar App",
/// );
///
/// // If the local file exists and has the right hash, just return its path.
/// // Otherwise, download the file, confirm its hash, and return its path.
/// let local_path = fetch_data.fetch_file("small.bim")?;
/// assert!(local_path.exists());
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn fetch_file<P: AsRef<Path>>(&self, path: P) -> Result<PathBuf, FetchDataError> {
let path_list = vec![path.as_ref().to_path_buf()];
let vec = self.fetch_files(path_list)?;
Ok(vec[0].clone())
}
/// Given a list of files, returns a list of their local paths. If necessary, the files will be downloaded.
///
/// # Example
/// ```
/// use fetch_data::{FetchData};
///
/// // Create a new FetchData instance.
/// let fetch_data = FetchData::new(
/// "small.fam 36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2
/// small.bim 56b6657a3766e2e52273f89d28be6135f9424ca1d204d29f3fa1c5a90eca794e",
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/",
/// "BAR_APP_DATA_DIR",
/// "com",
/// "Foo Corp",
/// "Bar App",
/// );
///
/// // If a local file exists and has the right hash, just return its path
/// // in a list. Otherwise, download the file, confirm its hash, and return
/// // its path in the list.
/// let local_path_list = fetch_data.fetch_files(["small.bim", "small.bim"])?;
/// assert!(local_path_list[0].exists() && local_path_list[1].exists());
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn fetch_files<I, P>(&self, path_list: I) -> Result<Vec<PathBuf>, FetchDataError>
where
// Any list-like iterable of path-like items
I: IntoIterator<Item = P>,
P: AsRef<Path>,
{
let lock = self.lock();
let internals = FetchData::internals(lock.as_ref())?;
let hash_registry = &internals.hash_registry;
let cache_dir = &internals.cache_dir;
let url_root = &internals.url_root;
let mut local_list: Vec<PathBuf> = Vec::new();
for path in path_list {
let path = path.as_ref();
let path_as_string = if let Some(path_as_string) = path.to_str() {
path_as_string
} else {
return Err(FetchDataSpecificError::UnknownOrBadFile("???".to_string()).into());
};
let hash = if let Some(hash) = hash_registry.get(path) {
hash
} else {
return Err(
FetchDataSpecificError::UnknownOrBadFile(path_as_string.to_string()).into(),
);
};
let local_path = cache_dir.join(path);
let url = format!("{url_root}{path_as_string}");
fetch(url, &hash, &local_path)?;
local_list.push(local_path);
}
Ok(local_list)
}
fn internals<'a>(
lock_ref: Result<&'a Internals, &FetchDataError>,
) -> Result<&'a Internals, FetchDataError> {
match lock_ref {
Ok(internals) => Ok(internals),
Err(e) => Err(FetchDataSpecificError::FetchDataNewFailed(e.to_string()).into()),
}
}
/// Compute registry contents by downloading items and hashing them.
///
/// # Tips
///
/// * If you put the returned contents into a file, you can use Rust's [`std::include_str`](https://doc.rust-lang.org/std/macro.include_str.html)
/// macro to include the contents of that file in [`FetchData::new`](struct.FetchData.html#method.new).
///
/// * Use utility function [`fetch_data::dir_to_file_list`](fn.dir_to_file_list.html) to create a list of files in any local directory.
/// Note the hash is computed on download files, not any original local files.
///
/// # Example
///
/// ```
/// use fetch_data::{FetchData};
///
/// // Create a new FetchData object.
/// let fetch_data = FetchData::new(
/// "", // ignored
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/",
/// "BAR_APP_DATA_DIR",
/// "com",
/// "Foo Corp",
/// "Bar App",
/// );
///
/// // Even if local files exist, download each file. Hash each file. Return the results as a string.
/// let registry_contents = fetch_data.gen_registry_contents(["small.fam", "small.bim"])?;
/// println!("{registry_contents}"); // Prints:
/// // small.fam 36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2
/// // small.bim 56b6657a3766e2e52273f89d28be6135f9424ca1d204d29f3fa1c5a90eca794e
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn gen_registry_contents<I, P>(&self, path_list: I) -> Result<String, FetchDataError>
where
I: IntoIterator<Item = P>,
P: AsRef<Path>,
{
let lock = self.lock();
let internals = FetchData::internals(lock.as_ref())?;
let cache_dir = &internals.cache_dir;
let url_root = &internals.url_root;
let mut s = String::new();
for path in path_list {
let path = path.as_ref();
let path_as_string = if let Some(path_as_string) = path.to_str() {
path_as_string
} else {
return Err(FetchDataSpecificError::UnknownOrBadFile("???".to_string()).into());
};
let local_path = cache_dir.join(path);
let url = format!("{url_root}{path_as_string}");
download(url, &local_path)?;
let hash = hash_file(&local_path)?;
s.push_str(&format!("{} {hash}\n", path.display()));
}
Ok(s)
}
/// Return the path to the local cache directory.
pub fn cache_dir(&self) -> Result<PathBuf, FetchDataError> {
let lock = self.lock();
let internals = FetchData::internals(lock.as_ref())?;
let cache_dir = &internals.cache_dir;
Ok(cache_dir.to_owned())
}
}
/// All possible errors returned by this crate and the crates it depends on.
// Based on `<https://nick.groenen.me/posts/rust-error-handling/#the-library-error-type>`
#[derive(Error, Debug)]
pub enum FetchDataError {
#[allow(missing_docs)]
#[error(transparent)]
FetchDataError(#[from] FetchDataSpecificError),
#[allow(missing_docs)]
#[error(transparent)]
IOError(#[from] std::io::Error),
#[allow(missing_docs)]
#[error(transparent)]
UreqError(#[from] ureq::Error),
}
/// All errors specific to this crate.
#[derive(Error, Debug, Clone)]
pub enum FetchDataSpecificError {
#[allow(missing_docs)]
#[error("Unknown or bad file '{0}'")]
UnknownOrBadFile(String),
#[allow(missing_docs)]
#[error("The registry of files is invalid")]
RegistryProblem(),
#[allow(missing_docs)]
#[error("FetchData new failed with error: {0}")]
FetchDataNewFailed(String),
#[allow(missing_docs)]
#[error("Downloaded file not seen: {0}")]
DownloadedFileNotSeen(String),
#[allow(missing_docs)]
#[error("Downloaded file has wrong hash: {0},expected: {1}, actual: {2}")]
DownloadedFileWrongHash(String, String, String),
#[allow(missing_docs)]
#[error("Cannot create cache directory")]
CannotCreateCacheDir(),
}
/// If necessary, retrieve a file from a URL, checking its hash.
/// # Example
/// ```
/// use fetch_data::fetch;
/// use temp_testdir::TempDir;
///
/// // Create a temporary local directory.
/// let temp_dir = TempDir::default();
/// // Download the file and check its hash.
/// let path = temp_dir.join("small.fam");
/// fetch(
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/small.fam",
/// "36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2",
/// &path,
/// )?;
/// assert!(&path.exists());
/// // This time, because the local file exists and has the correct hash, no download is performed.
/// fetch(
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/small.fam",
/// "36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2",
/// &path,
/// )?;
/// assert!(&path.exists());
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn fetch<U: AsRef<str>, H: AsRef<str>, P: AsRef<Path>>(
url: U,
hash: H,
path: P,
) -> Result<(), FetchDataError> {
let path = path.as_ref();
if !path.exists() {
download(url, &path)?;
}
let actual_hash = hash_file(&path)?;
if !actual_hash.eq(hash.as_ref()) {
return Err(FetchDataSpecificError::DownloadedFileWrongHash(
path.display().to_string(),
hash.as_ref().to_string(),
actual_hash,
)
.into());
}
Ok(())
}
/// Download a file from a URL and compute its hash.
///
/// # Example
/// ```
/// use fetch_data::hash_download;
/// use temp_testdir::TempDir;
///
/// // Create a temporary local directory.
/// let temp_dir = TempDir::default();
/// let path = temp_dir.join("small.fam");
/// // Download a file and compute its hash.
/// let hash = hash_download(
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/small.fam",
/// &path,
/// )?;
/// assert!(hash.eq("36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2"));
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn hash_download<U: AsRef<str>, P: AsRef<Path>>(
url: U,
path: P,
) -> Result<String, FetchDataError> {
let path = path.as_ref();
download(url, &path)?;
hash_file(&path)
}
/// Compute the hash (SHA256) of a local file.
///
/// # Example
/// ```
/// use fetch_data::{hash_file, download};
/// use temp_testdir::TempDir;
///
/// // Download a file to a temporary directory.
/// let temp_dir = TempDir::default();
/// let path = temp_dir.join("small.fam");
/// download(
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/small.fam",
/// &path,
/// )?;
/// // Compute the hash of the file.
/// let hash = hash_file(&path)?;
/// assert!(hash.eq("36e0086c0353ff336d0533330dbacb12c75e37dc3cba174313635b98dfe86ed2"));
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
pub fn hash_file<P: AsRef<Path>>(path: P) -> Result<String, FetchDataError> {
let mut sha256 = Sha256::new();
let mut file = File::open(path)?;
std::io::copy(&mut file, &mut sha256)?;
let hash_bytes = sha256.finalize();
let hex_hash = base16ct::lower::encode_string(&hash_bytes);
Ok(hex_hash)
}
/// Download a file from a URL.
///
/// # Example
/// ```
/// use fetch_data::download;
/// use temp_testdir::TempDir;
///
/// // Create a temporary local directory.
/// let temp_dir = TempDir::default();
/// // Download a file to the temporary directory.
/// let path = temp_dir.join("small.fam");
/// download(
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/small.fam",
/// &path,
/// )?;
/// assert!(path.exists());
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn download<S: AsRef<str>, P: AsRef<Path>>(url: S, path: P) -> Result<(), FetchDataError> {
let path = path.as_ref();
let req = ureq::get(url.as_ref()).call()?;
let mut reader = req.into_reader();
let mut file = File::create(&path)?;
std::io::copy(&mut reader, &mut file)?;
if !path.exists() {
return Err(
FetchDataSpecificError::DownloadedFileNotSeen(path.display().to_string()).into(),
);
}
Ok(())
}
fn hash_registry(registry_contents: &str) -> Result<HashMap<PathBuf, String>, FetchDataError> {
let mut hash_map = HashMap::new();
for line in registry_contents.lines() {
let mut parts = line.split_whitespace();
let url = if let Some(url) = parts.next() {
if url.is_empty() {
return Err(FetchDataSpecificError::RegistryProblem().into());
}
PathBuf::from(url)
} else {
return Err(FetchDataSpecificError::RegistryProblem().into());
};
let hash = if let Some(hash) = parts.next() {
hash.to_string()
} else {
return Err(FetchDataSpecificError::RegistryProblem().into());
};
if hash.is_empty() || parts.next().is_some() {
return Err(FetchDataSpecificError::RegistryProblem().into());
}
hash_map.insert(url, hash.to_owned());
}
Ok(hash_map)
}
/// List all the files in a local directory.
///
/// # Example
/// ```
/// use fetch_data::{dir_to_file_list, download};
/// use temp_testdir::TempDir;
///
/// // Create a local directory and download two files to it.
/// let temp_dir = TempDir::default();
/// download(
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/small.fam",
/// temp_dir.join("small.fam"),
/// )?;
/// download(
/// "https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/small.bim",
/// temp_dir.join("small.bim"),
/// )?;
/// // List the files in the directory.
/// let file_list = dir_to_file_list(temp_dir)?;
/// println!("{file_list:?}"); // Prints ["small.bim", "small.fam"]
/// # use fetch_data::FetchDataError;
/// # Ok::<(), FetchDataError>(())
/// ```
pub fn dir_to_file_list<P: AsRef<Path>>(
path: P,
) -> Result<Vec<std::ffi::OsString>, FetchDataError> {
let file_list = read_dir(path)?
.map(|res| res.map(|e| e.file_name()))
.collect::<Result<Vec<_>, std::io::Error>>()?;
Ok(file_list)
}
struct Internals {
cache_dir: PathBuf,
hash_registry: HashMap<PathBuf, String>,
url_root: String,
}
impl Internals {
fn new(
registry_contents: &str,
url_root: &str,
env_key: &str,
qualifier: &str,
organization: &str,
application: &str,
) -> Result<Internals, FetchDataError> {
let cache_dir = Internals::cache_dir(env_key, qualifier, organization, application)?;
let hash_registry = hash_registry(registry_contents)?;
Ok(Internals {
cache_dir,
hash_registry,
url_root: url_root.to_string(),
})
}
fn cache_dir(
env_key: &str,
qualifier: &str,
organization: &str,
application: &str,
) -> Result<PathBuf, FetchDataError> {
let cache_dir = if let Ok(cache_dir) = std::env::var(env_key) {
PathBuf::from(cache_dir)
} else if let Some(proj_dirs) = ProjectDirs::from(qualifier, organization, application) {
proj_dirs.cache_dir().to_owned()
} else {
return Err(FetchDataSpecificError::CannotCreateCacheDir().into());
};
if !cache_dir.exists() {
fs::create_dir_all(&cache_dir)?;
}
Ok(cache_dir)
}
}
#[ctor]
static STATIC_FETCH_DATA: FetchData = FetchData::new(
include_str!("../registry.txt"),
"https://raw.githubusercontent.com/CarlKCarlK/fetch-data/main/tests/data/",
"BAR_APP_DATA_DIR",
"com",
"Foo Corp",
"Bar App",
);
/// A sample sample_file. Don't use this. Instead, define your own `sample_file` function
/// that knows how to fetch your data files.
pub fn sample_file<P: AsRef<Path>>(path: P) -> Result<PathBuf, FetchDataError> {
STATIC_FETCH_DATA.fetch_file(path)
}