1 /*
2 * Copyright (C) 2021 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 #define LOG_TAG "powerhal-adaptivecpu"
18 #define ATRACE_TAG (ATRACE_TAG_POWER | ATRACE_TAG_HAL)
19
20 #include "RealFilesystem.h"
21
22 #include <android-base/logging.h>
23 #include <dirent.h>
24 #include <utils/Trace.h>
25
26 #include <fstream>
27 #include <istream>
28
29 namespace aidl {
30 namespace google {
31 namespace hardware {
32 namespace power {
33 namespace impl {
34 namespace pixel {
35
ListDirectory(const std::string & path,std::vector<std::string> * result) const36 bool RealFilesystem::ListDirectory(const std::string &path,
37 std::vector<std::string> *result) const {
38 ATRACE_CALL();
39 // We can't use std::filesystem, see aosp/894015 & b/175635923.
40 auto dir = std::unique_ptr<DIR, decltype(&closedir)>{opendir(path.c_str()), closedir};
41 if (!dir) {
42 LOG(ERROR) << "Failed to open directory " << path;
43 return false;
44 }
45 dirent *entry;
46 while ((entry = readdir(&*dir)) != nullptr) {
47 result->emplace_back(entry->d_name);
48 }
49 return true;
50 }
51
ReadFileStream(const std::string & path,std::unique_ptr<std::istream> * result) const52 bool RealFilesystem::ReadFileStream(const std::string &path,
53 std::unique_ptr<std::istream> *result) const {
54 ATRACE_CALL();
55 *result = std::make_unique<std::ifstream>(path);
56 if ((*result)->fail()) {
57 LOG(ERROR) << "Failed to read file stream: " << path;
58 return false;
59 }
60 return true;
61 }
62
ResetFileStream(const std::unique_ptr<std::istream> & fileStream) const63 bool RealFilesystem::ResetFileStream(const std::unique_ptr<std::istream> &fileStream) const {
64 if (fileStream->seekg(0).bad()) {
65 LOG(ERROR) << "Failed to reset file stream";
66 return false;
67 }
68 return true;
69 }
70
71 } // namespace pixel
72 } // namespace impl
73 } // namespace power
74 } // namespace hardware
75 } // namespace google
76 } // namespace aidl
77