blob: 6ec6e27452d2b9b9450455edcf00657d3a0e23b6 [file] [log] [blame]
Tom Marshalla08c6f12019-01-04 14:37:31 -08001/*
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
30namespace android {
31namespace 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 */
47class 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; }
85
86 status_t setDiskId(const std::string& diskId);
87 status_t setPartGuid(const std::string& partGuid);
88 status_t setPartLabel(const std::string& partLabel);
89 status_t setMountFlags(int mountFlags);
90 status_t setSilent(bool silent);
91
92 status_t create();
93 status_t destroy();
94 status_t mount();
95 status_t unmount(bool detach = false);
96
97 protected:
98 explicit VolumeBase(Type type);
99
100 virtual status_t doCreate();
101 virtual status_t doDestroy();
102 virtual status_t doMount() = 0;
103 virtual status_t doUnmount(bool detach = false) = 0;
104
105 status_t setId(const std::string& id);
106 status_t setPath(const std::string& path);
107
108 private:
109 /* ID that uniquely references volume while alive */
110 std::string mId;
111 /* ID that uniquely references parent disk while alive */
112 std::string mDiskId;
113 /* Partition GUID of this volume */
114 std::string mPartGuid;
115 /* Partition label of this volume */
116 std::string mPartLabel;
117 /* Volume type */
118 Type mType;
119 /* Flags used when mounting this volume */
120 int mMountFlags;
121 /* Flag indicating object is created */
122 bool mCreated;
123 /* Current state of volume */
124 State mState;
125 /* Path to mounted volume */
126 std::string mPath;
127 /* Flag indicating that volume should emit no events */
128 bool mSilent;
129
130 void setState(State state);
131
132 DISALLOW_COPY_AND_ASSIGN(VolumeBase);
133};
134
135} // namespace volmgr
136} // namespace android
137
138#endif