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 | |
| 5 | #ifndef CROS_DISKS_MOUNT_INFO_H_ |
| 6 | #define CROS_DISKS_MOUNT_INFO_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 11 | #include <gtest/gtest_prod.h> |
| 12 | |
| 13 | namespace cros_disks { |
| 14 | |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 15 | struct MountPointData; |
| 16 | |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 17 | // A class for querying information about mount points. |
| 18 | class MountInfo { |
| 19 | public: |
| 20 | MountInfo(); |
Qijiang Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame] | 21 | MountInfo(const MountInfo&) = delete; |
| 22 | MountInfo& operator=(const MountInfo&) = delete; |
| 23 | |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 24 | ~MountInfo(); |
| 25 | |
| 26 | // Decodes an encoded path by replacing any occurrence of \xxx, a backslash |
| 27 | // followed by an octal number, with an ASCII character of the same octal |
| 28 | // value. |
| 29 | std::string DecodePath(const std::string& encoded_path) const; |
| 30 | |
| 31 | // Returns the list of mount paths associated with a given source path. |
| 32 | std::vector<std::string> GetMountPaths(const std::string& source_path) const; |
| 33 | |
Ben Chan | 8dcede8 | 2011-07-25 20:56:13 -0700 | [diff] [blame] | 34 | // Returns true if a given mount path is found among the mount points. |
| 35 | bool HasMountPath(const std::string& mount_path) const; |
| 36 | |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 37 | // Retrieves the list of mount points from a given file, which has |
| 38 | // the same format as /proc/self/mountinfo. Returns true on success. |
| 39 | // Refer to <linux source>/Documentation/filesystems/proc.txt for details |
| 40 | // about /proc/self/mountinfo. |
Sergei Datsenko | 2d9c7a3 | 2021-01-05 23:13:13 +1100 | [diff] [blame] | 41 | // TODO(crbug.com/1163081): This should be replaced with using libmount. |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 42 | bool RetrieveFromFile(const std::string& path); |
| 43 | |
| 44 | // Retrieves the list of mount points of the current process by reading |
| 45 | // /proc/self/mountinfo. Returns true on success. |
| 46 | bool RetrieveFromCurrentProcess(); |
| 47 | |
| 48 | private: |
| 49 | // Converts a 3-character octal string into a decimal integer. |
| 50 | // Returns -1 if the conversion fails. |
| 51 | int ConvertOctalStringToInt(const std::string& octal) const; |
| 52 | |
| 53 | // A list of mount points gathered by the last call to RetrieveMountInfo(). |
Sergei Datsenko | 3cf72cb | 2019-04-01 11:27:50 +1100 | [diff] [blame] | 54 | std::vector<MountPointData> mount_points_; |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 55 | |
| 56 | FRIEND_TEST(MountInfoTest, ConvertOctalStringToInt); |
Ben Chan | beefd0d | 2011-07-25 09:31:34 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | } // namespace cros_disks |
| 60 | |
| 61 | #endif // CROS_DISKS_MOUNT_INFO_H_ |