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/mount_options.h" |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 6 | |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
François Degros | 12db406 | 2020-09-10 14:30:06 +1000 | [diff] [blame] | 9 | #include <base/containers/adapters.h> |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 10 | #include <base/stl_util.h> |
François Degros | 12db406 | 2020-09-10 14:30:06 +1000 | [diff] [blame] | 11 | #include <base/strings/string_util.h> |
Sergei Datsenko | f5553d1 | 2020-11-25 07:51:59 +1100 | [diff] [blame] | 12 | #include <base/strings/strcat.h> |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 13 | |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 14 | #include "cros-disks/quote.h" |
| 15 | |
Ben Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 16 | namespace cros_disks { |
| 17 | |
Sergei Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 18 | namespace { |
Sergei Datsenko | f5553d1 | 2020-11-25 07:51:59 +1100 | [diff] [blame] | 19 | bool 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 Datsenko | 0f014d2 | 2018-04-04 16:37:22 +1000 | [diff] [blame] | 30 | } // namespace |
| 31 | |
Sergei Datsenko | d2b83a1 | 2020-11-18 12:27:41 +1100 | [diff] [blame] | 32 | bool IsReadOnlyMount(const std::vector<std::string>& options) { |
| 33 | for (const auto& option : base::Reversed(options)) { |
Sergei Datsenko | 3928f78 | 2020-12-31 09:14:04 +1100 | [diff] [blame] | 34 | if (option == "ro") |
Sergei Datsenko | d2b83a1 | 2020-11-18 12:27:41 +1100 | [diff] [blame] | 35 | return true; |
Sergei Datsenko | 3928f78 | 2020-12-31 09:14:04 +1100 | [diff] [blame] | 36 | if (option == "rw") |
Sergei Datsenko | d2b83a1 | 2020-11-18 12:27:41 +1100 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | |
Sergei Datsenko | f5553d1 | 2020-11-25 07:51:59 +1100 | [diff] [blame] | 42 | bool 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 | |
| 52 | void 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 Datsenko | 7e7c764 | 2021-01-08 19:15:34 +1100 | [diff] [blame] | 58 | bool HasExactParam(const std::vector<std::string>& params, |
| 59 | base::StringPiece param) { |
| 60 | return base::Contains(params, param); |
| 61 | } |
| 62 | |
| 63 | size_t RemoveParamsEqualTo(std::vector<std::string>* params, |
| 64 | base::StringPiece param) { |
| 65 | return base::Erase(*params, param); |
| 66 | } |
| 67 | |
| 68 | size_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 Datsenko | 1bb0bdc | 2021-02-17 11:26:43 +1100 | [diff] [blame] | 76 | bool 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 Chan | e31d2aa | 2011-06-15 13:52:59 -0700 | [diff] [blame] | 86 | } // namespace cros_disks |