Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send VPN system extension crashes to Sentry #2002

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Changes from 2 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
38 changes: 27 additions & 11 deletions DuckDuckGo/CrashReports/Model/CrashReportReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,32 @@ import Foundation
final class CrashReportReader {

static let displayName = Bundle.main.displayName
static let vpnExtensionDisplayName = "com.duckduckgo.macos.vpn.network-extension"

func getCrashReports(since lastCheckDate: Date) -> [CrashReport] {
let allPaths: [URL]
var allPaths: [URL]

do {
allPaths = try FileManager.default.contentsOfDirectory(at: FileManager.diagnosticReports,
includingPropertiesForKeys: nil,
options: [])
allPaths = try FileManager.default.contentsOfDirectory(at: FileManager.userDiagnosticReports, includingPropertiesForKeys: nil)
} catch {
assertionFailure("CrashReportReader: Can't read content of diagnostic reports \(error.localizedDescription)")
return []
}

return allPaths
.filter({ isCrashReportPath($0) &&
belongsToThisApp($0) &&
isFile(at: $0, newerThan: lastCheckDate) })
.compactMap(crashReport(from:))
#if NETP_SYSTEM_EXTENSION
do {
let systemPaths = try FileManager.default.contentsOfDirectory(at: FileManager.systemDiagnosticReports, includingPropertiesForKeys: nil)
allPaths.append(contentsOf: systemPaths)
} catch {
assertionFailure("Failed to read system crash reports: \(error)")
}
#endif

let filteredPaths = allPaths.filter({
isCrashReportPath($0) && belongsToThisApp($0) && isFile(at: $0, newerThan: lastCheckDate)
})
Comment on lines +45 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nitpicky and not at all important, but we can remove the parentheses here.


return filteredPaths.compactMap(crashReport(from:))
}

private func isCrashReportPath(_ path: URL) -> Bool {
Expand All @@ -46,7 +55,10 @@ final class CrashReportReader {
}

private func belongsToThisApp(_ path: URL) -> Bool {
return path.lastPathComponent.hasPrefix(Self.displayName ?? "DuckDuckGo")
let hasAppPrefix = path.lastPathComponent.hasPrefix(Self.displayName ?? "DuckDuckGo")
let hasVPNPrefix = path.lastPathComponent.hasPrefix(Self.vpnExtensionDisplayName)

return hasAppPrefix || hasVPNPrefix
}

private func isFile(at path: URL, newerThan lastCheckDate: Date) -> Bool {
Expand All @@ -70,12 +82,16 @@ final class CrashReportReader {

fileprivate extension FileManager {

static let diagnosticReports: URL = {
static let userDiagnosticReports: URL = {
let homeDirectoryURL = FileManager.default.homeDirectoryForCurrentUser
return homeDirectoryURL
.appendingPathComponent("Library/Logs/DiagnosticReports")
}()

static let systemDiagnosticReports: URL = {
return URL(fileURLWithPath: "/Library/Logs/DiagnosticReports")
}()

func fileCreationDate(url: URL) -> Date? {
let fileAttributes: [FileAttributeKey: Any] = (try? self.attributesOfItem(atPath: url.path)) ?? [:]
return fileAttributes[.creationDate] as? Date
Expand Down
Loading