Skip to content

Commit e41b3c3

Browse files
Revert "Fix crash in file manager (getsentry#5535)"
This reverts commit aa0b738.
1 parent 7392745 commit e41b3c3

11 files changed

+471
-389
lines changed

SentryTestUtils/ClearTestState.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class TestCleanup: NSObject {
3939
SentryAppStartTracker.load()
4040
SentryDependencyContainer.sharedInstance().uiViewControllerPerformanceTracker.alwaysWaitForFullDisplay = false
4141
SentryDependencyContainer.sharedInstance().swizzleWrapper.removeAllCallbacks()
42-
SentryDependencyContainer.sharedInstance().fileManager?.clearDiskState()
42+
SentryDependencyContainer.sharedInstance().fileManager.clearDiskState()
4343

4444
#endif // os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
4545

Sources/Sentry/SentryDependencyContainer.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ - (instancetype)init
211211
return self;
212212
}
213213

214-
- (nullable SentryFileManager *)fileManager SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
214+
- (SentryFileManager *)fileManager SENTRY_THREAD_SANITIZER_DOUBLE_CHECKED_LOCK
215215
{
216216
SENTRY_LAZY_INIT(_fileManager, ({
217217
NSError *error;

Sources/Sentry/SentrySessionReplayIntegration.m

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,9 @@ - (void)startWithOptions:(SentryReplayOptions *)replayOptions
434434
options:replayOptions];
435435
}
436436

437-
- (nullable NSURL *)replayDirectory
437+
- (NSURL *)replayDirectory
438438
{
439439
NSString *sentryPath = [SentryDependencyContainer.sharedInstance.fileManager sentryPath];
440-
if (!sentryPath) {
441-
return nil;
442-
}
443440
NSURL *dir = [NSURL fileURLWithPath:sentryPath];
444441
return [dir URLByAppendingPathComponent:SENTRY_REPLAY_FOLDER];
445442
}

Sources/Sentry/include/HybridPublic/SentryDependencyContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ SENTRY_NO_INIT
105105

106106
#pragma mark - Lazy Dependencies
107107

108-
@property (nonatomic, strong, nullable) SentryFileManager *fileManager;
108+
@property (nonatomic, strong) SentryFileManager *fileManager;
109109
@property (nonatomic, strong) SentryAppStateManager *appStateManager;
110110
@property (nonatomic, strong) SentryThreadInspector *threadInspector;
111111
@property (nonatomic, strong) SentryFileIOTracker *fileIOTracker;

Sources/Swift/Persistence/SentryScopeContextPersistentStore.swift

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,55 @@
11
@_implementationOnly import _SentryPrivate
22

3+
@_spi(Private) @objc public protocol SentryFileManagerProtocol {
4+
func moveState(_ stateFilePath: String, toPreviousState previousStateFilePath: String)
5+
func readData(fromPath path: String) throws -> Data
6+
@objc(writeData:toPath:)
7+
@discardableResult func write(_ data: Data, toPath path: String) -> Bool
8+
func removeFile(atPath path: String)
9+
func getSentryPathAsURL() -> URL
10+
}
11+
312
@objcMembers
413
@_spi(Private) public class SentryScopeContextPersistentStore: NSObject {
5-
private let fileManager: SentryFileManager
14+
private let fileManager: SentryFileManagerProtocol
615

7-
init(fileManager: SentryFileManager) {
16+
public init(fileManager: SentryFileManagerProtocol) {
817
self.fileManager = fileManager
918
}
1019

1120
// MARK: - Context
12-
13-
<<<<<<< HEAD
14-
func moveCurrentFileToPreviousFile() {
15-
=======
21+
1622
public func moveCurrentFileToPreviousFile() {
17-
>>>>>>> parent of 2609f7a4 ([Structured Logging] Rename SentryLog to SentrySDKLog (#5473))
18-
SentryLog.debug("Moving context file to previous context file")
23+
SentrySDKLog.debug("Moving context file to previous context file")
1924
self.fileManager.moveState(contextFileURL.path, toPreviousState: previousContextFileURL.path)
2025
}
2126

2227
public func readPreviousContextFromDisk() -> [String: [String: Any]]? {
23-
SentryLog.debug("Reading previous context file at path: \(previousContextFileURL.path)")
28+
SentrySDKLog.debug("Reading previous context file at path: \(previousContextFileURL.path)")
2429
do {
2530
let data = try fileManager.readData(fromPath: previousContextFileURL.path)
2631
return decodeContext(from: data)
2732
} catch {
28-
SentryLog.error("Failed to read previous context file at path: \(previousContextFileURL.path), reason: \(error)")
33+
SentrySDKLog.error("Failed to read previous context file at path: \(previousContextFileURL.path), reason: \(error)")
2934
return nil
3035
}
3136
}
3237

3338
func writeContextToDisk(context: [String: [String: Any]]) {
34-
SentryLog.debug("Writing context to disk at path: \(contextFileURL.path)")
39+
SentrySDKLog.debug("Writing context to disk at path: \(contextFileURL.path)")
3540
guard let data = encode(context: context) else {
3641
return
3742
}
3843
fileManager.write(data, toPath: contextFileURL.path)
3944
}
4045

4146
func deleteContextOnDisk() {
42-
SentryLog.debug("Deleting context file at path: \(contextFileURL.path)")
47+
SentrySDKLog.debug("Deleting context file at path: \(contextFileURL.path)")
4348
fileManager.removeFile(atPath: contextFileURL.path)
4449
}
4550

4651
func deletePreviousContextOnDisk() {
47-
SentryLog.debug("Deleting context file at path: \(contextFileURL.path)")
52+
SentrySDKLog.debug("Deleting context file at path: \(contextFileURL.path)")
4853
fileManager.removeFile(atPath: previousContextFileURL.path)
4954
}
5055

@@ -55,19 +60,19 @@
5560
// Otherwise it will throw an unhandled `NSInvalidArgumentException` exception.
5661
// The error handler is required due but seems not to be executed.
5762
guard let sanitizedContext = sentry_sanitize(context) else {
58-
SentryLog.error("Failed to sanitize context, reason: context is not valid json: \(context)")
63+
SentrySDKLog.error("Failed to sanitize context, reason: context is not valid json: \(context)")
5964
return nil
6065
}
6166
guard let data = SentrySerialization.data(withJSONObject: sanitizedContext) else {
62-
SentryLog.error("Failed to serialize context, reason: context is not valid json: \(context)")
67+
SentrySDKLog.error("Failed to serialize context, reason: context is not valid json: \(context)")
6368
return nil
6469
}
6570
return data
6671
}
6772

6873
private func decodeContext(from data: Data) -> [String: [String: Any]]? {
6974
guard let deserialized = SentrySerialization.deserializeDictionary(fromJsonData: data) else {
70-
SentryLog.error("Failed to deserialize context, reason: data is not valid json")
75+
SentrySDKLog.error("Failed to deserialize context, reason: data is not valid json")
7176
return nil
7277
}
7378

@@ -87,7 +92,7 @@
8792
// additional memory (like when mapping values).
8893
for (key, value) in deserialized {
8994
guard value is [String: Any] else {
90-
SentryLog.error("Failed to deserialize context, reason: value for key \(key) is not a valid dictionary")
95+
SentrySDKLog.error("Failed to deserialize context, reason: value for key \(key) is not a valid dictionary")
9196
return nil
9297
}
9398
}

Tests/SentryTests/Helper/SentryFileManagerTests.swift

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ class SentryFileManagerTests: XCTestCase {
177177

178178
func testDeleteOldEnvelopes_LogsIgnoreDSStoreFiles() throws {
179179
let logOutput = TestLogOutput()
180-
SentryLog.setLogOutput(logOutput)
181-
SentryLog.configureLog(true, diagnosticLevel: .debug)
180+
SentrySDKLog.setLogOutput(logOutput)
181+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
182182

183183
let dsStoreFile = "\(sut.basePath)/.DS_Store"
184184

@@ -198,8 +198,8 @@ class SentryFileManagerTests: XCTestCase {
198198

199199
func testDeleteOldEnvelopes_LogsDebugForTextFiles() throws {
200200
let logOutput = TestLogOutput()
201-
SentryLog.setLogOutput(logOutput)
202-
SentryLog.configureLog(true, diagnosticLevel: .debug)
201+
SentrySDKLog.setLogOutput(logOutput)
202+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
203203

204204
let sut = fixture.getSut()
205205

@@ -221,8 +221,8 @@ class SentryFileManagerTests: XCTestCase {
221221

222222
func testGetEnvelopesPath_ForNonExistentPath_LogsWarning() throws {
223223
let logOutput = TestLogOutput()
224-
SentryLog.setLogOutput(logOutput)
225-
SentryLog.configureLog(true, diagnosticLevel: .debug)
224+
SentrySDKLog.setLogOutput(logOutput)
225+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
226226

227227
let sut = fixture.getSut()
228228

@@ -302,7 +302,7 @@ class SentryFileManagerTests: XCTestCase {
302302

303303
func testDeleteFileNotExists() {
304304
let logOutput = TestLogOutput()
305-
SentryLog.setLogOutput(logOutput)
305+
SentrySDKLog.setLogOutput(logOutput)
306306
sut.removeFile(atPath: "x")
307307
XCTAssertFalse(logOutput.loggedMessages.contains(where: { $0.contains("[error]") }))
308308
}
@@ -642,8 +642,8 @@ class SentryFileManagerTests: XCTestCase {
642642

643643
func testGetAllEnvelopesWhenNoEnvelopesPath_LogsInfoMessage() {
644644
let logOutput = TestLogOutput()
645-
SentryLog.setLogOutput(logOutput)
646-
SentryLog.configureLog(true, diagnosticLevel: .debug)
645+
SentrySDKLog.setLogOutput(logOutput)
646+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
647647

648648
sut.deleteAllFolders()
649649
sut.getAllEnvelopes()
@@ -850,13 +850,13 @@ class SentryFileManagerTests: XCTestCase {
850850

851851
func testReadPreviousBreadcrumbs() throws {
852852
let breadcrumbProcessor = SentryWatchdogTerminationBreadcrumbProcessor(maxBreadcrumbs: 2, fileManager: sut)
853-
let attributesProcessor = try SentryWatchdogTerminationAttributesProcessor(
853+
let contextProcessor = SentryWatchdogTerminationContextProcessor(
854854
withDispatchQueueWrapper: SentryDispatchQueueWrapper(),
855-
scopePersistentStore: XCTUnwrap(SentryScopePersistentStore(fileManager: sut))
855+
scopeContextStore: SentryScopeContextPersistentStore(fileManager: sut)
856856
)
857857
let observer = SentryWatchdogTerminationScopeObserver(
858858
breadcrumbProcessor: breadcrumbProcessor,
859-
attributesProcessor: attributesProcessor
859+
contextProcessor: contextProcessor
860860
)
861861

862862
for count in 0..<3 {
@@ -880,13 +880,13 @@ class SentryFileManagerTests: XCTestCase {
880880

881881
func testReadPreviousBreadcrumbsCorrectOrderWhenFileTwoHasMoreCrumbs() throws {
882882
let breadcrumbProcessor = SentryWatchdogTerminationBreadcrumbProcessor(maxBreadcrumbs: 2, fileManager: sut)
883-
let attributesProcessor = try SentryWatchdogTerminationAttributesProcessor(
883+
let contextProcessor = SentryWatchdogTerminationContextProcessor(
884884
withDispatchQueueWrapper: TestSentryDispatchQueueWrapper(),
885-
scopePersistentStore: XCTUnwrap(SentryScopePersistentStore(fileManager: sut))
885+
scopeContextStore: SentryScopeContextPersistentStore(fileManager: sut)
886886
)
887887
let observer = SentryWatchdogTerminationScopeObserver(
888888
breadcrumbProcessor: breadcrumbProcessor,
889-
attributesProcessor: attributesProcessor
889+
contextProcessor: contextProcessor
890890
)
891891

892892
for count in 0..<5 {
@@ -1044,8 +1044,8 @@ class SentryFileManagerTests: XCTestCase {
10441044
func testCreateDirectoryIfNotExists_successful_shouldNotLogError() throws {
10451045
// -- Arrange -
10461046
let logOutput = TestLogOutput()
1047-
SentryLog.setLogOutput(logOutput)
1048-
SentryLog.configureLog(true, diagnosticLevel: .debug)
1047+
SentrySDKLog.setLogOutput(logOutput)
1048+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
10491049

10501050
let path = fixture.getValidDirectoryPath()
10511051
var error: NSError?
@@ -1059,8 +1059,8 @@ class SentryFileManagerTests: XCTestCase {
10591059
func testCreateDirectoryIfNotExists_pathTooLogError_shouldLogError() throws {
10601060
// -- Arrange -
10611061
let logOutput = TestLogOutput()
1062-
SentryLog.setLogOutput(logOutput)
1063-
SentryLog.configureLog(true, diagnosticLevel: .debug)
1062+
SentrySDKLog.setLogOutput(logOutput)
1063+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
10641064

10651065
let path = fixture.getTooLongPath()
10661066
var error: NSError?
@@ -1076,8 +1076,8 @@ class SentryFileManagerTests: XCTestCase {
10761076
func testCreateDirectoryIfNotExists_otherError_shouldNotLogError() throws {
10771077
// -- Arrange -
10781078
let logOutput = TestLogOutput()
1079-
SentryLog.setLogOutput(logOutput)
1080-
SentryLog.configureLog(true, diagnosticLevel: .debug)
1079+
SentrySDKLog.setLogOutput(logOutput)
1080+
SentrySDKLog.configureLog(true, diagnosticLevel: .debug)
10811081

10821082
let path = fixture.getInvalidPath()
10831083
var error: NSError?
@@ -1121,7 +1121,7 @@ class SentryFileManagerTests: XCTestCase {
11211121
let data = Data("<TEST DATA>".utf8)
11221122

11231123
let logOutput = TestLogOutput()
1124-
SentryLog.setLogOutput(logOutput)
1124+
SentrySDKLog.setLogOutput(logOutput)
11251125

11261126
// Check pre-conditions
11271127
let fm = FileManager.default
@@ -1167,7 +1167,7 @@ extension SentryFileManagerTests {
11671167
profilesSampleRate: expectedProfilesSampleRate,
11681168
profilesSampleRand: expectedProfilesSampleRand
11691169
)
1170-
let config = sentry_persistedLaunchProfileConfigurationOptions()
1170+
let config = sentry_appLaunchProfileConfiguration()
11711171

11721172
// -- Assert --
11731173
let actualTracesSampleRate = try XCTUnwrap(config?[kSentryLaunchProfileConfigKeyTracesSampleRate]).doubleValue
@@ -1183,7 +1183,7 @@ extension SentryFileManagerTests {
11831183
// if a file isn't present when we expect it to be, like if there was an issue when we went to write it to disk
11841184
func testsentry_appLaunchProfileConfiguration_noConfigurationExists() throws {
11851185
try ensureAppLaunchProfileConfig(exists: false)
1186-
XCTAssertNil(sentry_persistedLaunchProfileConfigurationOptions())
1186+
XCTAssertNil(sentry_appLaunchProfileConfiguration())
11871187
}
11881188

11891189
func testWriteAppLaunchProfilingConfigFile_noCurrentFileExists() throws {
@@ -1200,10 +1200,9 @@ extension SentryFileManagerTests {
12001200
kSentryLaunchProfileConfigKeyProfilesSampleRate: expectedProfilesSampleRate,
12011201
kSentryLaunchProfileConfigKeyProfilesSampleRand: expectedProfilesSampleRand
12021202
])
1203-
1204-
let configURL = try XCTUnwrap(launchProfileConfigFileURL())
1205-
let config = NSDictionary(contentsOf: configURL)
1206-
1203+
1204+
let config = NSDictionary(contentsOf: launchProfileConfigFileURL())
1205+
12071206
let actualTracesSampleRate = try XCTUnwrap(config?[kSentryLaunchProfileConfigKeyTracesSampleRate] as? NSNumber).doubleValue
12081207
let actualTracesSampleRand = try XCTUnwrap(config?[kSentryLaunchProfileConfigKeyTracesSampleRand] as? NSNumber).doubleValue
12091208
let actualProfilesSampleRate = try XCTUnwrap(config?[kSentryLaunchProfileConfigKeyProfilesSampleRate] as? NSNumber).doubleValue
@@ -1233,9 +1232,8 @@ extension SentryFileManagerTests {
12331232
])
12341233

12351234
// -- Assert --
1236-
let configURL = try XCTUnwrap(launchProfileConfigFileURL())
1237-
let config = NSDictionary(contentsOf: configURL)
1238-
1235+
let config = NSDictionary(contentsOf: launchProfileConfigFileURL())
1236+
12391237
let actualTracesSampleRate = try XCTUnwrap(config?[kSentryLaunchProfileConfigKeyTracesSampleRate] as? NSNumber).doubleValue
12401238
let actualTracesSampleRand = try XCTUnwrap(config?[kSentryLaunchProfileConfigKeyTracesSampleRand] as? NSNumber).doubleValue
12411239
let actualProfilesSampleRate = try XCTUnwrap(config?[kSentryLaunchProfileConfigKeyProfilesSampleRate] as? NSNumber).doubleValue
@@ -1248,17 +1246,17 @@ extension SentryFileManagerTests {
12481246

12491247
func testRemoveAppLaunchProfilingConfigFile() throws {
12501248
try ensureAppLaunchProfileConfig(exists: true)
1251-
XCTAssertNotNil(NSDictionary(contentsOf: try XCTUnwrap(launchProfileConfigFileURL())))
1249+
XCTAssertNotNil(NSDictionary(contentsOf: launchProfileConfigFileURL()))
12521250
removeAppLaunchProfilingConfigFile()
1253-
XCTAssertNil(NSDictionary(contentsOf: try XCTUnwrap(launchProfileConfigFileURL())))
1251+
XCTAssertNil(NSDictionary(contentsOf: launchProfileConfigFileURL()))
12541252
}
12551253

12561254
// if there's not a file when we expect one, just make sure we don't crash
12571255
func testRemoveAppLaunchProfilingConfigFile_noFileExists() throws {
12581256
try ensureAppLaunchProfileConfig(exists: false)
1259-
XCTAssertNil(NSDictionary(contentsOf: try XCTUnwrap(launchProfileConfigFileURL())))
1257+
XCTAssertNil(NSDictionary(contentsOf: launchProfileConfigFileURL()))
12601258
removeAppLaunchProfilingConfigFile()
1261-
XCTAssertNil(NSDictionary(contentsOf: try XCTUnwrap(launchProfileConfigFileURL())))
1259+
XCTAssertNil(NSDictionary(contentsOf: launchProfileConfigFileURL()))
12621260
}
12631261

12641262
func testCheckForLaunchProfilingConfigFile_URLDoesNotExist() {
@@ -1272,7 +1270,7 @@ extension SentryFileManagerTests {
12721270
XCTAssertFalse(appLaunchProfileConfigFileExists())
12731271

12741272
// set the original value back so other tests don't crash
1275-
sentryLaunchConfigFileURL = (originalURL as? NSURL)
1273+
sentryLaunchConfigFileURL = (originalURL as NSURL)
12761274
}
12771275

12781276
func testSentryGetScopedCachesDirectory_targetIsNotMacOS_shouldReturnSamePath() throws {
@@ -1385,7 +1383,7 @@ extension SentryFileManagerTests {
13851383
// MARK: Private profiling tests
13861384
private extension SentryFileManagerTests {
13871385
func ensureAppLaunchProfileConfig(exists: Bool = true, tracesSampleRate: Double = 1, tracesSampleRand: Double = 1.0, profilesSampleRate: Double = 1, profilesSampleRand: Double = 1.0) throws {
1388-
let url = try XCTUnwrap(launchProfileConfigFileURL())
1386+
let url = launchProfileConfigFileURL()
13891387

13901388
if exists {
13911389
let dict = [

0 commit comments

Comments
 (0)