1 /*
2  * Copyright (C) 2018 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_FILE_SYSTEM_H
18 #define ANDROID_VINTF_FILE_SYSTEM_H
19 
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include <utils/Errors.h>
26 
27 namespace android {
28 namespace vintf {
29 
30 // Queries the file system in the correct way. Files can come from
31 // an actual file system, a sub-directory, or from ADB, depending on the
32 // implementation.
33 //
34 // This class can be used to create a mock for overriding.
35 class FileSystem {
36    public:
37     virtual ~FileSystem() {}
38     // Return NAME_NOT_FOUND if file is not found,
39     //        OK if file is retrieved and written to "fetched".
40     virtual status_t fetch(const std::string& path, std::string* fetched,
41                            std::string* error) const = 0;
42     // Return NAME_NOT_FOUND if directory is not found,
43     //        OK if file names are retrieved and written to out.
44     virtual status_t listFiles(const std::string& path, std::vector<std::string>* out,
45                                std::string* error) const = 0;
46 };
47 
48 namespace details {
49 
50 // Class that actually queries the file system.
51 class FileSystemImpl : public FileSystem {
52    public:
53     status_t fetch(const std::string&, std::string*, std::string*) const;
54     status_t listFiles(const std::string&, std::vector<std::string>*, std::string*) const;
55 };
56 
57 // Class that does nothing.
58 class FileSystemNoOp : public FileSystem {
59    public:
60     status_t fetch(const std::string&, std::string*, std::string*) const;
61     status_t listFiles(const std::string&, std::vector<std::string>*, std::string*) const;
62 };
63 
64 // The root is mounted to a given path.
65 class FileSystemUnderPath : public FileSystem {
66    public:
67     FileSystemUnderPath(const std::string& rootdir);
68     virtual status_t fetch(const std::string& path, std::string* fetched,
69                            std::string* error) const override;
70     virtual status_t listFiles(const std::string& path, std::vector<std::string>* out,
71                                std::string* error) const override;
72 
73    protected:
74     const std::string& getRootDir() const;
75 
76    private:
77     std::string mRootDir;
78     FileSystemImpl mImpl;
79 };
80 
81 }  // namespace details
82 }  // namespace vintf
83 }  // namespace android
84 
85 #endif
86