Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 1 | // Copyright 2019 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_MOUNT_POINT_H_ |
| 6 | #define CROS_DISKS_MOUNT_POINT_H_ |
| 7 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 8 | #include <sys/mount.h> |
| 9 | |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 10 | #include <memory> |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 11 | #include <string> |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 12 | |
| 13 | #include <base/files/file_path.h> |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 14 | #include <chromeos/dbus/service_constants.h> |
| 15 | |
| 16 | namespace cros_disks { |
| 17 | |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 18 | // Holds information about a mount point. |
| 19 | struct MountPointData { |
| 20 | // Mount point path. |
| 21 | base::FilePath mount_path; |
| 22 | // Source description used to mount. |
| 23 | std::string source = {}; |
| 24 | // Filesystem type of the mount. |
| 25 | std::string filesystem_type = {}; |
| 26 | // Flags of the mount point. |
| 27 | int flags = 0; |
| 28 | // Additional data passed during mount. |
| 29 | std::string data = {}; |
| 30 | }; |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 31 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 32 | class Platform; |
| 33 | |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 34 | // Class representing a mount created by a mounter. |
| 35 | class MountPoint { |
| 36 | public: |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 37 | // Creates a MountPoint that does nothing on unmount and 'leaks' the mount |
| 38 | // point. |
| 39 | static std::unique_ptr<MountPoint> CreateLeaking(const base::FilePath& path); |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 40 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 41 | static std::unique_ptr<MountPoint> Mount(MountPointData data, |
| 42 | const Platform* platform, |
| 43 | MountErrorType* error); |
| 44 | |
| 45 | explicit MountPoint(MountPointData data, const Platform* platform = nullptr); |
| 46 | |
Sergei Datsenko | 50c204d | 2020-11-14 21:18:40 +1100 | [diff] [blame] | 47 | MountPoint(const MountPoint&) = delete; |
| 48 | MountPoint& operator=(const MountPoint&) = delete; |
| 49 | |
Sergei Datsenko | 18a6146 | 2021-02-04 12:36:45 +1100 | [diff] [blame] | 50 | // Unmounts the mount point as a last resort, but as it's unable to handle |
| 51 | // errors an explicit call to Unmount() is the better alternative. |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 52 | virtual ~MountPoint(); |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 53 | |
| 54 | // Releases (leaks) the ownership of the mount point. |
| 55 | // Until all places handle ownership of mount points properly |
| 56 | // it's necessary to be able to leave the mount alone. |
Anand K Mistry | c7eba32 | 2020-01-15 17:45:38 +1100 | [diff] [blame] | 57 | virtual void Release(); |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 58 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 59 | // Unmounts right now. |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 60 | MountErrorType Unmount(); |
| 61 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 62 | // Remount with specified ro/rw. |
| 63 | MountErrorType Remount(bool read_only); |
| 64 | |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 65 | const base::FilePath& path() const { return data_.mount_path; } |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 66 | const std::string& source() const { return data_.source; } |
| 67 | const std::string& fstype() const { return data_.filesystem_type; } |
| 68 | int flags() const { return data_.flags; } |
| 69 | const std::string& data() const { return data_.data; } |
| 70 | bool is_read_only() const { return (data_.flags & MS_RDONLY) == MS_RDONLY; } |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 71 | |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 72 | protected: |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 73 | // Unmounts the mount point. If MOUNT_ERROR_NONE is returned, will only be |
| 74 | // called once, regardless of the number of times Unmount() is called. If |
| 75 | // Release() is called, this function will not be called. |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 76 | virtual MountErrorType UnmountImpl(); |
| 77 | |
| 78 | // Remounts with new flags. Only called if mount is assumed to be mounted. |
| 79 | virtual MountErrorType RemountImpl(int flags); |
| 80 | |
| 81 | MountPointData data_; |
| 82 | const Platform* platform_; |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 83 | |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 84 | private: |
Anand K Mistry | f1bd086 | 2019-12-20 11:54:55 +1100 | [diff] [blame] | 85 | bool released_ = false; |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace cros_disks |
| 89 | |
| 90 | #endif // CROS_DISKS_MOUNT_POINT_H_ |