Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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_PLATFORM_H_ |
| 6 | #define CROS_DISKS_PLATFORM_H_ |
| 7 | |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 8 | #include <sys/mount.h> |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 9 | #include <sys/stat.h> |
Ben Chan | 93dec93 | 2011-08-03 12:57:49 -0700 | [diff] [blame] | 10 | #include <sys/types.h> |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 11 | |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 12 | #ifndef MS_NOSYMFOLLOW |
| 13 | // Added locally in kernel 5.4, upstream TBD. |
| 14 | #define MS_NOSYMFOLLOW 256 |
| 15 | #endif |
| 16 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 17 | #include <string> |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 18 | #include <unordered_set> |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 19 | |
Sergei Datsenko | e2d1d0b | 2020-11-18 12:48:13 +1100 | [diff] [blame] | 20 | #include <base/files/file.h> |
Sergei Datsenko | b362e4a | 2019-04-03 17:23:24 +1100 | [diff] [blame] | 21 | #include <chromeos/dbus/service_constants.h> |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 22 | |
| 23 | namespace cros_disks { |
| 24 | |
| 25 | // A class that provides functionalities such as creating and removing |
| 26 | // directories, and getting user ID and group ID for a username. |
| 27 | class Platform { |
| 28 | public: |
| 29 | Platform(); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 30 | Platform(const Platform&) = delete; |
| 31 | Platform& operator=(const Platform&) = delete; |
| 32 | |
Ben Chan | 5512355 | 2014-08-24 16:22:16 -0700 | [diff] [blame] | 33 | virtual ~Platform() = default; |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 34 | |
Ben Chan | adc5d00 | 2014-03-12 15:02:26 -0700 | [diff] [blame] | 35 | // Gets the canonicalized absolute path of |path| using realpath() and returns |
| 36 | // that via |real_path|. Return true on success. |
| 37 | virtual bool GetRealPath(const std::string& path, |
| 38 | std::string* real_path) const; |
| 39 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 40 | // Returns whether |path| exists. |
| 41 | virtual bool PathExists(const std::string& path) const; |
| 42 | |
| 43 | // Returns whether |path| exists and is a directory. |
| 44 | virtual bool DirectoryExists(const std::string& path) const; |
| 45 | |
Sergei Datsenko | e2d1d0b | 2020-11-18 12:48:13 +1100 | [diff] [blame] | 46 | // lstats the |path|. |
| 47 | virtual bool Lstat(const std::string& path, base::stat_wrapper_t* out) const; |
| 48 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 49 | // Creates a directory at |path| if it does not exist. Returns true on |
| 50 | // success. |
| 51 | virtual bool CreateDirectory(const std::string& path) const; |
| 52 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 53 | // Creates a directory at |path| if it does not exist. If |path| already |
| 54 | // exists and is a directory, this function tries to reuse it if it is empty |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 55 | // not in use. The created directory is only accessible by the current user. |
| 56 | // Returns true if the directory is created successfully. |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 57 | virtual bool CreateOrReuseEmptyDirectory(const std::string& path) const; |
| 58 | |
Ben Chan | f869288 | 2011-08-21 10:15:30 -0700 | [diff] [blame] | 59 | // Creates a directory at |path| similar to CreateOrReuseEmptyDirectory() |
| 60 | // but avoids using any paths in the |reserved_paths| set and retries on |
| 61 | // failure by augmenting a numeric suffix (e.g. "mydir (1)"), starting from |
| 62 | // 1 to |max_suffix_to_retry|, to the directory name. The created directory |
| 63 | // is only accessible by the current user. Returns true if the directory is |
| 64 | // created successfully. |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 65 | virtual bool CreateOrReuseEmptyDirectoryWithFallback( |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 66 | std::string* path, |
| 67 | unsigned max_suffix_to_retry, |
Sergei Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 68 | const std::unordered_set<std::string>& reserved_paths) const; |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 69 | |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 70 | // Creates a temporary directory inside |dir| and sets its path to |path|. |
| 71 | virtual bool CreateTemporaryDirInDir(const std::string& dir, |
| 72 | const std::string& prefix, |
| 73 | std::string* path) const; |
| 74 | |
Sergei Datsenko | ad2cb6a | 2018-05-15 17:34:26 +1000 | [diff] [blame] | 75 | // Writes contents of the |data| to a file. Returns the number of bytes |
| 76 | // written, or -1 on error. |
| 77 | virtual int WriteFile(const std::string& file, |
| 78 | const char* data, |
| 79 | int size) const; |
| 80 | |
| 81 | // Reads at most |size| bytes from the |file| to a buffer |data| and returns |
| 82 | // number of bytes actually read, or -1 on error. |
| 83 | virtual int ReadFile(const std::string& file, char* data, int size) const; |
Sergei Datsenko | bcd8e46 | 2018-04-20 15:44:56 +1000 | [diff] [blame] | 84 | |
Ben Chan | ec4eaab | 2012-02-05 23:26:58 -0800 | [diff] [blame] | 85 | // Returns the fallback directory name of |path| using |suffix| as follows: |
| 86 | // "|path| (|suffix|)" if |path| ends with a ASCII digit, or |
| 87 | // "|path| |suffix|" otherwise. |
| 88 | std::string GetDirectoryFallbackName(const std::string& path, |
| 89 | unsigned suffix) const; |
| 90 | |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 91 | // Gets the group ID of a given group name. Returns true on success. |
| 92 | virtual bool GetGroupId(const std::string& group_name, gid_t* group_id) const; |
| 93 | |
| 94 | // Gets the user ID and group ID of a given user name. Returns true on |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 95 | // success. |
Ben Chan | 1d5a8e7 | 2011-08-01 15:21:39 -0700 | [diff] [blame] | 96 | virtual bool GetUserAndGroupId(const std::string& user_name, |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 97 | uid_t* user_id, |
| 98 | gid_t* group_id) const; |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 99 | |
Ben Chan | b1ac5a8 | 2011-08-02 17:53:55 -0700 | [diff] [blame] | 100 | // Gets the user ID and group ID of |path|. If |path| is a symbolic link, the |
| 101 | // ownership of the linked file, not the symbolic link itself, is obtained. |
| 102 | // Returns true on success. |
| 103 | virtual bool GetOwnership(const std::string& path, |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 104 | uid_t* user_id, |
| 105 | gid_t* group_id) const; |
Ben Chan | b1ac5a8 | 2011-08-02 17:53:55 -0700 | [diff] [blame] | 106 | |
| 107 | // Gets the permissions of |path|. If |path| is a symbolic link, the |
| 108 | // permissions of the linked file, not the symbolic link itself, is obtained. |
| 109 | // Returns true on success. |
| 110 | virtual bool GetPermissions(const std::string& path, mode_t* mode) const; |
| 111 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 112 | // Removes a directory at |path| if it is empty and not in use. |
| 113 | // Returns true on success. |
| 114 | virtual bool RemoveEmptyDirectory(const std::string& path) const; |
| 115 | |
Ben Chan | 93dec93 | 2011-08-03 12:57:49 -0700 | [diff] [blame] | 116 | // Makes |user_name| to perform mount operations, which changes the value of |
| 117 | // mount_group_id_ and mount_user_id_. When |user_name| is a non-root user, a |
Ben Chan | 9ed09e3 | 2011-11-22 16:24:06 -0800 | [diff] [blame] | 118 | // mount operation respecting the value of mount_group_id_ and mount_user_id_ |
Ben Chan | 93dec93 | 2011-08-03 12:57:49 -0700 | [diff] [blame] | 119 | // becomes non-privileged. Returns false if it fails to obtain the user and |
| 120 | // group ID of |user_name|. |
| 121 | bool SetMountUser(const std::string& user_name); |
| 122 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 123 | // Sets the user ID and group ID of |path| to |user_id| and |group_id|, |
| 124 | // respectively. Returns true on success. |
| 125 | virtual bool SetOwnership(const std::string& path, |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 126 | uid_t user_id, |
| 127 | gid_t group_id) const; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 128 | |
| 129 | // Sets the permissions of |path| to |mode|. Returns true on success. |
| 130 | virtual bool SetPermissions(const std::string& path, mode_t mode) const; |
| 131 | |
Sergei Datsenko | b362e4a | 2019-04-03 17:23:24 +1100 | [diff] [blame] | 132 | // Unmounts |path| with |flags|. |
| 133 | virtual MountErrorType Unmount(const std::string& path, int flags) const; |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 134 | |
François Degros | 5abcca2 | 2020-09-10 16:51:13 +1000 | [diff] [blame] | 135 | // Mounts the |source| filesystem of type |filesystem_type| at mount point |
| 136 | // |target| with |flags| and |options|. |
Sergei Datsenko | b362e4a | 2019-04-03 17:23:24 +1100 | [diff] [blame] | 137 | virtual MountErrorType Mount(const std::string& source, |
| 138 | const std::string& target, |
| 139 | const std::string& filesystem_type, |
François Degros | 5abcca2 | 2020-09-10 16:51:13 +1000 | [diff] [blame] | 140 | uint64_t flags, |
| 141 | const std::string& options) const; |
Sam McNally | c56ae31 | 2018-05-22 13:14:27 +1000 | [diff] [blame] | 142 | |
Ben Chan | 93dec93 | 2011-08-03 12:57:49 -0700 | [diff] [blame] | 143 | gid_t mount_group_id() const { return mount_group_id_; } |
| 144 | |
| 145 | uid_t mount_user_id() const { return mount_user_id_; } |
| 146 | |
Ben Chan | 98f8ae0 | 2011-10-04 16:34:34 -0700 | [diff] [blame] | 147 | const std::string& mount_user() const { return mount_user_; } |
| 148 | |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 149 | private: |
Ben Chan | 93dec93 | 2011-08-03 12:57:49 -0700 | [diff] [blame] | 150 | // Group ID to perform mount operations. |
| 151 | gid_t mount_group_id_; |
| 152 | |
| 153 | // User ID to perform mount operations. |
| 154 | uid_t mount_user_id_; |
| 155 | |
Ben Chan | 98f8ae0 | 2011-10-04 16:34:34 -0700 | [diff] [blame] | 156 | // User ID to perform mount operations. |
| 157 | std::string mount_user_; |
Ben Chan | 29be915 | 2011-07-25 14:39:48 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | } // namespace cros_disks |
| 161 | |
| 162 | #endif // CROS_DISKS_PLATFORM_H_ |