• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
20 #include <dirent.h>
21 
22 #include <fstream>
23 #include <iostream>
24 #include <memory>
25 #include <sstream>
26 
27 #include <utils/Errors.h>
28 #include <vintf/RuntimeInfo.h>
29 #include <vintf/parse_xml.h>
30 
31 namespace android {
32 namespace vintf {
33 namespace details {
34 
35 // Return the file from the given location as a string.
36 //
37 // This class can be used to create a mock for overriding.
38 class FileFetcher {
39    public:
~FileFetcher()40     virtual ~FileFetcher() {}
fetchInternal(const std::string & path,std::string & fetched,std::string * error)41     status_t fetchInternal(const std::string& path, std::string& fetched, std::string* error) {
42         std::ifstream in;
43 
44         in.open(path);
45         if (!in.is_open()) {
46             if (error) {
47                 *error = "Cannot open " + path;
48             }
49             return NAME_NOT_FOUND;
50         }
51 
52         std::stringstream ss;
53         ss << in.rdbuf();
54         fetched = ss.str();
55 
56         return OK;
57     }
fetch(const std::string & path,std::string & fetched,std::string * error)58     virtual status_t fetch(const std::string& path, std::string& fetched, std::string* error) {
59         return fetchInternal(path, fetched, error);
60     }
fetch(const std::string & path,std::string & fetched)61     virtual status_t fetch(const std::string& path, std::string& fetched) {
62         return fetchInternal(path, fetched, nullptr);
63     }
listFiles(const std::string & path,std::vector<std::string> * out,std::string * error)64     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     }
82 };
83 
84 extern FileFetcher* gFetcher;
85 
86 class PartitionMounter {
87    public:
~PartitionMounter()88     virtual ~PartitionMounter() {}
mountSystem()89     virtual status_t mountSystem() const { return OK; }
mountVendor()90     virtual status_t mountVendor() const { return OK; }
umountSystem()91     virtual status_t umountSystem() const { return OK; }
umountVendor()92     virtual status_t umountVendor() const { return OK; }
93 };
94 
95 extern PartitionMounter* gPartitionMounter;
96 
97 template <typename T>
98 status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter,
99                              T* outObject, std::string* error = nullptr) {
100     std::string info;
101 
102     if (gFetcher == nullptr) {
103         // Should never happen.
104         return NO_INIT;
105     }
106 
107     status_t result = gFetcher->fetch(path, info, error);
108 
109     if (result != OK) {
110         return result;
111     }
112 
113     bool success = converter(outObject, info, error);
114     if (!success) {
115         if (error) {
116             *error = "Illformed file: " + path + ": " + *error;
117         }
118         return BAD_VALUE;
119     }
120     return OK;
121 }
122 
123 template <typename T>
124 class ObjectFactory {
125    public:
126     virtual ~ObjectFactory() = default;
make_shared()127     virtual std::shared_ptr<T> make_shared() const { return std::make_shared<T>(); }
128 };
129 extern ObjectFactory<RuntimeInfo>* gRuntimeInfoFactory;
130 
131 // TODO(b/70628538): Do not infer from Shipping API level.
convertFromApiLevel(size_t apiLevel)132 inline 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 
144 class PropertyFetcher {
145    public:
146     virtual ~PropertyFetcher() = default;
147     virtual std::string getProperty(const std::string& key,
148                                     const std::string& defaultValue = "") const;
149     virtual uint64_t getUintProperty(const std::string& key, uint64_t defaultValue,
150                                      uint64_t max = UINT64_MAX) const;
151     virtual bool getBoolProperty(const std::string& key, bool defaultValue) const;
152 };
153 
154 const PropertyFetcher& getPropertyFetcher();
155 
156 }  // namespace details
157 }  // namespace vintf
158 }  // namespace android
159 
160 
161 
162 #endif
163