oadsrust 0.2.0

Content-addressed dataset packs and loaders for OA
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
//! Versioned, content-addressed dataset packs.
//!
//! A [`Pack`] is an immutable manifest compiled into the owning dataset
//! module. [`ensure_pack`] materializes a pack under a cache root —
//! `$OA_DATA_DIR`, `~/.oads`, or `$XDG_CACHE_HOME/oa/data` — fetching
//! missing members through `curl` and verifying every fetched byte against the
//! manifest SHAKE256-256 digest before publishing it into the pack directory.
//! A cache hit checks the recorded byte size only; delete the pack directory
//! to force a refetch and full re-verification.
//!
//! Downloads write to `*.partial` members and resume from the recorded
//! byte length (`curl -C -`); a completed-but-unpublished partial is
//! verified in place instead of refetched. [`FetchProgress`] samples stream
//! to [`ensure_pack_with_progress`] callers for progress reporting, and a
//! callback returning `false` cancels the fetch while keeping the partial.

use std::fs;
use std::io::{IsTerminal, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

use oa::crypto::{Hash, Hasher};
use oa::{Error, Result};

const HASH_CHUNK: usize = 1 << 20;
const FETCH_POLL: Duration = Duration::from_millis(100);

/// One fetch progress sample reported while materializing a pack member.
#[derive(Clone, Copy, Debug)]
pub struct FetchProgress {
	/// Member file name within the pack.
	pub file: &'static str,
	/// Bytes written to the partial so far, including a resumed prefix.
	pub fetched: u64,
	/// Expected final byte count from the manifest.
	pub total: u64,
}

/// Terminal renderer for [`FetchProgress`] samples.
///
/// Mirrors `oads._progress.FetchProgress` on the Python side: on a TTY it
/// rewrites one line per member with a bar, percentage, byte counts,
/// speed, elapsed, and ETA; on pipes it emits a start and a complete
/// line. Pass `|sample| renderer.report(sample)` as the
/// [`ensure_pack_with_progress`] or `load_with_progress` callback.
pub struct TerminalProgress {
	file: Option<&'static str>,
	start: Instant,
	last: Instant,
	tty: bool,
}

impl Default for TerminalProgress {
	fn default() -> Self {
		Self::new()
	}
}

impl TerminalProgress {
	const WIDTH: usize = 28;
	const THROTTLE: Duration = Duration::from_millis(100);

	/// Create a renderer writing to stderr.
	pub fn new() -> Self {
		Self {
			file: None,
			start: Instant::now(),
			last: Instant::now(),
			tty: std::io::stderr().is_terminal(),
		}
	}

	/// Format one sample as the single-line bar shown on a TTY.
	pub fn render(sample: &FetchProgress, elapsed: Duration) -> String {
		let fetched = sample.fetched as f64;
		let total = sample.total as f64;
		let seconds = elapsed.as_secs_f64();
		let speed = if seconds > 0.0 {
			fetched / seconds
		} else {
			0.0
		};
		let eta = if speed > 0.0 {
			(total - fetched) / speed
		} else {
			0.0
		};
		let ratio = if total > 0.0 {
			(fetched / total).min(1.0)
		} else {
			1.0
		};
		let done = (ratio * Self::WIDTH as f64) as usize;
		format!(
			"{} {:5.1}%|{}{}| {}/{} {}/s elapsed {} eta {}",
			sample.file,
			ratio * 100.0,
			"█".repeat(done),
			"░".repeat(Self::WIDTH - done),
			Self::size(sample.fetched as f64),
			Self::size(sample.total as f64),
			Self::size(speed),
			Self::duration(seconds),
			Self::duration(eta),
		)
	}

	/// Record one [`FetchProgress`] sample; always returns `true`.
	///
	/// Cancellation is an application concern — wire a signal handler to
	/// the callback when a Rust binary needs abort-to-partial behavior.
	pub fn report(&mut self, sample: FetchProgress) -> bool {
		let now = Instant::now();
		if self.file != Some(sample.file) {
			if self.tty && self.file.is_some() {
				eprintln!();
			}
			self.file = Some(sample.file);
			self.start = now;
			self.last = now;
			if !self.tty {
				eprintln!("oads: downloading {}", sample.file);
			}
		}
		if !self.tty {
			if sample.fetched >= sample.total {
				eprintln!("oads: {} complete", sample.file);
			}
			return true;
		}
		if now.duration_since(self.last) < Self::THROTTLE && sample.fetched < sample.total {
			return true;
		}
		self.last = now;
		eprint!(
			"\r{}   ",
			Self::render(&sample, now.duration_since(self.start))
		);
		if sample.fetched >= sample.total {
			eprintln!();
		}
		true
	}

	fn size(mut value: f64) -> String {
		for unit in ["B", "KiB", "MiB", "GiB"] {
			if value < 1024.0 || unit == "GiB" {
				return if unit == "B" {
					format!("{} {unit}", value as u64)
				} else {
					format!("{value:.1} {unit}")
				};
			}
			value /= 1024.0;
		}
		unreachable!()
	}

	fn duration(seconds: f64) -> String {
		let seconds = seconds.max(0.0) as u64;
		let minutes = seconds / 60;
		let hours = minutes / 60;
		if hours > 0 {
			format!("{hours}h{:02}m", minutes % 60)
		} else if minutes > 0 {
			format!("{minutes}m{:02}s", seconds % 60)
		} else {
			format!("{seconds}s")
		}
	}
}

/// One content-addressed member of a [`Pack`].
pub struct PackFile {
	/// File name inside the pack directory.
	pub name: &'static str,
	/// Exact byte length of the payload.
	pub size: u64,
	/// SHAKE256-256 digest of the payload as lowercase hexadecimal.
	pub shake256: &'static str,
	/// Mirror URLs tried in order until one verifies. `file://` is supported.
	pub urls: &'static [&'static str],
}

