2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "VolumeBase.h"
19 #include "VolumeManager.h"
20 #include "ResponseCode.h"
22 #include <android-base/stringprintf.h>
23 #include <android-base/logging.h>
27 #include <sys/mount.h>
29 #include <sys/types.h>
31 using android::base::StringPrintf;
36 VolumeBase::VolumeBase(Type type) :
37 mType(type), mMountFlags(0), mMountUserId(-1), mCreated(false), mState(
38 State::kUnmounted), mSilent(false) {
41 VolumeBase::~VolumeBase() {
45 void VolumeBase::setState(State state) {
47 notifyEvent(ResponseCode::VolumeStateChanged, StringPrintf("%d", mState));
50 status_t VolumeBase::setDiskId(const std::string& diskId) {
52 LOG(WARNING) << getId() << " diskId change requires destroyed";
60 status_t VolumeBase::setPartGuid(const std::string& partGuid) {
62 LOG(WARNING) << getId() << " partGuid change requires destroyed";
70 status_t VolumeBase::setMountFlags(int mountFlags) {
71 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
72 LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
76 mMountFlags = mountFlags;
80 status_t VolumeBase::setMountUserId(userid_t mountUserId) {
81 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
82 LOG(WARNING) << getId() << " user change requires state unmounted or unmountable";
86 mMountUserId = mountUserId;
90 status_t VolumeBase::setSilent(bool silent) {
92 LOG(WARNING) << getId() << " silence change requires destroyed";
100 status_t VolumeBase::setId(const std::string& id) {
102 LOG(WARNING) << getId() << " id change requires not created";
110 status_t VolumeBase::setPath(const std::string& path) {
111 if (mState != State::kChecking) {
112 LOG(WARNING) << getId() << " path change requires state checking";
117 notifyEvent(ResponseCode::VolumePathChanged, mPath);
121 status_t VolumeBase::setInternalPath(const std::string& internalPath) {
122 if (mState != State::kChecking) {
123 LOG(WARNING) << getId() << " internal path change requires state checking";
127 mInternalPath = internalPath;
128 notifyEvent(ResponseCode::VolumeInternalPathChanged, mInternalPath);
132 void VolumeBase::notifyEvent(int event) {
134 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
135 getId().c_str(), false);
138 void VolumeBase::notifyEvent(int event, const std::string& value) {
140 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
141 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
144 void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
145 mVolumes.push_back(volume);
148 void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
149 mVolumes.remove(volume);
152 std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
153 for (auto vol : mVolumes) {
154 if (vol->getId() == id) {
161 status_t VolumeBase::create() {
165 status_t res = doCreate();
166 notifyEvent(ResponseCode::VolumeCreated,
167 StringPrintf("%d \"%s\" \"%s\"", mType, mDiskId.c_str(), mPartGuid.c_str()));
168 setState(State::kUnmounted);
172 status_t VolumeBase::doCreate() {
176 status_t VolumeBase::destroy() {
179 if (mState == State::kMounted) {
181 setState(State::kBadRemoval);
183 setState(State::kRemoved);
186 notifyEvent(ResponseCode::VolumeDestroyed);
187 status_t res = doDestroy();
192 status_t VolumeBase::doDestroy() {
196 status_t VolumeBase::mount() {
197 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
198 LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
202 setState(State::kChecking);
203 status_t res = doMount();
205 setState(State::kMounted);
207 setState(State::kUnmountable);
213 status_t VolumeBase::unmount() {
214 if (mState != State::kMounted) {
215 LOG(WARNING) << getId() << " unmount requires state mounted";
219 setState(State::kEjecting);
220 for (const auto& vol : mVolumes) {
221 if (vol->destroy()) {
222 LOG(WARNING) << getId() << " failed to destroy " << vol->getId()
228 status_t res = doUnmount();
229 setState(State::kUnmounted);
233 status_t VolumeBase::format(const std::string& fsType) {
234 if (mState == State::kMounted) {
238 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
239 LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
243 setState(State::kFormatting);
244 status_t res = doFormat(fsType);
245 setState(State::kUnmounted);
249 status_t VolumeBase::doFormat(const std::string& fsType) {
254 } // namespace android