use std::io;
use std::sync::{Arc, Mutex, MutexGuard, PoisonError};
use std::time::{Duration, Instant, SystemTime};
use crate::backend::FileInfo;
use crate::error::{SmbError, SmbResult};
use crate::utils::now_filetime;
use super::cachedir::new_id;
use super::data::DataFile;
use super::dirmove::Batch;
use super::error::S3Error;
use super::keys::{dir_prefix, last_name, path_below};
use super::lookup::{DirRef, Found};
use super::meta;
use super::object::ObjectHead;
use super::ranges::RangeSet;
use super::readahead::{Pin, ReadAhead, Version};
use super::record::{BaseRecord, EntryRecord, LocalRecord, RecordId, copy_source};
use super::retry::Stop;
pub(super) struct Local {
pub(super) file: Arc<DataFile>,
pub(super) len: u64,
pub(super) present: RangeSet,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Own {
pub(super) times: [u64; 4],
pub(super) attributes: u32,
}
impl Own {
pub(super) fn changed_from(&mut self, before: &Own) -> bool {
if self == before {
return false;
}
if self.times[3] == before.times[3] {
self.times[3] = now_filetime();
}
true
}
}
pub(super) struct Base {
pub(super) key: String,
pub(super) head: Arc<ObjectHead>,
pub(super) bare: bool,
_pin: Option<Pin>,
}
impl Base {
pub(super) fn new(
ahead: &Arc<ReadAhead>,
key: String,
head: Arc<ObjectHead>,
is_file: bool,
) -> Self {
let pin = is_file
.then(|| Version::of(&key, &head))
.flatten()
.map(|version| ahead.pin(version));
Base {
key,
head,
bare: false,
_pin: pin,
}
}
pub(super) fn listed(
ahead: &Arc<ReadAhead>,
key: String,
head: Arc<ObjectHead>,
is_file: bool,
) -> Self {
Base {
bare: true,
..Base::new(ahead, key, head, is_file)
}
}
}
#[cfg(test)]
impl Base {
pub(super) fn at(key: &str) -> Base {
let ahead = Arc::new(ReadAhead::new());
let head = Arc::new(ObjectHead {
size: 1,
etag: "\"e\"".to_owned(),
last_modified: SystemTime::UNIX_EPOCH,
metadata: super::object::Metadata::default(),
});
Base::new(&ahead, key.to_owned(), head, true)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) struct Sealed {
pub(super) generation: u64,
pub(super) len: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct Upload {
pub(super) key: String,
pub(super) id: String,
}
#[derive(Debug, Default)]
pub(super) struct Counted {
pub(super) gone: Vec<String>,
pub(super) source: Option<String>,
}
pub(super) enum Body {
File {
local: Option<Local>,
sealed: Option<Sealed>,
upload: Option<Upload>,
},
Dir {
marker_pending: bool,
},
}
pub(super) struct State {
pub(super) id: Option<RecordId>,
pub(super) key: String,
pub(super) base: Option<Base>,
pub(super) may_exist: bool,
pub(super) own: Option<Own>,
pub(super) generation: u64,
pub(super) handles: u32,
pub(super) open: u32,
pub(super) changed: Instant,
pub(super) rest_until: Option<Instant>,
pub(super) recorded: bool,
pub(super) failure: Option<S3Error>,
pub(super) gone: Vec<String>,
pub(super) job: Option<Stop>,
pub(super) queued: bool,
pub(super) eager: bool,
pub(super) uploaded: u64,
pub(super) creating: bool,
pub(super) detached: bool,
pub(super) counted: Counted,
pub(super) writing: Vec<String>,
pub(super) prior: Option<String>,
pub(super) recorded_source: Option<String>,
pub(super) batch: Option<Arc<Batch>>,
pub(super) body: Body,
}
impl State {
pub(super) fn new(key: String, is_dir: bool) -> Self {
State {
id: None,
key,
base: None,
may_exist: false,
own: None,
generation: 0,
handles: 0,
open: 0,
changed: Instant::now(),
rest_until: None,
recorded: false,
failure: None,
gone: Vec::new(),
job: None,
queued: false,
eager: false,
uploaded: 0,
creating: false,
detached: false,
counted: Counted::default(),
writing: Vec::new(),
prior: None,
recorded_source: None,
batch: None,
body: if is_dir {
Body::Dir {
marker_pending: false,
}
} else {
Body::File {
local: None,
sealed: None,
upload: None,
}
},
}
}
pub(super) fn is_dir(&self) -> bool {
matches!(self.body, Body::Dir { .. })
}
pub(super) fn local(&self) -> Option<&Local> {
match &self.body {
Body::File { local, .. } => local.as_ref(),
Body::Dir { .. } => None,
}
}
pub(super) fn local_mut(&mut self) -> Option<&mut Local> {
match &mut self.body {
Body::File { local, .. } => local.as_mut(),
Body::Dir { .. } => None,
}
}
pub(super) fn set_local(&mut self, new: Local) {
if self.open > 0 {
new.file.keep();
} else {
new.file.release();
}
self.put_local(new);
}
fn put_local(&mut self, new: Local) {
if let Body::File { local, .. } = &mut self.body {
*local = Some(new);
}
}
pub(super) fn opened(&mut self) {
self.open += 1;
if let Some(local) = self.local() {
local.file.keep();
}
}
pub(super) fn closed(&mut self) {
self.open = self.open.saturating_sub(1);
if self.open == 0
&& let Some(local) = self.local()
{
local.file.release();
}
}
pub(super) fn sealed(&self) -> Option<Sealed> {
match &self.body {
Body::File { sealed, .. } => *sealed,
Body::Dir { .. } => None,
}
}
pub(super) fn set_sealed(&mut self, new: Sealed) {
if let Body::File { sealed, .. } = &mut self.body {
*sealed = Some(new);
}
}
pub(super) fn unsealed(&self) -> bool {
self.local().is_some()
&& self
.sealed()
.is_none_or(|sealed| sealed.generation != self.generation)
}
pub(super) fn in_batch(&self, batch: &Arc<Batch>) -> bool {
self.batch
.as_ref()
.is_some_and(|held| Arc::ptr_eq(held, batch))
}
pub(super) fn set_marker_pending(&mut self, pending: bool) {
if let Body::Dir { marker_pending } = &mut self.body {
*marker_pending = pending;
}
}
pub(super) fn attached(&self) -> SmbResult<()> {
if self.detached {
return Err(SmbError::AccessDenied);
}
Ok(())
}
pub(super) fn bump(&mut self) {
self.generation += 1;
self.changed = Instant::now();
self.eager = false;
if let Some(stop) = &self.job {
stop.stop();
}
}
pub(super) fn has_work(&self) -> bool {
self.writes() || !self.gone.is_empty()
}
pub(super) fn writes(&self) -> bool {
let held = match &self.body {
Body::File { local, .. } => local.is_some(),
Body::Dir { marker_pending } => *marker_pending,
};
held || self.own.is_some() || self.moved()
}
pub(super) fn in_bucket(&self) -> bool {
self.may_exist
|| self
.base
.as_ref()
.is_some_and(|base| self.is_write_key(&base.key))
}
pub(super) fn write_key(&self) -> String {
if self.is_dir() {
dir_prefix(&self.key)
} else {
self.key.clone()
}
}
pub(super) fn is_write_key(&self, key: &str) -> bool {
if self.is_dir() {
key.strip_suffix('/') == Some(self.key.as_str())
} else {
key == self.key
}
}
pub(super) fn source(&self) -> Option<&str> {
copy_source(
self.is_dir(),
&self.key,
self.base.as_ref().map(|base| base.key.as_str()),
|| self.covers(),
)
.or(self.recorded_source.as_deref())
}
pub(super) fn needs_source(&self) -> bool {
self.source().is_some()
}
pub(super) fn set_recorded(&mut self, record: &EntryRecord) {
self.recorded = true;
self.recorded_source = record.source().map(str::to_owned);
}
fn covers(&self) -> bool {
self.local()
.is_some_and(|local| local.present.covers(local.len))
}
pub(super) fn moved_from(&self) -> Option<&str> {
copy_source(
self.is_dir(),
&self.key,
self.base.as_ref().map(|base| base.key.as_str()),
|| false,
)
}
pub(super) fn moved(&self) -> bool {
self.moved_from().is_some()
}
pub(super) fn job_due(&self, delay: Duration) -> Option<Instant> {
let blocked = self.job.is_some()
|| self.failure.is_some()
|| self.detached
|| self.creating
|| !self.has_work();
if blocked {
return None;
}
if self.eager {
return Some(self.changed);
}
let due = self.changed.checked_add(delay)?;
Some(self.rest_until.map_or(due, |rest| due.max(rest)))
}
pub(super) fn path(&self, prefix: &str) -> String {
path_below(prefix, &self.key)
}
pub(super) fn pending(&self) -> bool {
self.has_work() || self.recorded
}
pub(super) fn id(&mut self) -> io::Result<RecordId> {
if let Some(id) = &self.id {
return Ok(id.clone());
}
let id = new_id()?;
self.id = Some(id.clone());
Ok(id)
}
pub(super) fn name(&self) -> &str {
last_name(&self.key)
}
pub(super) fn dir_ref(&self) -> DirRef {
DirRef {
key: self.key.clone(),
name: self.name().to_owned(),
marker: None,
}
}
pub(super) fn own_or_base(&self, built: SystemTime) -> Own {
if let Some(own) = self.own {
return own;
}
match &self.base {
Some(base) => Own {
times: meta::head_times(&base.head),
attributes: meta::attributes(&base.head),
},
None => Own {
times: meta::same_times(built),
attributes: 0,
},
}
}
pub(super) fn size(&self) -> u64 {
match (&self.body, &self.base) {
(
Body::File {
local: Some(local), ..
},
_,
) => local.len,
(Body::File { local: None, .. }, Some(base)) => base.head.size,
_ => 0,
}
}
pub(super) fn wrote(&mut self, built: SystemTime, now: u64) {
let own = self.own_or_base(built);
let [created, accessed, ..] = own.times;
self.own = Some(Own {
times: [created, accessed, now, now],
attributes: own.attributes,
});
self.bump();
}
pub(super) fn landed(
&mut self,
ahead: &Arc<ReadAhead>,
generation: u64,
key: &str,
head: Arc<ObjectHead>,
) -> Landing {
if self.key != key {
return Landing::Moved;
}
self.prior = Some(head.etag.clone()).filter(|etag| !etag.is_empty());
self.base = Some(Base::new(ahead, key.to_owned(), head, true));
self.may_exist = true;
self.uploaded = generation;
if let Body::File { upload, .. } = &mut self.body {
*upload = None;
}
if self.generation == generation {
Landing::Whole
} else {
Landing::Newer
}
}
pub(super) fn stored(&mut self) {
if let Body::File { local, sealed, .. } = &mut self.body {
*local = None;
*sealed = None;
}
self.own = None;
}
pub(super) fn settled(&mut self) {
self.stored();
self.id = None;
self.recorded = false;
self.recorded_source = None;
}
pub(super) fn complete_base(
&mut self,
ahead: &Arc<ReadAhead>,
key: &str,
found: Arc<ObjectHead>,
) {
let bare = self
.base
.as_ref()
.is_some_and(|base| base.bare && base.key == key);
if bare {
self.base = Some(Base::new(ahead, key.to_owned(), found, !self.is_dir()));
}
}
pub(super) fn follow(&mut self, found: Found, ahead: &Arc<ReadAhead>) {
match found {
Found::File(file) => {
self.may_exist = true;
self.key = file.key.clone();
self.prior = Some(file.head.etag.clone()).filter(|etag| !etag.is_empty());
let same = self
.base
.as_ref()
.is_some_and(|base| *base.head == *file.head);
if !same {
self.base = Some(Base::new(ahead, file.key, file.head, true));
}
}
Found::Dir(dir) => {
self.may_exist = dir.marker.is_some();
self.key = dir.key;
self.base = dir.marker.map(|head| {
let key = format!("{}/", self.key);
Base::new(ahead, key, head, false)
});
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Landing {
Moved,
Newer,
Whole,
}
pub(super) enum Progress {
Done,
Waiting,
Failed(S3Error),
}
pub(super) struct Entry {
is_dir: bool,
io: Mutex<()>,
seal: Mutex<()>,
state: Mutex<State>,
}
impl Entry {
pub(super) fn from_state(state: State) -> Arc<Entry> {
Arc::new(Entry {
is_dir: state.is_dir(),
io: Mutex::new(()),
seal: Mutex::new(()),
state: Mutex::new(state),
})
}
pub(super) fn is_dir(&self) -> bool {
self.is_dir
}
pub(super) fn lock_io(&self) -> MutexGuard<'_, ()> {
self.io.lock().unwrap_or_else(PoisonError::into_inner)
}
pub(super) fn lock_seal(&self) -> MutexGuard<'_, ()> {
self.seal.lock().unwrap_or_else(PoisonError::into_inner)
}
pub(super) fn state(&self) -> MutexGuard<'_, State> {
self.state.lock().unwrap_or_else(PoisonError::into_inner)
}
pub(super) fn lock_all<'a>(
entries: &[&'a Arc<Entry>],
) -> Vec<(MutexGuard<'a, ()>, MutexGuard<'a, ()>)> {
let mut order = entries.to_vec();
order.sort_by_key(|entry| Arc::as_ptr(entry) as usize);
order.dedup_by(|a, b| Arc::ptr_eq(a, b));
order
.iter()
.map(|entry| (entry.lock_io(), entry.lock_seal()))
.collect()
}
pub(super) fn created_file(key: String, id: RecordId, now: u64, file: DataFile) -> Arc<Entry> {
let mut state = State::new(key, false);
state.id = Some(id);
state.put_local(Local {
file: Arc::new(file),
len: 0,
present: RangeSet::new(),
});
state.own = Some(Own {
times: [now; 4],
attributes: 0,
});
state.generation = 1;
Entry::from_state(state)
}
pub(super) fn created_dir(key: String, now: u64) -> Arc<Entry> {
let mut state = State::new(key, true);
state.own = Some(Own {
times: [now; 4],
attributes: 0,
});
state.generation = 1;
state.may_exist = true;
state.creating = true;
state.set_marker_pending(true);
Entry::from_state(state)
}
pub(super) fn found(found: Found, ahead: &Arc<ReadAhead>) -> Arc<Entry> {
let (key, is_dir) = match &found {
Found::File(file) => (file.key.clone(), false),
Found::Dir(dir) => (dir.key.clone(), true),
};
let mut state = State::new(key, is_dir);
state.follow(found, ahead);
Entry::from_state(state)
}
pub(super) fn recovered(
record: EntryRecord,
file: Option<DataFile>,
ahead: &Arc<ReadAhead>,
) -> Arc<Entry> {
let source = record.source().map(str::to_owned);
let mut state = State::new(record.key, record.is_dir);
state.id = Some(record.id);
state.may_exist = record.remote;
state.base = record.base.map(|base| {
let make = if base.bare { Base::listed } else { Base::new };
make(ahead, base.key, Arc::new(base.head), !record.is_dir)
});
match &mut state.body {
Body::File {
local,
sealed,
upload,
} => {
if let (Some(held), Some(file)) = (record.local, file) {
*sealed = Some(Sealed {
generation: record.generation,
len: held.len,
});
*local = Some(Local {
file: Arc::new(file),
len: held.len,
present: held.present,
});
}
*upload = record
.upload_id
.zip(record.upload_key)
.map(|(id, key)| Upload { key, id });
}
Body::Dir { marker_pending } => *marker_pending = record.marker_pending,
}
if record.meta_pending {
state.own = Some(Own {
times: record.times,
attributes: record.attributes,
});
}
state.generation = record.generation;
state.recorded = true;
state.recorded_source = source;
state.gone = record.gone;
state.eager = true;
Entry::from_state(state)
}
pub(super) fn progress(&self, generation: u64, gone: &[String]) -> Progress {
let state = self.state();
if let Some(failure) = &state.failure {
return Progress::Failed(failure.clone());
}
if state.detached || !state.pending() {
return Progress::Done;
}
let deleted = !state.gone.iter().any(|key| gone.contains(key));
if state.uploaded >= generation && deleted && state.job.is_none() {
return Progress::Done;
}
Progress::Waiting
}
pub(super) fn info(&self, built: SystemTime) -> FileInfo {
let state = self.state();
let own = state.own_or_base(built);
meta::entry_info(
state.name(),
self.is_dir,
state.size(),
own.times,
own.attributes,
)
}
pub(super) fn record(
state: &mut State,
prefix: &str,
built: SystemTime,
) -> io::Result<EntryRecord> {
let id = state.id()?;
let own = state.own_or_base(built);
let path = state.path(prefix);
let (local, upload, marker_pending) = match &state.body {
Body::File { local, upload, .. } => (
local.as_ref().map(|local| LocalRecord {
len: local.len,
present: local.present.clone(),
}),
upload.clone(),
false,
),
Body::Dir { marker_pending } => (None, None, *marker_pending),
};
Ok(EntryRecord {
id,
is_dir: state.is_dir(),
path,
key: state.key.clone(),
gone: state.gone.clone(),
base: state.base.as_ref().map(|base| BaseRecord {
key: base.key.clone(),
head: (*base.head).clone(),
bare: base.bare,
}),
remote: state.may_exist,
local,
times: own.times,
attributes: own.attributes,
generation: state.generation,
upload_id: upload.as_ref().map(|upload| upload.id.clone()),
upload_key: upload.map(|upload| upload.key),
failed: state.failure.is_some(),
meta_pending: state.own.is_some(),
marker_pending,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::s3::object::Metadata;
use crate::tests::util::scratch_dir;
fn head(etag: &str, size: u64) -> Arc<ObjectHead> {
Arc::new(ObjectHead {
size,
etag: etag.to_owned(),
last_modified: SystemTime::UNIX_EPOCH,
metadata: Metadata::default(),
})
}
fn kept(generation: u64) -> State {
let mut state = State::new("f".to_owned(), false);
state.id = Some(RecordId::parse(&"a".repeat(32)).unwrap());
state.set_local(Local {
file: Arc::new(DataFile::absent()),
len: 10,
present: std::iter::once(0..10).collect(),
});
state.set_sealed(Sealed {
generation,
len: 10,
});
if let Body::File { upload, .. } = &mut state.body {
*upload = Some(Upload {
key: "f".to_owned(),
id: "upload".to_owned(),
});
}
state.own = Some(Own {
times: [1; 4],
attributes: 0,
});
state.recorded = true;
state.generation = generation;
state.gone = vec!["a".to_owned(), "b".to_owned()];
state
}
#[test]
fn an_entry_has_work_for_local_data_times_a_marker_a_move_or_keys_to_delete() {
let mut file = State::new("f".to_owned(), false);
assert!(!file.has_work());
file.base = Some(Base::at("f"));
assert!(!file.has_work(), "a base at its own key is nothing to do");
file.own = Some(Own {
times: [1; 4],
attributes: 0,
});
assert!(file.has_work(), "pending times");
file.own = None;
file.base = Some(Base::at("g"));
assert!(file.has_work(), "a base at another key is copied");
assert_eq!(file.moved_from(), Some("g"));
file.base = Some(Base::at("f"));
file.gone.push("h".to_owned());
assert!(file.has_work(), "keys to delete");
file.gone.clear();
file.set_local(Local {
file: Arc::new(DataFile::absent()),
len: 0,
present: RangeSet::new(),
});
assert!(file.has_work(), "local data");
let mut dir = State::new("d".to_owned(), true);
assert!(!dir.has_work());
dir.set_marker_pending(true);
assert!(dir.has_work());
dir.set_marker_pending(false);
dir.own = Some(Own {
times: [1; 4],
attributes: 0,
});
assert!(dir.has_work(), "times of a directory go in its marker");
dir.base = Some(Base::at("other"));
dir.own = None;
assert!(!dir.has_work(), "a directory has no base to copy");
}
#[test]
fn an_entry_with_work_counts_as_pending_without_a_record() {
let mut file = State::new("f".to_owned(), false);
assert!(!file.pending());
file.gone.push("old".to_owned());
assert!(file.pending());
file.gone.clear();
file.recorded = true;
assert!(file.pending());
}
#[test]
fn a_job_that_stored_the_object_for_an_entry_that_moved_takes_nothing_in() {
let ahead = Arc::new(ReadAhead::new());
let mut state = kept(3);
state.key = "g".to_owned();
assert_eq!(
state.landed(&ahead, 3, "f", head("\"new\"", 10)),
Landing::Moved
);
assert!(state.base.is_none());
assert_eq!(state.uploaded, 0);
assert!(state.local().is_some());
}
#[test]
fn a_change_moves_the_generation_and_stops_the_job_that_runs() {
let mut state = kept(3);
state.eager = true;
let stop = Stop::new();
state.job = Some(stop.clone());
state.bump();
assert_eq!(state.generation, 4);
assert!(stop.is_stopped());
assert!(!state.eager);
let mut idle = kept(3);
idle.bump();
assert_eq!(idle.generation, 4);
}
#[test]
fn a_job_of_a_generation_the_entry_still_has_takes_everything_the_entry_held() {
let ahead = Arc::new(ReadAhead::new());
let mut state = kept(3);
assert_eq!(
state.landed(&ahead, 3, "f", head("\"new\"", 10)),
Landing::Whole
);
assert!(state.local().is_some(), "settled drops it");
state.settled();
assert!(state.local().is_none());
assert!(state.sealed().is_none());
assert!(state.own.is_none());
assert!(!state.recorded);
assert!(state.id.is_none());
assert_eq!(state.gone, ["a", "b"]);
assert_eq!(state.uploaded, 3);
assert!(state.may_exist);
let base = state.base.as_ref().unwrap();
assert_eq!(
(base.key.as_str(), base.head.etag.as_str()),
("f", "\"new\"")
);
assert!(matches!(state.body, Body::File { upload: None, .. }));
assert!(state.has_work(), "the keys to delete are left");
state.gone.clear();
assert!(!state.has_work());
}
#[test]
fn a_job_of_an_older_generation_keeps_what_the_entry_holds() {
let ahead = Arc::new(ReadAhead::new());
let mut state = kept(3);
state.bump();
assert_eq!(
state.landed(&ahead, 3, "f", head("\"new\"", 10)),
Landing::Newer
);
assert_eq!(state.local().unwrap().len, 10);
assert!(state.own.is_some());
assert!(state.recorded);
assert!(state.id.is_some());
assert_eq!(
state.uploaded, 3,
"the generation the job stored, not the entry's"
);
assert_eq!(state.base.as_ref().unwrap().head.etag, "\"new\"");
assert!(matches!(state.body, Body::File { upload: None, .. }));
assert!(state.has_work());
}
#[test]
fn an_entry_waits_its_delay_after_the_last_change_unless_it_came_back_from_disk() {
let delay = Duration::from_secs(5);
let mut state = kept(1);
let changed = state.changed;
assert_eq!(state.job_due(delay), Some(changed + delay));
state.eager = true;
assert_eq!(state.job_due(delay), Some(changed));
state.eager = false;
state.bump();
assert_eq!(state.job_due(delay), Some(state.changed + delay));
}
#[test]
fn an_entry_that_met_a_full_disk_waits_for_its_rest_when_that_is_longer_than_the_delay() {
let mut state = kept(1);
let rest = state.changed + Duration::from_secs(1);
state.rest_until = Some(rest);
assert_eq!(state.job_due(Duration::ZERO), Some(rest));
assert_eq!(
state.job_due(Duration::from_secs(5)),
Some(state.changed + Duration::from_secs(5))
);
}
#[test]
fn the_data_file_of_an_entry_stays_open_only_while_a_handle_is_open() {
let dir = scratch_dir();
let path = dir.path().join("data");
crate::s3::data::create(&path, 4).unwrap();
let mut state = State::new("f".to_owned(), false);
state.set_local(Local {
file: Arc::new(DataFile::new(path, None)),
len: 4,
present: RangeSet::new(),
});
let data = Arc::clone(&state.local().unwrap().file);
drop(data.file().unwrap());
assert!(!data.is_open(), "no handle is open");
state.opened();
state.opened();
drop(data.file().unwrap());
assert!(data.is_open());
state.closed();
assert!(data.is_open(), "a handle is still open");
state.closed();
assert!(!data.is_open());
drop(data.file().unwrap());
assert!(!data.is_open());
}
#[test]
fn a_created_file_keeps_the_descriptor_its_data_file_was_made_with() {
let dir = scratch_dir();
let path = dir.path().join("data");
let file = crate::s3::data::create(&path, 0).unwrap();
let id = RecordId::parse(&"c".repeat(32)).unwrap();
let entry = Entry::created_file("f".to_owned(), id, 1, DataFile::new(path, Some(file)));
let mut state = entry.state();
let data = Arc::clone(&state.local().unwrap().file);
assert!(data.is_open());
let made = data.file().unwrap();
state.opened();
assert!(
Arc::ptr_eq(&made, &data.file().unwrap()),
"the handle does not open the file again"
);
state.closed();
assert!(!data.is_open());
}
#[test]
fn an_entry_gets_no_job_while_one_runs_after_it_failed_or_left_or_has_nothing_to_store() {
let delay = Duration::from_secs(5);
let mut running = kept(1);
running.job = Some(Stop::new());
assert_eq!(running.job_due(delay), None);
let mut failed = kept(1);
failed.failure = Some(S3Error::Stopped);
assert_eq!(failed.job_due(delay), None);
let mut detached = kept(1);
detached.detached = true;
assert_eq!(detached.job_due(delay), None);
assert_eq!(State::new("g".to_owned(), false).job_due(delay), None);
let mut dir = State::new("d".to_owned(), true);
assert_eq!(dir.job_due(delay), None);
dir.set_marker_pending(true);
assert!(dir.job_due(delay).is_some());
dir.creating = true;
assert_eq!(dir.job_due(delay), None);
}
#[test]
fn a_waiting_call_is_not_done_while_the_job_that_stored_the_entry_still_runs() {
let entry = Entry::from_state(kept(4));
entry.state().uploaded = 4;
entry.state().job = Some(Stop::new());
assert!(matches!(entry.progress(4, &[]), Progress::Waiting));
entry.state().job = None;
assert!(matches!(entry.progress(4, &[]), Progress::Done));
}
#[test]
fn a_waiting_call_sees_done_waiting_and_failed() {
let entry = Entry::from_state(kept(4));
assert!(matches!(entry.progress(4, &[]), Progress::Waiting));
entry.state().uploaded = 4;
assert!(matches!(entry.progress(4, &[]), Progress::Done));
assert!(matches!(entry.progress(5, &[]), Progress::Waiting));
entry.state().failure = Some(S3Error::Stopped);
assert!(matches!(entry.progress(5, &[]), Progress::Failed(_)));
entry.state().detached = true;
assert!(
matches!(entry.progress(5, &[]), Progress::Failed(_)),
"a file moved to conflicts fails the call"
);
entry.state().failure = None;
assert!(matches!(entry.progress(5, &[]), Progress::Done));
}
#[test]
fn the_etag_of_the_key_is_known_from_a_lookup_and_from_a_landing_and_not_from_a_base_the_entry_left()
{
let ahead = Arc::new(ReadAhead::new());
let found = |etag: &str| {
Found::File(crate::s3::lookup::FileRef {
key: "f".to_owned(),
name: "f".to_owned(),
head: head(etag, 1),
})
};
let mut state = State::new("f".to_owned(), false);
assert_eq!(state.prior, None);
state.follow(found("\"a\""), &ahead);
assert_eq!(state.prior.as_deref(), Some("\"a\""));
state.follow(found("\"b\""), &ahead);
assert_eq!(state.prior.as_deref(), Some("\"b\""));
state.follow(found(""), &ahead);
assert_eq!(state.prior, None, "an object without an ETag");
assert_eq!(
state.landed(&ahead, 0, "f", head("\"c\"", 1)),
Landing::Whole
);
assert_eq!(state.prior.as_deref(), Some("\"c\""));
state.key = "g".to_owned();
state.prior = None;
assert_eq!(
state.landed(&ahead, 0, "f", head("\"d\"", 1)),
Landing::Moved
);
assert_eq!(state.prior, None, "an entry that moved took nothing in");
}
fn record_of(state: &mut State) -> EntryRecord {
state.id = Some(RecordId::parse(&"b".repeat(32)).unwrap());
Entry::record(state, "", SystemTime::UNIX_EPOCH).unwrap()
}
#[test]
fn the_source_is_the_base_key_until_every_byte_is_local_and_the_record_says_so() {
let mut state = State::new("new".to_owned(), false);
state.base = Some(Base::at("old"));
state.set_local(Local {
file: Arc::new(DataFile::absent()),
len: 4,
present: std::iter::once(0..2).collect(),
});
assert_eq!(state.source(), Some("old"));
let record = record_of(&mut state);
assert_eq!(record.source(), Some("old"));
state.set_recorded(&record);
state.local_mut().unwrap().present.insert(2..4);
assert_eq!(state.source(), Some("old"), "the record still names it");
let record = record_of(&mut state);
assert_eq!(record.source(), None);
state.set_recorded(&record);
assert_eq!(state.source(), None);
}
#[test]
fn a_landed_copy_keeps_the_source_until_a_record_without_it_is_written() {
let ahead = Arc::new(ReadAhead::new());
let mut copy = State::new("new".to_owned(), false);
copy.base = Some(Base::at("old"));
let record = record_of(&mut copy);
copy.set_recorded(&record);
assert_eq!(copy.source(), Some("old"));
assert_eq!(
copy.landed(&ahead, 0, "new", head("\"e\"", 1)),
Landing::Whole
);
assert_eq!(copy.source(), Some("old"), "the record is not written yet");
let record = record_of(&mut copy);
copy.set_recorded(&record);
assert_eq!(copy.source(), None);
copy.recorded_source = Some("old".to_owned());
copy.settled();
assert_eq!(copy.source(), None, "no record, no source");
}
#[test]
fn a_record_names_no_source_for_a_directory_or_a_base_at_the_key() {
let mut state = State::new("k".to_owned(), false);
state.base = Some(Base::at("k"));
assert_eq!(record_of(&mut state).source(), None);
let mut dir = State::new("d".to_owned(), true);
dir.base = Some(Base::at("other"));
assert_eq!(record_of(&mut dir).source(), None);
}
}