• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
~FileSystem()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 // Interface to a writable filesystem.
49 class WritableFileSystem : public FileSystem {
50    public:
51     // Return OK if successful. On error, return -errno, or UNKNOWN_ERROR if unknown.
52     virtual status_t write(const std::string& path, const std::string& content,
53                            std::string* error) const = 0;
54     // Return OK if successful. On error, return -errno, or UNKNOWN_ERROR if unknown.
55     virtual status_t deleteFile(const std::string& path, std::string* error) const = 0;
56 };
57 
58 namespace details {
59 
60 // Class that actually queries the file system.
61 class FileSystemImpl : public FileSystem {
62    public:
63     status_t fetch(const std::string&, std::string*, std::string*) const;
64     status_t listFiles(const std::string&, std::vector<std::string>*, std::string*) const;
65 };
66 
67 // Class that does nothing.
68 class FileSystemNoOp : public FileSystem {
69    public:
70     status_t fetch(const std::string&, std::string*, std::string*) const;
71     status_t listFiles(const std::string&, std::vector<std::string>*, std::string*) const;
72 };
73 
74 // The root is mounted to a given path.
75 class FileSystemUnderPath : public FileSystem {
76    public:
77     FileSystemUnderPath(const std::string& rootdir);
78     virtual status_t fetch(const std::string& path, std::string* fetched,
79                            std::string* error) const override;
80     virtual status_t listFiles(const std::string& path, std::vector<std::string>* out,
81                                std::string* error) const override;
82 
83    protected:
84     const std::string& getRootDir() const;
85 
86    private:
87     std::string mRootDir;
88     FileSystemImpl mImpl;
89 };
90 
91 }  // namespace details
92 }  // namespace vintf
93 }  // namespace android
94 
95 #endif
96