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 <base/stringprintf.h>
23 #include <base/logging.h>
27 #include <sys/mount.h>
29 #include <sys/types.h>
31 using android::base::StringPrintf;
38 VolumeBase::VolumeBase(Type type) :
39 mType(type), mMountFlags(0), mMountUserId(-1), mCreated(false), mState(
40 State::kUnmounted), mSilent(false) {
43 VolumeBase::~VolumeBase() {
47 void VolumeBase::setState(State state) {
49 notifyEvent(ResponseCode::VolumeStateChanged, StringPrintf("%d", mState));
52 status_t VolumeBase::setDiskId(const std::string& diskId) {
54 LOG(WARNING) << getId() << " diskId change requires destroyed";
62 status_t VolumeBase::setPartGuid(const std::string& partGuid) {
64 LOG(WARNING) << getId() << " partGuid change requires destroyed";
72 status_t VolumeBase::setMountFlags(int mountFlags) {
73 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
74 LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
78 mMountFlags = mountFlags;
82 status_t VolumeBase::setMountUserId(userid_t mountUserId) {
83 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
84 LOG(WARNING) << getId() << " user change requires state unmounted or unmountable";
88 mMountUserId = mountUserId;
92 status_t VolumeBase::setSilent(bool silent) {
94 LOG(WARNING) << getId() << " silence change requires destroyed";
102 status_t VolumeBase::setId(const std::string& id) {
104 LOG(WARNING) << getId() << " id change requires not created";
112 status_t VolumeBase::setPath(const std::string& path) {
113 if (mState != State::kChecking) {
114 LOG(WARNING) << getId() << " path change requires state checking";
119 notifyEvent(ResponseCode::VolumePathChanged, mPath);
123 status_t VolumeBase::setInternalPath(const std::string& internalPath) {
124 if (mState != State::kChecking) {
125 LOG(WARNING) << getId() << " internal path change requires state checking";
129 mInternalPath = internalPath;
130 notifyEvent(ResponseCode::VolumeInternalPathChanged, mInternalPath);
134 void VolumeBase::notifyEvent(int event) {
136 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
137 getId().c_str(), false);
140 void VolumeBase::notifyEvent(int event, const std::string& value) {
142 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
143 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
146 void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
147 mVolumes.push_back(volume);
150 void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
151 mVolumes.remove(volume);
154 std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
155 for (auto vol : mVolumes) {
156 if (vol->getId() == id) {
163 status_t VolumeBase::create() {
167 status_t res = doCreate();
168 notifyEvent(ResponseCode::VolumeCreated,
169 StringPrintf("%d \"%s\" \"%s\"", mType, mDiskId.c_str(), mPartGuid.c_str()));
170 setState(State::kUnmounted);
174 status_t VolumeBase::doCreate() {
178 status_t VolumeBase::destroy() {
181 if (mState == State::kMounted) {
183 setState(State::kBadRemoval);
185 setState(State::kRemoved);
188 notifyEvent(ResponseCode::VolumeDestroyed);
189 status_t res = doDestroy();
194 status_t VolumeBase::doDestroy() {
198 status_t VolumeBase::mount() {
199 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
200 LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
204 setState(State::kChecking);
205 status_t res = doMount();
207 setState(State::kMounted);
209 setState(State::kUnmountable);
215 status_t VolumeBase::unmount() {
216 if (mState != State::kMounted) {
217 LOG(WARNING) << getId() << " unmount requires state mounted";
221 setState(State::kEjecting);
222 for (auto vol : mVolumes) {
223 if (vol->destroy()) {
224 LOG(WARNING) << getId() << " failed to destroy " << vol->getId()
230 status_t res = doUnmount();
231 setState(State::kUnmounted);
235 status_t VolumeBase::format(const std::string& fsType) {
236 if (mState == State::kMounted) {
240 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
241 LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
245 setState(State::kFormatting);
246 status_t res = doFormat(fsType);
247 setState(State::kUnmounted);
251 status_t VolumeBase::doFormat(const std::string& fsType) {
256 } // namespace android