Skip to content

Commit

Permalink
fix: Fix FileManager logs warning for .DS_Store (#3584)
Browse files Browse the repository at this point in the history
Don't log a warning in SentryFileManager for .DS_Store files.
  • Loading branch information
philipphofmann authored Jan 31, 2024
1 parent 86f7249 commit f2daa68
Show file tree
Hide file tree
Showing 4 changed files with 109 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)
- Fix FileManager logs info instead of error when a path doesn't exist (#3594)

## 8.19.0
Expand Down
56 changes: 39 additions & 17 deletions Sources/Sentry/SentryFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,51 @@ - (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 @@ -16,6 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
@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 @@ -54,6 +55,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
63 changes: 63 additions & 0 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,69 @@ 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("[Sentry] [debug]") &&
$0.contains("Ignoring .DS_Store file when building envelopes path at path: \(dsStoreFile)")
}
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("[Sentry] [debug]") &&
$0.contains("Ignoring non directory when deleting old envelopes at path: \(textFilePath)")
}
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 nonExistentFile = "nonExistentFile.txt"
let nonExistentFileFullPath = "\(sut.basePath)/\(nonExistentFile)"

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

let logMessages = logOutput.loggedMessages.filter {
$0.contains("[Sentry] [warning]") &&
$0.contains("Could not get attributes of item at path: \(nonExistentFileFullPath)")
}
expect(logMessages.count) == 1
}

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

0 comments on commit f2daa68

Please sign in to comment.