use actix_web::{web, HttpResponse};
use rusqlite::{params, Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::path::{Component, Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use super::catalog::{fetch_repo_files, Kind, HUB, TOKEN_ENV};
use super::state::AppState;
use std::collections::HashMap;
type State = web::Data<AppState>;
const FREE_SPACE_RESERVE: u64 = 2 * 1024 * 1024 * 1024;
const DEFAULT_MAX_BYTES: u64 = 20 * 1024 * 1024 * 1024;
const PROGRESS_INTERVAL_BYTES: u64 = 4 * 1024 * 1024;
fn cancellations() -> &'static Mutex<HashMap<String, Arc<AtomicBool>>> {
static REGISTRY: OnceLock<Mutex<HashMap<String, Arc<AtomicBool>>>> = OnceLock::new();
REGISTRY.get_or_init(|| Mutex::new(HashMap::new()))
}
fn init_download_tables(conn: &Connection) -> rusqlite::Result<()> {
conn.execute(
"CREATE TABLE IF NOT EXISTS downloads (
id TEXT PRIMARY KEY,
kind TEXT NOT NULL,
repo_id TEXT NOT NULL,
file_path TEXT NOT NULL,
dest_path TEXT NOT NULL,
total_bytes INTEGER NOT NULL,
downloaded_bytes INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL,
error TEXT,
started_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
completed_at INTEGER
)",
[],
)?;
Ok(())
}
fn reconcile_stale_jobs(conn: &Connection) -> rusqlite::Result<()> {
let live: Vec<String> = cancellations().lock().unwrap().keys().cloned().collect();
let mut stmt = conn.prepare("SELECT id FROM downloads WHERE status = 'running'")?;
let running = stmt
.query_map([], |r| r.get::<_, String>(0))?
.collect::<rusqlite::Result<Vec<_>>>()?;
for id in running {
if !live.contains(&id) {
conn.execute(
"UPDATE downloads SET status = 'failed', error = ?2, updated_at = ?3 WHERE id = ?1",
params![
id,
"The server stopped while this download was running.",
now_ms()
],
)?;
}
}
Ok(())
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StartDownloadRequest {
pub kind: String,
pub repo_id: String,
pub file_path: String,
}
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DownloadJob {
pub id: String,
pub kind: String,
pub repo_id: String,
pub file_path: String,
pub dest_path: String,
pub total_bytes: i64,
pub downloaded_bytes: i64,
pub status: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<String>,
pub started_at: i64,
pub updated_at: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub completed_at: Option<i64>,
}
fn ok<T: Serialize>(payload: T) -> HttpResponse {
HttpResponse::Ok().json(json!({ "success": true, "data": payload, "error": null }))
}
fn fail(status: actix_web::http::StatusCode, message: impl Into<String>) -> HttpResponse {
HttpResponse::build(status).json(json!({
"success": false,
"data": null,
"error": message.into(),
}))
}
fn db_error(e: rusqlite::Error) -> HttpResponse {
fail(
actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Database error: {e}"),
)
}
fn bad_request(message: impl Into<String>) -> HttpResponse {
fail(actix_web::http::StatusCode::BAD_REQUEST, message)
}
fn now_ms() -> i64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64)
.unwrap_or(0)
}
pub(crate) fn safe_relative_path(raw: &str) -> Option<PathBuf> {
if raw.trim().is_empty() {
return None;
}
let mut out = PathBuf::new();
for component in Path::new(raw).components() {
match component {
Component::Normal(part) => {
let text = part.to_str()?;
if text.chars().all(|c| c == '.') {
return None;
}
out.push(part);
}
Component::ParentDir
| Component::RootDir
| Component::Prefix(_)
| Component::CurDir => return None,
}
}
if out.as_os_str().is_empty() {
None
} else {
Some(out)
}
}
fn download_root(db_path: &Path) -> PathBuf {
if let Ok(dir) = std::env::var("IRONBRIDGE_DOWNLOAD_DIR") {
if !dir.trim().is_empty() {
return PathBuf::from(dir.trim());
}
}
db_path
.parent()
.unwrap_or_else(|| Path::new("."))
.join("downloads")
}
fn max_download_bytes() -> u64 {
std::env::var("IRONBRIDGE_MAX_DOWNLOAD_BYTES")
.ok()
.and_then(|v| v.trim().parse::<u64>().ok())
.filter(|v| *v > 0)
.unwrap_or(DEFAULT_MAX_BYTES)
}
fn free_space_for(path: &Path) -> Option<u64> {
let disks = sysinfo::Disks::new_with_refreshed_list();
disks
.list()
.iter()
.filter(|d| path.starts_with(d.mount_point()))
.max_by_key(|d| d.mount_point().as_os_str().len())
.map(|d| d.available_space())
}
fn read_job(conn: &Connection, id: &str) -> rusqlite::Result<Option<DownloadJob>> {
conn.query_row(
"SELECT id, kind, repo_id, file_path, dest_path, total_bytes,
downloaded_bytes, status, error, started_at, updated_at, completed_at
FROM downloads WHERE id = ?1",
params![id],
|row| {
Ok(DownloadJob {
id: row.get(0)?,
kind: row.get(1)?,
repo_id: row.get(2)?,
file_path: row.get(3)?,
dest_path: row.get(4)?,
total_bytes: row.get(5)?,
downloaded_bytes: row.get(6)?,
status: row.get(7)?,
error: row.get(8)?,
started_at: row.get(9)?,
updated_at: row.get(10)?,
completed_at: row.get(11)?,
})
},
)
.optional()
}
async fn run_download(
db_path: PathBuf,
id: String,
url: String,
dest: PathBuf,
cancel: Arc<AtomicBool>,
) {
let outcome = download_to_disk(&id, &url, &dest, &cancel).await;
cancellations().lock().unwrap().remove(&id);
let Ok(conn) = Connection::open(&db_path) else {
eprintln!("[WARN] downloads: could not record the outcome of {id}");
return;
};
let now = now_ms();
let result = match outcome {
Ok(bytes) => conn.execute(
"UPDATE downloads SET status='completed', downloaded_bytes=?2,
updated_at=?3, completed_at=?3, error=NULL WHERE id=?1",
params![id, bytes as i64, now],
),
Err(DownloadError::Cancelled) => {
let _ = std::fs::remove_file(dest.with_extension("part"));
conn.execute(
"UPDATE downloads SET status='cancelled', updated_at=?2, completed_at=?2
WHERE id=?1",
params![id, now],
)
}
Err(DownloadError::Failed(message)) => {
let _ = std::fs::remove_file(dest.with_extension("part"));
conn.execute(
"UPDATE downloads SET status='failed', error=?2, updated_at=?3, completed_at=?3
WHERE id=?1",
params![id, message, now],
)
}
};
if let Err(e) = result {
eprintln!("[WARN] downloads: could not record the outcome of {id}: {e}");
}
}
enum DownloadError {
Cancelled,
Failed(String),
}
async fn download_to_disk(
id: &str,
url: &str,
dest: &Path,
cancel: &AtomicBool,
) -> Result<u64, DownloadError> {
use std::io::Write;
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent)
.map_err(|e| DownloadError::Failed(format!("Could not create {parent:?}: {e}")))?;
}
let client = reqwest::Client::builder()
.connect_timeout(std::time::Duration::from_secs(30))
.build()
.map_err(|e| DownloadError::Failed(format!("HTTP client error: {e}")))?;
let mut request = client.get(url);
if let Ok(token) = std::env::var(TOKEN_ENV) {
if !token.trim().is_empty() {
request = request.bearer_auth(token.trim());
}
}
let mut response = request
.send()
.await
.map_err(|e| DownloadError::Failed(format!("Could not reach the Hub: {e}")))?;
if !response.status().is_success() {
return Err(DownloadError::Failed(format!(
"The Hub returned {} for this file. Gated repositories need {TOKEN_ENV} set \
on the server, and the terms accepted on the Hub first.",
response.status()
)));
}
let part = dest.with_extension("part");
let mut file = std::fs::File::create(&part)
.map_err(|e| DownloadError::Failed(format!("Could not open {part:?}: {e}")))?;
let mut written: u64 = 0;
let mut since_report: u64 = 0;
loop {
if cancel.load(Ordering::Relaxed) {
return Err(DownloadError::Cancelled);
}
let chunk = match response.chunk().await {
Ok(Some(c)) => c,
Ok(None) => break,
Err(e) => return Err(DownloadError::Failed(format!("Transfer failed: {e}"))),
};
file.write_all(&chunk)
.map_err(|e| DownloadError::Failed(format!("Write failed: {e}")))?;
written += chunk.len() as u64;
since_report += chunk.len() as u64;
if since_report >= PROGRESS_INTERVAL_BYTES {
since_report = 0;
report_progress(id, written);
}
}
file.flush()
.map_err(|e| DownloadError::Failed(format!("Flush failed: {e}")))?;
drop(file);
std::fs::rename(&part, dest)
.map_err(|e| DownloadError::Failed(format!("Could not finalise {dest:?}: {e}")))?;
Ok(written)
}
fn report_progress(id: &str, written: u64) {
let Some(path) = PROGRESS_DB.get() else {
return;
};
if let Ok(conn) = Connection::open(path) {
let _ = conn.execute(
"UPDATE downloads SET downloaded_bytes = ?2, updated_at = ?3 WHERE id = ?1",
params![id, written as i64, now_ms()],
);
}
}
fn remember_db_path(path: &Path) {
PROGRESS_DB.get_or_init(|| path.to_path_buf());
}
static PROGRESS_DB: OnceLock<PathBuf> = OnceLock::new();
pub async fn start_download(state: State, body: web::Json<StartDownloadRequest>) -> HttpResponse {
let body = body.into_inner();
let Some(kind) = Kind::parse(body.kind.trim()) else {
return bad_request("kind is required and must be `models` or `datasets`");
};
let repo_id = body.repo_id.trim().to_string();
let file_path = body.file_path.trim().to_string();
let files = match fetch_repo_files(kind, &repo_id).await {
Ok(f) => f,
Err(message) if message.contains("not a repository id") => return bad_request(message),
Err(message) => return fail(actix_web::http::StatusCode::BAD_GATEWAY, message),
};
let Some(entry) = files.iter().find(|f| f.path == file_path) else {
return bad_request(format!(
"`{file_path}` is not a file in {} `{repo_id}`.",
kind.noun()
));
};
let Some(relative) = safe_relative_path(&file_path) else {
return bad_request(format!("`{file_path}` is not a usable file path."));
};
let size = entry.size.max(0) as u64;
let cap = max_download_bytes();
if size > cap {
return bad_request(format!(
"That file is {size} bytes, over the {cap}-byte limit. Raise \
IRONBRIDGE_MAX_DOWNLOAD_BYTES on the server to allow it."
));
}
let root = download_root(&state.db_path);
let Some(repo_dir) = safe_relative_path(&repo_id) else {
return bad_request(format!("`{repo_id}` is not a usable repository id."));
};
let dest = root.join(kind.noun()).join(repo_dir).join(&relative);
if dest.exists() {
return bad_request(format!(
"{} already exists. Delete it first to download again.",
dest.display()
));
}
if let Some(parent) = dest.parent() {
if let Err(e) = std::fs::create_dir_all(parent) {
return fail(
actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
format!("Could not create {}: {e}", parent.display()),
);
}
}
match free_space_for(&dest) {
Some(free) if free < size.saturating_add(FREE_SPACE_RESERVE) => {
return bad_request(format!(
"That file is {size} bytes and only {free} are free. IronBridge keeps \
{FREE_SPACE_RESERVE} bytes in reserve so a download cannot fill the \
volume its database is on."
));
}
None => {
return fail(
actix_web::http::StatusCode::INTERNAL_SERVER_ERROR,
format!(
"Could not determine free space on the volume holding {}.",
dest.display()
),
);
}
Some(_) => {}
}
let id = uuid::Uuid::new_v4().to_string();
let now = now_ms();
{
let db = state.db.lock().unwrap();
if let Err(e) = init_download_tables(&db.conn) {
return db_error(e);
}
if let Err(e) = db.conn.execute(
"INSERT INTO downloads
(id, kind, repo_id, file_path, dest_path, total_bytes, downloaded_bytes,
status, started_at, updated_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, 0, 'running', ?7, ?7)",
params![
id,
kind.noun(),
repo_id,
file_path,
dest.to_string_lossy(),
size as i64,
now,
],
) {
return db_error(e);
}
}
remember_db_path(&state.db_path);
let cancel = Arc::new(AtomicBool::new(false));
cancellations()
.lock()
.unwrap()
.insert(id.clone(), cancel.clone());
let url = format!(
"{HUB}/{}{repo_id}/resolve/main/{file_path}",
match kind {
Kind::Models => "",
Kind::Datasets => "datasets/",
}
);
let db_path = state.db_path.clone();
let job_id = id.clone();
let dest_for_task = dest.clone();
actix_web::rt::spawn(async move {
run_download(db_path, job_id, url, dest_for_task, cancel).await;
});
ok(DownloadJob {
id,
kind: kind.noun().to_string(),
repo_id,
file_path,
dest_path: dest.to_string_lossy().to_string(),
total_bytes: size as i64,
downloaded_bytes: 0,
status: "running".to_string(),
error: None,
started_at: now,
updated_at: now,
completed_at: None,
})
}
pub async fn list_downloads(state: State) -> HttpResponse {
let db = state.db.lock().unwrap();
if let Err(e) = init_download_tables(&db.conn) {
return db_error(e);
}
if let Err(e) = reconcile_stale_jobs(&db.conn) {
return db_error(e);
}
let result: rusqlite::Result<Vec<DownloadJob>> = (|| {
let mut stmt = db.conn.prepare(
"SELECT id, kind, repo_id, file_path, dest_path, total_bytes,
downloaded_bytes, status, error, started_at, updated_at, completed_at
FROM downloads ORDER BY started_at DESC",
)?;
let rows = stmt
.query_map([], |row| {
Ok(DownloadJob {
id: row.get(0)?,
kind: row.get(1)?,
repo_id: row.get(2)?,
file_path: row.get(3)?,
dest_path: row.get(4)?,
total_bytes: row.get(5)?,
downloaded_bytes: row.get(6)?,
status: row.get(7)?,
error: row.get(8)?,
started_at: row.get(9)?,
updated_at: row.get(10)?,
completed_at: row.get(11)?,
})
})?
.collect::<rusqlite::Result<Vec<_>>>()?;
Ok(rows)
})();
match result {
Ok(jobs) => ok(jobs),
Err(e) => db_error(e),
}
}
pub async fn get_download(state: State, path: web::Path<String>) -> HttpResponse {
let id = path.into_inner();
let db = state.db.lock().unwrap();
if let Err(e) = init_download_tables(&db.conn) {
return db_error(e);
}
if let Err(e) = reconcile_stale_jobs(&db.conn) {
return db_error(e);
}
match read_job(&db.conn, &id) {
Ok(Some(job)) => ok(job),
Ok(None) => fail(
actix_web::http::StatusCode::NOT_FOUND,
"No download with that id",
),
Err(e) => db_error(e),
}
}
pub async fn cancel_download(state: State, path: web::Path<String>) -> HttpResponse {
let id = path.into_inner();
let db = state.db.lock().unwrap();
if let Err(e) = init_download_tables(&db.conn) {
return db_error(e);
}
let job = match read_job(&db.conn, &id) {
Ok(Some(j)) => j,
Ok(None) => {
return fail(
actix_web::http::StatusCode::NOT_FOUND,
"No download with that id",
)
}
Err(e) => return db_error(e),
};
if job.status == "running" {
if let Some(flag) = cancellations().lock().unwrap().get(&id) {
flag.store(true, Ordering::Relaxed);
return ok(json!({ "cancelling": true }));
}
if let Err(e) = db.conn.execute(
"UPDATE downloads SET status='failed', error=?2, updated_at=?3 WHERE id=?1",
params![
id,
"The server stopped while this download was running.",
now_ms()
],
) {
return db_error(e);
}
return ok(json!({ "cancelling": false, "reconciled": true }));
}
match db
.conn
.execute("DELETE FROM downloads WHERE id = ?1", params![id])
{
Ok(0) => fail(
actix_web::http::StatusCode::NOT_FOUND,
"No download with that id",
),
Ok(_) => ok(json!({ "deleted": true })),
Err(e) => db_error(e),
}
}
pub fn configure_download_routes(cfg: &mut web::ServiceConfig) {
cfg.service(
web::scope("/api/downloads")
.route("", web::get().to(list_downloads))
.route("", web::post().to(start_download))
.route("/{id}", web::get().to(get_download))
.route("/{id}", web::delete().to(cancel_download)),
);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ChatDatabase;
use actix_web::{test, App};
fn temp_state(tag: &str) -> (web::Data<AppState>, tempfile::TempDir) {
let dir = tempfile::tempdir().expect("tempdir");
let path: PathBuf = dir.path().join(format!("{tag}.db"));
crate::commands::create_harvest_database(&path).expect("harvest schema");
let db = ChatDatabase::open(&path).expect("open");
(web::Data::new(AppState::new(db, path)), dir)
}
macro_rules! app {
($state:expr) => {
test::init_service(
App::new()
.app_data($state.clone())
.configure(configure_download_routes),
)
.await
};
}
async fn body_json(resp: actix_web::dev::ServiceResponse) -> serde_json::Value {
serde_json::from_slice(&test::read_body(resp).await).expect("json body")
}
#[test]
fn no_path_that_could_escape_the_root_is_accepted() {
for bad in [
"",
" ",
"..",
"../secrets",
"../../etc/passwd",
"a/../../b",
"/absolute/path",
".",
"./",
"a/./../..",
"...",
] {
assert!(
safe_relative_path(bad).is_none(),
"accepted an escaping path: {bad:?}"
);
}
}
#[cfg(windows)]
#[test]
fn windows_absolute_and_unc_paths_are_rejected() {
for bad in [
"\\windows\\absolute",
"C:\\Windows\\System32\\drivers\\etc\\hosts",
"C:/Windows/System32",
"\\\\server\\share\\file",
] {
assert!(
safe_relative_path(bad).is_none(),
"accepted an absolute Windows path: {bad:?}"
);
}
}
#[cfg(unix)]
#[test]
fn a_windows_shaped_path_is_an_ordinary_filename_on_unix() {
let root = Path::new("/downloads/models/org/repo");
for raw in [
"\\windows\\absolute",
"C:\\Windows\\System32\\drivers\\etc\\hosts",
"\\\\server\\share\\file",
] {
let safe = safe_relative_path(raw)
.unwrap_or_else(|| panic!("{raw:?} is a legal Unix filename"));
let joined = root.join(&safe);
assert!(
joined.starts_with(root),
"{raw:?} escaped the root as {}",
joined.display()
);
}
let joined = root.join(safe_relative_path("C:/Windows/System32").expect("relative"));
assert!(joined.starts_with(root));
}
#[test]
fn ordinary_repository_paths_are_kept() {
for (raw, expected) in [
("config.json", "config.json"),
("onnx/model.onnx", "onnx/model.onnx"),
("a/b/c/weights.safetensors", "a/b/c/weights.safetensors"),
("model-00001-of-00002.bin", "model-00001-of-00002.bin"),
] {
let got = safe_relative_path(raw).unwrap_or_else(|| panic!("rejected {raw}"));
let normalised = got.to_string_lossy().replace('\\', "/");
assert_eq!(normalised, expected);
}
}
#[test]
fn a_safe_path_always_stays_under_its_root() {
let root = Path::new("/downloads/models/org/repo");
for raw in [
"a.bin",
"sub/dir/b.bin",
"deep/deeper/deepest/c.safetensors",
"\\windows\\absolute",
"C:\\Windows\\System32",
"C:/Windows/System32",
"\\\\server\\share\\file",
] {
let Some(safe) = safe_relative_path(raw) else {
continue; };
let joined = root.join(&safe);
assert!(
joined.starts_with(root),
"{raw} escaped: {}",
joined.display()
);
}
}
#[tokio::test]
async fn listing_works_before_anything_is_downloaded() {
let (state, _dir) = temp_state("dl-empty");
let app = app!(&state);
let resp = test::call_service(
&app,
test::TestRequest::get().uri("/api/downloads").to_request(),
)
.await;
assert_eq!(resp.status(), 200);
assert_eq!(
body_json(resp).await["data"].as_array().map(|a| a.len()),
Some(0)
);
}
#[tokio::test]
async fn an_unknown_kind_is_rejected_before_the_hub_is_touched() {
let (state, _dir) = temp_state("dl-kind");
let app = app!(&state);
let resp = test::call_service(
&app,
test::TestRequest::post()
.uri("/api/downloads")
.set_json(json!({
"kind": "spaces",
"repoId": "openai-community/gpt2",
"filePath": "config.json",
}))
.to_request(),
)
.await;
assert_eq!(resp.status(), 400);
}
#[tokio::test]
async fn a_traversing_repo_id_is_rejected() {
let (state, _dir) = temp_state("dl-repo");
let app = app!(&state);
let resp = test::call_service(
&app,
test::TestRequest::post()
.uri("/api/downloads")
.set_json(json!({
"kind": "models",
"repoId": "../../etc",
"filePath": "passwd",
}))
.to_request(),
)
.await;
assert_eq!(resp.status(), 400);
}
#[tokio::test]
async fn an_unknown_job_is_a_404() {
let (state, _dir) = temp_state("dl-404");
let app = app!(&state);
for req in [
test::TestRequest::get()
.uri("/api/downloads/nope")
.to_request(),
test::TestRequest::delete()
.uri("/api/downloads/nope")
.to_request(),
] {
let resp = test::call_service(&app, req).await;
assert_eq!(resp.status(), 404);
}
}
#[tokio::test]
async fn a_job_orphaned_by_a_restart_is_reported_as_failed() {
let (state, _dir) = temp_state("dl-stale");
{
let db = state.db.lock().unwrap();
init_download_tables(&db.conn).expect("tables");
db.conn
.execute(
"INSERT INTO downloads
(id, kind, repo_id, file_path, dest_path, total_bytes,
downloaded_bytes, status, started_at, updated_at)
VALUES ('ghost','models','a/b','f.bin','/tmp/f.bin',100,40,'running',1,1)",
[],
)
.expect("insert");
}
let app = app!(&state);
let resp = test::call_service(
&app,
test::TestRequest::get()
.uri("/api/downloads/ghost")
.to_request(),
)
.await;
assert_eq!(resp.status(), 200);
let body = body_json(resp).await;
assert_eq!(body["data"]["status"], "failed");
assert!(
body["data"]["error"]
.as_str()
.unwrap_or_default()
.contains("server stopped"),
"{body}"
);
assert_eq!(body["data"]["downloadedBytes"], 40);
}
#[tokio::test]
async fn deleting_a_finished_job_removes_only_the_record() {
let (state, dir) = temp_state("dl-del");
let artifact = dir.path().join("kept.bin");
std::fs::write(&artifact, b"payload").expect("write");
{
let db = state.db.lock().unwrap();
init_download_tables(&db.conn).expect("tables");
db.conn
.execute(
"INSERT INTO downloads
(id, kind, repo_id, file_path, dest_path, total_bytes,
downloaded_bytes, status, started_at, updated_at, completed_at)
VALUES ('done','models','a/b','kept.bin',?1,7,7,'completed',1,1,1)",
params![artifact.to_string_lossy()],
)
.expect("insert");
}
let app = app!(&state);
let resp = test::call_service(
&app,
test::TestRequest::delete()
.uri("/api/downloads/done")
.to_request(),
)
.await;
assert_eq!(resp.status(), 200);
assert!(artifact.exists(), "deleting the record deleted the file");
let db = state.db.lock().unwrap();
let left: i64 = db
.conn
.query_row("SELECT COUNT(*) FROM downloads", [], |r| r.get(0))
.expect("count");
assert_eq!(left, 0);
}
#[test]
fn the_download_root_defaults_beside_the_database() {
std::env::remove_var("IRONBRIDGE_DOWNLOAD_DIR");
let root = download_root(Path::new("/var/lib/ironbridge/ironbridge.db"));
assert!(root.ends_with("downloads"), "{}", root.display());
assert!(
root.starts_with("/var/lib/ironbridge"),
"{}",
root.display()
);
}
#[test]
fn the_size_cap_falls_back_to_the_default() {
std::env::remove_var("IRONBRIDGE_MAX_DOWNLOAD_BYTES");
assert_eq!(max_download_bytes(), DEFAULT_MAX_BYTES);
std::env::set_var("IRONBRIDGE_MAX_DOWNLOAD_BYTES", "not a number");
assert_eq!(max_download_bytes(), DEFAULT_MAX_BYTES);
std::env::set_var("IRONBRIDGE_MAX_DOWNLOAD_BYTES", "0");
assert_eq!(max_download_bytes(), DEFAULT_MAX_BYTES, "zero disables");
std::env::set_var("IRONBRIDGE_MAX_DOWNLOAD_BYTES", "1024");
assert_eq!(max_download_bytes(), 1024);
std::env::remove_var("IRONBRIDGE_MAX_DOWNLOAD_BYTES");
}
#[test]
fn free_space_is_readable_for_a_real_directory() {
let dir = tempfile::tempdir().expect("tempdir");
assert!(
free_space_for(dir.path()).is_some(),
"no disk matched {}",
dir.path().display()
);
}
}