A close waited, unbounded, for a running books.back_up_now (git only Backups::shut could cancel), for a restore, create or adopt (they hold the one lifecycle lock, and at the server's close admin_jobs.wait() waited for them before anything was released), and, through the store lock, for a verification or a package reading every stored file. The bound's T_calls term was not finite. Now a close calls hurry at its start, and the stoppable work looks at it: books.back_up_now and timer-asked backups (git stopped, a stopped run records 'stopped' and no result of its own), books.verify (per journal line and file; a heal of a drifted database is begun only if no stop came before it: stopped half way it would leave the live ledger part replayed), books.export_cpa_package (per stored file). Such a call answers that it was stopped because the books are closing, and its access record says refused: stopped_by_close. On the admin socket restore, adopt, create and open take a JobStop: asked at each phase and file copied, they remove what they wrote and leave the root byte for byte as it was; the renames that put the books in place are the point of no return, after which they finish (two renames and two syncs). A close of any book, and the server's own, raises the flag before it waits for the lifecycle lock. A close whose last backup had to be killed in the owner's folder now says which file was left, and the target's backup is marked failed by shut when the run's future was dropped at its bound. INVARIANT 10 is rewritten so each term of the bound is enforced by code, and lists the calls that can run long (and the one known exception, books.import_bank_csv); INVARIANTS 8 and 9 say exactly which refusals are recorded and why an unrecorded one never ran; the format upgrade's wording says a stop is honoured up to its final look. Tests: the close is held to the bound while a backup call, a verification, a package and each admin job is running and would never end; the stopped job leaves the root byte for byte as it was; a lock left in a backup folder is reported and never removed. Sleeps that stood for 'the other side got there' now wait on the condition (Expect: 100-continue for a half-sent body, a writable named pipe for a fetch that is reading). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
575 lines
22 KiB
Rust
575 lines
22 KiB
Rust
//! Files on their way out (plan 4.7, D-29, D-46): a file the server hands
|
|
//! an agent (the CPA package, the 1099 worksheet, a W-9, a report export,
|
|
//! a label sheet, a receipt's copy) is kept here a while, for the token
|
|
//! that asked for it, until `wanderbooks download` fetches it.
|
|
//!
|
|
//! ```text
|
|
//! GOAL: an agent hands the human a file of any size the books make,
|
|
//! without its bytes passing through the agent's context or naming a
|
|
//! path on the server.
|
|
//! SOURCE: plan 4.7 (`download_link`: "valid 10 minutes, access-logged");
|
|
//! D-29 ("their files are made by the server and handed over by a
|
|
//! command, `wanderbooks download`"); D-46 ("files never pass
|
|
//! through an agent's context, in either direction").
|
|
//! INVARIANTS:
|
|
//! 1. An export is kept for one token, under an id of no digits (the
|
|
//! access log masks what could be a TIN), and lasts EXPORT_TTL; only
|
|
//! that token fetches it, as often as it likes until it lapses.
|
|
//! 2. It is written aside and renamed whole: a fetch gets the whole file
|
|
//! or none, and its bytes hash to the SHA-256 kept with it.
|
|
//! 3. Exports live in the book's `exports/` folder (0700, files 0600);
|
|
//! lapsed ones are swept when the books open, whenever one is made,
|
|
//! and by the server's sweeper. A token has at most MAX_LIVE.
|
|
//! 4. No error names a path.
|
|
//! 5. Exports can be closed (`close`, under the lock every change to the
|
|
//! folder is made under): after it none is made, given or swept by
|
|
//! any holder of these exports from before, so a book let go is
|
|
//! never touched by a late request or a sweep, whoever has it open
|
|
//! by then. A fetch (`get`) reads under that lock: `close` waits for
|
|
//! one under way, so no byte of a closed book's folder is read for a
|
|
//! caller after `close` has returned.
|
|
//! ```
|
|
|
|
use crate::journal::sha256_hex;
|
|
use crate::uploads::{file_name_problem, mime_problem};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::io::Write;
|
|
use std::path::{Path, PathBuf};
|
|
|
|
/// The exports' folder, in a book's folder.
|
|
pub const EXPORTS_DIR: &str = "exports";
|
|
/// How long an export is kept, seconds.
|
|
pub const EXPORT_TTL: i64 = 15 * 60;
|
|
/// The most exports one token may have kept at once (INVARIANT 3): every
|
|
/// file an agent takes away is one since D-46 (each report it exports,
|
|
/// each receipt it reads back), so a month-end's worth fits.
|
|
pub const MAX_LIVE: usize = 64;
|
|
|
|
/// What an export is (INVARIANT 1): its bytes are beside it.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct Export {
|
|
/// Its id: `ex_` and 32 letters `a` to `p`.
|
|
pub id: String,
|
|
/// The token it was made for.
|
|
pub token: String,
|
|
/// The file's name.
|
|
pub file_name: String,
|
|
/// Its MIME type.
|
|
pub mime: String,
|
|
/// Its size.
|
|
pub size: u64,
|
|
/// Its SHA-256, hex.
|
|
pub sha256: String,
|
|
/// When it lapses (Unix seconds).
|
|
pub expires: i64,
|
|
}
|
|
|
|
/// Why an export can't be kept or fetched. Never names a path.
|
|
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
|
pub enum ExportError {
|
|
/// No such export, or it lapsed.
|
|
#[error("there is no export {0:?}, or it lapsed (an export is kept 15 minutes): make it again")]
|
|
Unknown(String),
|
|
/// Another token's.
|
|
#[error("that export was made for another token")]
|
|
NotYours,
|
|
/// Kept, but not whole (changed on the disk).
|
|
#[error("that export was damaged on the disk: make it again")]
|
|
Damaged,
|
|
/// A request that can't make one.
|
|
#[error("{0}")]
|
|
Bad(String),
|
|
/// The disk refused.
|
|
#[error("the export couldn't be kept: {0}")]
|
|
Io(std::io::ErrorKind),
|
|
}
|
|
|
|
impl From<std::io::Error> for ExportError {
|
|
fn from(e: std::io::Error) -> Self {
|
|
ExportError::Io(e.kind())
|
|
}
|
|
}
|
|
|
|
/// A book's exports.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Exports {
|
|
dir: PathBuf,
|
|
/// Held while exports are made or swept (INVARIANT 3). True once
|
|
/// closed (INVARIANT 5).
|
|
lock: std::sync::Arc<std::sync::Mutex<bool>>,
|
|
}
|
|
|
|
/// `ex_` and 32 letters `a` to `p`, a letter a nibble (INVARIANT 1).
|
|
pub fn is_id(id: &str) -> bool {
|
|
id.len() == 3 + 32
|
|
&& id.starts_with("ex_")
|
|
&& id[3..].bytes().all(|b| (b'a'..=b'p').contains(&b))
|
|
}
|
|
|
|
fn new_id(raw: &[u8; 16]) -> String {
|
|
let mut id = String::from("ex_");
|
|
for b in raw {
|
|
id.push(char::from(b'a' + (b >> 4)));
|
|
id.push(char::from(b'a' + (b & 0x0f)));
|
|
}
|
|
id
|
|
}
|
|
|
|
fn private_new(path: &Path) -> std::io::Result<std::fs::File> {
|
|
let mut o = std::fs::OpenOptions::new();
|
|
o.write(true).create_new(true);
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::OpenOptionsExt;
|
|
o.mode(0o600);
|
|
}
|
|
o.open(path)
|
|
}
|
|
|
|
impl Exports {
|
|
/// The exports of the book in `book_dir` (its folder made, private, if
|
|
/// missing), lapsed ones swept.
|
|
pub fn open(book_dir: &Path, now: i64) -> Result<Self, ExportError> {
|
|
let dir = crate::root::private_book_folder(book_dir, EXPORTS_DIR)
|
|
.map_err(|e| ExportError::Bad(format!("the exports can't be kept: {e}")))?;
|
|
let exports = Self {
|
|
dir,
|
|
lock: Default::default(),
|
|
};
|
|
exports.sweep(now);
|
|
Ok(exports)
|
|
}
|
|
|
|
fn locked(&self) -> std::sync::MutexGuard<'_, bool> {
|
|
self.lock.lock().unwrap_or_else(|e| e.into_inner())
|
|
}
|
|
|
|
/// Change nothing in these exports, and give none, from now on
|
|
/// (INVARIANT 5): waits for the change being made, if one is.
|
|
pub fn close(&self) {
|
|
*self.locked() = true;
|
|
}
|
|
|
|
fn path(&self, id: &str, ext: &str) -> PathBuf {
|
|
self.dir.join(format!("{id}.{ext}"))
|
|
}
|
|
|
|
/// Keep `bytes` as `file_name` for `token` (INVARIANTS 1-3).
|
|
pub fn put(
|
|
&self,
|
|
token: &str,
|
|
file_name: &str,
|
|
mime: &str,
|
|
bytes: &[u8],
|
|
now: i64,
|
|
) -> Result<Export, ExportError> {
|
|
if let Some(why) = file_name_problem(file_name).or_else(|| mime_problem(mime)) {
|
|
return Err(ExportError::Bad(why));
|
|
}
|
|
let held = self.locked();
|
|
if *held {
|
|
return Err(ExportError::Bad("the books aren't open".into()));
|
|
}
|
|
self.sweep_held(now);
|
|
if self.live_for(token, now) >= MAX_LIVE {
|
|
return Err(ExportError::Bad(format!(
|
|
"this agent has {MAX_LIVE} exports kept: each lapses 15 minutes after it was made; make this one again then"
|
|
)));
|
|
}
|
|
let mut raw = [0u8; 16];
|
|
getrandom::getrandom(&mut raw)
|
|
.map_err(|_| ExportError::Bad("the system gave no randomness".into()))?;
|
|
let export = Export {
|
|
id: new_id(&raw),
|
|
token: token.to_string(),
|
|
file_name: file_name.to_string(),
|
|
mime: mime.to_string(),
|
|
size: bytes.len() as u64,
|
|
sha256: sha256_hex(bytes),
|
|
expires: now + EXPORT_TTL,
|
|
};
|
|
// The bytes first, then the record that names them: each written
|
|
// aside and renamed (INVARIANT 2).
|
|
for (ext, content) in [
|
|
("bin", bytes.to_vec()),
|
|
(
|
|
"json",
|
|
serde_json::to_vec(&export).expect("an export serializes"),
|
|
),
|
|
] {
|
|
let aside = self.path(&export.id, &format!("{ext}.new"));
|
|
let mut f = private_new(&aside)?;
|
|
f.write_all(&content)?;
|
|
f.sync_all()?;
|
|
drop(f);
|
|
std::fs::rename(&aside, self.path(&export.id, ext))?;
|
|
}
|
|
Ok(export)
|
|
}
|
|
|
|
/// Export `id` for `token` and its bytes, checked (INVARIANTS 1, 2).
|
|
pub fn get(&self, id: &str, token: &str, now: i64) -> Result<(Export, Vec<u8>), ExportError> {
|
|
if !is_id(id) {
|
|
return Err(ExportError::Unknown(id.chars().take(40).collect()));
|
|
}
|
|
let unknown = || ExportError::Unknown(id.to_string());
|
|
// Read under the lock a close takes (INVARIANT 5): `close` waits
|
|
// for a fetch under way, and none begins after it.
|
|
let held = self.locked();
|
|
if *held {
|
|
return Err(ExportError::Bad("the books aren't open".into()));
|
|
}
|
|
let record = std::fs::read(self.path(id, "json")).map_err(|_| unknown())?;
|
|
let export: Export = serde_json::from_slice(&record).map_err(|_| unknown())?;
|
|
if export.expires <= now {
|
|
return Err(unknown());
|
|
}
|
|
if export.token != token {
|
|
return Err(ExportError::NotYours);
|
|
}
|
|
let bytes = std::fs::read(self.path(id, "bin")).map_err(|_| unknown())?;
|
|
drop(held);
|
|
if bytes.len() as u64 != export.size || sha256_hex(&bytes) != export.sha256 {
|
|
return Err(ExportError::Damaged);
|
|
}
|
|
Ok((export, bytes))
|
|
}
|
|
|
|
/// How many exports `token` has kept at `now`.
|
|
fn live_for(&self, token: &str, now: i64) -> usize {
|
|
let Ok(entries) = std::fs::read_dir(&self.dir) else {
|
|
return 0;
|
|
};
|
|
entries
|
|
.flatten()
|
|
.filter(|e| e.file_name().to_string_lossy().ends_with(".json"))
|
|
.filter_map(|e| std::fs::read(e.path()).ok())
|
|
.filter_map(|b| serde_json::from_slice::<Export>(&b).ok())
|
|
.filter(|x| x.token == token && x.expires > now)
|
|
.count()
|
|
}
|
|
|
|
/// Sweep away every export that lapsed by `now`, and anything left
|
|
/// without one (INVARIANT 3).
|
|
pub fn sweep(&self, now: i64) {
|
|
let held = self.locked();
|
|
if !*held {
|
|
self.sweep_held(now);
|
|
}
|
|
}
|
|
|
|
fn sweep_held(&self, now: i64) {
|
|
let Ok(entries) = std::fs::read_dir(&self.dir) else {
|
|
return;
|
|
};
|
|
for e in entries.flatten() {
|
|
let name = e.file_name().to_string_lossy().to_string();
|
|
let Some((id, _)) = name.split_once('.') else {
|
|
let _ = std::fs::remove_file(e.path());
|
|
continue;
|
|
};
|
|
let lapsed = std::fs::read(self.path(id, "json"))
|
|
.ok()
|
|
.and_then(|b| serde_json::from_slice::<Export>(&b).ok())
|
|
.is_none_or(|x| x.expires <= now);
|
|
if lapsed {
|
|
let _ = std::fs::remove_file(e.path());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
const NOW: i64 = 1_800_000_000;
|
|
|
|
/// Closed, exports are made, given and swept by no one (INVARIANT 5).
|
|
#[test]
|
|
fn closed_exports_change_nothing_and_give_nothing() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let e = x.put("t", "a.csv", "text/csv", b"x", NOW).unwrap();
|
|
x.close();
|
|
let seen = || {
|
|
let mut names: Vec<_> = std::fs::read_dir(dir.path().join(EXPORTS_DIR))
|
|
.unwrap()
|
|
.map(|f| f.unwrap().file_name())
|
|
.collect();
|
|
names.sort();
|
|
names
|
|
};
|
|
let before = seen();
|
|
assert!(x.put("t", "b.csv", "text/csv", b"y", NOW).is_err());
|
|
assert!(x.get(&e.id, "t", NOW).is_err());
|
|
// A sweep long after everything lapsed removes nothing.
|
|
x.sweep(NOW + 10 * EXPORT_TTL);
|
|
assert_eq!(seen(), before);
|
|
}
|
|
|
|
/// INVARIANT 5: a fetch reads under the lock a close takes, so a close
|
|
/// waits for the fetch under way instead of returning while bytes of
|
|
/// the folder are still being read for a caller. The fetch is held
|
|
/// inside its read (the export's file is a named pipe nothing writes
|
|
/// to yet), and `close` must not return until it is let go.
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn a_close_waits_for_a_fetch_that_is_reading() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let e = x.put("t", "a.csv", "text/csv", b"x", NOW).unwrap();
|
|
let bin = x.path(&e.id, "bin");
|
|
std::fs::remove_file(&bin).unwrap();
|
|
let made = std::process::Command::new("mkfifo").arg(&bin).status();
|
|
assert!(made.unwrap().success());
|
|
let fetching = {
|
|
let (x, id) = (x.clone(), e.id.clone());
|
|
std::thread::spawn(move || x.get(&id, "t", NOW))
|
|
};
|
|
// The fetch holds the lock, blocked opening the pipe for reading (no
|
|
// writer yet). A writer can be opened without blocking only once a
|
|
// reader has the pipe open, so that is the condition waited for (the
|
|
// writer, kept, is the one that lets the read end later).
|
|
use std::os::unix::fs::OpenOptionsExt;
|
|
let by = std::time::Instant::now() + std::time::Duration::from_secs(60);
|
|
let writer = loop {
|
|
match std::fs::OpenOptions::new()
|
|
.write(true)
|
|
.custom_flags(libc::O_NONBLOCK)
|
|
.open(&bin)
|
|
{
|
|
Ok(w) => break w,
|
|
Err(_) => {
|
|
assert!(
|
|
std::time::Instant::now() < by,
|
|
"the fetch never reached the pipe"
|
|
);
|
|
std::thread::sleep(std::time::Duration::from_millis(5));
|
|
}
|
|
}
|
|
};
|
|
let (begun, started) = std::sync::mpsc::channel();
|
|
let (done, closed) = std::sync::mpsc::channel();
|
|
let closing = {
|
|
let x = x.clone();
|
|
std::thread::spawn(move || {
|
|
let _ = begun.send(());
|
|
x.close();
|
|
let _ = done.send(());
|
|
})
|
|
};
|
|
started.recv().unwrap();
|
|
// A negative window: it can only fail if the close returned, never
|
|
// because the machine was slow.
|
|
assert!(
|
|
closed
|
|
.recv_timeout(std::time::Duration::from_millis(500))
|
|
.is_err(),
|
|
"the close returned while a fetch was reading the folder"
|
|
);
|
|
// A writer that sends nothing lets the read end (an empty file,
|
|
// not the size kept: the fetch is refused).
|
|
drop(writer);
|
|
assert!(fetching.join().unwrap().is_err());
|
|
closed
|
|
.recv_timeout(std::time::Duration::from_secs(5))
|
|
.expect("the close returns once the fetch has ended");
|
|
closing.join().unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn an_export_is_kept_fifteen_minutes() {
|
|
// What its refusal tells the human: kept 15 minutes, then gone.
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let e = x.put("t", "a.csv", "text/csv", b"x", NOW).unwrap();
|
|
assert_eq!(e.expires, NOW + 15 * 60);
|
|
assert!(x.get(&e.id, "t", NOW + 15 * 60 - 1).is_ok());
|
|
let lapsed = x.get(&e.id, "t", NOW + 15 * 60).unwrap_err();
|
|
assert!(lapsed.to_string().contains("kept 15 minutes"), "{lapsed}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_id_is_ex_and_thirty_two_letters_a_to_p_and_nothing_else() {
|
|
assert!(is_id(&format!("ex_{}", "a".repeat(32))));
|
|
assert!(is_id(&format!("ex_{}", "p".repeat(32))));
|
|
for bad in [
|
|
String::new(),
|
|
"ex_".into(),
|
|
format!("ex_{}", "a".repeat(31)),
|
|
format!("ex_{}", "a".repeat(33)),
|
|
format!("up_{}", "a".repeat(32)),
|
|
format!("exx{}", "a".repeat(32)),
|
|
format!("ex_{}", "q".repeat(32)),
|
|
format!("ex_{}0", "a".repeat(31)),
|
|
format!("ex_{}.", "a".repeat(31)),
|
|
] {
|
|
assert!(!is_id(&bad), "{bad}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_fetch_never_reaches_past_the_exports_folder() {
|
|
// A whole export kept beside the books, not among their exports:
|
|
// no id names it.
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let bytes = b"not an export".to_vec();
|
|
for id in ["../outside", &format!("up_{}", "a".repeat(32))] {
|
|
let at = dir.path().join(EXPORTS_DIR).join(id);
|
|
let record = Export {
|
|
id: id.into(),
|
|
token: "t".into(),
|
|
file_name: "a.csv".into(),
|
|
mime: "text/csv".into(),
|
|
size: bytes.len() as u64,
|
|
sha256: sha256_hex(&bytes),
|
|
expires: NOW + EXPORT_TTL,
|
|
};
|
|
std::fs::write(
|
|
at.with_extension("json"),
|
|
serde_json::to_vec(&record).unwrap(),
|
|
)
|
|
.unwrap();
|
|
std::fs::write(at.with_extension("bin"), &bytes).unwrap();
|
|
assert!(
|
|
matches!(x.get(id, "t", NOW).unwrap_err(), ExportError::Unknown(_)),
|
|
"{id}"
|
|
);
|
|
}
|
|
// And a refusal quotes no more than 40 characters of what it got.
|
|
let long = format!("../{}", "x".repeat(100));
|
|
let ExportError::Unknown(quoted) = x.get(&long, "t", NOW).unwrap_err() else {
|
|
panic!("an unknown export")
|
|
};
|
|
assert_eq!(quoted.chars().count(), 40);
|
|
}
|
|
|
|
#[test]
|
|
fn an_export_counts_against_its_token_until_the_moment_it_lapses() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let e = x.put("t", "a.csv", "text/csv", b"x", NOW).unwrap();
|
|
assert_eq!(x.live_for("t", e.expires - 1), 1);
|
|
// Lapsed at its expiry, as a fetch then finds (not yet swept).
|
|
assert_eq!(x.live_for("t", e.expires), 0);
|
|
assert_eq!(x.live_for("u", NOW), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn an_export_is_fetched_by_its_token_until_it_lapses() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let bytes = b"PK cpa package".to_vec();
|
|
let e = x
|
|
.put("tok_a", "cpa-2026.zip", "application/zip", &bytes, NOW)
|
|
.unwrap();
|
|
assert!(is_id(&e.id));
|
|
assert!(!e.id[3..].bytes().any(|b| b.is_ascii_digit()));
|
|
assert_eq!(e.expires, NOW + EXPORT_TTL);
|
|
assert_eq!(e.sha256, sha256_hex(&bytes));
|
|
// As often as it likes, by its own token only.
|
|
for _ in 0..2 {
|
|
let (got, b) = x.get(&e.id, "tok_a", NOW + 1).unwrap();
|
|
assert_eq!((got, b), (e.clone(), bytes.clone()));
|
|
}
|
|
assert_eq!(
|
|
x.get(&e.id, "tok_b", NOW).unwrap_err(),
|
|
ExportError::NotYours
|
|
);
|
|
assert!(matches!(
|
|
x.get(&e.id, "tok_a", NOW + EXPORT_TTL).unwrap_err(),
|
|
ExportError::Unknown(_)
|
|
));
|
|
for bad in [
|
|
"../../journal",
|
|
"ex_",
|
|
"up_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
"",
|
|
] {
|
|
assert!(matches!(
|
|
x.get(bad, "tok_a", NOW).unwrap_err(),
|
|
ExportError::Unknown(_)
|
|
));
|
|
}
|
|
// Swept once lapsed, nothing left.
|
|
x.sweep(NOW + EXPORT_TTL);
|
|
assert_eq!(
|
|
std::fs::read_dir(dir.path().join(EXPORTS_DIR))
|
|
.unwrap()
|
|
.count(),
|
|
0
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_damaged_export_is_never_handed_over() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let e = x
|
|
.put("t", "w9.pdf", "application/pdf", b"%PDF-1.4 W-9", NOW)
|
|
.unwrap();
|
|
let bin = dir.path().join(EXPORTS_DIR).join(format!("{}.bin", e.id));
|
|
std::fs::write(&bin, b"%PDF-1.4 W-8").unwrap();
|
|
assert_eq!(x.get(&e.id, "t", NOW).unwrap_err(), ExportError::Damaged);
|
|
std::fs::write(&bin, b"%PDF").unwrap();
|
|
assert_eq!(x.get(&e.id, "t", NOW).unwrap_err(), ExportError::Damaged);
|
|
}
|
|
|
|
#[test]
|
|
fn a_token_keeps_only_so_many_and_names_are_plain() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
for _ in 0..MAX_LIVE {
|
|
x.put("t", "a.csv", "text/csv", b"x", NOW).unwrap();
|
|
}
|
|
assert!(matches!(
|
|
x.put("t", "a.csv", "text/csv", b"x", NOW).unwrap_err(),
|
|
ExportError::Bad(_)
|
|
));
|
|
x.put("u", "a.csv", "text/csv", b"x", NOW).unwrap();
|
|
// Lapsed ones don't count: making one sweeps them.
|
|
x.put("t", "a.csv", "text/csv", b"x", NOW + EXPORT_TTL)
|
|
.unwrap();
|
|
assert_eq!(
|
|
std::fs::read_dir(dir.path().join(EXPORTS_DIR))
|
|
.unwrap()
|
|
.count(),
|
|
2,
|
|
"every lapsed one swept (both tokens'): the new one kept, a record and its bytes"
|
|
);
|
|
for (name, mime) in [("../x.zip", "application/zip"), ("x.zip", "zip")] {
|
|
assert!(matches!(
|
|
x.put("u", name, mime, b"x", NOW).unwrap_err(),
|
|
ExportError::Bad(_)
|
|
));
|
|
}
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[test]
|
|
fn its_folder_and_files_are_private_and_strays_go() {
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let x = Exports::open(dir.path(), NOW).unwrap();
|
|
let e = x.put("t", "a.csv", "text/csv", b"x", NOW).unwrap();
|
|
let folder = dir.path().join(EXPORTS_DIR);
|
|
let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777;
|
|
assert_eq!(mode(&folder), 0o700);
|
|
for ext in ["json", "bin"] {
|
|
assert_eq!(mode(&folder.join(format!("{}.{ext}", e.id))), 0o600);
|
|
}
|
|
// A file left half made, or with no record, is swept.
|
|
std::fs::write(folder.join("ex_x.bin.new"), b"half").unwrap();
|
|
std::fs::write(folder.join("stray"), b"x").unwrap();
|
|
let _ = Exports::open(dir.path(), NOW).unwrap();
|
|
let mut left: Vec<String> = std::fs::read_dir(&folder)
|
|
.unwrap()
|
|
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
|
.collect();
|
|
left.sort();
|
|
assert_eq!(left, [format!("{}.bin", e.id), format!("{}.json", e.id)]);
|
|
}
|
|
}
|