Ben Chan | e31d2aa | 2011-06-15 13:52:59 -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 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 5 | #include "cros-disks/system_mounter.h" |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 6 | |
Ben Chan | 476ae4e | 2011-08-03 09:20:40 -0700 | [diff] [blame] | 7 | #include <errno.h> |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 8 | #include <sys/mount.h> |
| 9 | |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | |
| 13 | #include <base/logging.h> |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 14 | #include <base/containers/util.h> |
| 15 | #include <base/strings/string_util.h> |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 16 | |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 17 | #include "cros-disks/mount_options.h" |
| 18 | #include "cros-disks/mount_point.h" |
Sam McNally | c56ae31 | 2018-05-22 13:14:27 +1000 | [diff] [blame] | 19 | #include "cros-disks/platform.h" |
| 20 | |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 21 | namespace cros_disks { |
| 22 | |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 23 | namespace { |
Ben Chan | b1ac5a8 | 2011-08-02 17:53:55 -0700 | [diff] [blame] | 24 | |
Sergei Datsenko | 3928f78 | 2020-12-31 09:14:04 +1100 | [diff] [blame] | 25 | constexpr int kExternalDiskMountFlags = |
| 26 | MS_NODEV | MS_NOSUID | MS_NOEXEC | MS_NOSYMFOLLOW | MS_DIRSYNC; |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 27 | |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 28 | } // namespace |
| 29 | |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 30 | SystemMounter::SystemMounter(const Platform* platform, |
| 31 | std::string filesystem_type, |
| 32 | bool read_only, |
| 33 | std::vector<std::string> options) |
| 34 | : platform_(platform), |
| 35 | filesystem_type_(std::move(filesystem_type)), |
| 36 | flags_(kExternalDiskMountFlags | (read_only ? MS_RDONLY : 0)), |
| 37 | options_(std::move(options)) {} |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 38 | |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 39 | SystemMounter::~SystemMounter() = default; |
| 40 | |
| 41 | std::unique_ptr<MountPoint> SystemMounter::Mount( |
| 42 | const std::string& source, |
| 43 | const base::FilePath& target_path, |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 44 | std::vector<std::string> params, |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 45 | MountErrorType* error) const { |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 46 | int flags = flags_; |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 47 | |
Sergei Datsenko | 68cd43a | 2020-11-16 22:57:16 +1100 | [diff] [blame] | 48 | // We only care about "ro" here. |
Sergei Datsenko | d2b83a1 | 2020-11-18 12:27:41 +1100 | [diff] [blame] | 49 | if (IsReadOnlyMount(params)) { |
Sergei Datsenko | e8faba5 | 2020-10-06 21:45:22 +1100 | [diff] [blame] | 50 | flags |= MS_RDONLY; |
| 51 | } |
| 52 | |
Sergei Datsenko | 68cd43a | 2020-11-16 22:57:16 +1100 | [diff] [blame] | 53 | std::vector<std::string> options = options_; |
| 54 | *error = ParseParams(std::move(params), &options); |
| 55 | if (*error != MOUNT_ERROR_NONE) { |
| 56 | return nullptr; |
| 57 | } |
| 58 | |
Sergei Datsenko | 1bb0bdc | 2021-02-17 11:26:43 +1100 | [diff] [blame] | 59 | std::string option_string; |
| 60 | if (!JoinParamsIntoOptions(options, &option_string)) { |
| 61 | *error = MOUNT_ERROR_INVALID_MOUNT_OPTIONS; |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 65 | return MountPoint::Mount( |
| 66 | { |
| 67 | .mount_path = target_path, |
| 68 | .source = source, |
| 69 | .filesystem_type = filesystem_type_, |
| 70 | .flags = flags, |
Sergei Datsenko | 1bb0bdc | 2021-02-17 11:26:43 +1100 | [diff] [blame] | 71 | .data = option_string, |
Sergei Datsenko | 0ba1203 | 2021-01-07 08:51:14 +1100 | [diff] [blame] | 72 | }, |
| 73 | platform_, error); |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | bool SystemMounter::CanMount(const std::string& source, |
Sergei Datsenko | 68cd43a | 2020-11-16 22:57:16 +1100 | [diff] [blame] | 77 | const std::vector<std::string>& /*params*/, |
Sergei Datsenko | 85a1833 | 2019-04-08 14:25:03 +1000 | [diff] [blame] | 78 | base::FilePath* suggested_dir_name) const { |
| 79 | if (source.empty()) { |
| 80 | *suggested_dir_name = base::FilePath("disk"); |
| 81 | } else { |
| 82 | *suggested_dir_name = base::FilePath(source).BaseName(); |
| 83 | } |
| 84 | return true; |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Sergei Datsenko | 68cd43a | 2020-11-16 22:57:16 +1100 | [diff] [blame] | 87 | MountErrorType SystemMounter::ParseParams( |
| 88 | std::vector<std::string> /*params*/, |
| 89 | std::vector<std::string>* /*mount_options*/) const { |
| 90 | return MOUNT_ERROR_NONE; |
| 91 | } |
| 92 | |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 93 | } // namespace cros_disks |