diff --git a/Piperly/Models/Book.swift b/Piperly/Models/Book.swift index d4ed1fb..ed9297b 100644 --- a/Piperly/Models/Book.swift +++ b/Piperly/Models/Book.swift @@ -18,6 +18,7 @@ import Foundation struct Book: Identifiable, Codable { let id: UUID + let contentIdentity: String let title: String let author: String let fileName: String @@ -25,15 +26,30 @@ struct Book: Identifiable, Codable { init( id: UUID = UUID(), + contentIdentity: String, title: String, author: String, fileName: String, coverImageName: String? = nil ) { self.id = id + self.contentIdentity = contentIdentity self.title = title self.author = author self.fileName = fileName self.coverImageName = coverImageName } + + /// Legacy blobs predate `contentIdentity`; default it to "" so the book + /// survives decode. BookStore backfills the real hash (and migrates the + /// filename) on load. See BookStore.backfillContentIdentities(). + init(from decoder: Decoder) throws { + let c = try decoder.container(keyedBy: CodingKeys.self) + id = try c.decode(UUID.self, forKey: .id) + contentIdentity = try c.decodeIfPresent(String.self, forKey: .contentIdentity) ?? "" + title = try c.decode(String.self, forKey: .title) + author = try c.decode(String.self, forKey: .author) + fileName = try c.decode(String.self, forKey: .fileName) + coverImageName = try c.decodeIfPresent(String.self, forKey: .coverImageName) + } } diff --git a/Piperly/Services/BookStore.swift b/Piperly/Services/BookStore.swift index 8131564..b9f0490 100644 --- a/Piperly/Services/BookStore.swift +++ b/Piperly/Services/BookStore.swift @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +import CryptoKit import Foundation import SwiftUI @preconcurrency import ReadiumShared @@ -30,6 +31,7 @@ class BookStore: ObservableObject { private let documentsURL: URL private let coversURL: URL + private let importSnapshotsURL: URL private let userDefaults: UserDefaults private let booksKey = "piperly_books" private let profilesKey = "piperly_reader_profiles" @@ -40,6 +42,8 @@ class BookStore: ObservableObject { private var debouncedBooksSave: Task? private var debouncedReadingStatesSave: Task? private var debouncedWordsSave: Task? + private var importsByContentIdentity: [String: Task] = [:] + nonisolated private static let fileReadChunkSize = 1_048_576 /// Keys whose persisted bytes were non-empty but decoded to nothing (total /// corruption). We refuse to overwrite them with an empty array so a bad @@ -84,16 +88,29 @@ class BookStore: ObservableObject { } } - init(userDefaults: UserDefaults = .standard) { + convenience init(userDefaults: UserDefaults = .standard) { let docs = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] - self.documentsURL = docs.appendingPathComponent("Books", isDirectory: true) - self.coversURL = docs.appendingPathComponent("Covers", isDirectory: true) + self.init( + documentsURL: docs.appendingPathComponent("Books", isDirectory: true), + coversURL: docs.appendingPathComponent("Covers", isDirectory: true), + userDefaults: userDefaults + ) + } + + init(documentsURL: URL, coversURL: URL, userDefaults: UserDefaults) { + self.documentsURL = documentsURL + self.coversURL = coversURL + self.importSnapshotsURL = documentsURL.deletingLastPathComponent() + .appendingPathComponent(".PiperlyImportSnapshots", isDirectory: true) self.userDefaults = userDefaults try? FileManager.default.createDirectory(at: documentsURL, withIntermediateDirectories: true) try? FileManager.default.createDirectory(at: coversURL, withIntermediateDirectories: true) + try? FileManager.default.createDirectory(at: importSnapshotsURL, withIntermediateDirectories: true) + Self.clearStaleSnapshots(in: importSnapshotsURL) Self.excludeFromBackup(documentsURL) Self.excludeFromBackup(coversURL) + Self.excludeFromBackup(importSnapshotsURL) loadBooks() loadProfiles() loadSelectedProfileID() @@ -105,7 +122,7 @@ class BookStore: ObservableObject { /// Books are re-importable and covers are re-derivable via `backfillCovers()`, /// so neither belongs in iCloud backups (Apple Data Storage Guidelines). - private nonisolated static func excludeFromBackup(_ url: URL) { + nonisolated private static func excludeFromBackup(_ url: URL) { var url = url var values = URLResourceValues() values.isExcludedFromBackup = true @@ -329,18 +346,79 @@ class BookStore: ObservableObject { let accessing = sourceURL.startAccessingSecurityScopedResource() defer { if accessing { sourceURL.stopAccessingSecurityScopedResource() } } - let originalName = sourceURL.lastPathComponent - let storedFileName = Self.uniqueFileName(for: originalName) + let snapshotsURL = importSnapshotsURL + let snapshotURL = try await Task.detached(priority: .userInitiated) { + try Self.snapshotSource(at: sourceURL, in: snapshotsURL) + }.value + defer { try? FileManager.default.removeItem(at: snapshotURL) } + + return try await importSnapshot(snapshotURL, originalName: sourceURL.lastPathComponent) + } + + private func importSnapshot(_ snapshotURL: URL, originalName: String) async throws -> Book { + let contentIdentity = try await Task.detached(priority: .userInitiated) { + try Self.contentIdentity(for: snapshotURL) + }.value + + if let importTask = importsByContentIdentity[contentIdentity] { + return try await importTask.value + } + + let importTask = Task { + try await finishImport( + snapshotURL, + originalName: originalName, + contentIdentity: contentIdentity + ) + } + importsByContentIdentity[contentIdentity] = importTask + + do { + let book = try await importTask.value + importsByContentIdentity[contentIdentity] = nil + return book + } catch { + importsByContentIdentity[contentIdentity] = nil + throw error + } + } + + private func finishImport( + _ snapshotURL: URL, + originalName: String, + contentIdentity: String + ) async throws -> Book { + let storedFileName = Self.storageFileName(for: contentIdentity) let destURL = documentsURL.appendingPathComponent(storedFileName) - try FileManager.default.copyItem(at: sourceURL, to: destURL) + if let existingBook = books.first(where: { $0.contentIdentity == contentIdentity }) { + if FileManager.default.fileExists(atPath: destURL.path) { + let filesMatch = try await Task.detached(priority: .userInitiated) { + try Self.filesHaveEqualBytes(snapshotURL, destURL) + }.value + guard filesMatch else { throw BookStoreError.contentIdentityCollision } + } else { + _ = try await openPublication(at: snapshotURL) + try await publishSnapshot(snapshotURL, to: destURL) + } + return existingBook + } - guard let (title, author, coverName) = try? await parseMetadataAndCover(from: destURL) else { - try? FileManager.default.removeItem(at: destURL) + guard let (title, author, coverName) = try? await parseMetadataAndCover(from: snapshotURL) else { throw BookStoreError.unreadableBook } + do { + try await publishSnapshot(snapshotURL, to: destURL) + } catch { + if let coverName { + try? FileManager.default.removeItem(at: coversURL.appendingPathComponent(coverName)) + } + throw error + } + let book = Book( + contentIdentity: contentIdentity, title: title ?? URL(fileURLWithPath: originalName).deletingPathExtension().lastPathComponent, author: author ?? "Unknown Author", fileName: storedFileName, @@ -352,10 +430,76 @@ class BookStore: ObservableObject { return book } - /// Builds a collision-free destination filename by prefixing a UUID, while - /// preserving the original name (and extension) for readability. - nonisolated static func uniqueFileName(for originalName: String) -> String { - "\(UUID().uuidString)-\(originalName)" + private func publishSnapshot(_ snapshotURL: URL, to destURL: URL) async throws { + if FileManager.default.fileExists(atPath: destURL.path) { + let filesMatch = try await Task.detached(priority: .userInitiated) { + try Self.filesHaveEqualBytes(snapshotURL, destURL) + }.value + guard filesMatch else { throw BookStoreError.contentIdentityCollision } + return + } + + do { + try FileManager.default.moveItem(at: snapshotURL, to: destURL) + } catch { + guard FileManager.default.fileExists(atPath: destURL.path) else { throw error } + let filesMatch = try await Task.detached(priority: .userInitiated) { + try Self.filesHaveEqualBytes(snapshotURL, destURL) + }.value + guard filesMatch else { throw BookStoreError.contentIdentityCollision } + } + } + + nonisolated private static func snapshotSource(at sourceURL: URL, in directoryURL: URL) throws -> URL { + try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true) + let snapshotURL = directoryURL.appendingPathComponent("\(UUID().uuidString).epub") + do { + try FileManager.default.copyItem(at: sourceURL, to: snapshotURL) + return snapshotURL + } catch { + try? FileManager.default.removeItem(at: snapshotURL) + throw error + } + } + + nonisolated private static func clearStaleSnapshots(in directoryURL: URL) { + guard let staleURLs = try? FileManager.default.contentsOfDirectory( + at: directoryURL, + includingPropertiesForKeys: nil + ) else { return } + + for staleURL in staleURLs { + try? FileManager.default.removeItem(at: staleURL) + } + } + + nonisolated static func contentIdentity(for url: URL) throws -> String { + let handle = try FileHandle(forReadingFrom: url) + defer { try? handle.close() } + + var hasher = SHA256() + while let bytes = try handle.read(upToCount: fileReadChunkSize), !bytes.isEmpty { + hasher.update(data: bytes) + } + return hasher.finalize().map { String(format: "%02x", $0) }.joined() + } + + nonisolated static func storageFileName(for contentIdentity: String) -> String { + "\(contentIdentity).epub" + } + + nonisolated private static func filesHaveEqualBytes(_ firstURL: URL, _ secondURL: URL) throws -> Bool { + let firstHandle = try FileHandle(forReadingFrom: firstURL) + defer { try? firstHandle.close() } + let secondHandle = try FileHandle(forReadingFrom: secondURL) + defer { try? secondHandle.close() } + + while true { + let firstBytes = try firstHandle.read(upToCount: fileReadChunkSize) + let secondBytes = try secondHandle.read(upToCount: fileReadChunkSize) + guard firstBytes == secondBytes else { return false } + guard let firstBytes, !firstBytes.isEmpty else { return true } + } } func bookURL(for book: Book) -> URL { @@ -517,27 +661,7 @@ class BookStore: ObservableObject { } private func importBundledBook(from bundleURL: URL) async throws -> Book { - let fileName = bundleURL.lastPathComponent - let destURL = documentsURL.appendingPathComponent(fileName) - - if !FileManager.default.fileExists(atPath: destURL.path) { - try FileManager.default.copyItem(at: bundleURL, to: destURL) - } - - let (title, author, coverName) = (try? await parseMetadataAndCover(from: destURL)) ?? (nil, nil, nil) - - let book = Book( - title: title ?? URL(fileURLWithPath: fileName).deletingPathExtension().lastPathComponent, - author: author ?? "Unknown Author", - fileName: fileName, - coverImageName: coverName - ) - - if !books.contains(where: { $0.fileName == fileName }) { - books.append(book) - saveBooks() - } - return book + try await importBook(from: bundleURL) } func deleteBook(_ book: Book) { @@ -594,6 +718,44 @@ class BookStore: ObservableObject { return name } + /// Legacy books decoded with an empty `contentIdentity` (persisted before + /// stable identity existed). Hash the on-disk file, adopt the real identity, + /// and migrate the file to the deterministic `.epub` name so dedupe + /// and asset sync work. Legacy files still resolve via their old `fileName` + /// until migrated, so nothing breaks before this runs. + func backfillContentIdentities() async { + var changed = false + for i in books.indices where books[i].contentIdentity.isEmpty { + let currentURL = bookURL(for: books[i]) + guard FileManager.default.fileExists(atPath: currentURL.path), + let identity = try? await Task.detached(priority: .utility, operation: { + try Self.contentIdentity(for: currentURL) + }).value else { continue } + + let storedName = Self.storageFileName(for: identity) + let destURL = documentsURL.appendingPathComponent(storedName) + if currentURL.path != destURL.path { + if FileManager.default.fileExists(atPath: destURL.path) { + // Deterministic name already present (same bytes): drop the + // duplicate legacy file rather than clobber the canonical one. + try? FileManager.default.removeItem(at: currentURL) + } else { + try? FileManager.default.moveItem(at: currentURL, to: destURL) + } + } + books[i] = Book( + id: books[i].id, + contentIdentity: identity, + title: books[i].title, + author: books[i].author, + fileName: storedName, + coverImageName: books[i].coverImageName + ) + changed = true + } + if changed { saveBooks() } + } + func backfillCovers() async { var changed = false for i in books.indices where books[i].coverImageName == nil { @@ -611,6 +773,7 @@ class BookStore: ObservableObject { enum BookStoreError: Error { case invalidURL case unreadableBook + case contentIdentityCollision } /// Decodes to `nil` instead of throwing, so a bad element in an array doesn't diff --git a/Piperly/Views/Library/LibraryView.swift b/Piperly/Views/Library/LibraryView.swift index 44f6a48..f82f160 100644 --- a/Piperly/Views/Library/LibraryView.swift +++ b/Piperly/Views/Library/LibraryView.swift @@ -121,6 +121,7 @@ struct LibraryView: View { } } .task { + await bookStore.backfillContentIdentities() await bookStore.backfillCovers() } .fileImporter( diff --git a/PiperlyTests/PiperlyTests.swift b/PiperlyTests/PiperlyTests.swift index fd53152..0864f73 100644 --- a/PiperlyTests/PiperlyTests.swift +++ b/PiperlyTests/PiperlyTests.swift @@ -1,4 +1,3 @@ -// swiftlint:disable force_unwrapping import Testing import Foundation @testable import Piperly @@ -8,22 +7,47 @@ import Foundation @Suite("Book") struct BookTests { @Test func defaultValues() { - let book = Book(title: "Test", author: "Author", fileName: "test.epub") + let book = Book( + contentIdentity: "abc123", + title: "Test", + author: "Author", + fileName: "test.epub" + ) #expect(book.coverImageName == nil) } @Test func codableRoundTrip() throws { - let book = Book(title: "The Secret Garden", author: "Frances Hodgson Burnett", fileName: "secret-garden.epub") + let book = Book( + contentIdentity: "abc123", + title: "The Secret Garden", + author: "Frances Hodgson Burnett", + fileName: "secret-garden.epub" + ) let data = try JSONEncoder().encode(book) let decoded = try JSONDecoder().decode(Book.self, from: data) #expect(decoded.id == book.id) + #expect(decoded.contentIdentity == book.contentIdentity) #expect(decoded.title == book.title) #expect(decoded.author == book.author) #expect(decoded.fileName == book.fileName) } + // Legacy blobs predate contentIdentity; decode must not throw (which under + // try? decode([Book].self) would wipe the whole library on upgrade). + @Test func decodingLegacyBookWithoutContentIdentitySurvives() throws { + let id = UUID() + let legacyJSON = """ + {"id":"\(id.uuidString)","title":"Old Book","author":"A","fileName":"\(id.uuidString)-old.epub"} + """ + let decoded = try JSONDecoder().decode(Book.self, from: Data(legacyJSON.utf8)) + #expect(decoded.id == id) + #expect(decoded.contentIdentity.isEmpty) + #expect(decoded.fileName == "\(id.uuidString)-old.epub") + } + @Test func codableWithAllFields() throws { let book = Book( + contentIdentity: "abc123", title: "Test", author: "Author", fileName: "test.epub", @@ -544,40 +568,259 @@ struct VoiceTests { @Suite("BookStoreImport") struct BookStoreImportTests { - @Test func uniqueFileNamePreservesOriginalName() { - let name = BookStore.uniqueFileName(for: "alice.epub") - #expect(name.hasSuffix("-alice.epub")) + @Test @MainActor func startupClearsStaleImportSnapshots() throws { + let rootURL = FileManager.default.temporaryDirectory + .appendingPathComponent("piperly-stale-snapshots-\(UUID().uuidString)", isDirectory: true) + let booksDirectory = rootURL.appendingPathComponent("Books", isDirectory: true) + let coversDirectory = rootURL.appendingPathComponent("Covers", isDirectory: true) + let snapshotsDirectory = rootURL.appendingPathComponent(".PiperlyImportSnapshots", isDirectory: true) + try FileManager.default.createDirectory(at: snapshotsDirectory, withIntermediateDirectories: true) + try Data("stale epub bytes".utf8).write( + to: snapshotsDirectory.appendingPathComponent("stale.epub") + ) + defer { try? FileManager.default.removeItem(at: rootURL) } + + let defaultsName = "PiperlyTests.BookStoreStartupCleanup.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsName)) + defer { defaults.removePersistentDomain(forName: defaultsName) } + _ = BookStore( + documentsURL: booksDirectory, + coversURL: coversDirectory, + userDefaults: defaults + ) + + #expect(FileManager.default.fileExists(atPath: snapshotsDirectory.path)) + #expect(try FileManager.default.contentsOfDirectory(atPath: snapshotsDirectory.path).isEmpty) } - @Test func uniqueFileNameDiffersForSameSource() { - let first = BookStore.uniqueFileName(for: "alice.epub") - let second = BookStore.uniqueFileName(for: "alice.epub") - #expect(first != second) + @Test func sameBytesProduceSameIdentity() throws { + let firstURL = try temporaryFile(containing: Data("same bytes".utf8)) + let secondURL = try temporaryFile(containing: Data("same bytes".utf8)) + defer { + try? FileManager.default.removeItem(at: firstURL) + try? FileManager.default.removeItem(at: secondURL) + } + + #expect(try BookStore.contentIdentity(for: firstURL) == BookStore.contentIdentity(for: secondURL)) + } + + @Test func differentBytesProduceDifferentIdentities() throws { + let firstURL = try temporaryFile(containing: Data("first".utf8)) + let secondURL = try temporaryFile(containing: Data("second".utf8)) + defer { + try? FileManager.default.removeItem(at: firstURL) + try? FileManager.default.removeItem(at: secondURL) + } + + #expect(try BookStore.contentIdentity(for: firstURL) != BookStore.contentIdentity(for: secondURL)) } - @Test func uniqueFileNameHasUUIDPrefix() { - let name = BookStore.uniqueFileName(for: "alice.epub") - let prefix = name.replacingOccurrences(of: "-alice.epub", with: "") - #expect(UUID(uuidString: prefix) != nil) + @Test func identityIncludesBytesBeyondFirstReadChunk() throws { + let prefix = Data(repeating: 0x41, count: 1_048_576) + let firstURL = try temporaryFile(containing: prefix + Data([0x42])) + let secondURL = try temporaryFile(containing: prefix + Data([0x43])) + defer { + try? FileManager.default.removeItem(at: firstURL) + try? FileManager.default.removeItem(at: secondURL) + } + + #expect(try BookStore.contentIdentity(for: firstURL) != BookStore.contentIdentity(for: secondURL)) + } + + @Test func identityIsDeterministicLowercaseHex() throws { + let fileURL = try temporaryFile(containing: Data("hello".utf8)) + defer { try? FileManager.default.removeItem(at: fileURL) } + + let identity = try BookStore.contentIdentity(for: fileURL) + #expect(identity == "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824") + #expect(identity.allSatisfy { $0.isHexDigit && !$0.isUppercase }) + #expect(BookStore.storageFileName(for: identity) == "\(identity).epub") } @Test @MainActor func rejectsUnreadableEPUBWithoutAddingBook() async throws { - let store = BookStore() - let sourceName = "garbage-\(UUID().uuidString).epub" - let sourceURL = FileManager.default.temporaryDirectory.appendingPathComponent(sourceName) + let rootURL = FileManager.default.temporaryDirectory + .appendingPathComponent("piperly-unreadable-\(UUID().uuidString)", isDirectory: true) + let booksDirectory = rootURL.appendingPathComponent("Books", isDirectory: true) + let coversDirectory = rootURL.appendingPathComponent("Covers", isDirectory: true) + try FileManager.default.createDirectory(at: rootURL, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: rootURL) } + + let defaultsName = "PiperlyTests.BookStoreUnreadable.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsName)) + defer { defaults.removePersistentDomain(forName: defaultsName) } + let store = BookStore( + documentsURL: booksDirectory, + coversURL: coversDirectory, + userDefaults: defaults + ) + + let sourceURL = rootURL.appendingPathComponent("garbage.epub") try Data("not an epub".utf8).write(to: sourceURL) - defer { try? FileManager.default.removeItem(at: sourceURL) } + let expectedStoredName = BookStore.storageFileName(for: try BookStore.contentIdentity(for: sourceURL)) + let contentsBefore = try FileManager.default.contentsOfDirectory(atPath: booksDirectory.path) + + await #expect(throws: BookStoreError.self) { + _ = try await store.importBook(from: sourceURL) + } + #expect(store.books.isEmpty) + #expect(try FileManager.default.contentsOfDirectory(atPath: booksDirectory.path) == contentsBefore) + #expect(!FileManager.default.fileExists( + atPath: booksDirectory.appendingPathComponent(expectedStoredName).path + )) + let snapshotsDirectory = rootURL.appendingPathComponent(".PiperlyImportSnapshots", isDirectory: true) + #expect(try FileManager.default.contentsOfDirectory(atPath: snapshotsDirectory.path).isEmpty) + } + + @Test @MainActor func duplicateImportReturnsExistingBookWithoutExtraFilesOrMetadata() async throws { + let rootURL = FileManager.default.temporaryDirectory + .appendingPathComponent("piperly-import-\(UUID().uuidString)", isDirectory: true) + let sourceDirectory = rootURL.appendingPathComponent("Sources", isDirectory: true) + let booksDirectory = rootURL.appendingPathComponent("Books", isDirectory: true) + let coversDirectory = rootURL.appendingPathComponent("Covers", isDirectory: true) + try FileManager.default.createDirectory(at: sourceDirectory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: rootURL) } + + let defaultsName = "PiperlyTests.BookStoreImport.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsName)) + defer { defaults.removePersistentDomain(forName: defaultsName) } + let store = BookStore( + documentsURL: booksDirectory, + coversURL: coversDirectory, + userDefaults: defaults + ) + + let bundledBook = try #require(Bundle.main.url(forResource: "the-secret-garden", withExtension: "epub")) + let firstSource = sourceDirectory.appendingPathComponent("first.epub") + let secondSource = sourceDirectory.appendingPathComponent("renamed.epub") + try FileManager.default.copyItem(at: bundledBook, to: firstSource) + try FileManager.default.copyItem(at: bundledBook, to: secondSource) + + let first = try await store.importBook(from: firstSource) + let second = try await store.importBook(from: secondSource) + + #expect(first.id == second.id) + #expect(first.contentIdentity == second.contentIdentity) + #expect(store.books.count == 1) + #expect(try FileManager.default.contentsOfDirectory(atPath: booksDirectory.path) == [first.fileName]) + let coverFiles = try FileManager.default.contentsOfDirectory(atPath: coversDirectory.path) + #expect(coverFiles.count <= 1) + } + + @Test @MainActor func reimportRestoresMissingDeterministicFile() async throws { + let rootURL = FileManager.default.temporaryDirectory + .appendingPathComponent("piperly-repair-\(UUID().uuidString)", isDirectory: true) + let sourceDirectory = rootURL.appendingPathComponent("Sources", isDirectory: true) + let booksDirectory = rootURL.appendingPathComponent("Books", isDirectory: true) + let coversDirectory = rootURL.appendingPathComponent("Covers", isDirectory: true) + try FileManager.default.createDirectory(at: sourceDirectory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: rootURL) } + + let defaultsName = "PiperlyTests.BookStoreRepair.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsName)) + defer { defaults.removePersistentDomain(forName: defaultsName) } + let store = BookStore( + documentsURL: booksDirectory, + coversURL: coversDirectory, + userDefaults: defaults + ) + + let bundledBook = try #require(Bundle.main.url(forResource: "the-secret-garden", withExtension: "epub")) + let firstSource = sourceDirectory.appendingPathComponent("first.epub") + let secondSource = sourceDirectory.appendingPathComponent("second.epub") + try FileManager.default.copyItem(at: bundledBook, to: firstSource) + try FileManager.default.copyItem(at: bundledBook, to: secondSource) + + let original = try await store.importBook(from: firstSource) + let storedURL = store.bookURL(for: original) + try FileManager.default.removeItem(at: storedURL) + #expect(!FileManager.default.fileExists(atPath: storedURL.path)) + + let repaired = try await store.importBook(from: secondSource) + + #expect(repaired.id == original.id) + #expect(store.books.count == 1) + #expect(FileManager.default.fileExists(atPath: storedURL.path)) + #expect(try Data(contentsOf: storedURL) == Data(contentsOf: bundledBook)) + } + + @Test @MainActor func concurrentSameContentImportsPublishOneBookAndFile() async throws { + let rootURL = FileManager.default.temporaryDirectory + .appendingPathComponent("piperly-concurrent-\(UUID().uuidString)", isDirectory: true) + let sourceDirectory = rootURL.appendingPathComponent("Sources", isDirectory: true) + let booksDirectory = rootURL.appendingPathComponent("Books", isDirectory: true) + let coversDirectory = rootURL.appendingPathComponent("Covers", isDirectory: true) + try FileManager.default.createDirectory(at: sourceDirectory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: rootURL) } + + let defaultsName = "PiperlyTests.BookStoreConcurrent.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsName)) + defer { defaults.removePersistentDomain(forName: defaultsName) } + let store = BookStore( + documentsURL: booksDirectory, + coversURL: coversDirectory, + userDefaults: defaults + ) + + let bundledBook = try #require(Bundle.main.url(forResource: "the-secret-garden", withExtension: "epub")) + let firstSource = sourceDirectory.appendingPathComponent("first.epub") + let secondSource = sourceDirectory.appendingPathComponent("second.epub") + try FileManager.default.copyItem(at: bundledBook, to: firstSource) + try FileManager.default.copyItem(at: bundledBook, to: secondSource) + + async let first = store.importBook(from: firstSource) + async let second = store.importBook(from: secondSource) + let (firstBook, secondBook) = try await (first, second) + + #expect(firstBook.id == secondBook.id) + #expect(store.books.count == 1) + #expect(try FileManager.default.contentsOfDirectory(atPath: booksDirectory.path) == [firstBook.fileName]) + let snapshotsDirectory = rootURL.appendingPathComponent(".PiperlyImportSnapshots", isDirectory: true) + #expect(try FileManager.default.contentsOfDirectory(atPath: snapshotsDirectory.path).isEmpty) + } + + @Test @MainActor func matchingIdentityNeverOverwritesDifferentStoredBytes() async throws { + let rootURL = FileManager.default.temporaryDirectory + .appendingPathComponent("piperly-collision-\(UUID().uuidString)", isDirectory: true) + let booksDirectory = rootURL.appendingPathComponent("Books", isDirectory: true) + let coversDirectory = rootURL.appendingPathComponent("Covers", isDirectory: true) + try FileManager.default.createDirectory(at: booksDirectory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: rootURL) } + + let defaultsName = "PiperlyTests.BookStoreCollision.\(UUID().uuidString)" + let defaults = try #require(UserDefaults(suiteName: defaultsName)) + defer { defaults.removePersistentDomain(forName: defaultsName) } + let store = BookStore( + documentsURL: booksDirectory, + coversURL: coversDirectory, + userDefaults: defaults + ) + + let sourceURL = rootURL.appendingPathComponent("source.epub") + let sourceBytes = Data("source bytes".utf8) + try sourceBytes.write(to: sourceURL) + let identity = try BookStore.contentIdentity(for: sourceURL) + let storedName = BookStore.storageFileName(for: identity) + let storedURL = booksDirectory.appendingPathComponent(storedName) + let storedBytes = Data("different stored bytes".utf8) + try storedBytes.write(to: storedURL) + store.books = [Book( + contentIdentity: identity, + title: "Existing", + author: "Author", + fileName: storedName + )] - let countBefore = store.books.count await #expect(throws: BookStoreError.self) { _ = try await store.importBook(from: sourceURL) } - #expect(store.books.count == countBefore) + #expect(try Data(contentsOf: storedURL) == storedBytes) + #expect(store.books.count == 1) + } - let booksDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] - .appendingPathComponent("Books", isDirectory: true) - let leftovers = (try? FileManager.default.contentsOfDirectory(atPath: booksDir.path)) ?? [] - #expect(!leftovers.contains { $0.hasSuffix(sourceName) }) + private func temporaryFile(containing data: Data) throws -> URL { + let url = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try data.write(to: url) + return url } } diff --git a/docs/adr/0001-icloud-book-sync-and-reader-profiles.md b/docs/adr/0001-icloud-book-sync-and-reader-profiles.md index 6e1a24d..90903c0 100644 --- a/docs/adr/0001-icloud-book-sync-and-reader-profiles.md +++ b/docs/adr/0001-icloud-book-sync-and-reader-profiles.md @@ -28,4 +28,4 @@ Piperly will make iCloud sync a private, family/account-scoped feature: ## Consequences -Book identity must become stable across devices, likely by hashing EPUB contents during import. The sync implementation must handle iCloud file availability, download progress, evicted files, deletion semantics, and conflicts. Privacy review and App Store privacy answers must be updated once iCloud sync is enabled because book titles, reading state, and saved words are child-adjacent data even when they remain in the user's private iCloud account. +Book identity is the lowercase SHA-256 hash of the imported EPUB bytes. Future CloudKit `Book` records must use this content identity as their record name, while the local UUID can continue to connect on-device reading data. The sync implementation must handle iCloud file availability, download progress, evicted files, deletion semantics, and conflicts. Privacy review and App Store privacy answers must be updated once iCloud sync is enabled because book titles, reading state, and saved words are child-adjacent data even when they remain in the user's private iCloud account.