Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * Copyright (C) 2019 The LineageOS Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #ifndef ANDROID_VOLMGR_VOLUMEBASE_H |
| 19 | #define ANDROID_VOLMGR_VOLUMEBASE_H |
| 20 | |
| 21 | #include "Utils.h" |
| 22 | |
| 23 | #include <cutils/multiuser.h> |
| 24 | #include <utils/Errors.h> |
| 25 | |
| 26 | #include <sys/types.h> |
| 27 | #include <list> |
| 28 | #include <string> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace volmgr { |
| 32 | |
| 33 | /* |
| 34 | * Representation of a mounted volume ready for presentation. |
| 35 | * |
| 36 | * Various subclasses handle the different mounting prerequisites, such as |
| 37 | * encryption details, etc. Volumes can also be "stacked" above other |
| 38 | * volumes to help communicate dependencies. For example, an ASEC volume |
| 39 | * can be stacked on a vfat volume. |
| 40 | * |
| 41 | * Mounted volumes can be asked to manage bind mounts to present themselves |
| 42 | * to specific users on the device. |
| 43 | * |
| 44 | * When an unmount is requested, the volume recursively unmounts any stacked |
| 45 | * volumes and removes any bind mounts before finally unmounting itself. |
| 46 | */ |
| 47 | class VolumeBase { |
| 48 | public: |
| 49 | virtual ~VolumeBase(); |
| 50 | |
| 51 | enum class Type { |
| 52 | kPublic = 0, |
| 53 | kPrivate, |
| 54 | kEmulated, |
| 55 | kAsec, |
| 56 | kObb, |
| 57 | }; |
| 58 | |
| 59 | enum MountFlags { |
| 60 | /* Flag that volume is primary external storage */ |
| 61 | kPrimary = 1 << 0, |
| 62 | /* Flag that volume is visible to normal apps */ |
| 63 | kVisible = 1 << 1, |
| 64 | }; |
| 65 | |
| 66 | enum class State { |
| 67 | kUnmounted = 0, |
| 68 | kChecking, |
| 69 | kMounted, |
| 70 | kMountedReadOnly, |
| 71 | kEjecting, |
| 72 | kUnmountable, |
| 73 | kRemoved, |
| 74 | kBadRemoval, |
| 75 | }; |
| 76 | |
| 77 | const std::string& getId() const { return mId; } |
| 78 | const std::string& getDiskId() const { return mDiskId; } |
| 79 | const std::string& getPartGuid() const { return mPartGuid; } |
| 80 | const std::string& getPartLabel() const { return mPartLabel; } |
| 81 | Type getType() const { return mType; } |
| 82 | int getMountFlags() const { return mMountFlags; } |
| 83 | State getState() const { return mState; } |
| 84 | const std::string& getPath() const { return mPath; } |
Tom Marshall | 0057c5f | 2019-07-24 21:12:07 +0200 | [diff] [blame] | 85 | bool isMountable() const { return mMountable; } |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 86 | |
| 87 | status_t setDiskId(const std::string& diskId); |
| 88 | status_t setPartGuid(const std::string& partGuid); |
| 89 | status_t setPartLabel(const std::string& partLabel); |
| 90 | status_t setMountFlags(int mountFlags); |
| 91 | status_t setSilent(bool silent); |
| 92 | |
| 93 | status_t create(); |
| 94 | status_t destroy(); |
| 95 | status_t mount(); |
| 96 | status_t unmount(bool detach = false); |
| 97 | |
| 98 | protected: |
| 99 | explicit VolumeBase(Type type); |
| 100 | |
| 101 | virtual status_t doCreate(); |
| 102 | virtual status_t doDestroy(); |
| 103 | virtual status_t doMount() = 0; |
| 104 | virtual status_t doUnmount(bool detach = false) = 0; |
| 105 | |
| 106 | status_t setId(const std::string& id); |
| 107 | status_t setPath(const std::string& path); |
| 108 | |
| 109 | private: |
| 110 | /* ID that uniquely references volume while alive */ |
| 111 | std::string mId; |
| 112 | /* ID that uniquely references parent disk while alive */ |
| 113 | std::string mDiskId; |
| 114 | /* Partition GUID of this volume */ |
| 115 | std::string mPartGuid; |
| 116 | /* Partition label of this volume */ |
| 117 | std::string mPartLabel; |
| 118 | /* Volume type */ |
| 119 | Type mType; |
| 120 | /* Flags used when mounting this volume */ |
| 121 | int mMountFlags; |
| 122 | /* Flag indicating object is created */ |
| 123 | bool mCreated; |
| 124 | /* Current state of volume */ |
| 125 | State mState; |
| 126 | /* Path to mounted volume */ |
| 127 | std::string mPath; |
| 128 | /* Flag indicating that volume should emit no events */ |
| 129 | bool mSilent; |
| 130 | |
Tom Marshall | 0057c5f | 2019-07-24 21:12:07 +0200 | [diff] [blame] | 131 | bool mMountable; |
| 132 | virtual bool detectMountable(); |
| 133 | |
Tom Marshall | 292bd23 | 2019-01-04 14:37:31 -0800 | [diff] [blame] | 134 | void setState(State state); |
| 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(VolumeBase); |
| 137 | }; |
| 138 | |
| 139 | } // namespace volmgr |
| 140 | } // namespace android |
| 141 | |
| 142 | #endif |