blob: 81c5d4f440cf984e14b2f3af1361a7540c5eac3e [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
5#ifndef CROS_DISKS_MOUNT_INFO_H_
6#define CROS_DISKS_MOUNT_INFO_H_
7
8#include <string>
9#include <vector>
10
Ben Chanbeefd0d2011-07-25 09:31:34 -070011#include <gtest/gtest_prod.h>
12
13namespace cros_disks {
14
Sergei Datsenko2d9c7a32021-01-05 23:13:13 +110015struct MountPointData;
16
Ben Chanbeefd0d2011-07-25 09:31:34 -070017// A class for querying information about mount points.
18class MountInfo {
19 public:
20 MountInfo();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090021 MountInfo(const MountInfo&) = delete;
22 MountInfo& operator=(const MountInfo&) = delete;
23
Ben Chanbeefd0d2011-07-25 09:31:34 -070024 ~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 Chan8dcede82011-07-25 20:56:13 -070034 // Returns true if a given mount path is found among the mount points.
35 bool HasMountPath(const std::string& mount_path) const;
36
Ben Chanbeefd0d2011-07-25 09:31:34 -070037 // 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 Datsenko2d9c7a32021-01-05 23:13:13 +110041 // TODO(crbug.com/1163081): This should be replaced with using libmount.
Ben Chanbeefd0d2011-07-25 09:31:34 -070042 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 Datsenko3cf72cb2019-04-01 11:27:50 +110054 std::vector<MountPointData> mount_points_;
Ben Chanbeefd0d2011-07-25 09:31:34 -070055
56 FRIEND_TEST(MountInfoTest, ConvertOctalStringToInt);
Ben Chanbeefd0d2011-07-25 09:31:34 -070057};
58
59} // namespace cros_disks
60
61#endif // CROS_DISKS_MOUNT_INFO_H_