blob: 7e7e70fb0a934db630a96230e35f6caec2e24514 [file] [log] [blame]
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +11001// 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 Datsenko0ba12032021-01-07 08:51:14 +11008#include <sys/mount.h>
9
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110010#include <memory>
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110011#include <string>
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110012
13#include <base/files/file_path.h>
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110014#include <chromeos/dbus/service_constants.h>
15
16namespace cros_disks {
17
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110018// Holds information about a mount point.
19struct 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 Datsenko3cf72cb2019-04-01 11:27:50 +110031
Sergei Datsenko0ba12032021-01-07 08:51:14 +110032class Platform;
33
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110034// Class representing a mount created by a mounter.
35class MountPoint {
36 public:
Anand K Mistryf1bd0862019-12-20 11:54:55 +110037 // 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 Datsenko3cf72cb2019-04-01 11:27:50 +110040
Sergei Datsenko0ba12032021-01-07 08:51:14 +110041 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 Datsenko50c204d2020-11-14 21:18:40 +110047 MountPoint(const MountPoint&) = delete;
48 MountPoint& operator=(const MountPoint&) = delete;
49
Sergei Datsenko18a61462021-02-04 12:36:45 +110050 // 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 Mistryf1bd0862019-12-20 11:54:55 +110052 virtual ~MountPoint();
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110053
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 Mistryc7eba322020-01-15 17:45:38 +110057 virtual void Release();
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110058
Sergei Datsenko0ba12032021-01-07 08:51:14 +110059 // Unmounts right now.
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110060 MountErrorType Unmount();
61
Sergei Datsenko0ba12032021-01-07 08:51:14 +110062 // Remount with specified ro/rw.
63 MountErrorType Remount(bool read_only);
64
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110065 const base::FilePath& path() const { return data_.mount_path; }
Sergei Datsenko0ba12032021-01-07 08:51:14 +110066 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 Datsenko3cf72cb2019-04-01 11:27:50 +110071
Anand K Mistryf1bd0862019-12-20 11:54:55 +110072 protected:
Anand K Mistryf1bd0862019-12-20 11:54:55 +110073 // 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 Datsenko0ba12032021-01-07 08:51:14 +110076 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 Mistryf1bd0862019-12-20 11:54:55 +110083
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110084 private:
Anand K Mistryf1bd0862019-12-20 11:54:55 +110085 bool released_ = false;
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110086};
87
88} // namespace cros_disks
89
90#endif // CROS_DISKS_MOUNT_POINT_H_