Ben Chan | beefd0d | 2011-07-25 09:31:34 -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_info.h" |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 6 | |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 7 | #include <base/logging.h> |
Ben Chan | 32e4b4b | 2014-07-23 16:18:37 -0700 | [diff] [blame] | 8 | #include <base/strings/string_split.h> |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 9 | |
Ben Chan | 5ccd9fe | 2013-11-13 18:28:27 -0800 | [diff] [blame] | 10 | #include "cros-disks/file_reader.h" |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 11 | #include "cros-disks/mount_point.h" |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 12 | #include "cros-disks/quote.h" |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 13 | |
Anand K Mistry | 40cff45 | 2019-07-30 10:24:48 +1000 | [diff] [blame] | 14 | namespace cros_disks { |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | |
Ben Chan | 460439f | 2011-09-13 09:16:28 -0700 | [diff] [blame] | 17 | bool IsOctalDigit(char digit) { |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 18 | return digit >= '0' && digit <= '7'; |
| 19 | } |
| 20 | |
| 21 | } // namespace |
| 22 | |
Ben Chan | fc77d71 | 2019-06-20 12:45:56 -0700 | [diff] [blame] | 23 | MountInfo::MountInfo() = default; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 24 | |
Ben Chan | fc77d71 | 2019-06-20 12:45:56 -0700 | [diff] [blame] | 25 | MountInfo::~MountInfo() = default; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 26 | |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 27 | int MountInfo::ConvertOctalStringToInt(const std::string& octal) const { |
Ben Chan | de0e3f6 | 2017-09-26 06:28:39 -0700 | [diff] [blame] | 28 | if (octal.size() == 3 && IsOctalDigit(octal[0]) && IsOctalDigit(octal[1]) && |
| 29 | IsOctalDigit(octal[2])) { |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 30 | return (octal[0] - '0') * 64 + (octal[1] - '0') * 8 + (octal[2] - '0'); |
| 31 | } |
| 32 | return -1; |
| 33 | } |
| 34 | |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 35 | std::string MountInfo::DecodePath(const std::string& encoded_path) const { |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 36 | size_t encoded_path_size = encoded_path.size(); |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 37 | std::string decoded_path; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 38 | decoded_path.reserve(encoded_path_size); |
| 39 | for (size_t index = 0; index < encoded_path_size; ++index) { |
| 40 | char path_char = encoded_path[index]; |
| 41 | if ((path_char == '\\') && (index + 3 < encoded_path_size)) { |
| 42 | int decimal = ConvertOctalStringToInt(encoded_path.substr(index + 1, 3)); |
| 43 | if (decimal != -1) { |
| 44 | decoded_path.push_back(static_cast<char>(decimal)); |
| 45 | index += 3; |
| 46 | continue; |
| 47 | } |
| 48 | } |
| 49 | decoded_path.push_back(path_char); |
| 50 | } |
| 51 | return decoded_path; |
| 52 | } |
| 53 | |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 54 | std::vector<std::string> MountInfo::GetMountPaths( |
| 55 | const std::string& source_path) const { |
| 56 | std::vector<std::string> mount_paths; |
Ben Chan | 63ec9c4 | 2014-04-21 19:13:23 -0700 | [diff] [blame] | 57 | for (const auto& mount_point : mount_points_) { |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 58 | if (mount_point.source == source_path) |
| 59 | mount_paths.push_back(mount_point.mount_path.value()); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 60 | } |
| 61 | return mount_paths; |
| 62 | } |
| 63 | |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 64 | bool MountInfo::HasMountPath(const std::string& mount_path) const { |
Ben Chan | 63ec9c4 | 2014-04-21 19:13:23 -0700 | [diff] [blame] | 65 | for (const auto& mount_point : mount_points_) { |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 66 | if (mount_point.mount_path.value() == mount_path) |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 67 | return true; |
| 68 | } |
| 69 | return false; |
| 70 | } |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 71 | bool MountInfo::RetrieveFromFile(const std::string& path) { |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 72 | mount_points_.clear(); |
| 73 | |
| 74 | FileReader reader; |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 75 | if (!reader.Open(base::FilePath(path))) { |
François Degros | 8b4e31e | 2019-07-29 11:39:19 +1000 | [diff] [blame] | 76 | LOG(ERROR) << "Cannot retrieve mount info from " << quote(path); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 80 | std::string line; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 81 | while (reader.ReadLine(&line)) { |
Ben Chan | 213c6d9 | 2019-04-10 16:21:52 -0700 | [diff] [blame] | 82 | std::vector<std::string> tokens = base::SplitString( |
| 83 | line, " ", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 84 | size_t num_tokens = tokens.size(); |
| 85 | if (num_tokens >= 10 && tokens[num_tokens - 4] == "-") { |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 86 | MountPointData mount_point; |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 87 | mount_point.source = DecodePath(tokens[num_tokens - 2]); |
| 88 | mount_point.mount_path = base::FilePath(DecodePath(tokens[4])); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 89 | mount_point.filesystem_type = tokens[num_tokens - 3]; |
| 90 | mount_points_.push_back(mount_point); |
| 91 | } |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | bool MountInfo::RetrieveFromCurrentProcess() { |
| 97 | return RetrieveFromFile("/proc/self/mountinfo"); |
| 98 | } |
| 99 | |
| 100 | } // namespace cros_disks |