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 <map> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include <vector> 25 26 #include <sys/stat.h> // for timespec 27 28 #include <utils/Errors.h> 29 30 namespace android { 31 namespace vintf { 32 33 // Queries the file system in the correct way. Files can come from 34 // an actual file system, a sub-directory, or from ADB, depending on the 35 // implementation. 36 // 37 // This class can be used to create a mock for overriding. 38 class FileSystem { 39 public: ~FileSystem()40 virtual ~FileSystem() {} 41 // Return NAME_NOT_FOUND if file is not found, 42 // OK if file is retrieved and written to "fetched". 43 virtual status_t fetch(const std::string& path, std::string* fetched, 44 std::string* error) const = 0; 45 // Return NAME_NOT_FOUND if directory is not found, 46 // OK if file names are retrieved and written to out. 47 virtual status_t listFiles(const std::string& path, std::vector<std::string>* out, 48 std::string* error) const = 0; 49 // Return NAME_NOT_FOUND if file is not found, 50 // OK if "mtime" is set with modified time of the file. 51 virtual status_t modifiedTime(const std::string& path, timespec* mtime, 52 std::string* error) const = 0; 53 }; 54 55 // Interface to a writable filesystem. 56 class WritableFileSystem : public FileSystem { 57 public: 58 // Return OK if successful. On error, return -errno, or UNKNOWN_ERROR if unknown. 59 virtual status_t write(const std::string& path, const std::string& content, 60 std::string* error) const = 0; 61 // Return OK if successful. On error, return -errno, or UNKNOWN_ERROR if unknown. 62 virtual status_t deleteFile(const std::string& path, std::string* error) const = 0; 63 }; 64 65 namespace details { 66 67 // Class that actually queries the file system. 68 class FileSystemImpl : public FileSystem { 69 public: 70 status_t fetch(const std::string&, std::string*, std::string*) const override; 71 status_t listFiles(const std::string&, std::vector<std::string>*, std::string*) const override; 72 status_t modifiedTime(const std::string& path, timespec* mtime, std::string* error) const; 73 }; 74 75 // Class that does nothing. 76 class FileSystemNoOp : public FileSystem { 77 public: 78 status_t fetch(const std::string&, std::string*, std::string*) const override; 79 status_t listFiles(const std::string&, std::vector<std::string>*, std::string*) const override; 80 status_t modifiedTime(const std::string& path, timespec* mtime, 81 std::string* error) const override; 82 }; 83 84 // The root is mounted to a given path. 85 class FileSystemUnderPath : public FileSystem { 86 public: 87 FileSystemUnderPath(const std::string& rootdir); 88 status_t fetch(const std::string& path, std::string* fetched, 89 std::string* error) const override; 90 status_t listFiles(const std::string& path, std::vector<std::string>* out, 91 std::string* error) const override; 92 status_t modifiedTime(const std::string& path, timespec* mtime, 93 std::string* error) const override; 94 95 protected: 96 const std::string& getRootDir() const; 97 98 private: 99 std::string mRootDir; 100 FileSystemImpl mImpl; 101 }; 102 103 // A FileSystem object that can redirect access for one path 104 // FileSystem is read via the internal impl. 105 class PathReplacingFileSystem : public FileSystem { 106 public: 107 // Use |impl| for any actual reads. Owns impl. 108 PathReplacingFileSystem(std::unique_ptr<FileSystem> impl, 109 const std::map<std::string, std::string>& path_replacements); 110 111 status_t fetch(const std::string& path, std::string* fetched, 112 std::string* error) const override; 113 status_t listFiles(const std::string& path, std::vector<std::string>* out, 114 std::string* error) const override; 115 status_t modifiedTime(const std::string& path, timespec* mtime, 116 std::string* error) const override; 117 118 private: 119 std::string path_replace(std::string_view path) const; 120 121 std::unique_ptr<FileSystem> impl_; 122 std::map<std::string, std::string> path_replacements_; 123 }; 124 } // namespace details 125 } // namespace vintf 126 } // namespace android 127 128 #endif 129