blob: 5c22e5aae9c3cf6713f7c799e17519b91181093c [file] [log] [blame]
Ben Chane31d2aa2011-06-15 13:52:59 -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
Ben Chan5ccd9fe2013-11-13 18:28:27 -08005#include "cros-disks/system_mounter.h"
Ben Chane31d2aa2011-06-15 13:52:59 -07006
Ben Chan476ae4e2011-08-03 09:20:40 -07007#include <errno.h>
Ben Chane31d2aa2011-06-15 13:52:59 -07008#include <sys/mount.h>
9
10#include <string>
11#include <utility>
12
13#include <base/logging.h>
Sergei Datsenkoe8faba52020-10-06 21:45:22 +110014#include <base/containers/util.h>
15#include <base/strings/string_util.h>
Ben Chane31d2aa2011-06-15 13:52:59 -070016
Sergei Datsenko85a18332019-04-08 14:25:03 +100017#include "cros-disks/mount_options.h"
18#include "cros-disks/mount_point.h"
Sam McNallyc56ae312018-05-22 13:14:27 +100019#include "cros-disks/platform.h"
20
Ben Chane31d2aa2011-06-15 13:52:59 -070021namespace cros_disks {
22
Sergei Datsenko85a18332019-04-08 14:25:03 +100023namespace {
Ben Chanb1ac5a82011-08-02 17:53:55 -070024
Sergei Datsenko3928f782020-12-31 09:14:04 +110025constexpr int kExternalDiskMountFlags =
26 MS_NODEV | MS_NOSUID | MS_NOEXEC | MS_NOSYMFOLLOW | MS_DIRSYNC;
Sergei Datsenkoe8faba52020-10-06 21:45:22 +110027
Sergei Datsenko85a18332019-04-08 14:25:03 +100028} // namespace
29
Sergei Datsenkoe8faba52020-10-06 21:45:22 +110030SystemMounter::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 Chane31d2aa2011-06-15 13:52:59 -070038
Sergei Datsenko85a18332019-04-08 14:25:03 +100039SystemMounter::~SystemMounter() = default;
40
41std::unique_ptr<MountPoint> SystemMounter::Mount(
42 const std::string& source,
43 const base::FilePath& target_path,
Sergei Datsenkoe8faba52020-10-06 21:45:22 +110044 std::vector<std::string> params,
Sergei Datsenko85a18332019-04-08 14:25:03 +100045 MountErrorType* error) const {
Sergei Datsenkoe8faba52020-10-06 21:45:22 +110046 int flags = flags_;
Sergei Datsenko85a18332019-04-08 14:25:03 +100047
Sergei Datsenko68cd43a2020-11-16 22:57:16 +110048 // We only care about "ro" here.
Sergei Datsenkod2b83a12020-11-18 12:27:41 +110049 if (IsReadOnlyMount(params)) {
Sergei Datsenkoe8faba52020-10-06 21:45:22 +110050 flags |= MS_RDONLY;
51 }
52
Sergei Datsenko68cd43a2020-11-16 22:57:16 +110053 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 Datsenko1bb0bdc2021-02-17 11:26:43 +110059 std::string option_string;
60 if (!JoinParamsIntoOptions(options, &option_string)) {
61 *error = MOUNT_ERROR_INVALID_MOUNT_OPTIONS;
62 return nullptr;
63 }
64
Sergei Datsenko0ba12032021-01-07 08:51:14 +110065 return MountPoint::Mount(
66 {
67 .mount_path = target_path,
68 .source = source,
69 .filesystem_type = filesystem_type_,
70 .flags = flags,
Sergei Datsenko1bb0bdc2021-02-17 11:26:43 +110071 .data = option_string,
Sergei Datsenko0ba12032021-01-07 08:51:14 +110072 },
73 platform_, error);
Sergei Datsenko85a18332019-04-08 14:25:03 +100074}
75
76bool SystemMounter::CanMount(const std::string& source,
Sergei Datsenko68cd43a2020-11-16 22:57:16 +110077 const std::vector<std::string>& /*params*/,
Sergei Datsenko85a18332019-04-08 14:25:03 +100078 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 Chane31d2aa2011-06-15 13:52:59 -070085}
86
Sergei Datsenko68cd43a2020-11-16 22:57:16 +110087MountErrorType SystemMounter::ParseParams(
88 std::vector<std::string> /*params*/,
89 std::vector<std::string>* /*mount_options*/) const {
90 return MOUNT_ERROR_NONE;
91}
92
Ben Chane31d2aa2011-06-15 13:52:59 -070093} // namespace cros_disks