blob: 07aebdc1ea5946edd2fa79956ac772b00e29d18c [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/mount_options.h"
Ben Chane31d2aa2011-06-15 13:52:59 -07006
Ben Chane31d2aa2011-06-15 13:52:59 -07007#include <algorithm>
8
François Degros12db4062020-09-10 14:30:06 +10009#include <base/containers/adapters.h>
Sergei Datsenko0f014d22018-04-04 16:37:22 +100010#include <base/stl_util.h>
François Degros12db4062020-09-10 14:30:06 +100011#include <base/strings/string_util.h>
Sergei Datsenkof5553d12020-11-25 07:51:59 +110012#include <base/strings/strcat.h>
Ben Chane31d2aa2011-06-15 13:52:59 -070013
François Degros8b4e31e2019-07-29 11:39:19 +100014#include "cros-disks/quote.h"
15
Ben Chane31d2aa2011-06-15 13:52:59 -070016namespace cros_disks {
17
Sergei Datsenko0f014d22018-04-04 16:37:22 +100018namespace {
Sergei Datsenkof5553d12020-11-25 07:51:59 +110019bool FindLastElementStartingWith(const std::vector<std::string>& container,
20 base::StringPiece prefix,
21 std::string* result) {
22 for (const auto& element : base::Reversed(container)) {
23 if (base::StartsWith(element, prefix, base::CompareCase::SENSITIVE)) {
24 *result = element;
25 return true;
26 }
27 }
28 return false;
29}
Sergei Datsenko0f014d22018-04-04 16:37:22 +100030} // namespace
31
Sergei Datsenkod2b83a12020-11-18 12:27:41 +110032bool IsReadOnlyMount(const std::vector<std::string>& options) {
33 for (const auto& option : base::Reversed(options)) {
Sergei Datsenko3928f782020-12-31 09:14:04 +110034 if (option == "ro")
Sergei Datsenkod2b83a12020-11-18 12:27:41 +110035 return true;
Sergei Datsenko3928f782020-12-31 09:14:04 +110036 if (option == "rw")
Sergei Datsenkod2b83a12020-11-18 12:27:41 +110037 return false;
38 }
39 return false;
40}
41
Sergei Datsenkof5553d12020-11-25 07:51:59 +110042bool GetParamValue(const std::vector<std::string>& params,
43 base::StringPiece name,
44 std::string* value) {
45 if (!FindLastElementStartingWith(params, base::StrCat({name, "="}), value)) {
46 return false;
47 }
48 *value = value->substr(name.length() + 1);
49 return true;
50}
51
52void SetParamValue(std::vector<std::string>* params,
53 base::StringPiece name,
54 base::StringPiece value) {
55 params->emplace_back(base::StrCat({name, "=", value}));
56}
57
Sergei Datsenko7e7c7642021-01-08 19:15:34 +110058bool HasExactParam(const std::vector<std::string>& params,
59 base::StringPiece param) {
60 return base::Contains(params, param);
61}
62
63size_t RemoveParamsEqualTo(std::vector<std::string>* params,
64 base::StringPiece param) {
65 return base::Erase(*params, param);
66}
67
68size_t RemoveParamsWithSameName(std::vector<std::string>* params,
69 base::StringPiece name) {
70 std::string prefix = base::StrCat({name, "="});
71 return base::EraseIf(*params, [prefix](const std::string& value) {
72 return base::StartsWith(value, prefix, base::CompareCase::SENSITIVE);
73 });
74}
75
Sergei Datsenko1bb0bdc2021-02-17 11:26:43 +110076bool JoinParamsIntoOptions(const std::vector<std::string>& params,
77 std::string* out) {
78 for (const auto& element : params) {
79 if (element.find(',') != std::string::npos)
80 return false;
81 }
82 *out = base::JoinString(params, ",");
83 return true;
84}
85
Ben Chane31d2aa2011-06-15 13:52:59 -070086} // namespace cros_disks