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
81 changes: 74 additions & 7 deletions Sources/WeiBei/Stores/CourseProjectRootSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ struct CourseFileSourceInfo: Equatable, Sendable {

enum CourseFileEntryPresence: Equatable, Sendable {
case present(ImportedFileIdentity)
/// iCloud 占位符在而实体未下载(计划 §5 阶段3):永不判 absent。
case presentUnmaterialized(ImportedFileIdentity)
case absent
case inaccessible
}
Expand Down Expand Up @@ -2649,7 +2651,9 @@ actor CourseProjectFileWorker {
guard let enumerator = fileManager.enumerator(
at: canonicalRoot,
includingPropertiesForKeys: Array(keys),
options: [.skipsHiddenFiles, .skipsPackageDescendants],
// 不再一刀切跳过隐藏文件:iCloud 占位符 `.名字.md.icloud` 是隐藏文件,
// 必须识别为逻辑路径(计划 §5 阶段3);其余隐藏项在循环内显式跳过。
options: [.skipsPackageDescendants],
errorHandler: { _, _ in false }
) else {
return CourseFileScanSnapshot(
Expand All @@ -2674,6 +2678,19 @@ actor CourseProjectFileWorker {
enumerator.skipDescendants()
continue
}
// iCloud 占位符还原为逻辑路径;其余隐藏项维持跳过。
let icloudLogicalName = Self.logicalName(
forICloudPlaceholderFileName: rawURL.lastPathComponent
)
if rawURL.lastPathComponent.hasPrefix(".") {
guard let icloudLogicalName else {
if (try? rawURL.resourceValues(forKeys: [.isDirectoryKey]))?
.isDirectory == true {
enumerator.skipDescendants()
}
continue
}
}
let values = try rawURL.resourceValues(forKeys: keys)
if values.isDirectory == true {
if Self.ignoresImportDirectory(rawURL) {
Expand Down Expand Up @@ -2714,27 +2731,39 @@ actor CourseProjectFileWorker {
fileURL = rawURL.standardizedFileURL
fileValues = values
}
// 占位符按逻辑路径登记:扩展名与相对路径取自还原后的逻辑名。
let effectiveName = icloudLogicalName ?? fileURL.lastPathComponent
let effectiveRelativePath: String
if let icloudLogicalName {
let dir = (relativePath as NSString).deletingLastPathComponent
effectiveRelativePath = dir.isEmpty
? icloudLogicalName
: dir + "/\(icloudLogicalName)"
} else {
effectiveRelativePath = relativePath
}
guard Self.supportedExtensions.contains(
fileURL.pathExtension.lowercased()
URL(fileURLWithPath: effectiveName)
.pathExtension.lowercased()
),
let identity = Self.identity(at: fileURL) else {
continue
}
let identityKey =
"\(identity.volumeID).\(identity.fileID).\(relativePath)"
"\(identity.volumeID).\(identity.fileID).\(effectiveRelativePath)"
if visitedFileKeys.contains(identityKey) {
continue
}
visitedFileKeys.insert(identityKey)
let firstComponent = relativePath
let firstComponent = effectiveRelativePath
.split(separator: "/", omittingEmptySubsequences: true)
.first
let isMarkdown = ["md", "markdown"]
.contains(fileURL.pathExtension.lowercased())
.contains(URL(fileURLWithPath: effectiveName).pathExtension.lowercased())
result.append(
CourseFileMetadata(
url: fileURL,
relativePath: relativePath,
relativePath: effectiveRelativePath,
identity: identity,
documentIdentifier: fileValues.documentIdentifier.flatMap {
$0 >= 0 ? UInt64($0) : nil
Expand Down Expand Up @@ -3536,10 +3565,48 @@ actor CourseProjectFileWorker {
)
}
return errno == ENOENT || errno == ENOTDIR
? .absent
? presenceWithICloudPlaceholderCheck(at: url)
: .inaccessible
}

/// 逻辑路径实体缺席但同名 iCloud 占位符在 ⇒ 未物化在场,永不判 absent。
nonisolated private static func presenceWithICloudPlaceholderCheck(
at url: URL
) -> CourseFileEntryPresence {
let placeholder = url.deletingLastPathComponent()
.appendingPathComponent("." + url.lastPathComponent + ".icloud")
var fileStat = Darwin.stat()
let result = placeholder.withUnsafeFileSystemRepresentation { path -> Int32 in
guard let path else { return -1 }
return Darwin.lstat(path, &fileStat)
}
guard result == 0 else { return .absent }
return .presentUnmaterialized(
ImportedFileIdentity(
volumeID: UInt64(fileStat.st_dev),
fileID: UInt64(fileStat.st_ino),
birthTimeSeconds: Int64(
fileStat.st_birthtimespec.tv_sec
),
birthTimeNanoseconds: Int64(
fileStat.st_birthtimespec.tv_nsec
)
)
)
}

/// iCloud 占位符文件名 `.讲义.md.icloud` ⇒ 逻辑名 `讲义.md`;非占位符返回 nil。
nonisolated static func logicalName(
forICloudPlaceholderFileName fileName: String
) -> String? {
guard fileName.hasPrefix("."), fileName.hasSuffix(".icloud") else {
return nil
}
let base = fileName.dropFirst().dropLast(".icloud".count)
guard !base.isEmpty, base.contains(".") else { return nil }
return String(base)
}

nonisolated static func isSymbolicLink(at url: URL) -> Bool {
var fileStat = Darwin.stat()
guard url.withUnsafeFileSystemRepresentation({ path in
Expand Down
8 changes: 6 additions & 2 deletions Sources/WeiBei/Stores/WorkspaceStore+GoneImportedItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension WorkspaceStore {
switch CourseProjectFileWorker.entryPresence(at: libraryRoot) {
case .absent, .inaccessible:
return keepUnavailableImportedItem(at: index)
case .present:
case .present, .presentUnmaterialized:
break
}
}
Expand All @@ -38,12 +38,16 @@ extension WorkspaceStore {
case .present:
fileMissingSinceByItemID.removeValue(forKey: item.id)
return (candidate.standardizedFileURL, false)
case .presentUnmaterialized:
// iCloud 占位符在:永不判缺席,也不进入灰态(计划 §5 阶段3)。
fileMissingSinceByItemID.removeValue(forKey: item.id)
return (candidate.standardizedFileURL, false)
case .inaccessible:
return keepUnavailableImportedItem(at: index)
case .absent:
let parent = candidate.deletingLastPathComponent()
switch CourseProjectFileWorker.entryPresence(at: parent) {
case .present:
case .present, .presentUnmaterialized:
// 灰态保护(计划 §5 阶段2):iCloud 瞬断/同步延迟期间文件缺席
// 不立即销毁条目;连续两个对账周期(≈6 秒)仍缺席才移除,
// 移除前对未落盘内容副本先行。文件重新出现按相对路径认领回原条目。
Expand Down
9 changes: 8 additions & 1 deletion Sources/WeiBei/Stores/WorkspaceStore+LibraryRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,15 @@ extension WorkspaceStore {
switch CourseProjectFileWorker.entryPresence(at: url) {
case .absent, .inaccessible:
importedItems[index].urlPath = nil
case .presentUnmaterialized:
// iCloud 占位符:保留路径供点开时触发系统下载(计划 §5 阶段3)。
importedItems[index].urlPath = url.path
case .present:
if let expectedDigest = importedItems[index].contentDigest,
// 阶段3 拆除启动指纹断链(笔记部分):digest 不符不再切断路径,
// 交给对账以文件为准;资料项(非笔记)世代保护保留。
let isNote = importedItems[index].isNotebookNote
if !isNote,
let expectedDigest = importedItems[index].contentDigest,
let actual = try? CourseProjectFileWorker.snapshotFile(at: url),
actual.sha256 != expectedDigest {
importedItems[index].urlPath = nil
Expand Down
18 changes: 17 additions & 1 deletion Sources/WeiBei/Stores/WorkspaceStore+NotesPersistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,23 @@ extension WorkspaceStore {
WeiBeiLog.noteRepair.error("note file error raised: \(message, privacy: .public)")
}
if !alreadyRecorded, activeNoteItemID == itemID {
showImportantOperationError(message)
// 阶段3 横幅降格(计划 §5 阶段3):文件缺席/未物化等不可用场景
// 只保留条内状态,不再弹重要操作横幅;其余真实错误维持横幅。
let itemUnavailable: Bool = {
guard let item = importedItems.first(where: { $0.id == itemID }),
let url = resolvedLibraryURL(for: item) else {
return true
}
switch CourseProjectFileWorker.entryPresence(at: url) {
case .present:
return false
case .presentUnmaterialized, .absent, .inaccessible:
return true
}
}()
if !itemUnavailable {
showImportantOperationError(message)
}
}
} else {
let cleared = noteOperationErrorsByItemID.removeValue(forKey: itemID)
Expand Down
32 changes: 24 additions & 8 deletions Sources/WeiBei/Stores/WorkspaceStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6162,7 +6162,7 @@ final class WorkspaceStore: ObservableObject {
switch CourseProjectFileWorker.entryPresence(at: libraryRoot) {
case .absent, .inaccessible:
return
case .present:
case .present, .presentUnmaterialized:
break
}
let reserved: Set<String> = [
Expand Down Expand Up @@ -18576,6 +18576,12 @@ final class WorkspaceStore: ObservableObject {
) else {
return
}
if case .presentUnmaterialized = CourseProjectFileWorker.entryPresence(at: access.url) {
setNoteFileError(
ui("正在从 iCloud 下载…", "Downloading from iCloud…"),
for: item.id
)
}
let url = access.url
let identity = access.fileIdentity
let itemID = item.id
Expand Down Expand Up @@ -18661,11 +18667,21 @@ final class WorkspaceStore: ObservableObject {
guard courseNoteLoadGenerationByItemID[itemID] == generation else {
return
}
let presence = CourseProjectFileWorker.entryPresence(at: url)
let unmaterialized: Bool = {
if case .presentUnmaterialized = presence { return true }
return false
}()
setNoteFileError(
ui(
"无法读取原 Markdown:\(url.lastPathComponent)",
"Could not read original Markdown: \(url.lastPathComponent)"
),
unmaterialized
? ui(
"当前不可用,iCloud 文件未下载。",
"Currently unavailable; the iCloud file has not been downloaded."
)
: ui(
"无法读取原 Markdown:\(url.lastPathComponent)",
"Could not read original Markdown: \(url.lastPathComponent)"
),
for: itemID
)
}
Expand Down Expand Up @@ -19380,7 +19396,7 @@ final class WorkspaceStore: ObservableObject {
documentIdentifier: UInt64?
)? {
switch CourseProjectFileWorker.entryPresence(at: candidate) {
case .absent:
case .absent, .presentUnmaterialized:
return nil
case .inaccessible:
throw CoursePortableStateError.unsafeRelativePath
Expand Down Expand Up @@ -19457,7 +19473,7 @@ final class WorkspaceStore: ObservableObject {
throw CoursePortableStateError.crossCourseReference
}
switch CourseProjectFileWorker.entryPresence(at: candidate) {
case .absent:
case .absent, .presentUnmaterialized:
return nil
case .inaccessible:
throw CoursePortableStateError.crossCourseReference
Expand Down Expand Up @@ -19498,7 +19514,7 @@ final class WorkspaceStore: ObservableObject {
sharedURL: URL
) throws -> ImportedFileIdentity? {
switch CourseProjectFileWorker.entryPresence(at: candidate) {
case .absent:
case .absent, .presentUnmaterialized:
return nil
case .inaccessible:
throw CoursePortableStateError.invalidItemStorage
Expand Down
Loading
Loading