1
2 /*
3 * Copyright (C) 2018 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #include <vintf/FileSystem.h>
19
20 #include <dirent.h>
21
22 #include <fstream>
23 #include <iostream>
24 #include <sstream>
25
26 namespace android {
27 namespace vintf {
28 namespace details {
29
fetch(const std::string & path,std::string * fetched,std::string * error) const30 status_t FileSystemImpl::fetch(const std::string& path, std::string* fetched,
31 std::string* error) const {
32 std::ifstream in;
33
34 errno = 0;
35 in.open(path);
36 if (!in || errno != 0) {
37 if (error) {
38 *error = "Cannot open " + path + ": " + strerror(errno);
39 }
40 return -errno;
41 }
42
43 std::stringstream ss;
44 ss << in.rdbuf();
45 *fetched = ss.str();
46
47 return -errno;
48 }
49
listFiles(const std::string & path,std::vector<std::string> * out,std::string * error) const50 status_t FileSystemImpl::listFiles(const std::string& path, std::vector<std::string>* out,
51 std::string* error) const {
52 errno = 0;
53 DIR* dirp = opendir(path.c_str());
54 if (!dirp || errno != 0) {
55 if (error) {
56 *error = "Cannot open " + path + ": " + strerror(errno);
57 }
58 return -errno;
59 }
60
61 std::unique_ptr<DIR, decltype(&closedir)> dir(dirp, closedir);
62 dirent* dp;
63 while ((dp = readdir(dir.get())) != nullptr) {
64 if (dp->d_type != DT_DIR) {
65 out->push_back(dp->d_name);
66 }
67 }
68 return -errno;
69 }
70
fetch(const std::string &,std::string *,std::string *) const71 status_t FileSystemNoOp::fetch(const std::string&, std::string*, std::string*) const {
72 return NAME_NOT_FOUND;
73 }
74
listFiles(const std::string &,std::vector<std::string> *,std::string *) const75 status_t FileSystemNoOp::listFiles(const std::string&, std::vector<std::string>*,
76 std::string*) const {
77 return NAME_NOT_FOUND;
78 }
79
FileSystemUnderPath(const std::string & rootdir)80 FileSystemUnderPath::FileSystemUnderPath(const std::string& rootdir) {
81 mRootDir = rootdir;
82 if (!mRootDir.empty() && mRootDir.back() != '/') {
83 mRootDir.push_back('/');
84 }
85 }
86
fetch(const std::string & path,std::string * fetched,std::string * error) const87 status_t FileSystemUnderPath::fetch(const std::string& path, std::string* fetched,
88 std::string* error) const {
89 return mImpl.fetch(mRootDir + path, fetched, error);
90 }
91
listFiles(const std::string & path,std::vector<std::string> * out,std::string * error) const92 status_t FileSystemUnderPath::listFiles(const std::string& path, std::vector<std::string>* out,
93 std::string* error) const {
94 return mImpl.listFiles(mRootDir + path, out, error);
95 }
96
getRootDir() const97 const std::string& FileSystemUnderPath::getRootDir() const {
98 return mRootDir;
99 }
100
101 } // namespace details
102 } // namespace vintf
103 } // namespace android
104