Skip to content

Commit

Permalink
Correct? access to secure url resources - these changes allow cloud /…
Browse files Browse the repository at this point in the history
… local files to import correctly
  • Loading branch information
proskd committed Oct 21, 2024
1 parent ba7d479 commit 6f0b1e3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
11 changes: 5 additions & 6 deletions PVLibrary/Sources/DirectoryWatcher/DirectoryWatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -505,14 +505,13 @@ extension DirectoryWatcher {
/// Handle an imported file
public func handleImportedFile(at url: URL) {
ILOG("Handling imported file: \(url.lastPathComponent)")
guard url.startAccessingSecurityScopedResource() else {
ELOG("Failed to access security scoped resource for file: \(url.lastPathComponent)")
return
}
let secureDoc = url.startAccessingSecurityScopedResource()

defer {
url.stopAccessingSecurityScopedResource()
ILOG("Stopped accessing security scoped resource for file: \(url.lastPathComponent)")
if secureDoc {
url.stopAccessingSecurityScopedResource()
ILOG("Stopped accessing security scoped resource for file: \(url.lastPathComponent)")
}
}

let coordinator = NSFileCoordinator()
Expand Down
10 changes: 5 additions & 5 deletions PVLibrary/Sources/PVLibrary/Importer/iCloud/iCloudSync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,13 @@ public enum iCloudSync {
await jsonFiles.concurrentForEach { @MainActor json in
let realm = try! await Realm()
do {
guard json.startAccessingSecurityScopedResource() else {
ELOG("startAccessingSecurityScopedResource failed")
return
}

let secureDoc = json.startAccessingSecurityScopedResource()

defer {
json.stopAccessingSecurityScopedResource()
if secureDoc {
json.stopAccessingSecurityScopedResource()
}
}

var dataMaybe = FileManager.default.contents(atPath: json.path)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,12 @@ public extension PVGameLibraryUpdatesController {
}

private func copyFileToImports(from sourceURL: URL) {
guard sourceURL.startAccessingSecurityScopedResource() else {
ELOG("startAccessingSecurityScopedResource failed for \(sourceURL.path)")
return
let secureDoc = sourceURL.startAccessingSecurityScopedResource()
defer {
if secureDoc {
sourceURL.stopAccessingSecurityScopedResource()
}
}
defer { sourceURL.stopAccessingSecurityScopedResource() }

let destinationURL = Paths.romsImportPath.appendingPathComponent(sourceURL.lastPathComponent)

Expand Down
9 changes: 6 additions & 3 deletions Provenance/PVAppDelegate+Open.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@ extension PVAppDelegate {
func handle(fileURL url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
let filename = url.lastPathComponent
let destinationPath = Paths.romsImportPath.appendingPathComponent(filename, isDirectory: false)

var secureDocument = false
do {
defer {
url.stopAccessingSecurityScopedResource()
if secureDocument {
url.stopAccessingSecurityScopedResource()
}

}

// Doesn't seem we need access in dev builds?
_ = url.startAccessingSecurityScopedResource()
secureDocument = url.startAccessingSecurityScopedResource()

if let openInPlace = options[.openInPlace] as? Bool, openInPlace {
try FileManager.default.copyItem(at: url, to: destinationPath)
Expand Down

0 comments on commit 6f0b1e3

Please sign in to comment.