Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 1 | // Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CROS_DISKS_FUSE_MOUNT_MANAGER_H_ |
| 6 | #define CROS_DISKS_FUSE_MOUNT_MANAGER_H_ |
| 7 | |
| 8 | #include <functional> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include <gtest/gtest_prod.h> |
| 14 | |
| 15 | #include "cros-disks/mount_manager.h" |
| 16 | |
| 17 | namespace cros_disks { |
| 18 | |
Sergei Datsenko | 25898b1 | 2020-11-26 08:07:15 +1100 | [diff] [blame] | 19 | class Mounter; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 20 | |
| 21 | // Implementation of MountManager for mounting arbitrary FUSE-based filesystems. |
| 22 | // It essentially does dispatching of mount requests to individual FUSE helpers. |
| 23 | class FUSEMountManager : public MountManager { |
| 24 | public: |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 25 | // |mount_root| - where mount points go. |
| 26 | // |working_dirs_root| - where temporary working directories go. |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 27 | FUSEMountManager(const std::string& mount_root, |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 28 | const std::string& working_dirs_root, |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 29 | Platform* platform, |
Sergei Datsenko | a910bba | 2019-06-18 13:31:59 +1000 | [diff] [blame] | 30 | Metrics* metrics, |
| 31 | brillo::ProcessReaper* process_reaper); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 32 | FUSEMountManager(const FUSEMountManager&) = delete; |
| 33 | FUSEMountManager& operator=(const FUSEMountManager&) = delete; |
| 34 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 35 | ~FUSEMountManager() override; |
| 36 | |
| 37 | bool Initialize() override; |
| 38 | |
| 39 | // Whether we know about FUSE driver able to handle this source. Note that |
| 40 | // source doesn't have to be an actual file or path, it could be anything |
| 41 | // identifying FUSE module and what instance to mount. |
| 42 | bool CanMount(const std::string& source) const override; |
| 43 | |
| 44 | // Returns the type of mount sources supported by the manager. |
| 45 | MountSourceType GetMountSourceType() const override { |
| 46 | // TODO(crbug.com/831491): Introduce generic "FUSE" storage. |
| 47 | return MOUNT_SOURCE_NETWORK_STORAGE; |
| 48 | } |
| 49 | |
| 50 | protected: |
| 51 | // Mounts |source| to |mount_path| as |fuse_type| with |options|. |
| 52 | // |fuse_type| can be used to specify the type of |source|. |
| 53 | // If |fuse_type| is an empty string, the type is determined based on the |
Nigel Tao | ca7a4ef | 2021-05-14 11:44:25 +1000 | [diff] [blame] | 54 | // format of the |source|. The underlying mounter may append their own mount |
| 55 | // options to |options|. |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 56 | std::unique_ptr<MountPoint> DoMount(const std::string& source, |
| 57 | const std::string& fuse_type, |
| 58 | const std::vector<std::string>& options, |
| 59 | const base::FilePath& mount_path, |
Anand K Mistry | d0a0523 | 2020-01-24 14:04:18 +1100 | [diff] [blame] | 60 | MountErrorType* error) override; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 61 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 62 | // Returns a suggested mount path for a source. |
| 63 | std::string SuggestMountPath(const std::string& source) const override; |
| 64 | |
Sergei Datsenko | 25898b1 | 2020-11-26 08:07:15 +1100 | [diff] [blame] | 65 | void RegisterHelper(std::unique_ptr<Mounter> mounter); |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 66 | |
| 67 | private: |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 68 | FRIEND_TEST(FUSEMountManagerTest, SuggestMountPath); |
| 69 | friend class FUSEMountManagerTest; |
| 70 | |
Sergei Datsenko | 25898b1 | 2020-11-26 08:07:15 +1100 | [diff] [blame] | 71 | std::vector<std::unique_ptr<Mounter>> helpers_; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 72 | const std::string working_dirs_root_; |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace cros_disks |
| 76 | |
| 77 | #endif // CROS_DISKS_FUSE_MOUNT_MANAGER_H_ |