[Bug] (+Solution?) Documents fail to load when opening from library view
This is the full writeup of the issue I already mentioned in #15.
I experienced a consistently occurring bug where opening a comic from the library view led to strange behavior. While I could open the comics, I could not cycle through the pages (and sometimes the window was even completely blank). Opening the comic directly from the Finder worked in all cases, however. Only the library was affected.
The steps are:
- open a .cbr file with Fomalhaut that was not added to the library yet
- be able to cycle through the pages ~, even when closing the comic and reopening it from the library window~ (this not necessary to get the bug after all)
- close the comic
- ~quit Fomalhaut~
- ~reopen Fomalhaut and~ open the comic from the library
- comic either displays a blank window or cannot cycle through all pages (neither scrolling, nor arrow keys, nor toolbar buttons work)
- if pages were still in the cache, then scrolling one or two pages sometimes worked, but not more; often none were in the cache so the window was blank
- open the same file directly from the finder and everything works again ~, even when opening it from the library - but only until quitting and reopening Fomalhaut again~ (not true after all)
This was the Xcode log for this process:
2025-03-14 17:13:52.642 [Debug] [BookCollectionViewController.swift:233] resolveBookURL(_:) > bookmarkDataIsStale = false 2025-03-14 17:13:52.744 [Error] [SpreadPageViewController.swift:349] updateWindowContentSize() > window is nil 2025-03-14 17:13:52.762 [Debug] [SpreadPageViewController.swift:353] updateWindowContentSize() > First Image is not yet loaded 2025-03-14 17:13:52.775 [Debug] [BookDocument.swift:99] image(at:) > fail to load from cache at 2 2025-03-14 17:13:52.802 [Info] [RarArchiver.swift:53] image(at:completion:) > Extracted 2 successfully. 2025-03-14 17:13:52.802 [Debug] [BookDocument.swift:99] image(at:) > fail to load from cache at 3 2025-03-14 17:13:52.805 [Info] [RarArchiver.swift:56] image(at:completion:) > Error while extracting at 3: badArchive Unhandled error happened: brokenFile Could not open() the item: [1: Operation not permitted]
(I added the "Extracted <page number> successfully" message myself to get a better idea what's happening)
What appears to happen is that somehow the macOS sandbox does not play well with accessing the file without the involvement of explicit user action through the Finder (or the file picker); at least on my machine. I put the comic file I tested this with in a normal folder within my user home folder, and I set no extra file or system restrictions or anything similar, just used the app as would be normal. In addition, the file is completely fine and not malformed, as can be seen from the fact that opening it with Fomalhaut from the Finder or using QuickLook is not a problem.
I was able to get the issue to disappear on my machine by adding the following block in RarArchiver.swift at line 46 before the do block:
// ...
let entry = self.entries[page]
guard self.fileURL.startAccessingSecurityScopedResource() else {
log.error("Failed to access security scoped resource for file \(self.fileURL)")
completion(.failure(ArchiverError.brokenFile))
return
}
defer { self.fileURL.stopAccessingSecurityScopedResource() }
do {
// ...
As I said before, since I am very new to Swift (but I do know other programming languages quite well), I am not sure if this is the most elegant or well-fitting solution in the context of the rest of the codebase; but I can confirm that it solves my problem.
This issue is still present for me in the new version 1.9. Let me know if you need any more input about this bug!
I was able to get the issue to disappear on my machine by adding the following block in RarArchiver.swift at line 46 before the do block:
It seems to be strange. Because Fomalhaut2 already calls NSURL.startAccessingSecurityScopedResource() in BookCollectionViewController.open(book:).
https://github.com/mtgto/Fomalhaut2/blob/36f35e17bd10647c7c8dd6f79ea1669f50af5634/Fomalhaut2/BookCollectionViewController.swift#L256-L259
Does this problem occur in other formats than Rar archives, e.g. PDF or Zip archives?
It only happens with RAR (.cbr) files, ZIP files or PDFs are unaffected. I tested this with some of my own regular PDFs and the comic book archive files provided in this project: https://github.com/clach04/sample_reading_media The issue with the .cbr files is consistently occurring, however: The bug appears every single time for every file I tested, be that my own files or the ones from the repository above.