File indexing completed on 2025-01-05 05:01:15

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2022-2023 Harald Sitter <sitter@kde.org>
0003 
0004 #include "sentrypaths.h"
0005 
0006 #include <QDir>
0007 #include <QStandardPaths>
0008 
0009 using namespace Qt::StringLiterals;
0010 
0011 namespace
0012 {
0013 QString cacheDir(const QString &subdir)
0014 {
0015     const auto dir = QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation);
0016     Q_ASSERT(!dir.isEmpty());
0017     if (dir.isEmpty()) {
0018         return {};
0019     }
0020     return dir + "/drkonqi/"_L1 + subdir;
0021 }
0022 } // namespace
0023 
0024 namespace SentryPaths
0025 {
0026 
0027 QString payloadsDir()
0028 {
0029     static const auto payloadsDir = []() {
0030         const auto dir = cacheDir(u"/sentry-envelopes"_qs);
0031         QDir().mkpath(dir);
0032         return dir;
0033     }();
0034     return payloadsDir;
0035 }
0036 QString sentPayloadsDir()
0037 {
0038     static const auto sentPayloadsDir = []() {
0039         const auto dir = cacheDir(u"/sentry-sent-envelopes"_qs);
0040         QDir().mkpath(dir);
0041         return dir;
0042     }();
0043     return sentPayloadsDir;
0044 }
0045 
0046 QString payloadPath(const QString &eventId)
0047 {
0048     const auto dir = payloadsDir();
0049     if (dir.isEmpty()) {
0050         return {};
0051     }
0052     // Note includes timestamp to facilitate multiple writes of the same event. We need this to support both
0053     // pre-submission feedback as well as post-submission feedback while also avoiding race conditions between
0054     // the postbox writing envelopes, and the postman picking them up.
0055     return dir + '/'_L1 + eventId + '.'_L1 + QString::number(std::chrono::system_clock::now().time_since_epoch().count());
0056 }
0057 
0058 QString sentPayloadPath(const QString &eventId)
0059 {
0060     const auto dir = sentPayloadsDir();
0061     if (dir.isEmpty()) {
0062         return {};
0063     }
0064     return dir + '/'_L1 + eventId;
0065 }
0066 } // namespace SentryPaths