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
254 changes: 0 additions & 254 deletions Docs/plans/2026-08-22-file-model-convergence-and-library-relocation.md

This file was deleted.

6 changes: 0 additions & 6 deletions Sources/WeiBei/Editing/WorkspaceStore+NoteEditing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,6 @@ extension WorkspaceStore {
noteEditorRecoveryConflict = nil
if useDisk {
let documentID = conflict.checkpoint.metadata.documentID
// 副本先行(计划 §5 阶段2):选磁盘版本前,用户版本先入备份环。
_ = try? NoteBackupRing.capture(
content: Data(conflict.checkpoint.markdown.utf8),
itemID: documentID,
rootURL: noteBackupRootURL
)
try? await noteRecoveryStore.remove(documentID: documentID)
cancelPendingNotePersistence(for: documentID)
pendingNotePersistenceByItemID.removeValue(forKey: documentID)
Expand Down
17 changes: 0 additions & 17 deletions Sources/WeiBei/Stores/CourseProjectRootSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5363,11 +5363,6 @@ enum CourseProjectRootError: LocalizedError {
case metadataConflict
case manifestMismatch
case workspaceSaveFailed
case destinationInsideLibrary
case destinationContainsLibrary
case destinationIsLibrary
case destinationNotEmpty
case migrationFailed(String)

var errorDescription: String? {
switch self {
Expand Down Expand Up @@ -5411,18 +5406,6 @@ enum CourseProjectRootError: LocalizedError {
return "课程 manifest 与当前课程记录冲突。"
case .workspaceSaveFailed:
return "课程状态没有成功保存。魏碑只撤销能确认属于本次操作的内容;如果磁盘内容已经变化,会原样保留。"
case .destinationInsideLibrary:
return "所选位置在当前资料库内部,请选择资料库之外的位置。"
case .destinationContainsLibrary:
return "所选位置是当前资料库的上级目录,请选择其他位置。"
case .destinationIsLibrary:
return "所选位置已经是一个魏碑资料库,可以直接认领。"
case .destinationNotEmpty:
return "所选文件夹不是空的,迁移需要一个空文件夹。"
case .migrationFailed(let detail):
return detail.isEmpty
? "资料库迁移未完成,原资料库保持不变。"
: "资料库迁移未完成:\(detail)。原资料库保持不变。"
}
}
}
Expand Down
30 changes: 2 additions & 28 deletions Sources/WeiBei/Stores/WorkspaceStore+GoneImportedItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,6 @@ import WeiBeiCore

@MainActor
extension WorkspaceStore {
/// 副本先行(计划 §5 阶段2):采用外部内容或销毁条目前,把未落盘内容存入备份环。
func backUpUnsavedNoteContentBeforeAdopting(itemID: String) {
guard let unsaved = pendingNotePersistenceByItemID[itemID]?.markdown
?? notesByItemID[itemID] else {
return
}
_ = try? NoteBackupRing.capture(
content: Data(unsaved.utf8),
itemID: itemID,
rootURL: noteBackupRootURL
)
}

@discardableResult
func forgetGoneImportedItem(at index: Int) -> (url: URL?, changed: Bool) {
guard importedItems.indices.contains(index) else { return (nil, false) }
Expand All @@ -36,28 +23,15 @@ extension WorkspaceStore {
}
switch CourseProjectFileWorker.entryPresence(at: candidate) {
case .present:
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:
// 灰态保护(计划 §5 阶段2):iCloud 瞬断/同步延迟期间文件缺席
// 不立即销毁条目;连续两个对账周期(≈6 秒)仍缺席才移除,
// 移除前对未落盘内容副本先行。文件重新出现按相对路径认领回原条目。
if let missingSince = fileMissingSinceByItemID[item.id] {
if Date().timeIntervalSince(missingSince) >= 6.0 {
backUpUnsavedNoteContentBeforeAdopting(itemID: item.id)
removeItemRegistration(item.id)
fileMissingSinceByItemID.removeValue(forKey: item.id)
return (nil, true)
}
} else {
fileMissingSinceByItemID[item.id] = Date()
}
return keepUnavailableImportedItem(at: index)
removeItemRegistration(item.id)
return (nil, true)
case .absent, .inaccessible:
return keepUnavailableImportedItem(at: index)
}
Expand Down
145 changes: 0 additions & 145 deletions Sources/WeiBei/Stores/WorkspaceStore+LibraryRoot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,151 +262,6 @@ extension WorkspaceStore {
}
}

struct LibraryMigrationResult {
let destination: URL
let movedItemCount: Int
}

func migrateLibrary(to destination: URL) async throws -> LibraryMigrationResult {
guard let libraryRoot = courseLibraryRootURL else {
throw CourseProjectRootError.missingLibrary
}
try validateMigrationDestinationRelationship(
destination,
libraryRoot: libraryRoot
)
let canonicalDestination: URL
if FileManager.default.fileExists(atPath: destination.path) {
canonicalDestination = try CourseProjectPathPolicy.existingDirectory(destination)
try validateMigrationDestinationContent(canonicalDestination)
} else {
let parent = try CourseProjectPathPolicy.existingDirectory(
destination.deletingLastPathComponent()
)
canonicalDestination = parent.appendingPathComponent(
destination.lastPathComponent,
isDirectory: true
)
}

flushPendingNotePersistence()
libraryMigrationInFlight = true
defer { libraryMigrationInFlight = false }

let fileManager = FileManager.default
let sameVolume = Self.volumeDeviceID(of: libraryRoot)
== Self.volumeDeviceID(of: canonicalDestination.deletingLastPathComponent())
var movedBack: [URL] = []
do {
if sameVolume {
if fileManager.fileExists(atPath: canonicalDestination.path) {
try fileManager.removeItem(at: canonicalDestination)
}
try fileManager.moveItem(at: libraryRoot, to: canonicalDestination)
} else {
let staging = canonicalDestination.deletingLastPathComponent()
.appendingPathComponent(
canonicalDestination.lastPathComponent + "-weibei-migrating",
isDirectory: true
)
try? fileManager.removeItem(at: staging)
try fileManager.createDirectory(at: staging, withIntermediateDirectories: true)
let entries = try fileManager.contentsOfDirectory(
at: libraryRoot,
includingPropertiesForKeys: nil
)
for entry in entries {
let target = staging.appendingPathComponent(entry.lastPathComponent)
try fileManager.moveItem(at: entry, to: target)
movedBack.append(target)
}
if fileManager.fileExists(atPath: canonicalDestination.path) {
try fileManager.removeItem(at: canonicalDestination)
}
try fileManager.moveItem(at: staging, to: canonicalDestination)
}
} catch {
for staged in movedBack.reversed() {
try? fileManager.moveItem(
at: staged,
to: libraryRoot.appendingPathComponent(staged.lastPathComponent)
)
}
throw CourseProjectRootError.migrationFailed(error.localizedDescription)
}

guard let identity = importedFileIdentityResolver(canonicalDestination),
let bookmark = courseRootBookmarkMaker(canonicalDestination) else {
applyBoundLibraryRoot(libraryRoot, identity: courseLibraryRootIdentity, bookmark: courseLibraryRootBookmarkData)
throw CourseProjectRootError.rootIdentityUnavailable
}
applyBoundLibraryRoot(canonicalDestination, identity: identity, bookmark: bookmark)
for course in courses {
guard let relativePath = course.sourceRootRelativePath,
let folder = CourseProjectPathPolicy.resolvedRelativePath(
relativePath,
inside: canonicalDestination
) else {
continue
}
resolvedCourseRootURLs[course.id] = folder
courseRootUnavailableReasons.removeValue(forKey: course.id)
_ = resolveCourseOwnedItems(for: course.id)
}
refreshRuntimeItemURLs()
for course in courses {
guard let relativePath = course.sourceRootRelativePath,
let folder = CourseProjectPathPolicy.resolvedRelativePath(
relativePath,
inside: canonicalDestination
),
courseManifestCourseID(at: folder) == course.id else {
throw CourseProjectRootError.migrationFailed(
ui("课程 \(course.title) 的清单校验未通过", "Course manifest check failed for \(course.title)")
)
}
}
courseDocumentSearchIndex.synchronize(allItems)
invalidateAgentContext()
libraryMigrationInFlight = false
flushPendingNotePersistence(flushWorkspace: false)
_ = await persistWorkspaceNow()
WeiBeiLog.workspace.notice("library_migration_completed")
return LibraryMigrationResult(
destination: canonicalDestination,
movedItemCount: (try? fileManager.contentsOfDirectory(at: canonicalDestination, includingPropertiesForKeys: nil))?.count ?? 0
)
}

private func validateMigrationDestinationRelationship(_ destination: URL, libraryRoot: URL) throws {
let rootPath = libraryRoot.standardizedFileURL.path
let destinationPath = destination.standardizedFileURL.path
if destinationPath == rootPath {
throw CourseProjectRootError.destinationIsLibrary
}
if destinationPath.hasPrefix(rootPath + "/") {
throw CourseProjectRootError.destinationInsideLibrary
}
if rootPath.hasPrefix(destinationPath + "/") {
throw CourseProjectRootError.destinationContainsLibrary
}
}

private func validateMigrationDestinationContent(_ destination: URL) throws {
let entries = try FileManager.default.contentsOfDirectory(atPath: destination.path)
guard !entries.isEmpty else { return }
if courseManifestCourseID(at: destination) != nil {
throw CourseProjectRootError.destinationIsLibrary
}
throw CourseProjectRootError.destinationNotEmpty
}

private static func volumeDeviceID(of url: URL) -> dev_t? {
var statBuffer = stat()
guard stat(url.path, &statBuffer) == 0 else { return nil }
return statBuffer.st_dev
}

/// Applies already-copied common-library files to the item model in one
/// MainActor transaction after the background copy loop finishes.
func applyImportedCommonFiles(
Expand Down
Loading