/// Immutable named-and-versioned file set compiled into a dataset module.
pub struct Pack {
	/// Dataset name; becomes the first cache path segment.
	pub name: &'static str,
	/// Pack version; becomes the second cache path segment.
	pub version: &'static str,
	/// License identifier for the upstream payload; texts live in `license/`.
	pub license: &'static str,
	/// Every member required for the pack to be complete.
	pub files: &'static [PackFile],
}

/// Resolve the dataset cache root.
///
/// Prefers `OA_DATA_DIR`, then `~/.oads`, then `$XDG_CACHE_HOME/oa/data`,
/// then `~/.cache/oa/data`, and creates the directory on first use.
///
/// # Errors
///
/// Returns an error when no cache location can be resolved or the directory
/// cannot be created.
pub fn data_root() -> Result<PathBuf> {
	let home = || {
		std::env::var_os("HOME")
			.filter(|dir| !dir.is_empty())
			.map(PathBuf::from)
	};
	let root = match std::env::var_os("OA_DATA_DIR") {
		Some(dir) if !dir.is_empty() => PathBuf::from(dir),
		_ => match home() {
			Some(home) => home.join(".oads"),
			None => match std::env::var_os("XDG_CACHE_HOME") {
				Some(dir) if !dir.is_empty() => PathBuf::from(dir).join("oa").join("data"),
				_ => {
					return Err(Error::not_found(
						"dataset cache root is unresolvable: set OA_DATA_DIR or HOME",
					));
				}
			},
		},
	};
	fs::create_dir_all(&root).map_err(|error| Error::io("create dataset cache root", error))?;
	Ok(root)
}

/// Materialize `pack` under `root`, fetching and verifying missing members.
///
/// Returns the pack directory `root/<name>/<version>`. A member already
/// present at the recorded byte size is accepted without rehashing; a missing
/// or size-mismatched member is downloaded to a temporary file, verified
/// against the manifest digest, and renamed into place.
///
/// # Errors
///
/// Returns an error when the directory cannot be created, every mirror fails,
/// or a fetched member's size or digest does not match the manifest.
pub fn ensure_pack(pack: &Pack, root: &Path) -> Result<PathBuf> {
	ensure_pack_with_progress(pack, root, &mut |_| true)
}

