Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions packages/app-lib/src/install/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct SharedInstanceUpdateRollback {
entries: Vec<ContentEntry>,
#[serde(default)]
bindings: Vec<crate::state::content_store::InstanceFileStorage>,
#[serde(default)]
missing_file_ids: std::collections::HashSet<String>,
#[serde(default)]
copied_file_ids: std::collections::HashSet<String>,
}

pub(super) async fn prepare_instance_update_backup(
Expand Down Expand Up @@ -60,7 +64,7 @@ pub(super) async fn prepare_instance_update_backup(
state,
)
.await?;
let files = content_rows::get_instance_files(
let mut files = content_rows::get_instance_files(
&metadata.instance.id,
&state.pool,
)
Expand All @@ -75,9 +79,11 @@ pub(super) async fn prepare_instance_update_backup(
&metadata.instance.id,
)
.await?;
let mut missing_file_ids = std::collections::HashSet::new();
let mut copied_file_ids = std::collections::HashSet::new();
for binding in &bindings {
let file = files
.iter()
.iter_mut()
.find(|file| file.id == binding.file_id)
.ok_or_else(|| {
crate::state::content_store::input(
Expand All @@ -86,20 +92,28 @@ pub(super) async fn prepare_instance_update_backup(
})?;
let file_status = state.content_store
.check_instance_file(&metadata.instance, file, binding).await?;
file.missing = file_status == crate::state::content_store::InstanceFileStatus::Missing;
if file.missing {
missing_file_ids.insert(file.id.clone());
continue;
}
let content = state.content_store.file_content(file).await?;
if file_status != crate::state::content_store::InstanceFileStatus::Healthy
|| !matches!(content, crate::state::content_store::FileContent::Stored { .. })
{
return Err(crate::state::content_store::input(format!(
"Restore or repair {} before updating this instance; its current content cannot be backed up safely",
file.relative_path,
)));
}
if !matches!(content, crate::state::content_store::FileContent::Stored { .. }) {
copied_file_ids.insert(file.id.clone());
}
}
let skipped = files
.iter()
.filter(|file| {
bindings.iter().any(|binding| binding.file_id == file.id)
&& !copied_file_ids.contains(&file.id)
})
.map(crate::state::content_store::content_file_path)
.collect();
Expand All @@ -111,6 +125,8 @@ pub(super) async fn prepare_instance_update_backup(
files,
entries,
bindings,
missing_file_ids,
copied_file_ids,
};
let instance_path = state
.directories
Expand Down Expand Up @@ -247,6 +263,30 @@ async fn restore_instance_update(
));
}
for binding in &snapshot.bindings {
if snapshot.missing_file_ids.contains(&binding.file_id) {
continue;
}
if snapshot.copied_file_ids.contains(&binding.file_id) {
let file = snapshot
.files
.iter()
.find(|file| file.id == binding.file_id)
.ok_or_else(|| {
crate::state::content_store::input(
"Backup content reference has no file record",
)
})?;
let path = backup_path
.join(crate::state::content_store::content_file_path(file));
if crate::state::content_store::hash_file(&path).await?.sha512
!= binding.blob_sha512
{
return Err(crate::state::content_store::input(
"The instance backup contains changed content",
));
}
continue;
}
if state
.content_store
.lookup(Some(&binding.blob_sha512), None)
Expand Down Expand Up @@ -282,6 +322,8 @@ async fn restore_instance_update(
&rollback.instance.instance,
&snapshot.files,
&snapshot.bindings,
&snapshot.missing_file_ids,
&snapshot.copied_file_ids,
)
.await?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ impl ContentStore {
instance: &Instance,
files: &[InstanceFile],
bindings: &[InstanceFileStorage],
missing_file_ids: &std::collections::HashSet<String>,
copied_file_ids: &std::collections::HashSet<String>,
) -> crate::Result<()> {
let files: HashMap<_, _> =
files.iter().map(|file| (file.id.as_str(), file)).collect();
Expand All @@ -131,6 +133,22 @@ impl ContentStore {
files.get(binding.file_id.as_str()).ok_or_else(|| {
input("Rollback content reference has no file record")
})?;
if missing_file_ids.contains(&binding.file_id) {
restored.push((
&binding.file_id,
&binding.blob_sha512,
binding.storage_kind,
));
continue;
}
if copied_file_ids.contains(&binding.file_id) {
restored.push((
&binding.file_id,
&binding.blob_sha512,
FileStorageKind::Copy,
));
continue;
}
let stored_file = self
.lookup(Some(&binding.blob_sha512), Some(file.size))
.await?
Expand Down
59 changes: 48 additions & 11 deletions packages/app-lib/src/state/instances/commands/content_mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ impl<'a> InstanceContent<'a> {
) -> crate::Result<PendingContentChange> {
self.content_scope()?;
let canonical_path = canonical_content_path(project_path);
let file = content_rows::get_instance_file_by_relative_path(
let mut file = content_rows::get_instance_file_by_relative_path(
&self.instance.id,
canonical_path,
&self.state.pool,
Expand All @@ -365,6 +365,7 @@ impl<'a> InstanceContent<'a> {
)
.await?;
let enabled = desired_enabled.unwrap_or(!file.enabled);
file.missing = false;
let file_change = match self
.state
.content_store
Expand Down Expand Up @@ -393,13 +394,15 @@ impl<'a> InstanceContent<'a> {
.await?
}
InstanceFileStatus::Missing => {
file.missing = !enabled;
self.state
.content_store
.prepare_file_change(
&self.instance,
FileChangeRequest {
relative_path: canonical_path,
replacement: Some(&stored_file),
replacement: enabled
.then_some(&stored_file),
enabled,
legacy_path: None,
previous_content: None,
Expand All @@ -414,16 +417,51 @@ impl<'a> InstanceContent<'a> {
}
}
}
FileContent::Damaged(binding) if !enabled => {
self.state
FileContent::Damaged(binding) => {
match self
.state
.content_store
.prepare_file_move(&self.instance, &file, &binding, false)
.check_instance_file(&self.instance, &file, &binding)
.await?
}
FileContent::Damaged(_) => {
return Err(input(
"Content needs repair or re-import before it can be enabled",
));
{
InstanceFileStatus::Healthy => {
self.state
.content_store
.prepare_file_move(
&self.instance,
&file,
&binding,
enabled,
)
.await?
}
InstanceFileStatus::Missing if !enabled => {
file.missing = true;
self.state
.content_store
.prepare_file_change(
&self.instance,
FileChangeRequest {
relative_path: canonical_path,
replacement: None,
enabled,
legacy_path: None,
previous_content: None,
},
)
.await?
}
InstanceFileStatus::Missing => {
return Err(input(
"Content needs repair or re-import before it can be enabled",
));
}
InstanceFileStatus::Conflict => {
return Err(input(
"Content was changed outside the app; resolve the conflict first",
));
}
}
}
FileContent::Unmanaged => {
let physical_path = content_file_path(&file);
Expand Down Expand Up @@ -685,7 +723,6 @@ impl<'a> InstanceContent<'a> {
PreparedChange::Toggle { file, enabled } => {
let mut updated = file.clone();
updated.enabled = *enabled;
updated.missing = false;
updated.modified_at = Utc::now();
let updated =
content_rows::upsert_instance_file(&updated, &mut tx)
Expand Down
4 changes: 4 additions & 0 deletions packages/ui/src/layouts/shared/content-tab/layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,10 @@ async function openUpdateAll(items: ContentItem[]) {
return
const candidates = getUpdateAllCandidates(items)
if (candidates.length === 0) return
if (candidates.length === 1 && ctx.updateItem) {
handleUpdateById(getItemId(candidates[0]))
return
}

const requestId = ++updateAllRequestId
updateAllItems.value = []
Expand Down
Loading