fix(desktop): make source identity backfill symmetric on Windows

上一提交只在 open_root/open_archive 单侧回填 volume/file-index,后续
复核路径重新计算的身份仍是 0,一比对必然报"来源(句柄)在检查后发生
变化",Windows 上选文件夹/ZIP 导入直接不可用。

现在身份补查下沉到 stable_from_metadata/source_identity 本身:所有
构造点(枚举、打开前 lstat、打开后句柄复核、根校验)一律经同源的
file_identity/open_file_identity 回填,路径侧与句柄侧取到的
volume/file-index 可直接比对;查询失败统一退化为 0,比对仍一致。
verify_root_path 因此恢复为全平台按对象身份比对。
This commit is contained in:
xiaomakuaiz committed 2026-08-29 00:06:44 +08:00
1 parent b3a1350f1e
commit 7150f70a23
3 files changed
+72 -38

No files matched your search

+26
View File
@@ -375,6 +375,32 @@ pub(crate) fn file_identity(path: &Path) -> Option<(u64, u64)> {
}
}
/// 已打开文件/目录句柄的稳定对象身份(Windows volume/file-index)。
/// 与 [`file_identity`] 同源,保证按路径与按句柄取到的身份可比对。
#[cfg(not(unix))]
pub(crate) fn open_file_identity(file: &fs::File) -> Option<(u64, u64)> {
#[cfg(windows)]
{
use std::os::windows::io::AsRawHandle as _;
use windows::Win32::Foundation::HANDLE;
use windows::Win32::Storage::FileSystem::{
GetFileInformationByHandle, BY_HANDLE_FILE_INFORMATION,
};
let mut information = BY_HANDLE_FILE_INFORMATION::default();
unsafe { GetFileInformationByHandle(HANDLE(file.as_raw_handle()), &mut information) }
.ok()?;
Some((
information.dwVolumeSerialNumber as u64,
((information.nFileIndexHigh as u64) << 32) | information.nFileIndexLow as u64,
))
}
#[cfg(not(any(unix, windows)))]
{
let _ = file;
None
}
}
pub(crate) fn sync_file(path: &Path) -> std::io::Result<()> {
#[cfg(windows)]
{
+13 -14
View File
@@ -2259,18 +2259,10 @@ mod platform {
if !opened.file_type().is_file() {
return Err(ArchiveImportError::SourceNotFile);
}
#[cfg_attr(unix, allow(unused_mut))]
let mut identity = source_identity(&opened);
if identity != source_identity(&selected) {
let identity = source_identity(path, &opened);
if identity != source_identity(path, &selected) {
return Err(ArchiveImportError::SourceChanged);
}
// 非 unix 的 std metadata 拿不到对象身份;ZIP 来源身份参与 SourceKey
// 去重与指纹比对,经全共享句柄补查(失败保持 0,仅弱化去重)。
#[cfg(not(unix))]
if let Some((object_a, object_b)) = crate::config::file_identity(path) {
identity.object_a = object_a;
identity.object_b = object_b;
}
Ok(OpenedArchive {
file: Some(file),
source_path: path.to_path_buf(),
@@ -2282,7 +2274,9 @@ mod platform {
let Ok(metadata) = fs::symlink_metadata(&opened.source_path) else {
return Err(ArchiveImportError::SourceChanged);
};
if !metadata.file_type().is_file() || source_identity(&metadata) != opened.identity {
if !metadata.file_type().is_file()
|| source_identity(&opened.source_path, &metadata) != opened.identity
{
return Err(ArchiveImportError::SourceChanged);
}
Ok(())
@@ -2292,7 +2286,9 @@ mod platform {
verify_handle(opened)
}
fn source_identity(metadata: &fs::Metadata) -> ArchiveIdentity {
fn source_identity(path: &Path, metadata: &fs::Metadata) -> ArchiveIdentity {
#[cfg(unix)]
let _ = path;
let (object_a, object_b);
#[cfg(unix)]
{
@@ -2302,8 +2298,11 @@ mod platform {
}
#[cfg(not(unix))]
{
object_a = 0;
object_b = 0;
// 经全共享句柄按路径补查对象身份(开-查-关,不钉扎);失败退化 0,
// 所有构造点同源,比对一致。
let identity = crate::config::file_identity(path).unwrap_or((0, 0));
object_a = identity.0;
object_b = identity.1;
}
let (modified_seconds, modified_nanos) = time_parts(metadata.modified());
#[cfg(unix)]
+33 -24
View File
@@ -1359,15 +1359,7 @@ mod platform {
if metadata.file_type().is_symlink() || !metadata.file_type().is_dir() {
return Err(FolderImportError::SourceNotDirectory);
}
#[cfg_attr(unix, allow(unused_mut))]
let mut stable = stable_from_metadata(&metadata, FolderEntryKind::Directory);
// 非 unix 的 std metadata 拿不到对象身份;来源根的身份参与 SourceKey
// 去重与指纹比对,经全共享句柄补查(失败保持 0,仅弱化去重)。
#[cfg(not(unix))]
if let Some((object_a, object_b)) = crate::config::file_identity(path) {
stable.object_a = object_a;
stable.object_b = object_b;
}
let stable = stable_from_metadata(path, &metadata, FolderEntryKind::Directory);
Ok((
RootHandle {
path: path.to_path_buf(),
@@ -1407,7 +1399,7 @@ mod platform {
let child_path = relative.child(name)?;
let metadata = fs::symlink_metadata(entry.path())
.map_err(|error| FolderImportError::io("检查来源条目", &child_path, error))?;
let stable = stable_for_entry(&metadata)?;
let stable = stable_for_entry(&entry.path(), &metadata)?;
output.push(PlatformDirectoryEntry {
name: name.to_string(),
stable,
@@ -1441,7 +1433,7 @@ mod platform {
let path = resolve(root, relative);
let metadata = fs::symlink_metadata(&path)
.map_err(|error| FolderImportError::io("检查来源文件", relative, error))?;
let stable = stable_for_entry(&metadata)?;
let stable = stable_for_entry(&path, &metadata)?;
if stable != *expected || stable.kind != FolderEntryKind::File {
return Err(FolderImportError::SourceChanged {
relative_path: relative.display().to_string(),
@@ -1474,7 +1466,15 @@ mod platform {
reason: "已打开对象不是普通磁盘文件",
});
};
Ok(stable_from_metadata(&metadata, kind))
#[cfg_attr(unix, allow(unused_mut))]
let mut stable = stable_from_metadata(Path::new("."), &metadata, kind);
// 句柄侧与路径侧身份同源(volume/file-index),同一文件必然可比对。
#[cfg(not(unix))]
if let Some((object_a, object_b)) = crate::config::open_file_identity(file) {
stable.object_a = object_a;
stable.object_b = object_b;
}
Ok(stable)
}
pub(super) fn verify_root_path(
@@ -1491,12 +1491,10 @@ mod platform {
relative_path: ".".into(),
});
}
let current = stable_from_metadata(&metadata, FolderEntryKind::Directory);
#[cfg(unix)]
let identity_unchanged =
current.object_a == expected.object_a && current.object_b == expected.object_b;
#[cfg(not(unix))]
let identity_unchanged = current.kind == expected.kind;
let current = stable_from_metadata(path, &metadata, FolderEntryKind::Directory);
let identity_unchanged = current.kind == expected.kind
&& current.object_a == expected.object_a
&& current.object_b == expected.object_b;
if !identity_unchanged {
return Err(FolderImportError::SourceChanged {
relative_path: ".".into(),
@@ -1515,7 +1513,7 @@ mod platform {
) -> Result<StableMetadata, FolderImportError> {
let metadata = fs::symlink_metadata(path)
.map_err(|error| FolderImportError::io("复核来源目录", relative, error))?;
let stable = stable_for_entry(&metadata)?;
let stable = stable_for_entry(path, &metadata)?;
if stable.kind != FolderEntryKind::Directory {
return Err(FolderImportError::SourceChanged {
relative_path: relative.display().to_string(),
@@ -1524,7 +1522,10 @@ mod platform {
Ok(stable)
}
fn stable_for_entry(metadata: &fs::Metadata) -> Result<StableMetadata, FolderImportError> {
fn stable_for_entry(
path: &Path,
metadata: &fs::Metadata,
) -> Result<StableMetadata, FolderImportError> {
let file_type = metadata.file_type();
if file_type.is_symlink() {
return Err(FolderImportError::UnsafeEntry {
@@ -1542,12 +1543,17 @@ mod platform {
reason: "只允许普通文件和目录",
});
};
Ok(stable_from_metadata(metadata, kind))
Ok(stable_from_metadata(path, metadata, kind))
}
fn stable_from_metadata(metadata: &fs::Metadata, kind: FolderEntryKind) -> StableMetadata {
fn stable_from_metadata(
path: &Path,
metadata: &fs::Metadata,
kind: FolderEntryKind,
) -> StableMetadata {
#[cfg(unix)]
{
let _ = path;
use std::os::unix::fs::MetadataExt as _;
StableMetadata {
object_a: metadata.dev(),
@@ -1563,10 +1569,13 @@ mod platform {
}
#[cfg(not(unix))]
{
// 非 unix 的 std metadata 拿不到对象身份;经全共享句柄按路径补查
// (开-查-关,不钉扎),失败退化为 0——所有构造点同源,比对一致。
let (object_a, object_b) = crate::config::file_identity(path).unwrap_or((0, 0));
let (modified_seconds, modified_nanos) = time_parts(metadata.modified());
StableMetadata {
object_a: 0,
object_b: 0,
object_a,
object_b,
object_c: 0,
size: metadata.len(),
modified_seconds,