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 <fstream> 21 #include <iostream> 22 #include <sstream> 23 24 #include <android-base/logging.h> 25 #include <utils/Errors.h> 26 27 #include "parse_xml.h" 28 29 namespace android { 30 namespace vintf { 31 namespace details { 32 33 // Return the file from the given location as a string. 34 // 35 // This class can be used to create a mock for overriding. 36 class FileFetcher { 37 public: 38 virtual ~FileFetcher() {} 39 virtual status_t fetch(const std::string& path, std::string& fetched) { 40 std::ifstream in; 41 42 in.open(path); 43 if (!in.is_open()) { 44 LOG(WARNING) << "Cannot open " << path; 45 return INVALID_OPERATION; 46 } 47 48 std::stringstream ss; 49 ss << in.rdbuf(); 50 fetched = ss.str(); 51 52 return OK; 53 } 54 }; 55 56 extern FileFetcher* gFetcher; 57 58 class PartitionMounter { 59 public: 60 virtual ~PartitionMounter() {} 61 virtual status_t mountSystem() const { return OK; } 62 virtual status_t mountVendor() const { return OK; } 63 virtual status_t umountSystem() const { return OK; } 64 virtual status_t umountVendor() const { return OK; } 65 }; 66 67 extern PartitionMounter* gPartitionMounter; 68 69 template <typename T> 70 status_t fetchAllInformation(const std::string& path, const XmlConverter<T>& converter, 71 T* outObject) { 72 std::string info; 73 74 if (gFetcher == nullptr) { 75 // Should never happen. 76 return NO_INIT; 77 } 78 79 status_t result = gFetcher->fetch(path, info); 80 81 if (result != OK) { 82 return result; 83 } 84 85 bool success = converter(outObject, info); 86 if (!success) { 87 LOG(ERROR) << "Illformed file: " << path << ": " 88 << converter.lastError(); 89 return BAD_VALUE; 90 } 91 return OK; 92 } 93 94 } // namespace details 95 } // namespace vintf 96 } // namespace android 97 98 99 100 #endif 101