blob: 9302d6b1a21d6db8c28ad3401b567ab9a5073505 [file] [log] [blame]
Ben Chan29be9152011-07-25 14:39:48 -07001// 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 Datsenkoe8faba52020-10-06 21:45:22 +11008#include <sys/mount.h>
Ben Chan8dcede82011-07-25 20:56:13 -07009#include <sys/stat.h>
Ben Chan93dec932011-08-03 12:57:49 -070010#include <sys/types.h>
Ben Chan8dcede82011-07-25 20:56:13 -070011
Sergei Datsenkoe8faba52020-10-06 21:45:22 +110012#ifndef MS_NOSYMFOLLOW
13// Added locally in kernel 5.4, upstream TBD.
14#define MS_NOSYMFOLLOW 256
15#endif
16
Ben Chan29be9152011-07-25 14:39:48 -070017#include <string>
Sergei Datsenko7e7c7642021-01-08 19:15:34 +110018#include <unordered_set>
Ben Chan29be9152011-07-25 14:39:48 -070019
Sergei Datsenkoe2d1d0b2020-11-18 12:48:13 +110020#include <base/files/file.h>
Sergei Datsenkob362e4a2019-04-03 17:23:24 +110021#include <chromeos/dbus/service_constants.h>
Ben Chan29be9152011-07-25 14:39:48 -070022
23namespace 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.
27class Platform {
28 public:
29 Platform();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090030 Platform(const Platform&) = delete;
31 Platform& operator=(const Platform&) = delete;
32
Ben Chan55123552014-08-24 16:22:16 -070033 virtual ~Platform() = default;
Ben Chan29be9152011-07-25 14:39:48 -070034
Ben Chanadc5d002014-03-12 15:02:26 -070035 // 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 Datsenko0f014d22018-04-04 16:37:22 +100040 // 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 Datsenkoe2d1d0b2020-11-18 12:48:13 +110046 // lstats the |path|.
47 virtual bool Lstat(const std::string& path, base::stat_wrapper_t* out) const;
48
Ben Chan8dcede82011-07-25 20:56:13 -070049 // 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 Chan29be9152011-07-25 14:39:48 -070053 // 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 Chan8dcede82011-07-25 20:56:13 -070055 // not in use. The created directory is only accessible by the current user.
56 // Returns true if the directory is created successfully.
Ben Chan29be9152011-07-25 14:39:48 -070057 virtual bool CreateOrReuseEmptyDirectory(const std::string& path) const;
58
Ben Chanf8692882011-08-21 10:15:30 -070059 // 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 Chan8dcede82011-07-25 20:56:13 -070065 virtual bool CreateOrReuseEmptyDirectoryWithFallback(
Ben Chande0e3f62017-09-26 06:28:39 -070066 std::string* path,
67 unsigned max_suffix_to_retry,
Sergei Datsenko7e7c7642021-01-08 19:15:34 +110068 const std::unordered_set<std::string>& reserved_paths) const;
Ben Chan29be9152011-07-25 14:39:48 -070069
Sergei Datsenkobcd8e462018-04-20 15:44:56 +100070 // 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 Datsenkoad2cb6a2018-05-15 17:34:26 +100075 // 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 Datsenkobcd8e462018-04-20 15:44:56 +100084
Ben Chanec4eaab2012-02-05 23:26:58 -080085 // 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 Chan1d5a8e72011-08-01 15:21:39 -070091 // 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 Chan29be9152011-07-25 14:39:48 -070095 // success.
Ben Chan1d5a8e72011-08-01 15:21:39 -070096 virtual bool GetUserAndGroupId(const std::string& user_name,
Ben Chande0e3f62017-09-26 06:28:39 -070097 uid_t* user_id,
98 gid_t* group_id) const;
Ben Chan29be9152011-07-25 14:39:48 -070099
Ben Chanb1ac5a82011-08-02 17:53:55 -0700100 // 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 Chande0e3f62017-09-26 06:28:39 -0700104 uid_t* user_id,
105 gid_t* group_id) const;
Ben Chanb1ac5a82011-08-02 17:53:55 -0700106
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 Chan29be9152011-07-25 14:39:48 -0700112 // 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 Chan93dec932011-08-03 12:57:49 -0700116 // 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 Chan9ed09e32011-11-22 16:24:06 -0800118 // mount operation respecting the value of mount_group_id_ and mount_user_id_
Ben Chan93dec932011-08-03 12:57:49 -0700119 // 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 Chan8dcede82011-07-25 20:56:13 -0700123 // 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 Chande0e3f62017-09-26 06:28:39 -0700126 uid_t user_id,
127 gid_t group_id) const;
Ben Chan8dcede82011-07-25 20:56:13 -0700128
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 Datsenkob362e4a2019-04-03 17:23:24 +1100132 // Unmounts |path| with |flags|.
133 virtual MountErrorType Unmount(const std::string& path, int flags) const;
Ben Chan8dcede82011-07-25 20:56:13 -0700134
François Degros5abcca22020-09-10 16:51:13 +1000135 // Mounts the |source| filesystem of type |filesystem_type| at mount point
136 // |target| with |flags| and |options|.
Sergei Datsenkob362e4a2019-04-03 17:23:24 +1100137 virtual MountErrorType Mount(const std::string& source,
138 const std::string& target,
139 const std::string& filesystem_type,
François Degros5abcca22020-09-10 16:51:13 +1000140 uint64_t flags,
141 const std::string& options) const;
Sam McNallyc56ae312018-05-22 13:14:27 +1000142
Ben Chan93dec932011-08-03 12:57:49 -0700143 gid_t mount_group_id() const { return mount_group_id_; }
144
145 uid_t mount_user_id() const { return mount_user_id_; }
146
Ben Chan98f8ae02011-10-04 16:34:34 -0700147 const std::string& mount_user() const { return mount_user_; }
148
Ben Chan29be9152011-07-25 14:39:48 -0700149 private:
Ben Chan93dec932011-08-03 12:57:49 -0700150 // 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 Chan98f8ae02011-10-04 16:34:34 -0700156 // User ID to perform mount operations.
157 std::string mount_user_;
Ben Chan29be9152011-07-25 14:39:48 -0700158};
159
160} // namespace cros_disks
161
162#endif // CROS_DISKS_PLATFORM_H_