| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | #pragma once |
| 3 | |
| 4 | extern "C" { |
| 5 | #include "mountutil.h" |
| 6 | } |
| 7 | |
| 8 | namespace mountutil { |
| 9 | |
| 10 | // C++ wrapper for the MOUNT_ENUM structure. |
| 11 | class MountEnum |
| 12 | { |
| 13 | public: |
| 14 | // Initialize a new instance of the MountEnum class. |
| 15 | MountEnum(const char* mountInfoFile = MOUNT_INFO_FILE) |
| 16 | { |
| 17 | THROW_LAST_ERROR_IF(MountEnumCreateEx(&m_mountEnum, mountInfoFile) < 0); |
| 18 | } |
| 19 | |
| 20 | // Destruct this instance of the MountEnum class. |
| 21 | ~MountEnum() |
| 22 | { |
| 23 | MountEnumFree(&m_mountEnum); |
| 24 | } |
| 25 | |
| 26 | MountEnum(const MountEnum&) = delete; |
| 27 | MountEnum& operator=(const MountEnum&) = delete; |
| 28 | |
| 29 | // Get the next entry in the mountinfo file. Returns false if the end is reached. |
| 30 | bool Next() |
| 31 | { |
| 32 | if (MountEnumNext(&m_mountEnum) < 0) |
| 33 | { |
| 34 | if (errno == 0) |
| 35 | { |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | THROW_ERRNO(errno); |
| 40 | } |
| 41 | |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // Return the current entry. |
| 46 | // N.B. You must call Next at least once before this is valid. |
| 47 | // N.B. The strings in the current entry are valid only until Next is called again or until this |
| 48 | // class is destructed. |
| 49 | MOUNT_ENTRY& Current() |
| 50 | { |
| 51 | return m_mountEnum.Current; |
| 52 | } |
| 53 | |
| 54 | // Finds a mount using the specified predicate. Returns false if there is no matching entry. |
| 55 | // N.B. If the function returns true, use Current to get the matching entry. |
| 56 | bool FindMount(const std::function<bool(const MOUNT_ENTRY&)>& predicate) |
| 57 | { |
| 58 | while (Next()) |
| 59 | { |
| 60 | if (predicate(m_mountEnum.Current)) |
| 61 | { |
| 62 | return true; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | MOUNT_ENUM m_mountEnum{}; |
| 71 | }; |
| 72 | |
| 73 | struct ParsedOptions |
| 74 | { |
| 75 | std::string StringOptions; |
| 76 | int MountFlags; |
| 77 | bool NoFail; |
| 78 | }; |
| 79 | |
| 80 | ParsedOptions MountParseFlags(std::string_view options); |
| 81 | |
| 82 | int MountFilesystem(const char* source, const char* target, const char* type, const char* options); |
| 83 | |
| 84 | } // namespace mountutil |