/// [`ensure_pack`] with a fetch progress callback.
///
/// The callback receives [`FetchProgress`] samples roughly every 100 ms
/// during downloads; returning `false` cancels the fetch with a
/// `Cancelled` error and keeps the partial for a later resume.
///
/// # Errors
///
/// Returns the same errors as [`ensure_pack`], plus `Cancelled` when the
/// callback aborts a fetch.
pub fn ensure_pack_with_progress(
	pack: &Pack,
	root: &Path,
	progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<PathBuf> {
	let dir = root.join(pack.name).join(pack.version);
	fs::create_dir_all(&dir).map_err(|error| Error::io("create pack directory", error))?;
	for file in pack.files {
		ensure_file(&dir, file, progress)?;
	}
	Ok(dir)
}

/// Stream a file through SHAKE256-256.
///
/// # Errors
///
/// Returns an error when the file cannot be opened or read.
pub fn hash_file(path: &Path) -> Result<Hash> {
	let mut file = fs::File::open(path).map_err(|error| Error::io("open for hashing", error))?;
	let mut hasher = Hasher::new();
	let mut buffer = vec![0_u8; HASH_CHUNK];
	loop {
		let read = file
			.read(&mut buffer)
			.map_err(|error| Error::io("read for hashing", error))?;
		if read == 0 {
			break;
		}
		hasher.update(&buffer[..read])?;
	}
	Ok(hasher.finalize())
}

fn ensure_file(
	dir: &Path,
	file: &PackFile,
	progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<()> {
	let target = dir.join(file.name);
	if let Ok(meta) = fs::metadata(&target) {
		if meta.len() == file.size {
			return Ok(());
		}
		fs::remove_file(&target).map_err(|error| Error::io("remove stale pack member", error))?;
	}
	let expected = Hash::new_from_hex(file.shake256).map_err(|_| {
		Error::internal(format!(
			"pack manifest digest for {} is not valid hex",
			file.name
		))
	})?;
	let partial = dir.join(format!("{}.partial", file.name));
	let mut last = None;
	for url in file.urls {
		match fetch_and_verify(url, file, &partial, &expected, progress) {
			Ok(()) => {
				fs::rename(&partial, &target).map_err(|error| Error::io("publish pack member", error))?;
				return Ok(());
			}
			Err(error) => {
				if error.kind() == oa::ErrorKind::Cancelled {
					return Err(error);
				}
				// Keep resumable partials; drop verified-corrupt or oversized
				// data so the next mirror or run starts clean.
				let length = fs::metadata(&partial).map(|meta| meta.len()).unwrap_or(0);
				if error.kind() == oa::ErrorKind::DataLoss || length > file.size {
					let _ = fs::remove_file(&partial);
				}
				last = Some(error);
			}
		}
	}
	Err(last.unwrap_or_else(|| {
		Error::unavailable(format!("pack member {} declares no mirrors", file.name))
	}))
}

fn fetch_and_verify(
	url: &str,
	file: &PackFile,
	partial: &Path,
	expected: &Hash,
	progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<()> {
	fetch(url, file, partial, progress)?;
	let length = fs::metadata(partial)
		.map_err(|error| Error::io("inspect fetched pack member", error))?
		.len();
	if length != file.size {
		return Err(Error::data_loss(format!(
			"pack member {url} fetched {length} bytes, expected {}",
			file.size
		)));
	}
	let digest = hash_file(partial)?;
	if digest.as_bytes() != expected.as_bytes() {
		return Err(Error::data_loss(format!(
			"pack member {url} digest {} does not match manifest",
			digest.to_hex()
		)));
	}
	Ok(())
}

fn fetch(
	url: &str,
	file: &PackFile,
	partial: &Path,
	progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<()> {
	if let Some(source) = url.strip_prefix("file://") {
		return copy_local(Path::new(source), file, partial, progress);
	}
	let existing = fs::metadata(partial).map(|meta| meta.len()).unwrap_or(0);
	if existing > file.size {
		fs::remove_file(partial).map_err(|error| Error::io("remove oversized partial", error))?;
	} else if existing == file.size && existing > 0 {
		// A previous run completed the download but crashed before publish;
		// let the digest check decide instead of refetching.
		return Ok(());
	}
	let mut child = Command::new("curl")
		.args(["-fLsS", "--retry", "2", "-C", "-", "-o"])
		.arg(partial)
		.arg(url)
		.stderr(Stdio::piped())
		.spawn()
		.map_err(|error| Error::io("spawn curl", error))?;
	loop {
		match child.try_wait() {
			Ok(Some(status)) => {
				if !status.success() {
					let stderr = child
						.stderr
						.take()
						.and_then(|mut pipe| {
							let mut text = String::new();
							pipe.read_to_string(&mut text).ok().map(|_| text)
						})
						.unwrap_or_default();
					return Err(Error::unavailable(format!(
						"fetch {url} failed: {}",
						stderr.trim()
					)));
				}
				let fetched = fs::metadata(partial).map(|meta| meta.len()).unwrap_or(0);
				let _ = progress(FetchProgress {
					file: file.name,
					fetched,
					total: file.size,
				});
				return Ok(());
			}
			Ok(None) => {
				let fetched = fs::metadata(partial).map(|meta| meta.len()).unwrap_or(0);
				if !progress(FetchProgress {
					file: file.name,
					fetched,
					total: file.size,
				}) {
					let _ = child.kill();
					let _ = child.wait();
					return Err(Error::cancelled(format!("fetch {} cancelled", file.name)));
				}
				std::thread::sleep(FETCH_POLL);
			}
			Err(error) => {
				let _ = child.kill();
				let _ = child.wait();
				return Err(Error::io("wait on curl", error));
			}
		}
	}
}

fn copy_local(
	source: &Path,
	file: &PackFile,
	partial: &Path,
	progress: &mut impl FnMut(FetchProgress) -> bool,
) -> Result<()> {
	let mut fetched = fs::metadata(partial).map(|meta| meta.len()).unwrap_or(0);
	if fetched > file.size {
		fs::remove_file(partial).map_err(|error| Error::io("remove oversized partial", error))?;
		fetched = 0;
	}
	if fetched == file.size && fetched > 0 {
		return Ok(());
	}
	let mut input =
		fs::File::open(source).map_err(|error| Error::io("open local pack member source", error))?;
	input
		.seek(SeekFrom::Start(fetched))
		.map_err(|error| Error::io("seek local pack member source", error))?;
	let mut output = fs::OpenOptions::new()
		.create(true)
		.append(true)
		.open(partial)
		.map_err(|error| Error::io("open pack partial", error))?;
	let mut buffer = vec![0_u8; HASH_CHUNK];
	loop {
		let read = input
			.read(&mut buffer)
			.map_err(|error| Error::io("read local pack member source", error))?;
		if read == 0 {
			return Ok(());
		}
		output
			.write_all(&buffer[..read])
			.map_err(|error| Error::io("write pack partial", error))?;
		fetched += read as u64;
		if !progress(FetchProgress {
			file: file.name,
			fetched,
			total: file.size,
		}) {
			return Err(Error::cancelled(format!("fetch {} cancelled", file.name)));
		}
	}
}