mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-01 02:20:07 +08:00
fix(profile-store): recover aliased subscription body creation
This commit is contained in:
@@ -40,6 +40,11 @@ pub enum CommitOutcome {
|
||||
NeedsRecovery(StoreError),
|
||||
}
|
||||
|
||||
enum InstallOutcome {
|
||||
Installed,
|
||||
Reverted(StoreError),
|
||||
}
|
||||
|
||||
pub struct Session {
|
||||
root: PathBuf,
|
||||
_lock: File,
|
||||
@@ -140,12 +145,16 @@ impl Session {
|
||||
checkpoint("decision-synced")?;
|
||||
self.install(checkpoint)
|
||||
})();
|
||||
if let Err(error) = published {
|
||||
return CommitOutcome::NeedsRecovery(error);
|
||||
}
|
||||
match self.disarm_and_clean(checkpoint) {
|
||||
Ok(()) => CommitOutcome::Committed,
|
||||
Err(error) => CommitOutcome::CommittedCleanupPending(error),
|
||||
match published {
|
||||
Err(error) => CommitOutcome::NeedsRecovery(error),
|
||||
Ok(InstallOutcome::Installed) => match self.disarm_and_clean(checkpoint) {
|
||||
Ok(()) => CommitOutcome::Committed,
|
||||
Err(error) => CommitOutcome::CommittedCleanupPending(error),
|
||||
},
|
||||
Ok(InstallOutcome::Reverted(rejection)) => match self.disarm_and_clean(checkpoint) {
|
||||
Ok(()) => CommitOutcome::Retryable(rejection),
|
||||
Err(error) => CommitOutcome::NeedsRecovery(error),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -242,7 +251,7 @@ impl Session {
|
||||
checkpoint("prepared")
|
||||
}
|
||||
|
||||
fn install(&self, checkpoint: &mut Checkpoint<'_>) -> Result<()> {
|
||||
fn install(&self, checkpoint: &mut Checkpoint<'_>) -> Result<InstallOutcome> {
|
||||
let journal = self.journal();
|
||||
let decision = read_decision(&journal.join(DECISION))?;
|
||||
let index_next = journal.join(INDEX_NEXT);
|
||||
@@ -253,11 +262,15 @@ impl Session {
|
||||
}
|
||||
|
||||
let profiles_dir = self.root.join(PROFILES);
|
||||
unshared_body(&profiles_dir, &candidate, &decision.file)?;
|
||||
let body_path = profiles_dir.join(&decision.file);
|
||||
let index_path = self.root.join(INDEX);
|
||||
let body_installed = verify_live(&body_path, decision.old_body_digest, decision.body_digest)?;
|
||||
let index_installed = verify_live(&index_path, Some(decision.old_index_digest), decision.index_digest)?;
|
||||
let can_revert_new_body = decision.old_body_digest.is_none()
|
||||
&& (!index_installed || decision.old_index_digest == decision.index_digest);
|
||||
if !can_revert_new_body {
|
||||
unshared_body(&profiles_dir, &candidate, &decision.file)?;
|
||||
}
|
||||
|
||||
let body_next = journal.join(BODY_NEXT);
|
||||
if body_installed {
|
||||
@@ -272,11 +285,34 @@ impl Session {
|
||||
copy_verified(&body_next, &apply, decision.body_digest)?;
|
||||
platform::replace(&apply, &body_path).map_err(|source| StoreError::PathIo {
|
||||
operation: "replace",
|
||||
path: body_path,
|
||||
path: body_path.clone(),
|
||||
source,
|
||||
})?;
|
||||
}
|
||||
checkpoint("body-replaced")?;
|
||||
if can_revert_new_body {
|
||||
// Native aliases of a missing file only resolve once the file exists.
|
||||
let check = (|| {
|
||||
unshared_body(&profiles_dir, &candidate, &decision.file)?;
|
||||
if !index_installed {
|
||||
let (index, _) = verified(&index_path, decision.old_index_digest)?;
|
||||
let current = serde_yaml_ng::from_slice(&index)?;
|
||||
unshared_body(&profiles_dir, ¤t, &decision.file)?;
|
||||
}
|
||||
Ok(())
|
||||
})();
|
||||
if let Err(error) = check {
|
||||
match error {
|
||||
StoreError::AliasedBody | StoreError::InvalidFilename => {}
|
||||
#[cfg(windows)]
|
||||
StoreError::InvalidWindowsFilename => {}
|
||||
_ => return Err(error),
|
||||
}
|
||||
fs::remove_file(&body_path)?;
|
||||
platform::sync_dir(&profiles_dir)?;
|
||||
return Ok(InstallOutcome::Reverted(error));
|
||||
}
|
||||
}
|
||||
if index_installed {
|
||||
platform::sync_file(&index_path)?;
|
||||
} else {
|
||||
@@ -294,7 +330,7 @@ impl Session {
|
||||
checkpoint("body-synced")?;
|
||||
platform::sync_dir(&self.root)?;
|
||||
checkpoint("index-synced")?;
|
||||
Ok(())
|
||||
Ok(InstallOutcome::Installed)
|
||||
}
|
||||
|
||||
fn disarm_and_clean(&self, checkpoint: &mut Checkpoint<'_>) -> Result<()> {
|
||||
@@ -436,22 +472,35 @@ fn body_filename<'a>(profiles: &'a IProfiles, uid: &str) -> Result<&'a str> {
|
||||
|
||||
fn unshared_body(dir: &Path, profiles: &IProfiles, file: &str) -> Result<Option<fs::Metadata>> {
|
||||
let path = dir.join(file);
|
||||
let Some(target) = optional_regular(&path)? else {
|
||||
return Ok(None);
|
||||
};
|
||||
let identity = platform::file_identity(&path, &target)?;
|
||||
if identity.links != 1 {
|
||||
let target = optional_regular(&path)?;
|
||||
let identity = target
|
||||
.as_ref()
|
||||
.map(|metadata| platform::file_identity(&path, metadata))
|
||||
.transpose()?;
|
||||
if identity.as_ref().is_some_and(|identity| identity.links != 1) {
|
||||
return Err(StoreError::HardLinkedBody);
|
||||
}
|
||||
for other in profiles.items.iter().flatten().filter_map(|item| item.file.as_deref()) {
|
||||
if other == file {
|
||||
continue;
|
||||
}
|
||||
// Other entries may use native filenames, but they must stay in this directory.
|
||||
let mut components = Path::new(other).components();
|
||||
if !matches!(
|
||||
(components.next(), components.next()),
|
||||
(Some(std::path::Component::Normal(name)), None) if name == other
|
||||
) || other.contains('\0')
|
||||
{
|
||||
return Err(StoreError::InvalidFilename);
|
||||
}
|
||||
#[cfg(windows)]
|
||||
validate_filename(other)?;
|
||||
let other = dir.join(other);
|
||||
match fs::metadata(&other) {
|
||||
Ok(metadata) => {
|
||||
if platform::file_identity(&other, &metadata)? == identity {
|
||||
if let Some(identity) = &identity
|
||||
&& platform::file_identity(&other, &metadata)? == *identity
|
||||
{
|
||||
return Err(StoreError::AliasedBody);
|
||||
}
|
||||
}
|
||||
@@ -459,7 +508,7 @@ fn unshared_body(dir: &Path, profiles: &IProfiles, file: &str) -> Result<Option<
|
||||
Err(error) => return Err(error.into()),
|
||||
}
|
||||
}
|
||||
Ok(Some(target))
|
||||
Ok(target)
|
||||
}
|
||||
|
||||
fn file_digest(path: &Path) -> Result<[u8; 32]> {
|
||||
|
||||
@@ -25,3 +25,71 @@ fn refresh_creates_missing_body_for_legacy_index() -> Result<(), clash_verge_pro
|
||||
assert_eq!(stored_body, body);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn refresh_allows_an_unrelated_unix_filename() -> Result<(), clash_verge_profile_store::StoreError> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
let root = dir.path();
|
||||
fs::create_dir(root.join("profiles"))?;
|
||||
fs::write(root.join("profiles/r.yaml"), b"old")?;
|
||||
fs::write(root.join("profiles/office:old.yaml"), b"local")?;
|
||||
let old = "items:\n- {uid: r, type: remote, file: r.yaml, updated: 1}\n- {uid: l, type: local, file: 'office:old.yaml'}\n";
|
||||
fs::write(root.join("profiles.yaml"), old)?;
|
||||
let candidate = serde_yaml_ng::from_str(&old.replace("updated: 1", "updated: 2"))?;
|
||||
|
||||
let outcome = Session::open(root)?.commit_refresh("r", &candidate, b"new");
|
||||
assert!(matches!(outcome, CommitOutcome::Committed), "{outcome:?}");
|
||||
assert_eq!(fs::read(root.join("profiles/office:old.yaml"))?, b"local");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refresh_rejects_legacy_path_alias_before_creating_body() -> Result<(), clash_verge_profile_store::StoreError> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
let root = dir.path();
|
||||
fs::create_dir(root.join("profiles"))?;
|
||||
let old = "items:\n- {uid: r, type: remote, file: r.yaml, updated: 1}\n- {uid: l, type: local, file: './r.yaml'}\n";
|
||||
fs::write(root.join("profiles.yaml"), old)?;
|
||||
let candidate = serde_yaml_ng::from_str(&old.replace("updated: 1", "updated: 2"))?;
|
||||
|
||||
let outcome = Session::open(root)?.commit_refresh("r", &candidate, b"new");
|
||||
assert!(matches!(
|
||||
outcome,
|
||||
CommitOutcome::Retryable(clash_verge_profile_store::StoreError::InvalidFilename)
|
||||
));
|
||||
assert!(!root.join("profiles/r.yaml").exists());
|
||||
assert!(!root.join(clash_verge_profile_store::RECOVERY_JOURNAL_NAME).exists());
|
||||
assert_eq!(fs::read_to_string(root.join("profiles.yaml"))?, old);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(unix, feature = "test-support"))]
|
||||
#[test]
|
||||
fn interrupted_creation_reverts_an_aliased_body() -> Result<(), clash_verge_profile_store::StoreError> {
|
||||
let dir = tempfile::tempdir()?;
|
||||
let root = dir.path();
|
||||
fs::create_dir(root.join("profiles"))?;
|
||||
std::os::unix::fs::symlink("r.yaml", root.join("profiles/alias.yaml"))?;
|
||||
let old = "items:\n- {uid: r, type: remote, file: r.yaml, updated: 1}\n- {uid: l, type: local, file: alias.yaml}\n";
|
||||
fs::write(root.join("profiles.yaml"), old)?;
|
||||
let candidate = serde_yaml_ng::from_str(&old.replace("updated: 1", "updated: 2"))?;
|
||||
let outcome = Session::open(root)?.commit_observed("r", &candidate, b"new", &mut |phase| {
|
||||
if phase == "body-replaced" {
|
||||
return Err(std::io::Error::other("interrupted after body replacement").into());
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
assert!(matches!(outcome, CommitOutcome::NeedsRecovery(_)), "{outcome:?}");
|
||||
assert_eq!(fs::read(root.join("profiles/r.yaml"))?, b"new");
|
||||
let session = Session::open(root)?;
|
||||
assert!(!root.join("profiles/r.yaml").exists());
|
||||
assert!(!root.join(clash_verge_profile_store::RECOVERY_JOURNAL_NAME).exists());
|
||||
assert_eq!(fs::read_to_string(root.join("profiles.yaml"))?, old);
|
||||
let outcome = session.commit_refresh("r", &candidate, b"new");
|
||||
assert!(matches!(
|
||||
outcome,
|
||||
CommitOutcome::Retryable(clash_verge_profile_store::StoreError::AliasedBody)
|
||||
));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user