blob: c3dcd53bab2affd8cde685d991f974de1d31d8ad [file] [log] [blame]
Yifan Hong2272bf82017-04-28 14:37:56 -07001/*
2 * Copyright (C) 2017 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#ifndef ANDROID_VINTF_UTILS_H
18#define ANDROID_VINTF_UTILS_H
19
Yifan Hongd52bf3e2018-01-11 16:56:51 -080020#include <dirent.h>
21
Yifan Hong2272bf82017-04-28 14:37:56 -070022#include <fstream>
23#include <iostream>
Yifan Hongd52bf3e2018-01-11 16:56:51 -080024#include <memory>
Yifan Hong2272bf82017-04-28 14:37:56 -070025#include <sstream>
26
Yifan Hong2272bf82017-04-28 14:37:56 -070027#include <utils/Errors.h>
Yifan Honga72bde72017-09-28 13:36:48 -070028#include <vintf/RuntimeInfo.h>
29#include <vintf/parse_xml.h>
Yifan Hong2272bf82017-04-28 14:37:56 -070030
31namespace android {
32namespace vintf {
33namespace details {
34
Michael Schwartz97dc0f92017-05-08 14:07:14 -070035// Return the file from the given location as a string.
36//
37// This class can be used to create a mock for overriding.
38class FileFetcher {
39 public:
40 virtual ~FileFetcher() {}
Yifan Hong60217032018-01-08 16:19:42 -080041 status_t fetchInternal(const std::string& path, std::string& fetched, std::string* error) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -070042 std::ifstream in;
43
44 in.open(path);
45 if (!in.is_open()) {
Yifan Hong60217032018-01-08 16:19:42 -080046 if (error) {
47 *error = "Cannot open " + path;
48 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -070049 return INVALID_OPERATION;
50 }
51
52 std::stringstream ss;
53 ss << in.rdbuf();
54 fetched = ss.str();
55
56 return OK;
Yifan Hong2272bf82017-04-28 14:37:56 -070057 }
Yifan Hong60217032018-01-08 16:19:42 -080058 virtual status_t fetch(const std::string& path, std::string& fetched, std::string* error) {
59 return fetchInternal(path, fetched, error);
60 }
61 virtual status_t fetch(const std::string& path, std::string& fetched) {
62 return fetchInternal(path, fetched, nullptr);
63 }
Yifan Hongd52bf3e2018-01-11 16:56:51 -080064 virtual status_t listFiles(const std::string& path, std::vector<std::string>* out,
65 std::string* error) {
66 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir);
67 if (!dir) {
68 if (error) {
69 *error = "Cannot open " + path;
70 }
71 return NAME_NOT_FOUND;
72 }
73
74 dirent* dp;
75 while ((dp = readdir(dir.get())) != nullptr) {
76 if (dp->d_type != DT_DIR) {
77 out->push_back(dp->d_name);
78 }
79 }
80 return OK;
81 }
Michael Schwartz97dc0f92017-05-08 14:07:14 -070082};
83
84extern FileFetcher* gFetcher;
85
Yifan Hong8640cd12017-05-17 12:02:28 -070086class PartitionMounter {
87 public:
88 virtual ~PartitionMounter() {}
89 virtual status_t mountSystem() const { return OK; }
90 virtual status_t mountVendor() const { return OK; }
91 virtual status_t umountSystem() const { return OK; }
92 virtual status_t umountVendor() const { return OK; }
93};
94
95extern PartitionMounter* gPartitionMounter;
96
Michael Schwartz97dc0f92017-05-08 14:07:14 -070097template <typename T>
98status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter,
Yifan Hong60217032018-01-08 16:19:42 -080099 T* outObject, std::string* error = nullptr) {
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700100 std::string info;
101
102 if (gFetcher == nullptr) {
103 // Should never happen.
104 return NO_INIT;
105 }
106
Yifan Hong60217032018-01-08 16:19:42 -0800107 status_t result = gFetcher->fetch(path, info, error);
Michael Schwartz97dc0f92017-05-08 14:07:14 -0700108
109 if (result != OK) {
110 return result;
111 }
112
113 bool success = converter(outObject, info);
Yifan Hong2272bf82017-04-28 14:37:56 -0700114 if (!success) {
Yifan Hong60217032018-01-08 16:19:42 -0800115 if (error) {
116 *error = "Illformed file: " + path + ": " + converter.lastError();
117 }
Yifan Hong2272bf82017-04-28 14:37:56 -0700118 return BAD_VALUE;
119 }
120 return OK;
121}
122
Yifan Hong29bb2d42017-09-27 13:28:00 -0700123template <typename T>
124class ObjectFactory {
125 public:
126 virtual ~ObjectFactory() = default;
127 virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
128};
129extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory;
130
Yifan Hongafae1982018-01-11 18:15:24 -0800131// TODO(b/70628538): Do not infer from Shipping API level.
132inline Level convertFromApiLevel(size_t apiLevel) {
133 if (apiLevel < 26) {
134 return Level::LEGACY;
135 } else if (apiLevel == 26) {
136 return Level::O;
137 } else if (apiLevel == 27) {
138 return Level::O_MR1;
139 } else {
140 return Level::UNSPECIFIED;
141 }
142}
143
Yifan Hong2272bf82017-04-28 14:37:56 -0700144} // namespace details
145} // namespace vintf
146} // namespace android
147
148
149
150#endif