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
11 changes: 9 additions & 2 deletions Sources/Kumo/Blobs/Storage/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ class FileSystem: StorageLocation {
let bundle = Bundle(for: type(of: self))
self.parentDirectory = parentDirectory ?? backingManager.cachesDirectory
.appendingPathComponent("\(bundle.bundleIdentifier ?? "kumo.caches").\(Bundle.main.bundleIdentifier ?? "filecache")")
if backingManager.fileExists(atPath: self.parentDirectory.path) { return }
try! backingManager.createDirectory(at: self.parentDirectory, withIntermediateDirectories: false, attributes: nil)
// Idempotent creation avoids a check-then-create race when cache setup happens concurrently.
do {
try backingManager.createDirectory(at: self.parentDirectory, withIntermediateDirectories: true, attributes: nil)
} catch {
// Concurrent creation can still surface as a file-exists error; ignore that case.
if !(error as NSError).isFileExistsError {
assertionFailure("Failed to create cache directory at \(self.parentDirectory.path): \(error)")
}
}
}

func fetch<D: _DataRepresentable>(for url: URL, arguments: D._RepresentationArguments) throws -> D? {
Expand Down
36 changes: 36 additions & 0 deletions Tests/KumoTests/Fixtures/Blobs/FileSystemInitializationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Foundation
@testable import Kumo
import XCTest

final class FileSystemInitializationTests: XCTestCase {

func testInitDoesNotTrapWhenDirectoryCreationReturnsFileExists() {
let backingManager = FileExistsErrorFileManager()
let parentDirectory = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(UUID().uuidString, isDirectory: true)

XCTAssertNoThrow(FileSystem(backingManager: backingManager, parentDirectory: parentDirectory))
}

func testInitIsIdempotentWhenParentDirectoryAlreadyExists() throws {
let backingManager = FileManager.default
let parentDirectory = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
.appendingPathComponent(UUID().uuidString, isDirectory: true)

try backingManager.createDirectory(at: parentDirectory, withIntermediateDirectories: true, attributes: nil)
defer { try? backingManager.removeItem(at: parentDirectory) }

XCTAssertNoThrow(FileSystem(backingManager: backingManager, parentDirectory: parentDirectory))
XCTAssertNoThrow(FileSystem(backingManager: backingManager, parentDirectory: parentDirectory))
}
}

private final class FileExistsErrorFileManager: FileManager {
override func createDirectory(
at url: URL,
withIntermediateDirectories createIntermediates: Bool,
attributes: [FileAttributeKey: Any]? = nil
) throws {
throw NSError(domain: NSCocoaErrorDomain, code: 516)
}
Comment on lines +34 to +35
}
Loading