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