1 /*
2 * Copyright (C) 2025 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 "sdc_file.h"
18
19 #include <cstdint>
20 #include <memory>
21 #include <regex>
22 #include <string>
23 #include <string_view>
24 #include <unordered_map>
25 #include <vector>
26
27 #include "android-base/file.h"
28 #include "android-base/parseint.h"
29 #include "android-base/scopeguard.h"
30 #include "base/macros.h"
31 #include "base/utils.h"
32
33 namespace art HIDDEN {
34
35 using ::android::base::ParseInt;
36 using ::android::base::ReadFileToString;
37 using ::android::base::WriteStringToFd;
38
Load(const std::string & filename,std::string * error_msg)39 std::unique_ptr<SdcReader> SdcReader::Load(const std::string& filename, std::string* error_msg) {
40 std::unique_ptr<SdcReader> reader(new SdcReader());
41
42 // The sdc file is supposed to be small, so read fully into memory for simplicity.
43 if (!ReadFileToString(filename, &reader->content_)) {
44 *error_msg = ART_FORMAT("Failed to load sdc file '{}': {}", filename, strerror(errno));
45 return nullptr;
46 }
47
48 std::vector<std::string_view> lines;
49 Split(reader->content_, '\n', &lines);
50 std::unordered_map<std::string_view, std::string_view> map;
51 for (std::string_view line : lines) {
52 size_t pos = line.find('=');
53 if (pos == std::string_view::npos || pos == 0) {
54 *error_msg = ART_FORMAT("Malformed line '{}' in sdc file '{}'", line, filename);
55 return nullptr;
56 }
57 if (!map.try_emplace(line.substr(0, pos), line.substr(pos + 1)).second) {
58 *error_msg = ART_FORMAT("Duplicate key '{}' in sdc file '{}'", line.substr(0, pos), filename);
59 return nullptr;
60 }
61 }
62
63 decltype(map)::iterator it;
64 if ((it = map.find("sdm-timestamp-ns")) == map.end()) {
65 *error_msg = ART_FORMAT("Missing key 'sdm-timestamp-ns' in sdc file '{}'", filename);
66 return nullptr;
67 }
68 if (!ParseInt(std::string(it->second), &reader->sdm_timestamp_ns_, /*min=*/INT64_C(1))) {
69 *error_msg = ART_FORMAT("Invalid 'sdm-timestamp-ns' {}", it->second);
70 return nullptr;
71 }
72
73 if ((it = map.find("apex-versions")) == map.end()) {
74 *error_msg = ART_FORMAT("Missing key 'apex-versions' in sdc file '{}'", filename);
75 return nullptr;
76 }
77 if (!std::regex_match(it->second.begin(), it->second.end(), std::regex("[0-9/]*"))) {
78 *error_msg = ART_FORMAT("Invalid 'apex-versions' {}", it->second);
79 return nullptr;
80 }
81 reader->apex_versions_ = it->second;
82
83 if (map.size() > 2) {
84 *error_msg = ART_FORMAT("Malformed sdc file '{}'. Unrecognized keys", filename);
85 return nullptr;
86 }
87
88 return reader;
89 }
90
Save(std::string * error_msg)91 bool SdcWriter::Save(std::string* error_msg) {
92 auto cleanup = android::base::make_scope_guard([this] { (void)file_.FlushClose(); });
93 if (sdm_timestamp_ns_ <= 0) {
94 *error_msg = ART_FORMAT("Invalid 'sdm-timestamp-ns' {}", sdm_timestamp_ns_);
95 return false;
96 }
97 DCHECK_EQ(file_.GetLength(), 0);
98 std::string content =
99 ART_FORMAT("sdm-timestamp-ns={}\napex-versions={}\n", sdm_timestamp_ns_, apex_versions_);
100 if (!WriteStringToFd(content, file_.Fd())) {
101 *error_msg = ART_FORMAT("Failed to write sdc file '{}': {}", file_.GetPath(), strerror(errno));
102 return false;
103 }
104 int res = file_.FlushClose();
105 if (res != 0) {
106 *error_msg =
107 ART_FORMAT("Failed to flush close sdc file '{}': {}", file_.GetPath(), strerror(-res));
108 return false;
109 }
110 cleanup.Disable();
111 return true;
112 }
113
114 } // namespace art
115