blob: d86119e87eaa66dc0fef06398f8ebfa9f72137da [file] [log] [blame]
Ben Chanbeefd0d2011-07-25 09:31:34 -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_info.h"
Ben Chanbeefd0d2011-07-25 09:31:34 -07006
Ben Chanbeefd0d2011-07-25 09:31:34 -07007#include <base/logging.h>
Ben Chan32e4b4b2014-07-23 16:18:37 -07008#include <base/strings/string_split.h>
Ben Chanbeefd0d2011-07-25 09:31:34 -07009
Ben Chan5ccd9fe2013-11-13 18:28:27 -080010#include "cros-disks/file_reader.h"
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110011#include "cros-disks/mount_point.h"
François Degros8b4e31e2019-07-29 11:39:19 +100012#include "cros-disks/quote.h"
Ben Chanbeefd0d2011-07-25 09:31:34 -070013
Anand K Mistry40cff452019-07-30 10:24:48 +100014namespace cros_disks {
Ben Chanbeefd0d2011-07-25 09:31:34 -070015namespace {
16
Ben Chan460439f2011-09-13 09:16:28 -070017bool IsOctalDigit(char digit) {
Ben Chanbeefd0d2011-07-25 09:31:34 -070018 return digit >= '0' && digit <= '7';
19}
20
21} // namespace
22
Ben Chanfc77d712019-06-20 12:45:56 -070023MountInfo::MountInfo() = default;
Ben Chanbeefd0d2011-07-25 09:31:34 -070024
Ben Chanfc77d712019-06-20 12:45:56 -070025MountInfo::~MountInfo() = default;
Ben Chanbeefd0d2011-07-25 09:31:34 -070026
Ben Chan213c6d92019-04-10 16:21:52 -070027int MountInfo::ConvertOctalStringToInt(const std::string& octal) const {
Ben Chande0e3f62017-09-26 06:28:39 -070028 if (octal.size() == 3 && IsOctalDigit(octal[0]) && IsOctalDigit(octal[1]) &&
29 IsOctalDigit(octal[2])) {
Ben Chanbeefd0d2011-07-25 09:31:34 -070030 return (octal[0] - '0') * 64 + (octal[1] - '0') * 8 + (octal[2] - '0');
31 }
32 return -1;
33}
34
Ben Chan213c6d92019-04-10 16:21:52 -070035std::string MountInfo::DecodePath(const std::string& encoded_path) const {
Ben Chanbeefd0d2011-07-25 09:31:34 -070036 size_t encoded_path_size = encoded_path.size();
Ben Chan213c6d92019-04-10 16:21:52 -070037 std::string decoded_path;
Ben Chanbeefd0d2011-07-25 09:31:34 -070038 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 Chan213c6d92019-04-10 16:21:52 -070054std::vector<std::string> MountInfo::GetMountPaths(
55 const std::string& source_path) const {
56 std::vector<std::string> mount_paths;
Ben Chan63ec9c42014-04-21 19:13:23 -070057 for (const auto& mount_point : mount_points_) {
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110058 if (mount_point.source == source_path)
59 mount_paths.push_back(mount_point.mount_path.value());
Ben Chanbeefd0d2011-07-25 09:31:34 -070060 }
61 return mount_paths;
62}
63
Ben Chan213c6d92019-04-10 16:21:52 -070064bool MountInfo::HasMountPath(const std::string& mount_path) const {
Ben Chan63ec9c42014-04-21 19:13:23 -070065 for (const auto& mount_point : mount_points_) {
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110066 if (mount_point.mount_path.value() == mount_path)
Ben Chan8dcede82011-07-25 20:56:13 -070067 return true;
68 }
69 return false;
70}
Ben Chan213c6d92019-04-10 16:21:52 -070071bool MountInfo::RetrieveFromFile(const std::string& path) {
Ben Chanbeefd0d2011-07-25 09:31:34 -070072 mount_points_.clear();
73
74 FileReader reader;
Ben Chan213c6d92019-04-10 16:21:52 -070075 if (!reader.Open(base::FilePath(path))) {
François Degros8b4e31e2019-07-29 11:39:19 +100076 LOG(ERROR) << "Cannot retrieve mount info from " << quote(path);
Ben Chanbeefd0d2011-07-25 09:31:34 -070077 return false;
78 }
79
Ben Chan213c6d92019-04-10 16:21:52 -070080 std::string line;
Ben Chanbeefd0d2011-07-25 09:31:34 -070081 while (reader.ReadLine(&line)) {
Ben Chan213c6d92019-04-10 16:21:52 -070082 std::vector<std::string> tokens = base::SplitString(
83 line, " ", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
Ben Chanbeefd0d2011-07-25 09:31:34 -070084 size_t num_tokens = tokens.size();
85 if (num_tokens >= 10 && tokens[num_tokens - 4] == "-") {
Sergei Datsenko3cf72cb2019-04-01 11:27:50 +110086 MountPointData mount_point;
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110087 mount_point.source = DecodePath(tokens[num_tokens - 2]);
88 mount_point.mount_path = base::FilePath(DecodePath(tokens[4]));
Ben Chanbeefd0d2011-07-25 09:31:34 -070089 mount_point.filesystem_type = tokens[num_tokens - 3];
90 mount_points_.push_back(mount_point);
91 }
92 }
93 return true;
94}
95
96bool MountInfo::RetrieveFromCurrentProcess() {
97 return RetrieveFromFile("/proc/self/mountinfo");
98}
99
100} // namespace cros_disks