Skip to content

Commit

Permalink
Merge 44e630a into 52e4912
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphofmann committed Jan 30, 2024
2 parents 52e4912 + 44e630a commit 354a3a2
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Clarify FramesTracker log message (#3570)
- Fix rare battery breadcrumbs crash (#3582)
- Fix synchronization issue in FramesTracker (#3571)
- Fix SentryFileManager logs warning for .DS_Files (#3584)

## 8.19.0

Expand Down
55 changes: 38 additions & 17 deletions Sources/Sentry/SentryFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,50 @@ - (SentryFileContents *_Nullable)getFileContents:(NSString *)folderPath
- (void)deleteOldEnvelopesFromAllSentryPaths
{
// First we find all directories in the base path, these are all the various hashed DSN paths
for (NSString *path in [self allFilesInFolder:self.basePath]) {
NSString *fullPath = [self.basePath stringByAppendingPathComponent:path];
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath
error:nil];
if (!dict || dict[NSFileType] != NSFileTypeDirectory) {
SENTRY_LOG_WARN(@"Could not get NSFileTypeDirectory from %@", fullPath);
continue;
}

// If the options don't have a DSN the sentry path doesn't contain a hash and the envelopes
// folder is stored in the base path.
NSString *envelopesPath;
if ([fullPath hasSuffix:EnvelopesPathComponent]) {
envelopesPath = fullPath;
} else {
envelopesPath = [fullPath stringByAppendingPathComponent:EnvelopesPathComponent];
}
for (NSString *filePath in [self allFilesInFolder:self.basePath]) {
NSString *envelopesPath = [self getEnvelopesPath:filePath];

// Then we will remove all old items from the envelopes subdirectory
[self deleteOldEnvelopesFromPath:envelopesPath];
}
}

- (nullable NSString *)getEnvelopesPath:(NSString *)filePath
{
NSString *fullPath = [self.basePath stringByAppendingPathComponent:filePath];

if ([fullPath hasSuffix:@".DS_Store"]) {
SENTRY_LOG_DEBUG(
@"Ignoring .DS_Store file when building envelopes path at path: %@", fullPath);
return nil;
}

NSError *error = nil;
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:fullPath
error:&error];
if (error != nil) {
SENTRY_LOG_WARN(@"Could not get attributes of item at path %@. Error: %@", fullPath, error);
return nil;
}

if (dict[NSFileType] != NSFileTypeDirectory) {
SENTRY_LOG_DEBUG(
@"Ignoring non directory when deleting old envelopes at path: %@", fullPath);
return nil;
}

// If the options don't have a DSN the sentry path doesn't contain a hash and the envelopes
// folder is stored in the base path.
NSString *envelopesPath;
if ([fullPath hasSuffix:EnvelopesPathComponent]) {
envelopesPath = fullPath;
} else {
envelopesPath = [fullPath stringByAppendingPathComponent:EnvelopesPathComponent];
}

return envelopesPath;
}

- (void)deleteOldEnvelopesFromPath:(NSString *)envelopesPath
{
NSTimeInterval now =
Expand Down
6 changes: 6 additions & 0 deletions Sources/Sentry/include/SentryFileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ NS_SWIFT_NAME(SentryFileManager)
@interface SentryFileManager : NSObject
SENTRY_NO_INIT

@property (nonatomic, readonly) NSString *basePath;
@property (nonatomic, readonly) NSString *sentryPath;
@property (nonatomic, readonly) NSString *breadcrumbsFilePathOne;
@property (nonatomic, readonly) NSString *breadcrumbsFilePathTwo;
Expand Down Expand Up @@ -55,6 +56,11 @@ SENTRY_NO_INIT

- (void)deleteOldEnvelopeItems;

/**
* Only used for testing.
*/
- (nullable NSString *)getEnvelopesPath:(NSString *)filePath;

/**
* Get all envelopes sorted ascending by the @c timeIntervalSince1970 the envelope was stored and if
* two envelopes are stored at the same time sorted by the order they were stored.
Expand Down
54 changes: 54 additions & 0 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Nimble
import Sentry
import SentryTestUtils
import XCTest
Expand Down Expand Up @@ -144,6 +145,59 @@ class SentryFileManagerTests: XCTestCase {
XCTAssertEqual(sut.getAllEnvelopes().count, 0)
}

func testDeleteOldEnvelopes_LogsIgnoreDSStoreFiles() throws {
let logOutput = TestLogOutput()
SentryLog.setLogOutput(logOutput)
SentryLog.configure(true, diagnosticLevel: .debug)

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

let result = FileManager.default.createFile(atPath: dsStoreFile, contents: "some data".data(using: .utf8))
expect(result) == true

sut.deleteOldEnvelopeItems()

let logMessages = logOutput.loggedMessages.filter { $0.contains("Ignoring .DS_Store file when building envelopes path at path") }
expect(logMessages.count) == 1

try FileManager.default.removeItem(atPath: dsStoreFile)
}

func testDeleteOldEnvelopes_LogsDebugForTextFiles() throws {
let logOutput = TestLogOutput()
SentryLog.setLogOutput(logOutput)
SentryLog.configure(true, diagnosticLevel: .debug)

let sut = fixture.getSut()

let textFilePath = "\(sut.basePath)/something.txt"

let result = FileManager.default.createFile(atPath: textFilePath, contents: "some data".data(using: .utf8))
expect(result) == true

sut.deleteOldEnvelopeItems()

let logMessages = logOutput.loggedMessages.filter { $0.contains("Ignoring non directory when deleting old envelopes at path") }
expect(logMessages.count) == 1

try FileManager.default.removeItem(atPath: textFilePath)
}

func testGetEnvelopesPath_ForNonExistentPath_LogsWarning() throws {
let logOutput = TestLogOutput()
SentryLog.setLogOutput(logOutput)
SentryLog.configure(true, diagnosticLevel: .debug)

let sut = fixture.getSut()

let textFilePath = "\(sut.basePath)/something.txt"

expect(sut.getEnvelopesPath(textFilePath)) == nil

let logMessages = logOutput.loggedMessages.filter { $0.contains("Could not get attributes of item at path") }
expect(logMessages.count) == 1
}

func testDeleteOldEnvelopes_WithEmptyDSN() throws {
fixture.options.dsn = nil
sut = fixture.getSut()
Expand Down

0 comments on commit 354a3a2

Please sign in to comment.