• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "StatsReporter.h"
18 #include <android-base/logging.h>
19 #include <stdlib.h>
20 #include <string>
21 #include <sys/stat.h>
22 
23 // Keep these constant in sync with COMPOS_METRIC_NAME & METRICS_FILE in OdsignStatsLogger.java.
24 constexpr const char* kOdsignMetricsFile = "/data/misc/odsign/metrics/odsign-metrics.txt";
25 constexpr const char* kComposMetricName = "comp_os_artifacts_check_record";
26 
~StatsReporter()27 StatsReporter::~StatsReporter() {
28     if (comp_os_artifacts_check_record_ == nullptr) {
29         LOG(INFO) << "Metrics report is empty";
30 
31         // Remove the metrics file if any old version of the file already exists
32         if (std::filesystem::remove(kOdsignMetricsFile) != 0 &&
33             !((errno = ENOENT) || errno == ENOTDIR)) {
34             PLOG(ERROR) << "Could not remove already present file";
35         }
36         return;
37     }
38 
39     std::ofstream odsign_metrics_file_;
40     odsign_metrics_file_.open(kOdsignMetricsFile, std::ios::trunc);
41     if (!odsign_metrics_file_) {
42         PLOG(ERROR) << "Could not open file: " << kOdsignMetricsFile;
43         return;
44     }
45 
46     odsign_metrics_file_ << kComposMetricName << ' ';
47     odsign_metrics_file_ << comp_os_artifacts_check_record_->current_artifacts_ok << ' ';
48     odsign_metrics_file_ << comp_os_artifacts_check_record_->comp_os_pending_artifacts_exists
49                          << ' ';
50     odsign_metrics_file_ << comp_os_artifacts_check_record_->use_comp_os_generated_artifacts
51                          << '\n';
52     if (chmod(kOdsignMetricsFile, 0644) != 0) {
53         PLOG(ERROR) << "Could not set correct file permissions for " << kOdsignMetricsFile;
54         return;
55     }
56     odsign_metrics_file_.close();
57     if (!odsign_metrics_file_) {
58         PLOG(ERROR) << "Failed to close the file";
59     }
60 }
61 
GetComposArtifactsCheckRecord()62 StatsReporter::CompOsArtifactsCheckRecord* StatsReporter::GetComposArtifactsCheckRecord() {
63     if (comp_os_artifacts_check_record_ == nullptr) {
64         comp_os_artifacts_check_record_ = std::make_unique<CompOsArtifactsCheckRecord>();
65     }
66     return comp_os_artifacts_check_record_.get();
67 }
68